// // HTTPHelper.m // // Created by Tyler Neylon on 6/20/09. // Copyright 2009 Bynomial. All rights reserved. // #import "HTTPHelper.h" // Declare private methods. @interface HTTPHelper() - (id) init; - (void) clearConnection; @end @implementation HTTPHelper @synthesize timeOut; + (HTTPHelper*) sharedInstance { static HTTPHelper* instance = nil; if (instance == nil) instance = [[HTTPHelper alloc] init]; return instance; } - (NSError*) synchronousGetURLAsString: (NSString*) URLAsString replyData: (NSString**) dataStr { NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLAsString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeOut]; NSURLResponse* response; NSError* error; NSData* rawData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error == nil) { *dataStr = [[NSString be] initWithBytes:[rawData bytes] length:[rawData length] encoding:NSUTF8StringEncoding]; } return error; } - (void) asynchronousGetURLAsString: (NSString*) URLAsString delegate: (id) delegate_ { [self clearConnection]; delegate = delegate_; connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URLAsString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeOut] delegate:self startImmediately:YES]; state = HTTPStateAwaitingFullResponse; dataSoFar = [[NSMutableData alloc] init]; } #pragma mark private methods - (id) init { self = [super init]; if (self == nil) return nil; timeOut = 35; state = HTTPStateNoConnection; return self; } - (void) clearConnection { if (state == HTTPStateAwaitingFullResponse) { [connection cancel]; state = HTTPStateNoConnection; // TODO(tyler): Send an error message to the waiting delegate. } [connection release]; connection = nil; [dataSoFar release]; dataSoFar = nil; } #pragma mark NSURLConnection Methods // TODO(tyler): Use these to build the asynchronous GET. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { // NSLog(@"HTTPHelper::didReceiveResponse"); // In this implementation, we don't use this data. // In the future, it would be wise to not expect data for response codes which indicate // no useful data will be sent. } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { if (delegate == nil) return; [dataSoFar appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { // NSLog(@"HTTPHelper::DidFinishLoading"); if (delegate) { // Note that we receive a string version of the text encoding in the NSURLResponse object; // right now we're ignoring that and assuming UTF8. NSString* dataStr = [[NSString be] initWithData:dataSoFar encoding:NSUTF8StringEncoding]; [delegate httpSuccessWithDataString:dataStr]; } [self clearConnection]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { // NSLog(@"HTTPHelper::didFailWithError"); [delegate httpFailWithError:error]; [self clearConnection]; } @end