Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FMDHeadersOperation.m
- // Free Mac Downloader
- //
- // Created by Diego Massanti on 9/24/09.
- // Copyright 2009 PlusMedia de Argentina LLC. All rights reserved.
- //
- #import "FMDHeadersOperation.h"
- @implementation FMDHeadersOperation
- @synthesize userName, password, httpVersion, responseCode, headers, curlError;
- // Curl "cheat" wrapper so i can move back to Obj-C
- // Header Function
- size_t fmdHeadersFunction( void *ptr, size_t size, size_t nmemb, void *inSelf)
- {
- return [(FMDHeadersOperation*)inSelf curlWritePtr:ptr size:size number:nmemb message:FMDMessageHeader];
- }
- - (void) dealloc
- {
- DLog(@"Deallocating Headers operation... BYE!");
- curl_easy_cleanup(curlHandle);
- [super dealloc];
- }
- - (id)initWithURL:(NSURL*)inURL
- {
- self = [super init];
- if (self != nil) {
- executing = NO;
- finished = NO;
- curlHandle = curl_easy_init();
- if (!curlHandle) {
- DLog(@"Error initializing libCURL, aborting FMDHeaders init...");
- return nil;
- }
- // Set CURL options
- curl_easy_setopt(curlHandle, CURLOPT_HEADERFUNCTION, fmdHeadersFunction);
- curl_easy_setopt(curlHandle, CURLOPT_WRITEHEADER, self);
- curl_easy_setopt(curlHandle, CURLOPT_URL, [[inURL absoluteString]UTF8String]);
- curl_easy_setopt(curlHandle, CURLOPT_NOBODY, 1);
- curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, 1);
- curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);
- headersData = [[NSMutableData alloc]init];
- DLog(@"Initialized FMDHeaders Instance");
- [self start];
- }
- return self;
- }
- - (BOOL)isConcurrent {
- return YES;
- }
- - (BOOL)isExecuting {
- return executing;
- }
- - (BOOL)isFinished {
- return finished;
- }
- - (void)start {
- // Always check for cancellation before launching the task.
- if ([self isCancelled])
- {
- // Must move the operation to the finished state if it is canceled.
- [self willChangeValueForKey:@"isFinished"];
- finished = YES;
- [self didChangeValueForKey:@"isFinished"];
- return;
- }
- // If the operation is not canceled, begin executing the task.
- [self willChangeValueForKey:@"isExecuting"];
- [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
- executing = YES;
- [self didChangeValueForKey:@"isExecuting"];
- }
- - (void)setUserName:(NSString *)theUserName andPassword:(NSString*)thePassword
- {
- self.userName = theUserName;
- self.password = thePassword;
- if ([userName isNotEqualTo:@""] && [password isNotEqualTo:@""])
- {
- NSString * userpwd = [NSString stringWithFormat:@"%@:%@", self.userName, self.password];
- curl_easy_setopt(curlHandle, CURLOPT_USERPWD, [userpwd UTF8String]);
- }
- }
- - (void)main
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- DLog(@"Headers operation main method");
- CURLcode curlResult = curl_easy_perform(curlHandle);
- headers = [[NSDictionary alloc]initWithDictionary:[self buildHeadersDict:headersData]];
- [self willChangeValueForKey:@"curlError"];
- self.curlError = curlResult;
- [self didChangeValueForKey:@"curlError"];
- [self willChangeValueForKey:@"isFinished"];
- finished = YES;
- [self didChangeValueForKey:@"isFinished"];
- [pool release];
- }
- - (NSDictionary*)buildHeadersDict:(NSData*)inData
- {
- NSMutableDictionary * httpHeaders = [[NSMutableDictionary alloc]init];
- NSString * headersString = [[NSString alloc]initWithData:inData encoding:NSUTF8StringEncoding];
- NSArray * headersArray = [[NSArray alloc]initWithArray:[headersString componentsSeparatedByString:@"\r\n"]];
- NSEnumerator * headerEnumerator = [headersArray objectEnumerator];
- NSString * line;
- while (line = [headerEnumerator nextObject]) {
- NSRange versionRange = [line rangeOfString:@"HTTP/"];
- if (NSNotFound != versionRange.location)
- {
- // THIS IS THE VERSION AND RESPONSE CODE LINE.
- // PARSING HERE
- // GET HTTP VERSION : HTTP/x.x
- NSRange versionEndSpace = [line rangeOfString:@" "];
- [self setHttpVersion:[line substringToIndex:versionEndSpace.location]];
- // GET RESPONSE CODE : 200
- int rCode = [[line substringWithRange:NSMakeRange(versionEndSpace.location + 1, 3)]intValue];
- [self setResponseCode:rCode];
- } else {
- // THESE ARE STANDARD HEADER LINES
- // PARSING HER
- NSRange fieldSeparator = [line rangeOfString:@":"];
- if (NSNotFound != fieldSeparator.location) {
- NSString * key = [line substringWithRange:NSMakeRange(0, fieldSeparator.location)];
- NSString * value = [line substringWithRange:NSMakeRange(fieldSeparator.location + 2, [line length] - [key length] - 2)];
- [httpHeaders setObject:value forKey:key];
- }
- }
- }
- return [NSDictionary dictionaryWithDictionary:httpHeaders];
- }
- - (size_t) curlWritePtr:(void *)inPtr size:(size_t)inSize number:(size_t)inNumber message:(FMDMessageType)inMessageID
- {
- if (inMessageID == FMDMessageHeader) {
- size_t written = inSize*inNumber;
- [headersData appendBytes:inPtr length:written];
- return written;
- }
- return 0;
- }
- @end
Add Comment
Please, Sign In to add comment