Guest User

Untitled

a guest
Jul 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. //
  2. // FMDHeadersOperation.m
  3. // Free Mac Downloader
  4. //
  5. // Created by Diego Massanti on 9/24/09.
  6. // Copyright 2009 PlusMedia de Argentina LLC. All rights reserved.
  7. //
  8.  
  9. #import "FMDHeadersOperation.h"
  10.  
  11.  
  12. @implementation FMDHeadersOperation
  13.  
  14. @synthesize userName, password, httpVersion, responseCode, headers, curlError;
  15.  
  16. // Curl "cheat" wrapper so i can move back to Obj-C
  17.  
  18. // Header Function
  19. size_t fmdHeadersFunction( void *ptr, size_t size, size_t nmemb, void *inSelf)
  20. {
  21. return [(FMDHeadersOperation*)inSelf curlWritePtr:ptr size:size number:nmemb message:FMDMessageHeader];
  22. }
  23.  
  24. - (void) dealloc
  25. {
  26. DLog(@"Deallocating Headers operation... BYE!");
  27. curl_easy_cleanup(curlHandle);
  28. [super dealloc];
  29. }
  30.  
  31. - (id)initWithURL:(NSURL*)inURL
  32. {
  33. self = [super init];
  34. if (self != nil) {
  35. executing = NO;
  36. finished = NO;
  37. curlHandle = curl_easy_init();
  38. if (!curlHandle) {
  39. DLog(@"Error initializing libCURL, aborting FMDHeaders init...");
  40. return nil;
  41. }
  42.  
  43. // Set CURL options
  44. curl_easy_setopt(curlHandle, CURLOPT_HEADERFUNCTION, fmdHeadersFunction);
  45. curl_easy_setopt(curlHandle, CURLOPT_WRITEHEADER, self);
  46. curl_easy_setopt(curlHandle, CURLOPT_URL, [[inURL absoluteString]UTF8String]);
  47. curl_easy_setopt(curlHandle, CURLOPT_NOBODY, 1);
  48. curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, 1);
  49. curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);
  50.  
  51. headersData = [[NSMutableData alloc]init];
  52.  
  53. DLog(@"Initialized FMDHeaders Instance");
  54. [self start];
  55.  
  56.  
  57. }
  58. return self;
  59. }
  60.  
  61. - (BOOL)isConcurrent {
  62. return YES;
  63. }
  64.  
  65. - (BOOL)isExecuting {
  66. return executing;
  67. }
  68.  
  69. - (BOOL)isFinished {
  70. return finished;
  71. }
  72.  
  73. - (void)start {
  74. // Always check for cancellation before launching the task.
  75. if ([self isCancelled])
  76. {
  77. // Must move the operation to the finished state if it is canceled.
  78. [self willChangeValueForKey:@"isFinished"];
  79. finished = YES;
  80. [self didChangeValueForKey:@"isFinished"];
  81. return;
  82. }
  83.  
  84. // If the operation is not canceled, begin executing the task.
  85. [self willChangeValueForKey:@"isExecuting"];
  86. [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
  87. executing = YES;
  88. [self didChangeValueForKey:@"isExecuting"];
  89. }
  90.  
  91. - (void)setUserName:(NSString *)theUserName andPassword:(NSString*)thePassword
  92. {
  93. self.userName = theUserName;
  94. self.password = thePassword;
  95. if ([userName isNotEqualTo:@""] && [password isNotEqualTo:@""])
  96. {
  97. NSString * userpwd = [NSString stringWithFormat:@"%@:%@", self.userName, self.password];
  98. curl_easy_setopt(curlHandle, CURLOPT_USERPWD, [userpwd UTF8String]);
  99. }
  100.  
  101.  
  102. }
  103.  
  104. - (void)main
  105.  
  106. {
  107. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  108. DLog(@"Headers operation main method");
  109. CURLcode curlResult = curl_easy_perform(curlHandle);
  110.  
  111. headers = [[NSDictionary alloc]initWithDictionary:[self buildHeadersDict:headersData]];
  112.  
  113. [self willChangeValueForKey:@"curlError"];
  114. self.curlError = curlResult;
  115. [self didChangeValueForKey:@"curlError"];
  116.  
  117. [self willChangeValueForKey:@"isFinished"];
  118. finished = YES;
  119. [self didChangeValueForKey:@"isFinished"];
  120.  
  121. [pool release];
  122. }
  123.  
  124. - (NSDictionary*)buildHeadersDict:(NSData*)inData
  125. {
  126. NSMutableDictionary * httpHeaders = [[NSMutableDictionary alloc]init];
  127. NSString * headersString = [[NSString alloc]initWithData:inData encoding:NSUTF8StringEncoding];
  128. NSArray * headersArray = [[NSArray alloc]initWithArray:[headersString componentsSeparatedByString:@"\r\n"]];
  129.  
  130. NSEnumerator * headerEnumerator = [headersArray objectEnumerator];
  131.  
  132. NSString * line;
  133.  
  134. while (line = [headerEnumerator nextObject]) {
  135. NSRange versionRange = [line rangeOfString:@"HTTP/"];
  136. if (NSNotFound != versionRange.location)
  137. {
  138. // THIS IS THE VERSION AND RESPONSE CODE LINE.
  139. // PARSING HERE
  140.  
  141. // GET HTTP VERSION : HTTP/x.x
  142. NSRange versionEndSpace = [line rangeOfString:@" "];
  143. [self setHttpVersion:[line substringToIndex:versionEndSpace.location]];
  144.  
  145. // GET RESPONSE CODE : 200
  146.  
  147. int rCode = [[line substringWithRange:NSMakeRange(versionEndSpace.location + 1, 3)]intValue];
  148. [self setResponseCode:rCode];
  149.  
  150. } else {
  151. // THESE ARE STANDARD HEADER LINES
  152. // PARSING HER
  153. NSRange fieldSeparator = [line rangeOfString:@":"];
  154. if (NSNotFound != fieldSeparator.location) {
  155. NSString * key = [line substringWithRange:NSMakeRange(0, fieldSeparator.location)];
  156. NSString * value = [line substringWithRange:NSMakeRange(fieldSeparator.location + 2, [line length] - [key length] - 2)];
  157.  
  158. [httpHeaders setObject:value forKey:key];
  159. }
  160.  
  161.  
  162. }
  163.  
  164. }
  165. return [NSDictionary dictionaryWithDictionary:httpHeaders];
  166.  
  167. }
  168.  
  169. - (size_t) curlWritePtr:(void *)inPtr size:(size_t)inSize number:(size_t)inNumber message:(FMDMessageType)inMessageID
  170. {
  171.  
  172. if (inMessageID == FMDMessageHeader) {
  173. size_t written = inSize*inNumber;
  174. [headersData appendBytes:inPtr length:written];
  175. return written;
  176. }
  177.  
  178. return 0;
  179. }
  180.  
  181.  
  182.  
  183. @end
Add Comment
Please, Sign In to add comment