diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da9f58e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +Pods/ +Podfile.lock +README.mdown +echoprint-codegen-master/ +Podfile.lock \ No newline at end of file diff --git a/ASIHTTP/ASIAuthenticationDialog.h b/ASIHTTP/ASIAuthenticationDialog.h deleted file mode 100644 index 6bbb282..0000000 --- a/ASIHTTP/ASIAuthenticationDialog.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// ASIAuthenticationDialog.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 21/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import -@class ASIHTTPRequest; - -typedef enum _ASIAuthenticationType { - ASIStandardAuthenticationType = 0, - ASIProxyAuthenticationType = 1 -} ASIAuthenticationType; - -@interface ASIAutorotatingViewController : UIViewController -@end - -@interface ASIAuthenticationDialog : ASIAutorotatingViewController { - ASIHTTPRequest *request; - ASIAuthenticationType type; - UITableView *tableView; - UIViewController *presentingController; - BOOL didEnableRotationNotifications; -} -+ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request; -+ (void)dismiss; - -@property (retain) ASIHTTPRequest *request; -@property (assign) ASIAuthenticationType type; -@property (assign) BOOL didEnableRotationNotifications; -@property (retain, nonatomic) UIViewController *presentingController; -@end diff --git a/ASIHTTP/ASIAuthenticationDialog.m b/ASIHTTP/ASIAuthenticationDialog.m deleted file mode 100644 index ffae591..0000000 --- a/ASIHTTP/ASIAuthenticationDialog.m +++ /dev/null @@ -1,483 +0,0 @@ -// -// ASIAuthenticationDialog.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 21/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIAuthenticationDialog.h" -#import "ASIHTTPRequest.h" -#import - -static ASIAuthenticationDialog *sharedDialog = nil; -BOOL isDismissing = NO; -static NSMutableArray *requestsNeedingAuthentication = nil; - -static const NSUInteger kUsernameRow = 0; -static const NSUInteger kUsernameSection = 0; -static const NSUInteger kPasswordRow = 1; -static const NSUInteger kPasswordSection = 0; -static const NSUInteger kDomainRow = 0; -static const NSUInteger kDomainSection = 1; - - -@implementation ASIAutorotatingViewController - -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation -{ - return YES; -} - -@end - - -@interface ASIAuthenticationDialog () -- (void)showTitle; -- (void)show; -- (NSArray *)requestsRequiringTheseCredentials; -- (void)presentNextDialog; -@property (retain) UITableView *tableView; -@end - -@implementation ASIAuthenticationDialog - -#pragma mark init / dealloc - -+ (void)initialize -{ - if (self == [ASIAuthenticationDialog class]) { - requestsNeedingAuthentication = [[NSMutableArray array] retain]; - } -} - -+ (void)presentAuthenticationDialogForRequest:(ASIHTTPRequest *)request -{ - // No need for a lock here, this will always be called on the main thread - if (!sharedDialog) { - sharedDialog = [[self alloc] init]; - [sharedDialog setRequest:request]; - if ([request authenticationNeeded] == ASIProxyAuthenticationNeeded) { - [sharedDialog setType:ASIProxyAuthenticationType]; - } else { - [sharedDialog setType:ASIStandardAuthenticationType]; - } - [sharedDialog show]; - } else { - [requestsNeedingAuthentication addObject:request]; - } -} - -- (id)init -{ - if ((self = [self initWithNibName:nil bundle:nil])) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { -#endif - if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) { - [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; - [self setDidEnableRotationNotifications:YES]; - } - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - } -#endif - } - return self; -} - -- (void)dealloc -{ - if ([self didEnableRotationNotifications]) { - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; - } - [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; - - [request release]; - [tableView release]; - [presentingController.view removeFromSuperview]; - [presentingController release]; - [super dealloc]; -} - -#pragma mark keyboard notifications - -- (void)keyboardWillShow:(NSNotification *)notification -{ -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { -#endif -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2 - NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; -#else - NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; -#endif - CGRect keyboardBounds; - [keyboardBoundsValue getValue:&keyboardBounds]; - UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); - [[self tableView] setScrollIndicatorInsets:e]; - [[self tableView] setContentInset:e]; -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - } -#endif -} - -// Manually handles orientation changes on iPhone -- (void)orientationChanged:(NSNotification *)notification -{ - [self showTitle]; - - UIDeviceOrientation o = [[UIApplication sharedApplication] statusBarOrientation]; - CGFloat angle = 0; - switch (o) { - case UIDeviceOrientationLandscapeLeft: angle = 90; break; - case UIDeviceOrientationLandscapeRight: angle = -90; break; - case UIDeviceOrientationPortraitUpsideDown: angle = 180; break; - default: break; - } - - CGRect f = [[UIScreen mainScreen] applicationFrame]; - - // Swap the frame height and width if necessary - if (UIDeviceOrientationIsLandscape(o)) { - CGFloat t; - t = f.size.width; - f.size.width = f.size.height; - f.size.height = t; - } - - CGAffineTransform previousTransform = self.view.layer.affineTransform; - CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle * M_PI / 180.0); - - // Reset the transform so we can set the size - self.view.layer.affineTransform = CGAffineTransformIdentity; - self.view.frame = (CGRect){0,0,f.size}; - - // Revert to the previous transform for correct animation - self.view.layer.affineTransform = previousTransform; - - [UIView beginAnimations:nil context:NULL]; - [UIView setAnimationDuration:0.3]; - - // Set the new transform - self.view.layer.affineTransform = newTransform; - - // Fix the view origin - self.view.frame = (CGRect){f.origin.x,f.origin.y,self.view.frame.size}; - [UIView commitAnimations]; -} - -#pragma mark utilities - -- (UIViewController *)presentingController -{ - if (!presentingController) { - presentingController = [[ASIAutorotatingViewController alloc] initWithNibName:nil bundle:nil]; - - // Attach to the window, but don't interfere. - UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; - [window addSubview:[presentingController view]]; - [[presentingController view] setFrame:CGRectZero]; - [[presentingController view] setUserInteractionEnabled:NO]; - } - - return presentingController; -} - -- (UITextField *)textFieldInRow:(NSUInteger)row section:(NSUInteger)section -{ - return [[[[[self tableView] cellForRowAtIndexPath: - [NSIndexPath indexPathForRow:row inSection:section]] - contentView] subviews] objectAtIndex:0]; -} - -- (UITextField *)usernameField -{ - return [self textFieldInRow:kUsernameRow section:kUsernameSection]; -} - -- (UITextField *)passwordField -{ - return [self textFieldInRow:kPasswordRow section:kPasswordSection]; -} - -- (UITextField *)domainField -{ - return [self textFieldInRow:kDomainRow section:kDomainSection]; -} - -#pragma mark show / dismiss - -+ (void)dismiss -{ - [[sharedDialog parentViewController] dismissModalViewControllerAnimated:YES]; -} - -- (void)viewDidDisappear:(BOOL)animated -{ - [self retain]; - [sharedDialog release]; - sharedDialog = nil; - [self presentNextDialog]; - [self release]; -} - -- (void)dismiss -{ - if (self == sharedDialog) { - [[self class] dismiss]; - } else { - [[self parentViewController] dismissModalViewControllerAnimated:YES]; - } -} - -- (void)showTitle -{ - UINavigationBar *navigationBar = [[[self view] subviews] objectAtIndex:0]; - UINavigationItem *navItem = [[navigationBar items] objectAtIndex:0]; - if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { - // Setup the title - if ([self type] == ASIProxyAuthenticationType) { - [navItem setPrompt:@"Login to this secure proxy server."]; - } else { - [navItem setPrompt:@"Login to this secure server."]; - } - } else { - [navItem setPrompt:nil]; - } - [navigationBar sizeToFit]; - CGRect f = [[self view] bounds]; - f.origin.y = [navigationBar frame].size.height; - f.size.height -= f.origin.y; - [[self tableView] setFrame:f]; -} - -- (void)show -{ - // Remove all subviews - UIView *v; - while ((v = [[[self view] subviews] lastObject])) { - [v removeFromSuperview]; - } - - // Setup toolbar - UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease]; - [bar setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; - - UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease]; - bar.items = [NSArray arrayWithObject:navItem]; - - [[self view] addSubview:bar]; - - [self showTitle]; - - // Setup toolbar buttons - if ([self type] == ASIProxyAuthenticationType) { - [navItem setTitle:[[self request] proxyHost]]; - } else { - [navItem setTitle:[[[self request] url] host]]; - } - - [navItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAuthenticationFromDialog:)] autorelease]]; - [navItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleDone target:self action:@selector(loginWithCredentialsFromDialog:)] autorelease]]; - - // We show the login form in a table view, similar to Safari's authentication dialog - [bar sizeToFit]; - CGRect f = [[self view] bounds]; - f.origin.y = [bar frame].size.height; - f.size.height -= f.origin.y; - - [self setTableView:[[[UITableView alloc] initWithFrame:f style:UITableViewStyleGrouped] autorelease]]; - [[self tableView] setDelegate:self]; - [[self tableView] setDataSource:self]; - [[self tableView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [[self view] addSubview:[self tableView]]; - - // Force reload the table content, and focus the first field to show the keyboard - [[self tableView] reloadData]; - [[[[[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].contentView subviews] objectAtIndex:0] becomeFirstResponder]; - -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - [self setModalPresentationStyle:UIModalPresentationFormSheet]; - } -#endif - - [[self presentingController] presentModalViewController:self animated:YES]; -} - -#pragma mark button callbacks - -- (void)cancelAuthenticationFromDialog:(id)sender -{ - for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { - [theRequest cancelAuthentication]; - [requestsNeedingAuthentication removeObject:theRequest]; - } - [self dismiss]; -} - -- (NSArray *)requestsRequiringTheseCredentials -{ - NSMutableArray *requestsRequiringTheseCredentials = [NSMutableArray array]; - NSURL *requestURL = [[self request] url]; - for (ASIHTTPRequest *otherRequest in requestsNeedingAuthentication) { - NSURL *theURL = [otherRequest url]; - if (([otherRequest authenticationNeeded] == [[self request] authenticationNeeded]) && [[theURL host] isEqualToString:[requestURL host]] && ([theURL port] == [requestURL port] || ([requestURL port] && [[theURL port] isEqualToNumber:[requestURL port]])) && [[theURL scheme] isEqualToString:[requestURL scheme]] && ((![otherRequest authenticationRealm] && ![[self request] authenticationRealm]) || ([otherRequest authenticationRealm] && [[self request] authenticationRealm] && [[[self request] authenticationRealm] isEqualToString:[otherRequest authenticationRealm]]))) { - [requestsRequiringTheseCredentials addObject:otherRequest]; - } - } - [requestsRequiringTheseCredentials addObject:[self request]]; - return requestsRequiringTheseCredentials; -} - -- (void)presentNextDialog -{ - if ([requestsNeedingAuthentication count]) { - ASIHTTPRequest *nextRequest = [requestsNeedingAuthentication objectAtIndex:0]; - [requestsNeedingAuthentication removeObjectAtIndex:0]; - [[self class] presentAuthenticationDialogForRequest:nextRequest]; - } -} - - -- (void)loginWithCredentialsFromDialog:(id)sender -{ - for (ASIHTTPRequest *theRequest in [self requestsRequiringTheseCredentials]) { - - NSString *username = [[self usernameField] text]; - NSString *password = [[self passwordField] text]; - - if (username == nil) { username = @""; } - if (password == nil) { password = @""; } - - if ([self type] == ASIProxyAuthenticationType) { - [theRequest setProxyUsername:username]; - [theRequest setProxyPassword:password]; - } else { - [theRequest setUsername:username]; - [theRequest setPassword:password]; - } - - // Handle NTLM domains - NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; - if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { - NSString *domain = [[self domainField] text]; - if ([self type] == ASIProxyAuthenticationType) { - [theRequest setProxyDomain:domain]; - } else { - [theRequest setDomain:domain]; - } - } - - [theRequest retryUsingSuppliedCredentials]; - [requestsNeedingAuthentication removeObject:theRequest]; - } - [self dismiss]; -} - -#pragma mark table view data source - -- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView -{ - NSString *scheme = ([self type] == ASIStandardAuthenticationType) ? [[self request] authenticationScheme] : [[self request] proxyAuthenticationScheme]; - if ([scheme isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeNTLM]) { - return 2; - } - return 1; -} - -- (CGFloat)tableView:(UITableView *)aTableView heightForFooterInSection:(NSInteger)section -{ - if (section == [self numberOfSectionsInTableView:aTableView]-1) { - return 30; - } - return 0; -} - -- (CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section -{ - if (section == 0) { -#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2 - if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - return 54; - } -#endif - return 30; - } - return 0; -} - -- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section -{ - if (section == 0) { - return [[self request] authenticationRealm]; - } - return nil; -} - -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath -{ -#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0 - UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; -#else - UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:nil] autorelease]; -#endif - - [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; - - CGRect f = CGRectInset([cell bounds], 10, 10); - UITextField *textField = [[[UITextField alloc] initWithFrame:f] autorelease]; - [textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; - [textField setAutocorrectionType:UITextAutocorrectionTypeNo]; - - NSUInteger s = [indexPath section]; - NSUInteger r = [indexPath row]; - - if (s == kUsernameSection && r == kUsernameRow) { - [textField setPlaceholder:@"User"]; - } else if (s == kPasswordSection && r == kPasswordRow) { - [textField setPlaceholder:@"Password"]; - [textField setSecureTextEntry:YES]; - } else if (s == kDomainSection && r == kDomainRow) { - [textField setPlaceholder:@"Domain"]; - } - [cell.contentView addSubview:textField]; - - return cell; -} - -- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section -{ - if (section == 0) { - return 2; - } else { - return 1; - } -} - -- (NSString *)tableView:(UITableView *)aTableView titleForFooterInSection:(NSInteger)section -{ - if (section == [self numberOfSectionsInTableView:aTableView]-1) { - // If we're using Basic authentication and the connection is not using SSL, we'll show the plain text message - if ([[[self request] authenticationScheme] isEqualToString:(NSString *)kCFHTTPAuthenticationSchemeBasic] && ![[[[self request] url] scheme] isEqualToString:@"https"]) { - return @"Password will be sent in the clear."; - // We are using Digest, NTLM, or any scheme over SSL - } else { - return @"Password will be sent securely."; - } - } - return nil; -} - -#pragma mark - - -@synthesize request; -@synthesize type; -@synthesize tableView; -@synthesize didEnableRotationNotifications; -@synthesize presentingController; -@end diff --git a/ASIHTTP/ASICacheDelegate.h b/ASIHTTP/ASICacheDelegate.h deleted file mode 100644 index e94c4fd..0000000 --- a/ASIHTTP/ASICacheDelegate.h +++ /dev/null @@ -1,55 +0,0 @@ -// -// ASICacheDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import -@class ASIHTTPRequest; - -typedef enum _ASICachePolicy { - ASIDefaultCachePolicy = 0, - ASIIgnoreCachePolicy = 1, - ASIReloadIfDifferentCachePolicy = 2, - ASIOnlyLoadIfNotCachedCachePolicy = 3, - ASIUseCacheIfLoadFailsCachePolicy = 4 -} ASICachePolicy; - -typedef enum _ASICacheStoragePolicy { - ASICacheForSessionDurationCacheStoragePolicy = 0, - ASICachePermanentlyCacheStoragePolicy = 1 -} ASICacheStoragePolicy; - - -@protocol ASICacheDelegate - -@required - -// Should return the cache policy that will be used when requests have their cache policy set to ASIDefaultCachePolicy -- (ASICachePolicy)defaultCachePolicy; - -// Should Remove cached data for a particular request -- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request; - -// Should return YES if the cache considers its cached response current for the request -// Should return NO is the data is not cached, or (for example) if the cached headers state the request should have expired -- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request; - -// Should store the response for the passed request in the cache -// When a non-zero maxAge is passed, it should be used as the expiry time for the cached response -- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge; - -// Should return an NSDictionary of cached headers for the passed request, if it is stored in the cache -- (NSDictionary *)cachedHeadersForRequest:(ASIHTTPRequest *)request; - -// Should return the cached body of a response for the passed request, if it is stored in the cache -- (NSData *)cachedResponseDataForRequest:(ASIHTTPRequest *)request; - -// Same as the above, but returns a path to the cached response body instead -- (NSString *)pathToCachedResponseDataForRequest:(ASIHTTPRequest *)request; - -// Clear cached data stored for the passed storage policy -- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)cachePolicy; -@end diff --git a/ASIHTTP/ASIDownloadCache.h b/ASIHTTP/ASIDownloadCache.h deleted file mode 100644 index b751060..0000000 --- a/ASIHTTP/ASIDownloadCache.h +++ /dev/null @@ -1,47 +0,0 @@ -// -// ASIDownloadCache.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASICacheDelegate.h" - -@interface ASIDownloadCache : NSObject { - - // The default cache policy for this cache - // Requests that store data in the cache will use this cache policy if their cache policy is set to ASIDefaultCachePolicy - // Defaults to ASIReloadIfDifferentCachePolicy - ASICachePolicy defaultCachePolicy; - - // The directory in which cached data will be stored - // Defaults to a directory called 'ASIHTTPRequestCache' in the temporary directory - NSString *storagePath; - - // Mediates access to the cache - NSRecursiveLock *accessLock; - - // When YES, the cache will look for cache-control / pragma: no-cache headers, and won't reuse store responses if it finds them - BOOL shouldRespectCacheControlHeaders; -} - -// Returns a static instance of an ASIDownloadCache -// In most circumstances, it will make sense to use this as a global cache, rather than creating your own cache -// To make ASIHTTPRequests use it automatically, use [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; -+ (id)sharedCache; - -// A helper function that determines if the server has requested data should not be cached by looking at the request's response headers -+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request; - -// A date formatter that can be used to construct an RFC 1123 date -// The returned formatter is safe to use on the calling thread -// Do not use this formatter for parsing dates because the format can vary slightly - use ASIHTTPRequest's dateFromRFC1123String: class method instead -+ (NSDateFormatter *)rfc1123DateFormatter; - -@property (assign, nonatomic) ASICachePolicy defaultCachePolicy; -@property (retain, nonatomic) NSString *storagePath; -@property (retain) NSRecursiveLock *accessLock; -@property (assign) BOOL shouldRespectCacheControlHeaders; -@end diff --git a/ASIHTTP/ASIDownloadCache.m b/ASIHTTP/ASIDownloadCache.m deleted file mode 100644 index 72fcf4f..0000000 --- a/ASIHTTP/ASIDownloadCache.m +++ /dev/null @@ -1,387 +0,0 @@ -// -// ASIDownloadCache.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 01/05/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -#import "ASIDownloadCache.h" -#import "ASIHTTPRequest.h" -#import - -static ASIDownloadCache *sharedCache = nil; - -static NSString *sessionCacheFolder = @"SessionStore"; -static NSString *permanentCacheFolder = @"PermanentStore"; - -@interface ASIDownloadCache () -+ (NSString *)keyForRequest:(ASIHTTPRequest *)request; -@end - -@implementation ASIDownloadCache - -- (id)init -{ - self = [super init]; - [self setShouldRespectCacheControlHeaders:YES]; - [self setDefaultCachePolicy:ASIReloadIfDifferentCachePolicy]; - [self setAccessLock:[[[NSRecursiveLock alloc] init] autorelease]]; - return self; -} - -+ (id)sharedCache -{ - if (!sharedCache) { - sharedCache = [[self alloc] init]; - [sharedCache setStoragePath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"ASIHTTPRequestCache"]]; - - } - return sharedCache; -} - -- (void)dealloc -{ - [storagePath release]; - [accessLock release]; - [super dealloc]; -} - -- (NSString *)storagePath -{ - [[self accessLock] lock]; - NSString *p = [[storagePath retain] autorelease]; - [[self accessLock] unlock]; - return p; -} - - -- (void)setStoragePath:(NSString *)path -{ - [[self accessLock] lock]; - [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; - [storagePath release]; - storagePath = [path retain]; - BOOL isDirectory = NO; - NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil]; - for (NSString *directory in directories) { - BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:&isDirectory]; - if (exists && !isDirectory) { - [[self accessLock] unlock]; - [NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",directory]; - } else if (!exists) { - [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil]; - if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",directory]; - } - } - } - [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy]; - [[self accessLock] unlock]; -} - -- (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge -{ - [[self accessLock] lock]; - - if ([request error] || ![request responseHeaders] || ([request responseStatusCode] != 200)) { - [[self accessLock] unlock]; - return; - } - - if ([self shouldRespectCacheControlHeaders] && ![[self class] serverAllowsResponseCachingForRequest:request]) { - [[self accessLock] unlock]; - return; - } - - // If the request is set to use the default policy, use this cache's default policy - ASICachePolicy policy = [request cachePolicy]; - if (policy == ASIDefaultCachePolicy) { - policy = [self defaultCachePolicy]; - } - - if (policy == ASIIgnoreCachePolicy) { - [[self accessLock] unlock]; - return; - } - NSString *path = nil; - if ([request cacheStoragePolicy] == ASICacheForSessionDurationCacheStoragePolicy) { - path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; - } else { - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; - } - path = [path stringByAppendingPathComponent:[[self class] keyForRequest:request]]; - NSString *metadataPath = [path stringByAppendingPathExtension:@"cachedheaders"]; - NSString *dataPath = [path stringByAppendingPathExtension:@"cacheddata"]; - - NSMutableDictionary *responseHeaders = [NSMutableDictionary dictionaryWithDictionary:[request responseHeaders]]; - if ([request isResponseCompressed]) { - [responseHeaders removeObjectForKey:@"Content-Encoding"]; - } - if (maxAge != 0) { - [responseHeaders removeObjectForKey:@"Expires"]; - [responseHeaders setObject:[NSString stringWithFormat:@"max-age=%i",(int)maxAge] forKey:@"Cache-Control"]; - } - // We use this special key to help expire the request when we get a max-age header - [responseHeaders setObject:[[[self class] rfc1123DateFormatter] stringFromDate:[NSDate date]] forKey:@"X-ASIHTTPRequest-Fetch-date"]; - [responseHeaders writeToFile:metadataPath atomically:NO]; - - if ([request responseData]) { - [[request responseData] writeToFile:dataPath atomically:NO]; - } else if ([request downloadDestinationPath]) { - NSError *error = nil; - [[NSFileManager defaultManager] copyItemAtPath:[request downloadDestinationPath] toPath:dataPath error:&error]; - } - [[self accessLock] unlock]; -} - -- (NSDictionary *)cachedHeadersForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return nil; - } - // Look in the session store - NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; - NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; - if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return [NSDictionary dictionaryWithContentsOfFile:dataPath]; - } - // Look in the permanent store - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; - dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; - if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return [NSDictionary dictionaryWithContentsOfFile:dataPath]; - } - [[self accessLock] unlock]; - return nil; -} - -- (NSData *)cachedResponseDataForRequest:(ASIHTTPRequest *)request -{ - NSString *path = [self pathToCachedResponseDataForRequest:request]; - if (path) { - return [NSData dataWithContentsOfFile:path]; - } - return nil; -} - -- (NSString *)pathToCachedResponseDataForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return nil; - } - // Look in the session store - NSString *path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; - NSString *dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]]; - if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return dataPath; - } - // Look in the permanent store - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; - dataPath = [path stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cacheddata"]]; - if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { - [[self accessLock] unlock]; - return dataPath; - } - [[self accessLock] unlock]; - return nil; -} - -- (void)removeCachedDataForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return; - } - NSString *cachedHeadersPath = [[self storagePath] stringByAppendingPathComponent:[[[self class] keyForRequest:request] stringByAppendingPathExtension:@"cachedheaders"]]; - if (!cachedHeadersPath) { - [[self accessLock] unlock]; - return; - } - NSString *dataPath = [self pathToCachedResponseDataForRequest:request]; - if (!dataPath) { - [[self accessLock] unlock]; - return; - } - [[NSFileManager defaultManager] removeItemAtPath:cachedHeadersPath error:NULL]; - [[NSFileManager defaultManager] removeItemAtPath:dataPath error:NULL]; - [[self accessLock] unlock]; -} - -- (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return NO; - } - NSDictionary *cachedHeaders = [self cachedHeadersForRequest:request]; - if (!cachedHeaders) { - [[self accessLock] unlock]; - return NO; - } - NSString *dataPath = [self pathToCachedResponseDataForRequest:request]; - if (!dataPath) { - [[self accessLock] unlock]; - return NO; - } - - if ([self shouldRespectCacheControlHeaders]) { - - // Look for an Expires header to see if the content is out of data - NSString *expires = [cachedHeaders objectForKey:@"Expires"]; - if (expires) { - if ([[ASIHTTPRequest dateFromRFC1123String:expires] timeIntervalSinceNow] < 0) { - [[self accessLock] unlock]; - return NO; - } - } - // Look for a max-age header - NSString *cacheControl = [[cachedHeaders objectForKey:@"Cache-Control"] lowercaseString]; - if (cacheControl) { - NSScanner *scanner = [NSScanner scannerWithString:cacheControl]; - if ([scanner scanString:@"max-age" intoString:NULL]) { - [scanner scanString:@"=" intoString:NULL]; - NSTimeInterval maxAge = 0; - [scanner scanDouble:&maxAge]; - NSDate *fetchDate = [ASIHTTPRequest dateFromRFC1123String:[cachedHeaders objectForKey:@"X-ASIHTTPRequest-Fetch-date"]]; - - NSDate *expiryDate = [[[NSDate alloc] initWithTimeInterval:maxAge sinceDate:fetchDate] autorelease]; - - if ([expiryDate timeIntervalSinceNow] < 0) { - [[self accessLock] unlock]; - return NO; - } - } - } - - } - - // If we already have response headers for this request, check to see if the new content is different - if ([request responseHeaders] && [request responseStatusCode] != 304) { - // If the Etag or Last-Modified date are different from the one we have, fetch the document again - NSArray *headersToCompare = [NSArray arrayWithObjects:@"Etag",@"Last-Modified",nil]; - for (NSString *header in headersToCompare) { - if (![[[request responseHeaders] objectForKey:header] isEqualToString:[cachedHeaders objectForKey:header]]) { - [[self accessLock] unlock]; - return NO; - } - } - } - [[self accessLock] unlock]; - return YES; -} - -- (ASICachePolicy)defaultCachePolicy -{ - [[self accessLock] lock]; - ASICachePolicy cp = defaultCachePolicy; - [[self accessLock] unlock]; - return cp; -} - - -- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy -{ - [[self accessLock] lock]; - if (cachePolicy == ASIDefaultCachePolicy) { - defaultCachePolicy = ASIReloadIfDifferentCachePolicy; - } else { - defaultCachePolicy = cachePolicy; - } - [[self accessLock] unlock]; -} - -- (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)storagePolicy -{ - [[self accessLock] lock]; - if (![self storagePath]) { - [[self accessLock] unlock]; - return; - } - NSString *path; - if (storagePolicy == ASICachePermanentlyCacheStoragePolicy) { - path = [[self storagePath] stringByAppendingPathComponent:permanentCacheFolder]; - } else { - path = [[self storagePath] stringByAppendingPathComponent:sessionCacheFolder]; - } - BOOL isDirectory = NO; - BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]; - if (exists && !isDirectory || !exists) { - [[self accessLock] unlock]; - return; - } - NSError *error = nil; - NSArray *cacheFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error]; - if (error) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToTraverseCacheDirectory" format:@"Listing cache directory failed at path '%@'",path]; - } - for (NSString *file in cacheFiles) { - NSString *extension = [file pathExtension]; - if ([extension isEqualToString:@"cacheddata"] || [extension isEqualToString:@"cachedheaders"]) { - [[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:file] error:&error]; - if (error) { - [[self accessLock] unlock]; - [NSException raise:@"FailedToRemoveCacheFile" format:@"Failed to remove cached data at path '%@'",path]; - } - } - } - [[self accessLock] unlock]; -} - -+ (BOOL)serverAllowsResponseCachingForRequest:(ASIHTTPRequest *)request -{ - NSString *cacheControl = [[[request responseHeaders] objectForKey:@"Cache-Control"] lowercaseString]; - if (cacheControl) { - if ([cacheControl isEqualToString:@"no-cache"] || [cacheControl isEqualToString:@"no-store"]) { - return NO; - } - } - NSString *pragma = [[[request responseHeaders] objectForKey:@"Pragma"] lowercaseString]; - if (pragma) { - if ([pragma isEqualToString:@"no-cache"]) { - return NO; - } - } - return YES; -} - -// Borrowed from: http://stackoverflow.com/questions/652300/using-md5-hash-on-a-string-in-cocoa -+ (NSString *)keyForRequest:(ASIHTTPRequest *)request -{ - const char *cStr = [[[request url] absoluteString] UTF8String]; - unsigned char result[16]; - CC_MD5(cStr, (CC_LONG)strlen(cStr), result); - return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]]; -} - -+ (NSDateFormatter *)rfc1123DateFormatter -{ - NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary]; - NSDateFormatter *dateFormatter = [threadDict objectForKey:@"ASIDownloadCacheDateFormatter"]; - if (dateFormatter == nil) { - dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; - [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; - [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; - [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss 'GMT'"]; - [threadDict setObject:dateFormatter forKey:@"ASIDownloadCacheDateFormatter"]; - } - return dateFormatter; -} - - -@synthesize storagePath; -@synthesize defaultCachePolicy; -@synthesize accessLock; -@synthesize shouldRespectCacheControlHeaders; -@end diff --git a/ASIHTTP/ASIFormDataRequest.h b/ASIHTTP/ASIFormDataRequest.h deleted file mode 100644 index e206fd2..0000000 --- a/ASIHTTP/ASIFormDataRequest.h +++ /dev/null @@ -1,76 +0,0 @@ -// -// ASIFormDataRequest.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASIHTTPRequest.h" -#import "ASIHTTPRequestConfig.h" - -typedef enum _ASIPostFormat { - ASIMultipartFormDataPostFormat = 0, - ASIURLEncodedPostFormat = 1 - -} ASIPostFormat; - -@interface ASIFormDataRequest : ASIHTTPRequest { - - // Parameters that will be POSTed to the url - NSMutableArray *postData; - - // Files that will be POSTed to the url - NSMutableArray *fileData; - - ASIPostFormat postFormat; - - NSStringEncoding stringEncoding; - -#if DEBUG_FORM_DATA_REQUEST - // Will store a string version of the request body that will be printed to the console when ASIHTTPREQUEST_DEBUG is set in GCC_PREPROCESSOR_DEFINITIONS - NSString *debugBodyString; -#endif - -} - -#pragma mark utilities -- (NSString*)encodeURL:(NSString *)string; - -#pragma mark setup request - -// Add a POST variable to the request -- (void)addPostValue:(id )value forKey:(NSString *)key; - -// Set a POST variable for this request, clearing any others with the same key -- (void)setPostValue:(id )value forKey:(NSString *)key; - -// Add the contents of a local file to the request -- (void)addFile:(NSString *)filePath forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of a local file to the request, clearing any others with the same key -- (void)setFile:(NSString *)filePath forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of an NSData object to the request -- (void)addData:(NSData *)data forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - -// Add the contents of an NSData object to the request, clearing any others with the same key -- (void)setData:(NSData *)data forKey:(NSString *)key; - -// Same as above, but you can specify the content-type and file name -- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key; - - -@property (assign) ASIPostFormat postFormat; -@property (assign) NSStringEncoding stringEncoding; -@end diff --git a/ASIHTTP/ASIFormDataRequest.m b/ASIHTTP/ASIFormDataRequest.m deleted file mode 100644 index d805831..0000000 --- a/ASIHTTP/ASIFormDataRequest.m +++ /dev/null @@ -1,355 +0,0 @@ -// -// ASIFormDataRequest.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIFormDataRequest.h" - - -// Private stuff -@interface ASIFormDataRequest () -- (void)buildMultipartFormDataPostBody; -- (void)buildURLEncodedPostBody; -- (void)appendPostString:(NSString *)string; - -@property (retain) NSMutableArray *postData; -@property (retain) NSMutableArray *fileData; - -#if DEBUG_FORM_DATA_REQUEST -- (void)addToDebugBody:(NSString *)string; -@property (retain, nonatomic) NSString *debugBodyString; -#endif - -@end - -@implementation ASIFormDataRequest - -#pragma mark utilities -- (NSString*)encodeURL:(NSString *)string -{ - NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]); - if (newString) { - return newString; - } - return @""; -} - -#pragma mark init / dealloc - -+ (id)requestWithURL:(NSURL *)newURL -{ - return [[[self alloc] initWithURL:newURL] autorelease]; -} - -- (id)initWithURL:(NSURL *)newURL -{ - self = [super initWithURL:newURL]; - [self setPostFormat:ASIURLEncodedPostFormat]; - [self setStringEncoding:NSUTF8StringEncoding]; - return self; -} - -- (void)dealloc -{ -#if DEBUG_FORM_DATA_REQUEST - [debugBodyString release]; -#endif - - [postData release]; - [fileData release]; - [super dealloc]; -} - -#pragma mark setup request - -- (void)addPostValue:(id )value forKey:(NSString *)key -{ - if (![self postData]) { - [self setPostData:[NSMutableArray array]]; - } - [[self postData] addObject:[NSDictionary dictionaryWithObjectsAndKeys:[value description],@"value",key,@"key",nil]]; -} - -- (void)setPostValue:(id )value forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self postData] count]; i++) { - NSDictionary *val = [[self postData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self postData] removeObjectAtIndex:i]; - i--; - } - } - [self addPostValue:value forKey:key]; -} - - -- (void)addFile:(NSString *)filePath forKey:(NSString *)key -{ - [self addFile:filePath withFileName:nil andContentType:nil forKey:key]; -} - -- (void)addFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - if (![self fileData]) { - [self setFileData:[NSMutableArray array]]; - } - - // If data is a path to a local file - if ([data isKindOfClass:[NSString class]]) { - BOOL isDirectory = NO; - BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:(NSString *)data isDirectory:&isDirectory]; - if (!fileExists || isDirectory) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"No file exists at %@",data],NSLocalizedDescriptionKey,nil]]]; - } - - // If the caller didn't specify a custom file name, we'll use the file name of the file we were passed - if (!fileName) { - fileName = [(NSString *)data lastPathComponent]; - } - - // If we were given the path to a file, and the user didn't specify a mime type, we can detect it from the file extension - if (!contentType) { - contentType = [ASIHTTPRequest mimeTypeForFileAtPath:data]; - } - } - - NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil]; - [[self fileData] addObject:fileInfo]; -} - - -- (void)setFile:(NSString *)filePath forKey:(NSString *)key -{ - [self setFile:filePath withFileName:nil andContentType:nil forKey:key]; -} - -- (void)setFile:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self fileData] count]; i++) { - NSDictionary *val = [[self fileData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self fileData] removeObjectAtIndex:i]; - i--; - } - } - [self addFile:data withFileName:fileName andContentType:contentType forKey:key]; -} - -- (void)addData:(NSData *)data forKey:(NSString *)key -{ - [self addData:data withFileName:@"file" andContentType:nil forKey:key]; -} - -- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - if (![self fileData]) { - [self setFileData:[NSMutableArray array]]; - } - if (!contentType) { - contentType = @"application/octet-stream"; - } - - NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys:data, @"data", contentType, @"contentType", fileName, @"fileName", key, @"key", nil]; - [[self fileData] addObject:fileInfo]; -} - -- (void)setData:(NSData *)data forKey:(NSString *)key -{ - [self setData:data withFileName:@"file" andContentType:nil forKey:key]; -} - -- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key -{ - // Remove any existing value - NSUInteger i; - for (i=0; i<[[self fileData] count]; i++) { - NSDictionary *val = [[self fileData] objectAtIndex:i]; - if ([[val objectForKey:@"key"] isEqualToString:key]) { - [[self fileData] removeObjectAtIndex:i]; - i--; - } - } - [self addData:data withFileName:fileName andContentType:contentType forKey:key]; -} - -- (void)buildPostBody -{ - if ([self haveBuiltPostBody]) { - return; - } - -#if DEBUG_FORM_DATA_REQUEST - [self setDebugBodyString:@""]; -#endif - - if (![self postData] && ![self fileData]) { - [super buildPostBody]; - return; - } - if ([[self fileData] count] > 0) { - [self setShouldStreamPostDataFromDisk:YES]; - } - - if ([self postFormat] == ASIURLEncodedPostFormat) { - [self buildURLEncodedPostBody]; - } else { - [self buildMultipartFormDataPostBody]; - } - - [super buildPostBody]; - -#if DEBUG_FORM_DATA_REQUEST - NSLog(@"%@",[self debugBodyString]); - [self setDebugBodyString:nil]; -#endif -} - - -- (void)buildMultipartFormDataPostBody -{ -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"\r\n==== Building a multipart/form-data body ====\r\n"]; -#endif - - NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); - - // Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does. - NSString *stringBoundary = @"0xKhTmLbOuNdArY"; - - [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, stringBoundary]]; - - [self appendPostString:[NSString stringWithFormat:@"--%@\r\n",stringBoundary]]; - - // Adds post data - NSString *endItemBoundary = [NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary]; - NSUInteger i=0; - for (NSDictionary *val in [self postData]) { - [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",[val objectForKey:@"key"]]]; - [self appendPostString:[val objectForKey:@"value"]]; - i++; - if (i != [[self postData] count] || [[self fileData] count] > 0) { //Only add the boundary if this is not the last item in the post body - [self appendPostString:endItemBoundary]; - } - } - - // Adds files to upload - i=0; - for (NSDictionary *val in [self fileData]) { - - [self appendPostString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", [val objectForKey:@"key"], [val objectForKey:@"fileName"]]]; - [self appendPostString:[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", [val objectForKey:@"contentType"]]]; - - id data = [val objectForKey:@"data"]; - if ([data isKindOfClass:[NSString class]]) { - [self appendPostDataFromFile:data]; - } else { - [self appendPostData:data]; - } - i++; - // Only add the boundary if this is not the last item in the post body - if (i != [[self fileData] count]) { - [self appendPostString:endItemBoundary]; - } - } - - [self appendPostString:[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary]]; - -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"==== End of multipart/form-data body ====\r\n"]; -#endif -} - -- (void)buildURLEncodedPostBody -{ - - // We can't post binary data using application/x-www-form-urlencoded - if ([[self fileData] count] > 0) { - [self setPostFormat:ASIMultipartFormDataPostFormat]; - [self buildMultipartFormDataPostBody]; - return; - } - -#if DEBUG_FORM_DATA_REQUEST - [self addToDebugBody:@"\r\n==== Building an application/x-www-form-urlencoded body ====\r\n"]; -#endif - - - NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding([self stringEncoding])); - - [self addRequestHeader:@"Content-Type" value:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset]]; - - - NSUInteger i=0; - NSUInteger count = [[self postData] count]-1; - for (NSDictionary *val in [self postData]) { - NSString *data = [NSString stringWithFormat:@"%@=%@%@", [self encodeURL:[val objectForKey:@"key"]], [self encodeURL:[val objectForKey:@"value"]],(i -#if TARGET_OS_IPHONE - #import -#endif -#import -#import "ASIHTTPRequestConfig.h" -#import "ASIHTTPRequestDelegate.h" -#import "ASIProgressDelegate.h" -#import "ASICacheDelegate.h" - -extern NSString *ASIHTTPRequestVersion; - -// Make targeting different platforms more reliable -// See: http://www.blumtnwerx.com/blog/2009/06/cross-sdk-code-hygiene-in-xcode/ -#ifndef __IPHONE_3_2 - #define __IPHONE_3_2 30200 -#endif -#ifndef __IPHONE_4_0 - #define __IPHONE_4_0 40000 -#endif -#ifndef __MAC_10_5 - #define __MAC_10_5 1050 -#endif -#ifndef __MAC_10_6 - #define __MAC_10_6 1060 -#endif - -typedef enum _ASIAuthenticationState { - ASINoAuthenticationNeededYet = 0, - ASIHTTPAuthenticationNeeded = 1, - ASIProxyAuthenticationNeeded = 2 -} ASIAuthenticationState; - -typedef enum _ASINetworkErrorType { - ASIConnectionFailureErrorType = 1, - ASIRequestTimedOutErrorType = 2, - ASIAuthenticationErrorType = 3, - ASIRequestCancelledErrorType = 4, - ASIUnableToCreateRequestErrorType = 5, - ASIInternalErrorWhileBuildingRequestType = 6, - ASIInternalErrorWhileApplyingCredentialsType = 7, - ASIFileManagementError = 8, - ASITooMuchRedirectionErrorType = 9, - ASIUnhandledExceptionError = 10 - -} ASINetworkErrorType; - -// The error domain that all errors generated by ASIHTTPRequest use -extern NSString* const NetworkRequestErrorDomain; - -// You can use this number to throttle upload and download bandwidth in iPhone OS apps send or receive a large amount of data -// This may help apps that might otherwise be rejected for inclusion into the app store for using excessive bandwidth -// This number is not official, as far as I know there is no officially documented bandwidth limit -extern unsigned long const ASIWWANBandwidthThrottleAmount; - -@interface ASIHTTPRequest : NSOperation { - - // The url for this operation, should include GET params in the query string where appropriate - NSURL *url; - - // Will always contain the original url used for making the request (the value of url can change when a request is redirected) - NSURL *originalURL; - - // The delegate, you need to manage setting and talking to your delegate in your subclasses - id delegate; - - // Another delegate that is also notified of request status changes and progress updates - // Generally, you won't use this directly, but ASINetworkQueue sets itself as the queue so it can proxy updates to its own delegates - id queue; - - // HTTP method to use (GET / POST / PUT / DELETE / HEAD). Defaults to GET - NSString *requestMethod; - - // Request body - only used when the whole body is stored in memory (shouldStreamPostDataFromDisk is false) - NSMutableData *postBody; - - // gzipped request body used when shouldCompressRequestBody is YES - NSData *compressedPostBody; - - // When true, post body will be streamed from a file on disk, rather than loaded into memory at once (useful for large uploads) - // Automatically set to true in ASIFormDataRequests when using setFile:forKey: - BOOL shouldStreamPostDataFromDisk; - - // Path to file used to store post body (when shouldStreamPostDataFromDisk is true) - // You can set this yourself - useful if you want to PUT a file from local disk - NSString *postBodyFilePath; - - // Path to a temporary file used to store a deflated post body (when shouldCompressPostBody is YES) - NSString *compressedPostBodyFilePath; - - // Set to true when ASIHTTPRequest automatically created a temporary file containing the request body (when true, the file at postBodyFilePath will be deleted at the end of the request) - BOOL didCreateTemporaryPostDataFile; - - // Used when writing to the post body when shouldStreamPostDataFromDisk is true (via appendPostData: or appendPostDataFromFile:) - NSOutputStream *postBodyWriteStream; - - // Used for reading from the post body when sending the request - NSInputStream *postBodyReadStream; - - // Dictionary for custom HTTP request headers - NSMutableDictionary *requestHeaders; - - // Set to YES when the request header dictionary has been populated, used to prevent this happening more than once - BOOL haveBuiltRequestHeaders; - - // Will be populated with HTTP response headers from the server - NSDictionary *responseHeaders; - - // Can be used to manually insert cookie headers to a request, but it's more likely that sessionCookies will do this for you - NSMutableArray *requestCookies; - - // Will be populated with cookies - NSArray *responseCookies; - - // If use useCookiePersistence is true, network requests will present valid cookies from previous requests - BOOL useCookiePersistence; - - // If useKeychainPersistence is true, network requests will attempt to read credentials from the keychain, and will save them in the keychain when they are successfully presented - BOOL useKeychainPersistence; - - // If useSessionPersistence is true, network requests will save credentials and reuse for the duration of the session (until clearSession is called) - BOOL useSessionPersistence; - - // If allowCompressedResponse is true, requests will inform the server they can accept compressed data, and will automatically decompress gzipped responses. Default is true. - BOOL allowCompressedResponse; - - // If shouldCompressRequestBody is true, the request body will be gzipped. Default is false. - // You will probably need to enable this feature on your webserver to make this work. Tested with apache only. - BOOL shouldCompressRequestBody; - - // When downloadDestinationPath is set, the result of this request will be downloaded to the file at this location - // If downloadDestinationPath is not set, download data will be stored in memory - NSString *downloadDestinationPath; - - //The location that files will be downloaded to. Once a download is complete, files will be decompressed (if necessary) and moved to downloadDestinationPath - NSString *temporaryFileDownloadPath; - - // Used for writing data to a file when downloadDestinationPath is set - NSOutputStream *fileDownloadOutputStream; - - // When the request fails or completes successfully, complete will be true - BOOL complete; - - // If an error occurs, error will contain an NSError - // If error code is = ASIConnectionFailureErrorType (1, Connection failure occurred) - inspect [[error userInfo] objectForKey:NSUnderlyingErrorKey] for more information - NSError *error; - - // Username and password used for authentication - NSString *username; - NSString *password; - - // Domain used for NTLM authentication - NSString *domain; - - // Username and password used for proxy authentication - NSString *proxyUsername; - NSString *proxyPassword; - - // Domain used for NTLM proxy authentication - NSString *proxyDomain; - - // Delegate for displaying upload progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) - id uploadProgressDelegate; - - // Delegate for displaying download progress (usually an NSProgressIndicator, but you can supply a different object and handle this yourself) - id downloadProgressDelegate; - - // Whether we've seen the headers of the response yet - BOOL haveExaminedHeaders; - - // Data we receive will be stored here. Data may be compressed unless allowCompressedResponse is false - you should use [request responseData] instead in most cases - NSMutableData *rawResponseData; - - // Used for sending and receiving data - CFHTTPMessageRef request; - NSInputStream *readStream; - - // Used for authentication - CFHTTPAuthenticationRef requestAuthentication; - NSDictionary *requestCredentials; - - // Used during NTLM authentication - int authenticationRetryCount; - - // Authentication scheme (Basic, Digest, NTLM) - NSString *authenticationScheme; - - // Realm for authentication when credentials are required - NSString *authenticationRealm; - - // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a server that requires authentication - // The dialog will not be shown if your delegate responds to authenticationNeededForRequest: - // Default is NO. - BOOL shouldPresentAuthenticationDialog; - - // When YES, ASIHTTPRequest will present a dialog allowing users to enter credentials when no-matching credentials were found for a proxy server that requires authentication - // The dialog will not be shown if your delegate responds to proxyAuthenticationNeededForRequest: - // Default is YES (basically, because most people won't want the hassle of adding support for authenticating proxies to their apps) - BOOL shouldPresentProxyAuthenticationDialog; - - // Used for proxy authentication - CFHTTPAuthenticationRef proxyAuthentication; - NSDictionary *proxyCredentials; - - // Used during authentication with an NTLM proxy - int proxyAuthenticationRetryCount; - - // Authentication scheme for the proxy (Basic, Digest, NTLM) - NSString *proxyAuthenticationScheme; - - // Realm for proxy authentication when credentials are required - NSString *proxyAuthenticationRealm; - - // HTTP status code, eg: 200 = OK, 404 = Not found etc - int responseStatusCode; - - // Description of the HTTP status code - NSString *responseStatusMessage; - - // Size of the response - unsigned long long contentLength; - - // Size of the partially downloaded content - unsigned long long partialDownloadSize; - - // Size of the POST payload - unsigned long long postLength; - - // The total amount of downloaded data - unsigned long long totalBytesRead; - - // The total amount of uploaded data - unsigned long long totalBytesSent; - - // Last amount of data read (used for incrementing progress) - unsigned long long lastBytesRead; - - // Last amount of data sent (used for incrementing progress) - unsigned long long lastBytesSent; - - // This lock prevents the operation from being cancelled at an inopportune moment - NSRecursiveLock *cancelledLock; - - // Called on the delegate (if implemented) when the request starts. Default is requestStarted: - SEL didStartSelector; - - // Called on the delegate (if implemented) when the request receives response headers. Default is requestDidReceiveResponseHeaders: - SEL didReceiveResponseHeadersSelector; - - // Called on the delegate (if implemented) when the request completes successfully. Default is requestFinished: - SEL didFinishSelector; - - // Called on the delegate (if implemented) when the request fails. Default is requestFailed: - SEL didFailSelector; - - // Called on the delegate (if implemented) when the request receives data. Default is request:didReceiveData: - // If you set this and implement the method in your delegate, you must handle the data yourself - ASIHTTPRequest will not populate responseData or write the data to downloadDestinationPath - SEL didReceiveDataSelector; - - // Used for recording when something last happened during the request, we will compare this value with the current date to time out requests when appropriate - NSDate *lastActivityTime; - - // Number of seconds to wait before timing out - default is 10 - NSTimeInterval timeOutSeconds; - - // Will be YES when a HEAD request will handle the content-length before this request starts - BOOL shouldResetUploadProgress; - BOOL shouldResetDownloadProgress; - - // Used by HEAD requests when showAccurateProgress is YES to preset the content-length for this request - ASIHTTPRequest *mainRequest; - - // When NO, this request will only update the progress indicator when it completes - // When YES, this request will update the progress indicator according to how much data it has received so far - // The default for requests is YES - // Also see the comments in ASINetworkQueue.h - BOOL showAccurateProgress; - - // Used to ensure the progress indicator is only incremented once when showAccurateProgress = NO - BOOL updatedProgress; - - // Prevents the body of the post being built more than once (largely for subclasses) - BOOL haveBuiltPostBody; - - // Used internally, may reflect the size of the internal buffer used by CFNetwork - // POST / PUT operations with body sizes greater than uploadBufferSize will not timeout unless more than uploadBufferSize bytes have been sent - // Likely to be 32KB on iPhone 3.0, 128KB on Mac OS X Leopard and iPhone 2.2.x - unsigned long long uploadBufferSize; - - // Text encoding for responses that do not send a Content-Type with a charset value. Defaults to NSISOLatin1StringEncoding - NSStringEncoding defaultResponseEncoding; - - // The text encoding of the response, will be defaultResponseEncoding if the server didn't specify. Can't be set. - NSStringEncoding responseEncoding; - - // Tells ASIHTTPRequest not to delete partial downloads, and allows it to use an existing file to resume a download. Defaults to NO. - BOOL allowResumeForFileDownloads; - - // Custom user information associated with the request - NSDictionary *userInfo; - - // Use HTTP 1.0 rather than 1.1 (defaults to false) - BOOL useHTTPVersionOne; - - // When YES, requests will automatically redirect when they get a HTTP 30x header (defaults to YES) - BOOL shouldRedirect; - - // Used internally to tell the main loop we need to stop and retry with a new url - BOOL needsRedirect; - - // Incremented every time this request redirects. When it reaches 5, we give up - int redirectCount; - - // When NO, requests will not check the secure certificate is valid (use for self-signed certificates during development, DO NOT USE IN PRODUCTION) Default is YES - BOOL validatesSecureCertificate; - - // Details on the proxy to use - you could set these yourself, but it's probably best to let ASIHTTPRequest detect the system proxy settings - NSString *proxyHost; - int proxyPort; - - // ASIHTTPRequest will assume kCFProxyTypeHTTP if the proxy type could not be automatically determined - // Set to kCFProxyTypeSOCKS if you are manually configuring a SOCKS proxy - NSString *proxyType; - - // URL for a PAC (Proxy Auto Configuration) file. If you want to set this yourself, it's probably best if you use a local file - NSURL *PACurl; - - // See ASIAuthenticationState values above. 0 == default == No authentication needed yet - ASIAuthenticationState authenticationNeeded; - - // When YES, ASIHTTPRequests will present credentials from the session store for requests to the same server before being asked for them - // This avoids an extra round trip for requests after authentication has succeeded, which is much for efficient for authenticated requests with large bodies, or on slower connections - // Set to NO to only present credentials when explicitly asked for them - // This only affects credentials stored in the session cache when useSessionPersistence is YES. Credentials from the keychain are never presented unless the server asks for them - // Default is YES - BOOL shouldPresentCredentialsBeforeChallenge; - - // YES when the request hasn't finished yet. Will still be YES even if the request isn't doing anything (eg it's waiting for delegate authentication). READ-ONLY - BOOL inProgress; - - // Used internally to track whether the stream is scheduled on the run loop or not - // Bandwidth throttling can unschedule the stream to slow things down while a request is in progress - BOOL readStreamIsScheduled; - - // Set to allow a request to automatically retry itself on timeout - // Default is zero - timeout will stop the request - int numberOfTimesToRetryOnTimeout; - - // The number of times this request has retried (when numberOfTimesToRetryOnTimeout > 0) - int retryCount; - - // When YES, requests will keep the connection to the server alive for a while to allow subsequent requests to re-use it for a substantial speed-boost - // Persistent connections will not be used if the server explicitly closes the connection - // Default is YES - BOOL shouldAttemptPersistentConnection; - - // Number of seconds to keep an inactive persistent connection open on the client side - // Default is 60 - // If we get a keep-alive header, this is this value is replaced with how long the server told us to keep the connection around - // A future date is created from this and used for expiring the connection, this is stored in connectionInfo's expires value - NSTimeInterval persistentConnectionTimeoutSeconds; - - // Set to yes when an appropriate keep-alive header is found - BOOL connectionCanBeReused; - - // Stores information about the persistent connection that is currently in use. - // It may contain: - // * The id we set for a particular connection, incremented every time we want to specify that we need a new connection - // * The date that connection should expire - // * A host, port and scheme for the connection. These are used to determine whether that connection can be reused by a subsequent request (all must match the new request) - // * An id for the request that is currently using the connection. This is used for determining if a connection is available or not (we store a number rather than a reference to the request so we don't need to hang onto a request until the connection expires) - // * A reference to the stream that is currently using the connection. This is necessary because we need to keep the old stream open until we've opened a new one. - // The stream will be closed + released either when another request comes to use the connection, or when the timer fires to tell the connection to expire - NSMutableDictionary *connectionInfo; - - // When set to YES, 301 and 302 automatic redirects will use the original method and and body, according to the HTTP 1.1 standard - // Default is NO (to follow the behaviour of most browsers) - BOOL shouldUseRFC2616RedirectBehaviour; - - // Used internally to record when a request has finished downloading data - BOOL downloadComplete; - - // An ID that uniquely identifies this request - primarily used for debugging persistent connections - NSNumber *requestID; - - // Will be ASIHTTPRequestRunLoopMode for synchronous requests, NSDefaultRunLoopMode for all other requests - NSString *runLoopMode; - - // This timer checks up on the request every 0.25 seconds, and updates progress - NSTimer *statusTimer; - - - // The download cache that will be used for this request (use [ASIHTTPRequest setDefaultCache:cache] to configure a default cache - id downloadCache; - - // The cache policy that will be used for this request - See ASICacheDelegate.h for possible values - ASICachePolicy cachePolicy; - - // The cache storage policy that will be used for this request - See ASICacheDelegate.h for possible values - ASICacheStoragePolicy cacheStoragePolicy; - - // Will be true when the response was pulled from the cache rather than downloaded - BOOL didUseCachedResponse; - - // Set secondsToCache to use a custom time interval for expiring the response when it is stored in a cache - NSTimeInterval secondsToCache; -} - -#pragma mark init / dealloc - -// Should be an HTTP or HTTPS url, may include username and password if appropriate -- (id)initWithURL:(NSURL *)newURL; - -// Convenience constructor -+ (id)requestWithURL:(NSURL *)newURL; - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache; -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy; - -#pragma mark setup request - -// Add a custom header to the request -- (void)addRequestHeader:(NSString *)header value:(NSString *)value; - -// Called during buildRequestHeaders and after a redirect to create a cookie header from request cookies and the global store -- (void)applyCookieHeader; - -// Populate the request headers dictionary. Called before a request is started, or by a HEAD request that needs to borrow them -- (void)buildRequestHeaders; - -// Used to apply authorization header to a request before it is sent (when shouldPresentCredentialsBeforeChallenge is YES) -- (void)applyAuthorizationHeader; - - -// Create the post body -- (void)buildPostBody; - -// Called to add data to the post body. Will append to postBody when shouldStreamPostDataFromDisk is false, or write to postBodyWriteStream when true -- (void)appendPostData:(NSData *)data; -- (void)appendPostDataFromFile:(NSString *)file; - -#pragma mark get information about this request - -// Returns the contents of the result as an NSString (not appropriate for binary data - used responseData instead) -- (NSString *)responseString; - -// Response data, automatically uncompressed where appropriate -- (NSData *)responseData; - -// Returns true if the response was gzip compressed -- (BOOL)isResponseCompressed; - -#pragma mark running a request - - -// Run a request synchronously, and return control when the request completes or fails -- (void)startSynchronous; - -// Run request in the background -- (void)startAsynchronous; - -#pragma mark request logic - -// Call to delete the temporary file used during a file download (if it exists) -// No need to call this if the request succeeds - it is removed automatically -- (void)removeTemporaryDownloadFile; - -// Call to remove the file used as the request body -// No need to call this if the request succeeds and you didn't specify postBodyFilePath manually - it is removed automatically -- (void)removePostDataFile; - -#pragma mark HEAD request - -// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) -- (ASIHTTPRequest *)HEADRequest; - -#pragma mark upload/download progress - -// Called approximately every 0.25 seconds to update the progress delegates -- (void)updateProgressIndicators; - -// Updates upload progress (notifies the queue and/or uploadProgressDelegate of this request) -- (void)updateUploadProgress; - -// Updates download progress (notifies the queue and/or uploadProgressDelegate of this request) -- (void)updateDownloadProgress; - -// Called when authorisation is needed, as we only find out we don't have permission to something when the upload is complete -- (void)removeUploadProgressSoFar; - -// Called when we get a content-length header and shouldResetDownloadProgress is true -- (void)incrementDownloadSizeBy:(long long)length; - -// Called when a request starts and shouldResetUploadProgress is true -// Also called (with a negative length) to remove the size of the underlying buffer used for uploading -- (void)incrementUploadSizeBy:(long long)length; - -// Helper method for interacting with progress indicators to abstract the details of different APIS (NSProgressIndicator and UIProgressView) -+ (void)updateProgressIndicator:(id)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total; - -// Helper method used for performing invocations on the main thread (used for progress) -+ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount; - -#pragma mark handling request complete / failure - -// Called when a request starts, lets the delegate know via didStartSelector -- (void)requestStarted; - -// Called when a request receives response headers, lets the delegate know via didReceiveResponseHeadersSelector -- (void)requestReceivedResponseHeaders; - -// Called when a request completes successfully, lets the delegate know via didFinishSelector -- (void)requestFinished; - -// Called when a request fails, and lets the delegate know via didFailSelector -- (void)failWithError:(NSError *)theError; - -// Called to retry our request when our persistent connection is closed -// Returns YES if we haven't already retried, and connection will be restarted -// Otherwise, returns NO, and nothing will happen -- (BOOL)retryUsingNewConnection; - -#pragma mark parsing HTTP response headers - -// Reads the response headers to find the content length, encoding, cookies for the session -// Also initiates request redirection when shouldRedirect is true -// And works out if HTTP auth is required -- (void)readResponseHeaders; - -// Attempts to set the correct encoding by looking at the Content-Type header, if this is one -- (void)parseStringEncodingFromHeaders; - -#pragma mark http authentication stuff - -// Apply credentials to this request -- (BOOL)applyCredentials:(NSDictionary *)newCredentials; -- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials; - -// Attempt to obtain credentials for this request from the URL, username and password or keychain -- (NSMutableDictionary *)findCredentials; -- (NSMutableDictionary *)findProxyCredentials; - -// Unlock (unpause) the request thread so it can resume the request -// Should be called by delegates when they have populated the authentication information after an authentication challenge -- (void)retryUsingSuppliedCredentials; - -// Should be called by delegates when they wish to cancel authentication and stop -- (void)cancelAuthentication; - -// Apply authentication information and resume the request after an authentication challenge -- (void)attemptToApplyCredentialsAndResume; -- (void)attemptToApplyProxyCredentialsAndResume; - -// Attempt to show the built-in authentication dialog, returns YES if credentials were supplied, NO if user cancelled dialog / dialog is disabled / running on main thread -// Currently only used on iPhone OS -- (BOOL)showProxyAuthenticationDialog; -- (BOOL)showAuthenticationDialog; - -// Construct a basic authentication header from the username and password supplied, and add it to the request headers -// Used when shouldPresentCredentialsBeforeChallenge is YES -- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword; - -#pragma mark stream status handlers - -// CFnetwork event handlers -- (void)handleNetworkEvent:(CFStreamEventType)type; -- (void)handleBytesAvailable; -- (void)handleStreamComplete; -- (void)handleStreamError; - -#pragma mark persistent connections - -// Get the ID of the connection this request used (only really useful in tests and debugging) -- (NSNumber *)connectionID; - -// Called automatically when a request is started to clean up any persistent connections that have expired -+ (void)expirePersistentConnections; - -#pragma mark default time out - -+ (NSTimeInterval)defaultTimeOutSeconds; -+ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds; - -#pragma mark session credentials - -+ (NSMutableArray *)sessionProxyCredentialsStore; -+ (NSMutableArray *)sessionCredentialsStore; - -+ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; -+ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials; - -+ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; -+ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials; - -- (NSDictionary *)findSessionProxyAuthenticationCredentials; -- (NSDictionary *)findSessionAuthenticationCredentials; - -#pragma mark keychain storage - -// Save credentials for this request to the keychain -- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials; - -// Save credentials to the keychain -+ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm; - -// Return credentials from the keychain -+ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; - -// Remove credentials from the keychain -+ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; -+ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm; - -// We keep track of any cookies we accept, so that we can remove them from the persistent store later -+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies; -+ (NSMutableArray *)sessionCookies; - -// Adds a cookie to our list of cookies we've accepted, checking first for an old version of the same cookie and removing that -+ (void)addSessionCookie:(NSHTTPCookie *)newCookie; - -// Dump all session data (authentication and cookies) -+ (void)clearSession; - -#pragma mark gzip decompression - -// Uncompress gzipped data with zlib -+ (NSData *)uncompressZippedData:(NSData*)compressedData; - -// Uncompress gzipped data from a file into another file, used when downloading to a file -+ (int)uncompressZippedDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; -+ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest; - -#pragma mark gzip compression - -// Compress data with gzip using zlib -+ (NSData *)compressData:(NSData*)uncompressedData; - -// gzip compress data from a file, saving to another file, used for uploading when shouldCompressRequestBody is true -+ (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath; -+ (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest; - -#pragma mark get user agent - -// Will be used as a user agent if requests do not specify a custom user agent -// Is only used when you have specified a Bundle Display Name (CFDisplayBundleName) or Bundle Name (CFBundleName) in your plist -+ (NSString *)defaultUserAgentString; - -#pragma mark proxy autoconfiguration - -// Returns an array of proxies to use for a particular url, given the url of a PAC script -+ (NSArray *)proxiesForURL:(NSURL *)theURL fromPAC:(NSURL *)pacScriptURL; - -#pragma mark mime-type detection - -// Return the mime type for a file -+ (NSString *)mimeTypeForFileAtPath:(NSString *)path; - -#pragma mark bandwidth measurement / throttling - -// The maximum number of bytes ALL requests can send / receive in a second -// This is a rough figure. The actual amount used will be slightly more, this does not include HTTP headers -+ (unsigned long)maxBandwidthPerSecond; -+ (void)setMaxBandwidthPerSecond:(unsigned long)bytes; - -// Get a rough average (for the last 5 seconds) of how much bandwidth is being used, in bytes -+ (unsigned long)averageBandwidthUsedPerSecond; - -- (void)performThrottling; - -// Will return YES is bandwidth throttling is currently in use -+ (BOOL)isBandwidthThrottled; - -// Used internally to record bandwidth use, and by ASIInputStreams when uploading. It's probably best if you don't mess with this. -+ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes; - -// On iPhone, ASIHTTPRequest can automatically turn throttling on and off as the connection type changes between WWAN and WiFi - -#if TARGET_OS_IPHONE -// Set to YES to automatically turn on throttling when WWAN is connected, and automatically turn it off when it isn't -+ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle; - -// Turns on throttling automatically when WWAN is connected using a custom limit, and turns it off automatically when it isn't -+ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit; - -#pragma mark reachability - -// Returns YES when an iPhone OS device is connected via WWAN, false when connected via WIFI or not connected -+ (BOOL)isNetworkReachableViaWWAN; - -#endif - -#pragma mark cache - -+ (void)setDefaultCache:(id )cache; -+ (id )defaultCache; - -// Returns the maximum amount of data we can read as part of the current measurement period, and sleeps this thread if our allowance is used up -+ (unsigned long)maxUploadReadLength; - -#pragma mark network activity - -+ (BOOL)isNetworkInUse; -#if TARGET_OS_IPHONE -+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate; -#endif - -#pragma mark miscellany - -// Used for generating Authorization header when using basic authentication when shouldPresentCredentialsBeforeChallenge is true -// And also by ASIS3Request -+ (NSString *)base64forData:(NSData *)theData; - -// Returns a date from a string in RFC1123 format -+ (NSDate *)dateFromRFC1123String:(NSString *)string; - -#pragma mark threading behaviour - -// In the default implementation, all requests run in a single background thread -// Advanced users only: Override this method in a subclass for a different threading behaviour -// Eg: return [NSThread mainThread] to run all requests in the main thread -// Alternatively, you can create a thread on demand, or manage a pool of threads -// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) -// Requests will stop the runloop when they complete -// If you have multiple requests sharing the thread you'll need to restart the runloop when this happens -+ (NSThread *)threadForRequest:(ASIHTTPRequest *)request; - - -#pragma mark === - -@property (retain) NSString *username; -@property (retain) NSString *password; -@property (retain) NSString *domain; - -@property (retain) NSString *proxyUsername; -@property (retain) NSString *proxyPassword; -@property (retain) NSString *proxyDomain; - -@property (retain) NSString *proxyHost; -@property (assign) int proxyPort; -@property (retain) NSString *proxyType; - -@property (retain,setter=setURL:) NSURL *url; -@property (retain) NSURL *originalURL; -@property (assign, nonatomic) id delegate; -@property (assign, nonatomic) id queue; -@property (assign, nonatomic) id uploadProgressDelegate; -@property (assign, nonatomic) id downloadProgressDelegate; -@property (assign) BOOL useKeychainPersistence; -@property (assign) BOOL useSessionPersistence; -@property (retain) NSString *downloadDestinationPath; -@property (retain) NSString *temporaryFileDownloadPath; -@property (assign) SEL didStartSelector; -@property (assign) SEL didReceiveResponseHeadersSelector; -@property (assign) SEL didFinishSelector; -@property (assign) SEL didFailSelector; -@property (assign) SEL didReceiveDataSelector; -@property (retain,readonly) NSString *authenticationRealm; -@property (retain,readonly) NSString *proxyAuthenticationRealm; -@property (retain) NSError *error; -@property (assign,readonly) BOOL complete; -@property (retain) NSDictionary *responseHeaders; -@property (retain) NSMutableDictionary *requestHeaders; -@property (retain) NSMutableArray *requestCookies; -@property (retain,readonly) NSArray *responseCookies; -@property (assign) BOOL useCookiePersistence; -@property (retain) NSDictionary *requestCredentials; -@property (retain) NSDictionary *proxyCredentials; -@property (assign,readonly) int responseStatusCode; -@property (retain,readonly) NSString *responseStatusMessage; -@property (retain) NSMutableData *rawResponseData; -@property (assign) NSTimeInterval timeOutSeconds; -@property (retain) NSString *requestMethod; -@property (retain) NSMutableData *postBody; -@property (assign,readonly) unsigned long long contentLength; -@property (assign) unsigned long long postLength; -@property (assign) BOOL shouldResetDownloadProgress; -@property (assign) BOOL shouldResetUploadProgress; -@property (assign) ASIHTTPRequest *mainRequest; -@property (assign) BOOL showAccurateProgress; -@property (assign,readonly) unsigned long long totalBytesRead; -@property (assign,readonly) unsigned long long totalBytesSent; -@property (assign) NSStringEncoding defaultResponseEncoding; -@property (assign,readonly) NSStringEncoding responseEncoding; -@property (assign) BOOL allowCompressedResponse; -@property (assign) BOOL allowResumeForFileDownloads; -@property (retain) NSDictionary *userInfo; -@property (retain) NSString *postBodyFilePath; -@property (assign) BOOL shouldStreamPostDataFromDisk; -@property (assign) BOOL didCreateTemporaryPostDataFile; -@property (assign) BOOL useHTTPVersionOne; -@property (assign, readonly) unsigned long long partialDownloadSize; -@property (assign) BOOL shouldRedirect; -@property (assign) BOOL validatesSecureCertificate; -@property (assign) BOOL shouldCompressRequestBody; -@property (retain) NSURL *PACurl; -@property (retain) NSString *authenticationScheme; -@property (retain) NSString *proxyAuthenticationScheme; -@property (assign) BOOL shouldPresentAuthenticationDialog; -@property (assign) BOOL shouldPresentProxyAuthenticationDialog; -@property (assign, readonly) ASIAuthenticationState authenticationNeeded; -@property (assign) BOOL shouldPresentCredentialsBeforeChallenge; -@property (assign, readonly) int authenticationRetryCount; -@property (assign, readonly) int proxyAuthenticationRetryCount; -@property (assign) BOOL haveBuiltRequestHeaders; -@property (assign, nonatomic) BOOL haveBuiltPostBody; -@property (assign, readonly) BOOL inProgress; -@property (assign) int numberOfTimesToRetryOnTimeout; -@property (assign, readonly) int retryCount; -@property (assign) BOOL shouldAttemptPersistentConnection; -@property (assign) NSTimeInterval persistentConnectionTimeoutSeconds; -@property (assign) BOOL shouldUseRFC2616RedirectBehaviour; -@property (assign, readonly) BOOL connectionCanBeReused; -@property (retain, readonly) NSNumber *requestID; -@property (assign) id downloadCache; -@property (assign) ASICachePolicy cachePolicy; -@property (assign) ASICacheStoragePolicy cacheStoragePolicy; -@property (assign, readonly) BOOL didUseCachedResponse; -@property (assign) NSTimeInterval secondsToCache; -@end diff --git a/ASIHTTP/ASIHTTPRequest.m b/ASIHTTP/ASIHTTPRequest.m deleted file mode 100644 index ff15080..0000000 --- a/ASIHTTP/ASIHTTPRequest.m +++ /dev/null @@ -1,4058 +0,0 @@ -// -// ASIHTTPRequest.m -// -// Created by Ben Copsey on 04/10/2007. -// Copyright 2007-2010 All-Seeing Interactive. All rights reserved. -// -// A guide to the main features is available at: -// http://allseeing-i.com/ASIHTTPRequest -// -// Portions are based on the ImageClient example from Apple: -// See: http://developer.apple.com/samplecode/ImageClient/listing37.html - -#import "ASIHTTPRequest.h" -#import -#if TARGET_OS_IPHONE -#import "Reachability.h" -#import "ASIAuthenticationDialog.h" -#import -#else -#import -#endif -#import "ASIInputStream.h" - - -// Automatically set on build - -NSString *ASIHTTPRequestVersion = @"v1.7-13 2010-07-02"; - -NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain"; - -static NSString *ASIHTTPRequestRunLoopMode = @"ASIHTTPRequestRunLoopMode"; - -static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred; - -// In memory caches of credentials, used on when useSessionPersistence is YES -static NSMutableArray *sessionCredentialsStore = nil; -static NSMutableArray *sessionProxyCredentialsStore = nil; - -// This lock mediates access to session credentials -static NSRecursiveLock *sessionCredentialsLock = nil; - -// We keep track of cookies we have received here so we can remove them from the sharedHTTPCookieStorage later -static NSMutableArray *sessionCookies = nil; - -// The number of times we will allow requests to redirect before we fail with a redirection error -const int RedirectionLimit = 5; - -// The default number of seconds to use for a timeout -static NSTimeInterval defaultTimeOutSeconds = 10; - -static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventType type, void *clientCallBackInfo) { - [((ASIHTTPRequest*)clientCallBackInfo) handleNetworkEvent: type]; -} - -// This lock prevents the operation from being cancelled while it is trying to update the progress, and vice versa -static NSRecursiveLock *progressLock; - -static NSError *ASIRequestCancelledError; -static NSError *ASIRequestTimedOutError; -static NSError *ASIAuthenticationError; -static NSError *ASIUnableToCreateRequestError; -static NSError *ASITooMuchRedirectionError; - -static NSMutableArray *bandwidthUsageTracker = nil; -static unsigned long averageBandwidthUsedPerSecond = 0; - -// These are used for queuing persistent connections on the same connection - -// Incremented every time we specify we want a new connection -static unsigned int nextConnectionNumberToCreate = 0; - -// An array of connectionInfo dictionaries. -// When attempting a persistent connection, we look here to try to find an existing connection to the same server that is currently not in use -static NSMutableArray *persistentConnectionsPool = nil; - -// Mediates access to the persistent connections pool -static NSRecursiveLock *connectionsLock = nil; - -// Each request gets a new id, we store this rather than a ref to the request itself in the connectionInfo dictionary. -// We do this so we don't have to keep the request around while we wait for the connection to expire -static unsigned int nextRequestID = 0; - -// Records how much bandwidth all requests combined have used in the last second -static unsigned long bandwidthUsedInLastSecond = 0; - -// A date one second in the future from the time it was created -static NSDate *bandwidthMeasurementDate = nil; - -// Since throttling variables are shared among all requests, we'll use a lock to mediate access -static NSLock *bandwidthThrottlingLock = nil; - -// the maximum number of bytes that can be transmitted in one second -static unsigned long maxBandwidthPerSecond = 0; - -// A default figure for throttling bandwidth on mobile devices -unsigned long const ASIWWANBandwidthThrottleAmount = 14800; - -#if TARGET_OS_IPHONE -// YES when bandwidth throttling is active -// This flag does not denote whether throttling is turned on - rather whether it is currently in use -// It will be set to NO when throttling was turned on with setShouldThrottleBandwidthForWWAN, but a WI-FI connection is active -static BOOL isBandwidthThrottled = NO; - -// When YES, bandwidth will be automatically throttled when using WWAN (3G/Edge/GPRS) -// Wifi will not be throttled -static BOOL shouldThrottleBandwithForWWANOnly = NO; -#endif - -// Mediates access to the session cookies so requests -static NSRecursiveLock *sessionCookiesLock = nil; - -// This lock ensures delegates only receive one notification that authentication is required at once -// When using ASIAuthenticationDialogs, it also ensures only one dialog is shown at once -// If a request can't aquire the lock immediately, it means a dialog is being shown or a delegate is handling the authentication challenge -// Once it gets the lock, it will try to look for existing credentials again rather than showing the dialog / notifying the delegate -// This is so it can make use of any credentials supplied for the other request, if they are appropriate -static NSRecursiveLock *delegateAuthenticationLock = nil; - -// When throttling bandwidth, Set to a date in future that we will allow all requests to wake up and reschedule their streams -static NSDate *throttleWakeUpTime = nil; - -static id defaultCache = nil; - - -// Used for tracking when requests are using the network -static unsigned int runningRequestCount = 0; - -#if TARGET_OS_IPHONE -// Use [ASIHTTPRequest setShouldUpdateNetworkActivityIndicator:NO] if you want to manage it yourself -static BOOL shouldUpdateNetworkActivityIndicator = YES; -#endif - -//**Queue stuff**/ - -// The thread all requests will run on -// Hangs around forever, but will be blocked unless there are requests underway -static NSThread *networkThread = nil; - -static NSOperationQueue *sharedQueue = nil; - -// Private stuff -@interface ASIHTTPRequest () - -- (void)cancelLoad; - -- (void)destroyReadStream; -- (void)scheduleReadStream; -- (void)unscheduleReadStream; - -- (BOOL)askDelegateForCredentials; -- (BOOL)askDelegateForProxyCredentials; -+ (void)measureBandwidthUsage; -+ (void)recordBandwidthUsage; -- (void)startRequest; -- (void)updateStatus:(NSTimer *)timer; -- (void)checkRequestStatus; - -- (void)markAsFinished; -- (void)performRedirect; -- (BOOL)shouldTimeOut; - - -- (BOOL)useDataFromCache; - -#if TARGET_OS_IPHONE -+ (void)registerForNetworkReachabilityNotifications; -+ (void)unsubscribeFromNetworkReachabilityNotifications; -// Called when the status of the network changes -+ (void)reachabilityChanged:(NSNotification *)note; - -#endif - -@property (assign) BOOL complete; -@property (retain) NSArray *responseCookies; -@property (assign) int responseStatusCode; -@property (retain, nonatomic) NSDate *lastActivityTime; -@property (assign) unsigned long long contentLength; -@property (assign) unsigned long long partialDownloadSize; -@property (assign, nonatomic) unsigned long long uploadBufferSize; -@property (assign) NSStringEncoding responseEncoding; -@property (retain, nonatomic) NSOutputStream *postBodyWriteStream; -@property (retain, nonatomic) NSInputStream *postBodyReadStream; -@property (assign) unsigned long long totalBytesRead; -@property (assign) unsigned long long totalBytesSent; -@property (assign, nonatomic) unsigned long long lastBytesRead; -@property (assign, nonatomic) unsigned long long lastBytesSent; -@property (retain) NSRecursiveLock *cancelledLock; -@property (retain, nonatomic) NSOutputStream *fileDownloadOutputStream; -@property (assign) int authenticationRetryCount; -@property (assign) int proxyAuthenticationRetryCount; -@property (assign, nonatomic) BOOL updatedProgress; -@property (assign, nonatomic) BOOL needsRedirect; -@property (assign, nonatomic) int redirectCount; -@property (retain, nonatomic) NSData *compressedPostBody; -@property (retain, nonatomic) NSString *compressedPostBodyFilePath; -@property (retain) NSString *authenticationRealm; -@property (retain) NSString *proxyAuthenticationRealm; -@property (retain) NSString *responseStatusMessage; -@property (assign) BOOL inProgress; -@property (assign) int retryCount; -@property (assign) BOOL connectionCanBeReused; -@property (retain, nonatomic) NSMutableDictionary *connectionInfo; -@property (retain, nonatomic) NSInputStream *readStream; -@property (assign) ASIAuthenticationState authenticationNeeded; -@property (assign, nonatomic) BOOL readStreamIsScheduled; -@property (assign, nonatomic) BOOL downloadComplete; -@property (retain) NSNumber *requestID; -@property (assign, nonatomic) NSString *runLoopMode; -@property (retain, nonatomic) NSTimer *statusTimer; -@property (assign) BOOL didUseCachedResponse; -@end - - -@implementation ASIHTTPRequest - -#pragma mark init / dealloc - -+ (void)initialize -{ - if (self == [ASIHTTPRequest class]) { - persistentConnectionsPool = [[NSMutableArray alloc] init]; - connectionsLock = [[NSRecursiveLock alloc] init]; - progressLock = [[NSRecursiveLock alloc] init]; - bandwidthThrottlingLock = [[NSLock alloc] init]; - sessionCookiesLock = [[NSRecursiveLock alloc] init]; - sessionCredentialsLock = [[NSRecursiveLock alloc] init]; - delegateAuthenticationLock = [[NSRecursiveLock alloc] init]; - bandwidthUsageTracker = [[NSMutableArray alloc] initWithCapacity:5]; - ASIRequestTimedOutError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestTimedOutErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request timed out",NSLocalizedDescriptionKey,nil]] retain]; - ASIAuthenticationError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIAuthenticationErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Authentication needed",NSLocalizedDescriptionKey,nil]] retain]; - ASIRequestCancelledError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIRequestCancelledErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request was cancelled",NSLocalizedDescriptionKey,nil]] retain]; - ASIUnableToCreateRequestError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnableToCreateRequestErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create request (bad url?)",NSLocalizedDescriptionKey,nil]] retain]; - ASITooMuchRedirectionError = [[NSError errorWithDomain:NetworkRequestErrorDomain code:ASITooMuchRedirectionErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The request failed because it redirected too many times",NSLocalizedDescriptionKey,nil]] retain]; - - sharedQueue = [[NSOperationQueue alloc] init]; - [sharedQueue setMaxConcurrentOperationCount:4]; - - } -} - - -- (id)initWithURL:(NSURL *)newURL -{ - self = [self init]; - [self setRequestMethod:@"GET"]; - - [self setRunLoopMode:NSDefaultRunLoopMode]; - [self setShouldAttemptPersistentConnection:YES]; - [self setPersistentConnectionTimeoutSeconds:60.0]; - [self setShouldPresentCredentialsBeforeChallenge:YES]; - [self setShouldRedirect:YES]; - [self setShowAccurateProgress:YES]; - [self setShouldResetDownloadProgress:YES]; - [self setShouldResetUploadProgress:YES]; - [self setAllowCompressedResponse:YES]; - [self setDefaultResponseEncoding:NSISOLatin1StringEncoding]; - [self setShouldPresentProxyAuthenticationDialog:YES]; - - [self setTimeOutSeconds:[ASIHTTPRequest defaultTimeOutSeconds]]; - [self setUseSessionPersistence:YES]; - [self setUseCookiePersistence:YES]; - [self setValidatesSecureCertificate:YES]; - [self setRequestCookies:[[[NSMutableArray alloc] init] autorelease]]; - [self setDidStartSelector:@selector(requestStarted:)]; - [self setDidReceiveResponseHeadersSelector:@selector(requestReceivedResponseHeaders:)]; - [self setDidFinishSelector:@selector(requestFinished:)]; - [self setDidFailSelector:@selector(requestFailed:)]; - [self setDidReceiveDataSelector:@selector(request:didReceiveData:)]; - [self setURL:newURL]; - [self setCancelledLock:[[[NSRecursiveLock alloc] init] autorelease]]; - [self setDownloadCache:[[self class] defaultCache]]; - return self; -} - -+ (id)requestWithURL:(NSURL *)newURL -{ - return [[[self alloc] initWithURL:newURL] autorelease]; -} - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache -{ - return [self requestWithURL:newURL usingCache:cache andCachePolicy:ASIDefaultCachePolicy]; -} - -+ (id)requestWithURL:(NSURL *)newURL usingCache:(id )cache andCachePolicy:(ASICachePolicy)policy -{ - ASIHTTPRequest *request = [[[self alloc] initWithURL:newURL] autorelease]; - [request setDownloadCache:cache]; - [request setCachePolicy:policy]; - return request; -} - -- (void)dealloc -{ - [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; - if (requestAuthentication) { - CFRelease(requestAuthentication); - } - if (proxyAuthentication) { - CFRelease(proxyAuthentication); - } - if (request) { - CFRelease(request); - } - [self cancelLoad]; - [userInfo release]; - [postBody release]; - [compressedPostBody release]; - [error release]; - [requestHeaders release]; - [requestCookies release]; - [downloadDestinationPath release]; - [temporaryFileDownloadPath release]; - [fileDownloadOutputStream release]; - [username release]; - [password release]; - [domain release]; - [authenticationRealm release]; - [authenticationScheme release]; - [requestCredentials release]; - [proxyHost release]; - [proxyType release]; - [proxyUsername release]; - [proxyPassword release]; - [proxyDomain release]; - [proxyAuthenticationRealm release]; - [proxyAuthenticationScheme release]; - [proxyCredentials release]; - [url release]; - [originalURL release]; - [lastActivityTime release]; - [responseCookies release]; - [rawResponseData release]; - [responseHeaders release]; - [requestMethod release]; - [cancelledLock release]; - [postBodyFilePath release]; - [compressedPostBodyFilePath release]; - [postBodyWriteStream release]; - [postBodyReadStream release]; - [PACurl release]; - [responseStatusMessage release]; - [connectionInfo release]; - [requestID release]; - [super dealloc]; -} - - -#pragma mark setup request - -- (void)addRequestHeader:(NSString *)header value:(NSString *)value -{ - if (!requestHeaders) { - [self setRequestHeaders:[NSMutableDictionary dictionaryWithCapacity:1]]; - } - [requestHeaders setObject:value forKey:header]; -} - -// This function will be called either just before a request starts, or when postLength is needed, whichever comes first -// postLength must be set by the time this function is complete -- (void)buildPostBody -{ - if ([self haveBuiltPostBody]) { - return; - } - - // Are we submitting the request body from a file on disk - if ([self postBodyFilePath]) { - - // If we were writing to the post body via appendPostData or appendPostDataFromFile, close the write stream - if ([self postBodyWriteStream]) { - [[self postBodyWriteStream] close]; - [self setPostBodyWriteStream:nil]; - } - - NSError *err = nil; - NSString *path; - if ([self shouldCompressRequestBody]) { - [self setCompressedPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - [ASIHTTPRequest compressDataFromFile:[self postBodyFilePath] toFile:[self compressedPostBodyFilePath]]; - path = [self compressedPostBodyFilePath]; - } else { - path = [self postBodyFilePath]; - } - [self setPostLength:[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:&err] fileSize]]; - if (err) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '@%'",path],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; - return; - } - - // Otherwise, we have an in-memory request body - } else { - if ([self shouldCompressRequestBody]) { - [self setCompressedPostBody:[ASIHTTPRequest compressData:[self postBody]]]; - [self setPostLength:[[self compressedPostBody] length]]; - } else { - [self setPostLength:[[self postBody] length]]; - } - } - - if ([self postLength] > 0) { - if ([requestMethod isEqualToString:@"GET"] || [requestMethod isEqualToString:@"DELETE"] || [requestMethod isEqualToString:@"HEAD"]) { - [self setRequestMethod:@"POST"]; - } - [self addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%llu",[self postLength]]]; - } - [self setHaveBuiltPostBody:YES]; -} - -// Sets up storage for the post body -- (void)setupPostBody -{ - if ([self shouldStreamPostDataFromDisk]) { - if (![self postBodyFilePath]) { - [self setPostBodyFilePath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - [self setDidCreateTemporaryPostDataFile:YES]; - } - if (![self postBodyWriteStream]) { - [self setPostBodyWriteStream:[[[NSOutputStream alloc] initToFileAtPath:[self postBodyFilePath] append:NO] autorelease]]; - [[self postBodyWriteStream] open]; - } - } else { - if (![self postBody]) { - [self setPostBody:[[[NSMutableData alloc] init] autorelease]]; - } - } -} - -- (void)appendPostData:(NSData *)data -{ - [self setupPostBody]; - if ([data length] == 0) { - return; - } - if ([self shouldStreamPostDataFromDisk]) { - [[self postBodyWriteStream] write:[data bytes] maxLength:[data length]]; - } else { - [[self postBody] appendData:data]; - } -} - -- (void)appendPostDataFromFile:(NSString *)file -{ - [self setupPostBody]; - NSInputStream *stream = [[[NSInputStream alloc] initWithFileAtPath:file] autorelease]; - [stream open]; - NSUInteger bytesRead; - while ([stream hasBytesAvailable]) { - - unsigned char buffer[1024*256]; - bytesRead = [stream read:buffer maxLength:sizeof(buffer)]; - if (bytesRead == 0) { - break; - } - if ([self shouldStreamPostDataFromDisk]) { - [[self postBodyWriteStream] write:buffer maxLength:bytesRead]; - } else { - [[self postBody] appendData:[NSData dataWithBytes:buffer length:bytesRead]]; - } - } - [stream close]; -} - -- (id)delegate -{ - [[self cancelledLock] lock]; - id d = [[delegate retain] autorelease]; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - delegate = newDelegate; - [[self cancelledLock] unlock]; -} - -- (id)queue -{ - [[self cancelledLock] lock]; - id q = [[queue retain] autorelease]; - [[self cancelledLock] unlock]; - return q; -} - - -- (void)setQueue:(id)newQueue -{ - [[self cancelledLock] lock]; - queue = newQueue; - [[self cancelledLock] unlock]; -} - -#pragma mark get information about this request - -- (void)cancel -{ - #if DEBUG_REQUEST_STATUS - NSLog(@"Request cancelled: %@",self); - #endif - - [[self cancelledLock] lock]; - - if ([self isCancelled] || [self complete]) { - [[self cancelledLock] unlock]; - return; - } - - [self failWithError:ASIRequestCancelledError]; - [self setComplete:YES]; - [self cancelLoad]; - - [[self retain] autorelease]; - [super cancel]; - - [[self cancelledLock] unlock]; -} - - -// Call this method to get the received data as an NSString. Don't use for binary data! -- (NSString *)responseString -{ - NSData *data = [self responseData]; - if (!data) { - return nil; - } - - return [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:[self responseEncoding]] autorelease]; -} - -- (BOOL)isResponseCompressed -{ - NSString *encoding = [[self responseHeaders] objectForKey:@"Content-Encoding"]; - return encoding && [encoding rangeOfString:@"gzip"].location != NSNotFound; -} - -- (NSData *)responseData -{ - if ([self isResponseCompressed]) { - return [ASIHTTPRequest uncompressZippedData:[self rawResponseData]]; - } else { - return [self rawResponseData]; - } -} - -#pragma mark running a request - -- (void)startSynchronous -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - NSLog(@"Starting synchronous request %@",self); -#endif - [self setRunLoopMode:ASIHTTPRequestRunLoopMode]; - [self setInProgress:YES]; - - if (![self isCancelled] && ![self complete]) { - [self main]; - while (!complete) { - [[NSRunLoop currentRunLoop] runMode:[self runLoopMode] beforeDate:[NSDate distantFuture]]; - } - } - - [self setInProgress:NO]; -} - -- (void)start -{ - [self setInProgress:YES]; - [self performSelector:@selector(main) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO]; -} - -- (void)startAsynchronous -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - NSLog(@"Starting asynchronous request %@",self); -#endif - [sharedQueue addOperation:self]; -} - -#pragma mark concurrency - -- (BOOL)isConcurrent -{ - return YES; -} - -- (BOOL)isFinished -{ - return [self complete]; -} - -- (BOOL)isExecuting { - return [self inProgress]; -} - -#pragma mark request logic - -// Create the request -- (void)main -{ - @try { - - [[self cancelledLock] lock]; - - // A HEAD request generated by an ASINetworkQueue may have set the error already. If so, we should not proceed. - if ([self error]) { - [self setComplete:YES]; - [self markAsFinished]; - return; - } - - [self setComplete:NO]; - - if (![self url]) { - [self failWithError:ASIUnableToCreateRequestError]; - return; - } - - // Must call before we create the request so that the request method can be set if needs be - if (![self mainRequest]) { - [self buildPostBody]; - } - - if (![[self requestMethod] isEqualToString:@"GET"]) { - [self setDownloadCache:nil]; - } - - - // If we're redirecting, we'll already have a CFHTTPMessageRef - if (request) { - CFRelease(request); - } - - // Create a new HTTP request. - request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, (CFStringRef)[self requestMethod], (CFURLRef)[self url], [self useHTTPVersionOne] ? kCFHTTPVersion1_0 : kCFHTTPVersion1_1); - if (!request) { - [self failWithError:ASIUnableToCreateRequestError]; - return; - } - - //If this is a HEAD request generated by an ASINetworkQueue, we need to let the main request generate its headers first so we can use them - if ([self mainRequest]) { - [[self mainRequest] buildRequestHeaders]; - } - - // Even if this is a HEAD request with a mainRequest, we still need to call to give subclasses a chance to add their own to HEAD requests (ASIS3Request does this) - [self buildRequestHeaders]; - - if ([self downloadCache]) { - if ([self cachePolicy] == ASIDefaultCachePolicy) { - [self setCachePolicy:[[self downloadCache] defaultCachePolicy]]; - } - - // See if we should pull from the cache rather than fetching the data - if ([self cachePolicy] == ASIOnlyLoadIfNotCachedCachePolicy) { - if ([self useDataFromCache]) { - return; - } - } else if ([self cachePolicy] == ASIReloadIfDifferentCachePolicy) { - - // Force a conditional GET if we have a cached version of this content already - NSDictionary *cachedHeaders = [[self downloadCache] cachedHeadersForRequest:self]; - if (cachedHeaders) { - NSString *etag = [cachedHeaders objectForKey:@"Etag"]; - if (etag) { - [[self requestHeaders] setObject:etag forKey:@"If-None-Match"]; - } - NSString *lastModified = [cachedHeaders objectForKey:@"Last-Modified"]; - if (lastModified) { - [[self requestHeaders] setObject:lastModified forKey:@"If-Modified-Since"]; - } - } - } - } - - [self applyAuthorizationHeader]; - - - NSString *header; - for (header in [self requestHeaders]) { - CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[[self requestHeaders] objectForKey:header]); - } - - [self startRequest]; - - } @catch (NSException *exception) { - NSError *underlyingError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[exception userInfo]]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIUnhandledExceptionError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[exception name],NSLocalizedDescriptionKey,[exception reason],NSLocalizedFailureReasonErrorKey,underlyingError,NSUnderlyingErrorKey,nil]]]; - - } @finally { - [[self cancelledLock] unlock]; - } -} - -- (void)applyAuthorizationHeader -{ - // Do we want to send credentials before we are asked for them? - if (![self shouldPresentCredentialsBeforeChallenge]) { - return; - } - - // First, see if we have any credentials we can use in the session store - NSDictionary *credentials = nil; - if ([self useSessionPersistence]) { - credentials = [self findSessionAuthenticationCredentials]; - } - - - // Are any credentials set on this request that might be used for basic authentication? - if ([self username] && [self password] && ![self domain]) { - - // If we have stored credentials, is this server asking for basic authentication? If we don't have credentials, we'll assume basic - if (!credentials || (CFStringRef)[credentials objectForKey:@"AuthenticationScheme"] == kCFHTTPAuthenticationSchemeBasic) { - [self addBasicAuthenticationHeaderWithUsername:[self username] andPassword:[self password]]; - } - } - - if (credentials && ![[self requestHeaders] objectForKey:@"Authorization"]) { - - // When the Authentication key is set, the credentials were stored after an authentication challenge, so we can let CFNetwork apply them - // (credentials for Digest and NTLM will always be stored like this) - if ([credentials objectForKey:@"Authentication"]) { - - // If we've already talked to this server and have valid credentials, let's apply them to the request - if (!CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { - [[self class] removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - } - - // If the Authentication key is not set, these credentials were stored after a username and password set on a previous request passed basic authentication - // When this happens, we'll need to create the Authorization header ourselves - } else { - NSDictionary *usernameAndPassword = [credentials objectForKey:@"Credentials"]; - [self addBasicAuthenticationHeaderWithUsername:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationUsername] andPassword:[usernameAndPassword objectForKey:(NSString *)kCFHTTPAuthenticationPassword]]; - } - } - if ([self useSessionPersistence]) { - credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials) { - if (!CFHTTPMessageApplyCredentialDictionary(request, (CFHTTPAuthenticationRef)[credentials objectForKey:@"Authentication"], (CFDictionaryRef)[credentials objectForKey:@"Credentials"], NULL)) { - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - } - } - } -} - -- (void)applyCookieHeader -{ - // Add cookies from the persistent (mac os global) store - if ([self useCookiePersistence]) { - NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[[self url] absoluteURL]]; - if (cookies) { - [[self requestCookies] addObjectsFromArray:cookies]; - } - } - - // Apply request cookies - NSArray *cookies; - if ([self mainRequest]) { - cookies = [[self mainRequest] requestCookies]; - } else { - cookies = [self requestCookies]; - } - if ([cookies count] > 0) { - NSHTTPCookie *cookie; - NSString *cookieHeader = nil; - for (cookie in cookies) { - if (!cookieHeader) { - cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie value]]; - } else { - cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie value]]; - } - } - if (cookieHeader) { - [self addRequestHeader:@"Cookie" value:cookieHeader]; - } - } -} - -- (void)buildRequestHeaders -{ - if ([self haveBuiltRequestHeaders]) { - return; - } - [self setHaveBuiltRequestHeaders:YES]; - - if ([self mainRequest]) { - for (NSString *header in [[self mainRequest] requestHeaders]) { - [self addRequestHeader:header value:[[[self mainRequest] requestHeaders] valueForKey:header]]; - } - return; - } - - [self applyCookieHeader]; - - // Build and set the user agent string if the request does not already have a custom user agent specified - if (![[self requestHeaders] objectForKey:@"User-Agent"]) { - NSString *userAgentString = [ASIHTTPRequest defaultUserAgentString]; - if (userAgentString) { - [self addRequestHeader:@"User-Agent" value:userAgentString]; - } - } - - - // Accept a compressed response - if ([self allowCompressedResponse]) { - [self addRequestHeader:@"Accept-Encoding" value:@"gzip"]; - } - - // Configure a compressed request body - if ([self shouldCompressRequestBody]) { - [self addRequestHeader:@"Content-Encoding" value:@"gzip"]; - } - - // Should this request resume an existing download? - if ([self allowResumeForFileDownloads] && [self downloadDestinationPath] && [self temporaryFileDownloadPath] && [[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { - NSError *err = nil; - [self setPartialDownloadSize:[[[NSFileManager defaultManager] attributesOfItemAtPath:[self temporaryFileDownloadPath] error:&err] fileSize]]; - if (err) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to get attributes for file at path '@%'",[self temporaryFileDownloadPath]],NSLocalizedDescriptionKey,error,NSUnderlyingErrorKey,nil]]]; - return; - } - [self addRequestHeader:@"Range" value:[NSString stringWithFormat:@"bytes=%llu-",[self partialDownloadSize]]]; - } -} - -- (void)startRequest -{ - if ([self isCancelled]) { - return; - } - - [self requestStarted]; - - [self setDownloadComplete:NO]; - [self setComplete:NO]; - [self setTotalBytesRead:0]; - [self setLastBytesRead:0]; - - if ([self redirectCount] == 0) { - [self setOriginalURL:[self url]]; - } - - // If we're retrying a request, let's remove any progress we made - if ([self lastBytesSent] > 0) { - [self removeUploadProgressSoFar]; - } - - [self setLastBytesSent:0]; - [self setContentLength:0]; - [self setResponseHeaders:nil]; - if (![self downloadDestinationPath]) { - [self setRawResponseData:[[[NSMutableData alloc] init] autorelease]]; - } - - - // - // Create the stream for the request - // - - [self setReadStreamIsScheduled:NO]; - - // Do we need to stream the request body from disk - if ([self shouldStreamPostDataFromDisk] && [self postBodyFilePath] && [[NSFileManager defaultManager] fileExistsAtPath:[self postBodyFilePath]]) { - - // Are we gzipping the request body? - if ([self compressedPostBodyFilePath] && [[NSFileManager defaultManager] fileExistsAtPath:[self compressedPostBodyFilePath]]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self compressedPostBodyFilePath] request:self]]; - } else { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithFileAtPath:[self postBodyFilePath] request:self]]; - } - [self setReadStream:[(NSInputStream *)CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream]) autorelease]]; - } else { - - // If we have a request body, we'll stream it from memory using our custom stream, so that we can measure bandwidth use and it can be bandwidth-throttled if nescessary - if ([self postBody] && [[self postBody] length] > 0) { - if ([self shouldCompressRequestBody] && [self compressedPostBody]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self compressedPostBody] request:self]]; - } else if ([self postBody]) { - [self setPostBodyReadStream:[ASIInputStream inputStreamWithData:[self postBody] request:self]]; - } - [self setReadStream:[(NSInputStream *)CFReadStreamCreateForStreamedHTTPRequest(kCFAllocatorDefault, request,(CFReadStreamRef)[self postBodyReadStream]) autorelease]]; - - } else { - [self setReadStream:[(NSInputStream *)CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request) autorelease]]; - } - } - - if (![self readStream]) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to create read stream",NSLocalizedDescriptionKey,nil]]]; - return; - } - - // Tell CFNetwork not to validate SSL certificates - if (![self validatesSecureCertificate] && [[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySSLSettings, [NSMutableDictionary dictionaryWithObject:(NSString *)kCFBooleanFalse forKey:(NSString *)kCFStreamSSLValidatesCertificateChain]); - } - - // - // Handle proxy settings - // - - // Have details of the proxy been set on this request - if (![self proxyHost] && ![self proxyPort]) { - - // If not, we need to figure out what they'll be - - NSArray *proxies = nil; - - // Have we been given a proxy auto config file? - if ([self PACurl]) { - - proxies = [ASIHTTPRequest proxiesForURL:[self url] fromPAC:[self PACurl]]; - - // Detect proxy settings and apply them - } else { - -#if TARGET_OS_IPHONE - NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)CFNetworkCopySystemProxySettings() autorelease]); -#else - NSDictionary *proxySettings = NSMakeCollectable([(NSDictionary *)SCDynamicStoreCopyProxies(NULL) autorelease]); -#endif - - proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForURL((CFURLRef)[self url], (CFDictionaryRef)proxySettings) autorelease]); - - // Now check to see if the proxy settings contained a PAC url, we need to run the script to get the real list of proxies if so - NSDictionary *settings = [proxies objectAtIndex:0]; - if ([settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]) { - proxies = [ASIHTTPRequest proxiesForURL:[self url] fromPAC:[settings objectForKey:(NSString *)kCFProxyAutoConfigurationURLKey]]; - } - } - - if (!proxies) { - [self setReadStream:nil]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to obtain information on proxy servers needed for request",NSLocalizedDescriptionKey,nil]]]; - return; - } - // I don't really understand why the dictionary returned by CFNetworkCopyProxiesForURL uses different key names from CFNetworkCopySystemProxySettings/SCDynamicStoreCopyProxies - // and why its key names are documented while those we actually need to use don't seem to be (passing the kCF* keys doesn't seem to work) - if ([proxies count] > 0) { - NSDictionary *settings = [proxies objectAtIndex:0]; - [self setProxyHost:[settings objectForKey:(NSString *)kCFProxyHostNameKey]]; - [self setProxyPort:[[settings objectForKey:(NSString *)kCFProxyPortNumberKey] intValue]]; - [self setProxyType:[settings objectForKey:(NSString *)kCFProxyTypeKey]]; - } - } - if ([self proxyHost] && [self proxyPort]) { - NSString *hostKey; - NSString *portKey; - - if (![self proxyType]) { - [self setProxyType:(NSString *)kCFProxyTypeHTTP]; - } - - if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { - hostKey = (NSString *)kCFStreamPropertySOCKSProxyHost; - portKey = (NSString *)kCFStreamPropertySOCKSProxyPort; - } else { - hostKey = (NSString *)kCFStreamPropertyHTTPProxyHost; - portKey = (NSString *)kCFStreamPropertyHTTPProxyPort; - if ([[[[self url] scheme] lowercaseString] isEqualToString:@"https"]) { - hostKey = (NSString *)kCFStreamPropertyHTTPSProxyHost; - portKey = (NSString *)kCFStreamPropertyHTTPSProxyPort; - } - } - NSMutableDictionary *proxyToUse = [NSMutableDictionary dictionaryWithObjectsAndKeys:[self proxyHost],hostKey,[NSNumber numberWithInt:[self proxyPort]],portKey,nil]; - - if ([[self proxyType] isEqualToString:(NSString *)kCFProxyTypeSOCKS]) { - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertySOCKSProxy, proxyToUse); - } else { - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPProxy, proxyToUse); - } - } - - // - // Handle persistent connections - // - - [ASIHTTPRequest expirePersistentConnections]; - - [connectionsLock lock]; - - - if (![[self url] host] || ![[self url] scheme]) { - [self setConnectionInfo:nil]; - [self setShouldAttemptPersistentConnection:NO]; - } - - // Will store the old stream that was using this connection (if there was one) so we can clean it up once we've opened our own stream - NSInputStream *oldStream = nil; - - // Use a persistent connection if possible - if ([self shouldAttemptPersistentConnection]) { - - - // If we are redirecting, we will re-use the current connection only if we are connecting to the same server - if ([self connectionInfo]) { - - if (![[[self connectionInfo] objectForKey:@"host"] isEqualToString:[[self url] host]] || ![[[self connectionInfo] objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] || [(NSNumber *)[[self connectionInfo] objectForKey:@"port"] intValue] != [[[self url] port] intValue]) { - [self setConnectionInfo:nil]; - - // Check if we should have expired this connection - } else if ([[[self connectionInfo] objectForKey:@"expires"] timeIntervalSinceNow] < 0) { - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Not re-using connection #%hi because it has expired",[[[self connectionInfo] objectForKey:@"id"] intValue]); - #endif - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [self setConnectionInfo:nil]; - } - } - - - - if (![self connectionInfo] && [[self url] host] && [[self url] scheme]) { // We must have a proper url with a host and scheme, or this will explode - - // Look for a connection to the same server in the pool - for (NSMutableDictionary *existingConnection in persistentConnectionsPool) { - if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"host"] isEqualToString:[[self url] host]] && [[existingConnection objectForKey:@"scheme"] isEqualToString:[[self url] scheme]] && [(NSNumber *)[existingConnection objectForKey:@"port"] intValue] == [[[self url] port] intValue]) { - [self setConnectionInfo:existingConnection]; - } - } - } - - if ([[self connectionInfo] objectForKey:@"stream"]) { - oldStream = [[[self connectionInfo] objectForKey:@"stream"] retain]; - - } - - // No free connection was found in the pool matching the server/scheme/port we're connecting to, we'll need to create a new one - if (![self connectionInfo]) { - [self setConnectionInfo:[NSMutableDictionary dictionary]]; - nextConnectionNumberToCreate++; - [[self connectionInfo] setObject:[NSNumber numberWithInt:nextConnectionNumberToCreate] forKey:@"id"]; - [[self connectionInfo] setObject:[[self url] host] forKey:@"host"]; - [[self connectionInfo] setObject:[NSNumber numberWithInt:[[[self url] port] intValue]] forKey:@"port"]; - [[self connectionInfo] setObject:[[self url] scheme] forKey:@"scheme"]; - [persistentConnectionsPool addObject:[self connectionInfo]]; - } - - // If we are retrying this request, it will already have a requestID - if (![self requestID]) { - nextRequestID++; - [self setRequestID:[NSNumber numberWithUnsignedInt:nextRequestID]]; - } - [[self connectionInfo] setObject:[self requestID] forKey:@"request"]; - [[self connectionInfo] setObject:[self readStream] forKey:@"stream"]; - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPAttemptPersistentConnection, kCFBooleanTrue); - - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Request #%@ will use connection #%hi",[self requestID],[[[self connectionInfo] objectForKey:@"id"] intValue]); - #endif - - - // Tag the stream with an id that tells it which connection to use behind the scenes - // See http://lists.apple.com/archives/macnetworkprog/2008/Dec/msg00001.html for details on this approach - - CFReadStreamSetProperty((CFReadStreamRef)[self readStream], CFSTR("ASIStreamID"), [[self connectionInfo] objectForKey:@"id"]); - - } - - [connectionsLock unlock]; - - // Schedule the stream - if (![self readStreamIsScheduled] && (!throttleWakeUpTime || [throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] < 0)) { - [self scheduleReadStream]; - } - - BOOL streamSuccessfullyOpened = NO; - - - // Start the HTTP connection - CFStreamClientContext ctxt = {0, self, NULL, NULL, NULL}; - if (CFReadStreamSetClient((CFReadStreamRef)[self readStream], kNetworkEvents, ReadStreamClientCallBack, &ctxt)) { - if (CFReadStreamOpen((CFReadStreamRef)[self readStream])) { - streamSuccessfullyOpened = YES; - } - } - - // Here, we'll close the stream that was previously using this connection, if there was one - // We've kept it open until now (when we've just opened a new stream) so that the new stream can make use of the old connection - // http://lists.apple.com/archives/Macnetworkprog/2006/Mar/msg00119.html - if (oldStream) { - [oldStream close]; - [oldStream release]; - oldStream = nil; - } - - if (!streamSuccessfullyOpened) { - [self setConnectionCanBeReused:NO]; - [self destroyReadStream]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileBuildingRequestType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Unable to start HTTP connection",NSLocalizedDescriptionKey,nil]]]; - return; - } - - if (![self mainRequest]) { - if ([self shouldResetUploadProgress]) { - if ([self showAccurateProgress]) { - [self incrementUploadSizeBy:[self postLength]]; - } else { - [self incrementUploadSizeBy:1]; - } - [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:0 ofTotal:1]; - } - if ([self shouldResetDownloadProgress] && ![self partialDownloadSize]) { - [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:0 ofTotal:1]; - } - } - - - // Record when the request started, so we can timeout if nothing happens - [self setLastActivityTime:[NSDate date]]; - [self setStatusTimer:[NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(updateStatus:) userInfo:nil repeats:YES]]; - [[NSRunLoop currentRunLoop] addTimer:[self statusTimer] forMode:[self runLoopMode]]; -} - -- (void)setStatusTimer:(NSTimer *)timer -{ - [self retain]; - // We must invalidate the old timer here, not before we've created and scheduled a new timer - // This is because the timer may be the only thing retaining an asynchronous request - if (statusTimer && timer != statusTimer) { - [statusTimer invalidate]; - [statusTimer release]; - } - statusTimer = [timer retain]; - [self release]; -} - -// This gets fired every 1/4 of a second to update the progress and work out if we need to timeout -- (void)updateStatus:(NSTimer*)timer -{ - [self checkRequestStatus]; - if (![self inProgress]) { - [self setStatusTimer:nil]; - } -} - -- (void)performRedirect -{ - [self setComplete:YES]; - [self setNeedsRedirect:NO]; - [self setRedirectCount:[self redirectCount]+1]; - - if ([self redirectCount] > RedirectionLimit) { - // Some naughty / badly coded website is trying to force us into a redirection loop. This is not cool. - [self failWithError:ASITooMuchRedirectionError]; - [self setComplete:YES]; - } else { - // Go all the way back to the beginning and build the request again, so that we can apply any new cookies - [self main]; - } -} - -- (BOOL)shouldTimeOut -{ - NSTimeInterval secondsSinceLastActivity = [[NSDate date] timeIntervalSinceDate:lastActivityTime]; - // See if we need to timeout - if ([self readStream] && [self readStreamIsScheduled] && [self lastActivityTime] && [self timeOutSeconds] > 0 && secondsSinceLastActivity > [self timeOutSeconds]) { - - // We have no body, or we've sent more than the upload buffer size,so we can safely time out here - if ([self postLength] == 0 || ([self uploadBufferSize] > 0 && [self totalBytesSent] > [self uploadBufferSize])) { - return YES; - - // ***Black magic warning*** - // We have a body, but we've taken longer than timeOutSeconds to upload the first small chunk of data - // Since there's no reliable way to track upload progress for the first 32KB (iPhone) or 128KB (Mac) with CFNetwork, we'll be slightly more forgiving on the timeout, as there's a strong chance our connection is just very slow. - } else if (secondsSinceLastActivity > [self timeOutSeconds]*1.5) { - return YES; - } - } - return NO; -} - -- (void)checkRequestStatus -{ - // We won't let the request cancel while we're updating progress / checking for a timeout - [[self cancelledLock] lock]; - - // See if our NSOperationQueue told us to cancel - if ([self isCancelled] || [self complete]) { - [[self cancelledLock] unlock]; - return; - } - - [self performThrottling]; - - if ([self shouldTimeOut]) { - // Do we need to auto-retry this request? - if ([self numberOfTimesToRetryOnTimeout] > [self retryCount]) { - [self setRetryCount:[self retryCount]+1]; - [self unscheduleReadStream]; - [[self cancelledLock] unlock]; - [self startRequest]; - return; - } - [self failWithError:ASIRequestTimedOutError]; - [self cancelLoad]; - [self setComplete:YES]; - [[self cancelledLock] unlock]; - return; - } - - // readStream will be null if we aren't currently running (perhaps we're waiting for a delegate to supply credentials) - if ([self readStream]) { - - // If we have a post body - if ([self postLength]) { - - [self setLastBytesSent:totalBytesSent]; - - // Find out how much data we've uploaded so far - [self setTotalBytesSent:[NSMakeCollectable([(NSNumber *)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease]) unsignedLongLongValue]]; - if (totalBytesSent > lastBytesSent) { - - // We've uploaded more data, reset the timeout - [self setLastActivityTime:[NSDate date]]; - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:(unsigned long)(totalBytesSent-lastBytesSent)]; - - #if DEBUG_REQUEST_STATUS - if ([self totalBytesSent] == [self postLength]) { - NSLog(@"Request %@ finished uploading data",self); - } - #endif - } - } - - [self updateProgressIndicators]; - - } - - [[self cancelledLock] unlock]; -} - - -// Cancel loading and clean up. DO NOT USE THIS TO CANCEL REQUESTS - use [request cancel] instead -- (void)cancelLoad -{ - [self destroyReadStream]; - - [[self postBodyReadStream] close]; - - if ([self rawResponseData]) { - [self setRawResponseData:nil]; - - // If we were downloading to a file - } else if ([self temporaryFileDownloadPath]) { - [[self fileDownloadOutputStream] close]; - - // If we haven't said we might want to resume, let's remove the temporary file too - if (![self allowResumeForFileDownloads]) { - [self removeTemporaryDownloadFile]; - } - } - - // Clean up any temporary file used to store request body for streaming - if (![self authenticationNeeded] && [self didCreateTemporaryPostDataFile]) { - [self removePostDataFile]; - [self setDidCreateTemporaryPostDataFile:NO]; - } - - [self setResponseHeaders:nil]; -} - - -- (void)removeTemporaryDownloadFile -{ - if ([self temporaryFileDownloadPath]) { - if ([[NSFileManager defaultManager] fileExistsAtPath:[self temporaryFileDownloadPath]]) { - NSError *removeError = nil; - [[NSFileManager defaultManager] removeItemAtPath:[self temporaryFileDownloadPath] error:&removeError]; - if (removeError) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self temporaryFileDownloadPath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; - } - } - [self setTemporaryFileDownloadPath:nil]; - } -} - -- (void)removePostDataFile -{ - if ([self postBodyFilePath]) { - NSError *removeError = nil; - [[NSFileManager defaultManager] removeItemAtPath:[self postBodyFilePath] error:&removeError]; - if (removeError) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self postBodyFilePath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; - } - [self setPostBodyFilePath:nil]; - } - if ([self compressedPostBodyFilePath]) { - NSError *removeError = nil; - [[NSFileManager defaultManager] removeItemAtPath:[self compressedPostBodyFilePath] error:&removeError]; - if (removeError) { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to delete file at path '%@'",[self compressedPostBodyFilePath]],NSLocalizedDescriptionKey,removeError,NSUnderlyingErrorKey,nil]]]; - } - [self setCompressedPostBodyFilePath:nil]; - } -} - -#pragma mark HEAD request - -// Used by ASINetworkQueue to create a HEAD request appropriate for this request with the same headers (though you can use it yourself) -- (ASIHTTPRequest *)HEADRequest -{ - ASIHTTPRequest *headRequest = [[self class] requestWithURL:[self url]]; - - // Copy the properties that make sense for a HEAD request - [headRequest setRequestHeaders:[[[self requestHeaders] mutableCopy] autorelease]]; - [headRequest setRequestCookies:[[[self requestCookies] mutableCopy] autorelease]]; - [headRequest setUseCookiePersistence:[self useCookiePersistence]]; - [headRequest setUseKeychainPersistence:[self useKeychainPersistence]]; - [headRequest setUseSessionPersistence:[self useSessionPersistence]]; - [headRequest setAllowCompressedResponse:[self allowCompressedResponse]]; - [headRequest setUsername:[self username]]; - [headRequest setPassword:[self password]]; - [headRequest setDomain:[self domain]]; - [headRequest setProxyUsername:[self proxyUsername]]; - [headRequest setProxyPassword:[self proxyPassword]]; - [headRequest setProxyDomain:[self proxyDomain]]; - [headRequest setProxyHost:[self proxyHost]]; - [headRequest setProxyPort:[self proxyPort]]; - [headRequest setProxyType:[self proxyType]]; - [headRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; - [headRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; - [headRequest setTimeOutSeconds:[self timeOutSeconds]]; - [headRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; - [headRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; - [headRequest setPACurl:[self PACurl]]; - [headRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; - [headRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; - [headRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; - [headRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; - [headRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; - - [headRequest setMainRequest:self]; - [headRequest setRequestMethod:@"HEAD"]; - return headRequest; -} - - -#pragma mark upload/download progress - - -- (void)updateProgressIndicators -{ - //Only update progress if this isn't a HEAD request used to preset the content-length - if (![self mainRequest]) { - if ([self showAccurateProgress] || ([self complete] && ![self updatedProgress])) { - [self updateUploadProgress]; - [self updateDownloadProgress]; - } - } -} - -- (id)uploadProgressDelegate -{ - [[self cancelledLock] lock]; - id d = [[uploadProgressDelegate retain] autorelease]; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setUploadProgressDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - uploadProgressDelegate = newDelegate; - - #if !TARGET_OS_IPHONE - // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView - double max = 1.0; - [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:[self uploadProgressDelegate] withObject:nil amount:&max]; - #endif - [[self cancelledLock] unlock]; -} - -- (id)downloadProgressDelegate -{ - [[self cancelledLock] lock]; - id d = [[downloadProgressDelegate retain] autorelease]; - [[self cancelledLock] unlock]; - return d; -} - -- (void)setDownloadProgressDelegate:(id)newDelegate -{ - [[self cancelledLock] lock]; - downloadProgressDelegate = newDelegate; - - #if !TARGET_OS_IPHONE - // If the downloadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can update it as if it were a UIProgressView - double max = 1.0; - [ASIHTTPRequest performSelector:@selector(setMaxValue:) onTarget:[self downloadProgressDelegate] withObject:nil amount:&max]; - #endif - [[self cancelledLock] unlock]; -} - - -- (void)updateDownloadProgress -{ - // We won't update download progress until we've examined the headers, since we might need to authenticate - if (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) { - return; - } - - unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize]; - unsigned long long value = 0; - - if ([self showAccurateProgress] && [self contentLength]) { - value = bytesReadSoFar-[self lastBytesRead]; - if (value == 0) { - return; - } - } else { - value = 1; - [self setUpdatedProgress:YES]; - } - if (!value) { - return; - } - - [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:[self queue] withObject:self amount:&value]; - [ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:[self downloadProgressDelegate] withObject:self amount:&value]; - [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]]; - - [self setLastBytesRead:bytesReadSoFar]; -} - - -- (void)updateUploadProgress -{ - if ([self isCancelled] || [self totalBytesSent] == 0) { - return; - } - - // If this is the first time we've written to the buffer, totalBytesSent will be the size of the buffer (currently seems to be 128KB on both Leopard and iPhone 2.2.1, 32KB on iPhone 3.0) - // If request body is less than the buffer size, totalBytesSent will be the total size of the request body - // We will remove this from any progress display, as kCFStreamPropertyHTTPRequestBytesWrittenCount does not tell us how much data has actually be written - if ([self uploadBufferSize] == 0 && [self totalBytesSent] != [self postLength]) { - [self setUploadBufferSize:[self totalBytesSent]]; - [self incrementUploadSizeBy:-[self uploadBufferSize]]; - } - - unsigned long long value = 0; - - if ([self showAccurateProgress]) { - if ([self totalBytesSent] == [self postLength] || [self lastBytesSent] > 0) { - value = [self totalBytesSent]-[self lastBytesSent]; - } else { - return; - } - } else { - value = 1; - [self setUpdatedProgress:YES]; - } - - if (!value) { - return; - } - - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self queue] withObject:self amount:&value]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self uploadProgressDelegate] withObject:self amount:&value]; - [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self totalBytesSent]-[self uploadBufferSize] ofTotal:[self postLength]-[self uploadBufferSize]]; -} - - -- (void)incrementDownloadSizeBy:(long long)length -{ - [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:[self queue] withObject:self amount:&length]; - [ASIHTTPRequest performSelector:@selector(request:incrementDownloadSizeBy:) onTarget:[self downloadProgressDelegate] withObject:self amount:&length]; -} - - -- (void)incrementUploadSizeBy:(long long)length -{ - [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:[self queue] withObject:self amount:&length]; - [ASIHTTPRequest performSelector:@selector(request:incrementUploadSizeBy:) onTarget:[self uploadProgressDelegate] withObject:self amount:&length]; -} - - --(void)removeUploadProgressSoFar -{ - long long progressToRemove = -[self totalBytesSent]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self queue] withObject:self amount:&progressToRemove]; - [ASIHTTPRequest performSelector:@selector(request:didSendBytes:) onTarget:[self uploadProgressDelegate] withObject:self amount:&progressToRemove]; - [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:0 ofTotal:[self postLength]]; -} - - -+ (void)performSelector:(SEL)selector onTarget:(id)target withObject:(id)object amount:(void *)amount -{ - if ([target respondsToSelector:selector]) { - NSMethodSignature *signature = nil; - signature = [[target class] instanceMethodSignatureForSelector:selector]; - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; - [invocation setTarget:target]; - [invocation setSelector:selector]; - - int argumentNumber = 2; - - // If we got an object parameter, we pass a pointer to the object pointer - if (object) { - [invocation setArgument:&object atIndex:argumentNumber]; - argumentNumber++; - } - - // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer to it - if (amount) { - [invocation setArgument:amount atIndex:argumentNumber]; - } - - [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:target waitUntilDone:[NSThread isMainThread]]; - } -} - - -+ (void)updateProgressIndicator:(id)indicator withProgress:(unsigned long long)progress ofTotal:(unsigned long long)total -{ - #if TARGET_OS_IPHONE - // Cocoa Touch: UIProgressView - SEL selector = @selector(setProgress:); - float progressAmount = (progress*1.0f)/(total*1.0f); - - #else - // Cocoa: NSProgressIndicator - double progressAmount = progressAmount = (progress*1.0)/(total*1.0); - SEL selector = @selector(setDoubleValue:); - #endif - - if (![indicator respondsToSelector:selector]) { - return; - } - - [progressLock lock]; - [ASIHTTPRequest performSelector:selector onTarget:indicator withObject:nil amount:&progressAmount]; - [progressLock unlock]; -} - - -#pragma mark handling request complete / failure - - - -- (void)requestReceivedResponseHeaders -{ - if ([self error] || [self mainRequest]) { - return; - } - // Let the delegate know we have started - if ([self didReceiveResponseHeadersSelector] && [[self delegate] respondsToSelector:[self didReceiveResponseHeadersSelector]]) { - [[self delegate] performSelectorOnMainThread:[self didReceiveResponseHeadersSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; - } - - // Let the queue know we have started - if ([[self queue] respondsToSelector:@selector(requestReceivedResponseHeaders:)]) { - [[self queue] performSelectorOnMainThread:@selector(requestReceivedResponseHeaders:) withObject:self waitUntilDone:[NSThread isMainThread]]; - } -} - -- (void)requestStarted -{ - if ([self error] || [self mainRequest]) { - return; - } - // Let the delegate know we have started - if ([self didStartSelector] && [[self delegate] respondsToSelector:[self didStartSelector]]) { - [[self delegate] performSelectorOnMainThread:[self didStartSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; - } - - // Let the queue know we have started - if ([[self queue] respondsToSelector:@selector(requestStarted:)]) { - [[self queue] performSelectorOnMainThread:@selector(requestStarted:) withObject:self waitUntilDone:[NSThread isMainThread]]; - } -} - -// Subclasses might override this method to process the result in the same thread -// If you do this, don't forget to call [super requestFinished] to let the queue / delegate know we're done -- (void)requestFinished -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - NSLog(@"Request finished: %@",self); -#endif - if ([self error] || [self mainRequest]) { - return; - } - // Let the delegate know we are done - if ([self didFinishSelector] && [[self delegate] respondsToSelector:[self didFinishSelector]]) { - [[self delegate] performSelectorOnMainThread:[self didFinishSelector] withObject:self waitUntilDone:[NSThread isMainThread]]; - } - - // Let the queue know we are done - if ([[self queue] respondsToSelector:@selector(requestFinished:)]) { - [[self queue] performSelectorOnMainThread:@selector(requestFinished:) withObject:self waitUntilDone:[NSThread isMainThread]]; - } - -} - -// Subclasses might override this method to perform error handling in the same thread -// If you do this, don't forget to call [super failWithError:] to let the queue / delegate know we're done -- (void)failWithError:(NSError *)theError -{ -#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING - NSLog(@"Request %@: %@",self,(theError == ASIRequestCancelledError ? @"Cancelled" : @"Failed")); -#endif - [self setComplete:YES]; - - // Invalidate the current connection so subsequent requests don't attempt to reuse it - if (theError && [theError code] != ASIAuthenticationErrorType && [theError code] != ASITooMuchRedirectionErrorType) { - [connectionsLock lock]; - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Request #%@ failed and will invalidate connection #%@",[self requestID],[[self connectionInfo] objectForKey:@"id"]); - #endif - [[self connectionInfo] removeObjectForKey:@"request"]; - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [connectionsLock unlock]; - [self destroyReadStream]; - } - if ([self connectionCanBeReused]) { - [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; - } - - if ([self isCancelled] || [self error]) { - return; - } - - if ([self downloadCache] && [self cachePolicy] == ASIUseCacheIfLoadFailsCachePolicy) { - if ([self useDataFromCache]) { - return; - } - } - - - [self setError:theError]; - - ASIHTTPRequest *failedRequest = self; - - // If this is a HEAD request created by an ASINetworkQueue or compatible queue delegate, make the main request fail - if ([self mainRequest]) { - failedRequest = [self mainRequest]; - [failedRequest setError:theError]; - } - - // Let the delegate know something went wrong - if ([failedRequest didFailSelector] && [[failedRequest delegate] respondsToSelector:[failedRequest didFailSelector]]) { - [[failedRequest delegate] performSelectorOnMainThread:[failedRequest didFailSelector] withObject:failedRequest waitUntilDone:[NSThread isMainThread]]; - } - - // Let the queue know something went wrong - if ([[failedRequest queue] respondsToSelector:@selector(requestFailed:)]) { - [[failedRequest queue] performSelectorOnMainThread:@selector(requestFailed:) withObject:failedRequest waitUntilDone:[NSThread isMainThread]]; - } - - // markAsFinished may well cause this object to be dealloced - [self retain]; - [self markAsFinished]; - [self release]; -} - -#pragma mark parsing HTTP response headers - -- (void)readResponseHeaders -{ - [self setAuthenticationNeeded:ASINoAuthenticationNeededYet]; - - CFHTTPMessageRef message = (CFHTTPMessageRef)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPResponseHeader); - if (!message) { - return; - } - - // Make sure we've received all the headers - if (!CFHTTPMessageIsHeaderComplete(message)) { - CFRelease(message); - return; - } - - #if DEBUG_REQUEST_STATUS - if ([self totalBytesSent] == [self postLength]) { - NSLog(@"Request %@ received response headers",self); - } - #endif - - CFDictionaryRef headerFields = CFHTTPMessageCopyAllHeaderFields(message); - [self setResponseHeaders:(NSDictionary *)headerFields]; - - CFRelease(headerFields); - - [self setResponseStatusCode:(int)CFHTTPMessageGetResponseStatusCode(message)]; - [self setResponseStatusMessage:[(NSString *)CFHTTPMessageCopyResponseStatusLine(message) autorelease]]; - - if ([self downloadCache] && [self cachePolicy] == ASIReloadIfDifferentCachePolicy) { - if ([self useDataFromCache]) { - CFRelease(message); - return; - } - } - - // Is the server response a challenge for credentials? - if ([self responseStatusCode] == 401) { - [self setAuthenticationNeeded:ASIHTTPAuthenticationNeeded]; - } else if ([self responseStatusCode] == 407) { - [self setAuthenticationNeeded:ASIProxyAuthenticationNeeded]; - } - - // Authentication succeeded, or no authentication was required - if (![self authenticationNeeded]) { - - // Did we get here without an authentication challenge? (which can happen when shouldPresentCredentialsBeforeChallenge is YES and basic auth was successful) - if (!requestAuthentication && [self username] && [self password] && [self useSessionPersistence]) { - - NSMutableDictionary *newCredentials = [NSMutableDictionary dictionaryWithCapacity:2]; - [newCredentials setObject:[self username] forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:[self password] forKey:(NSString *)kCFHTTPAuthenticationPassword]; - - // Store the credentials in the session - NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; - [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionCredentials setObject:[self url] forKey:@"URL"]; - [sessionCredentials setObject:(NSString *)kCFHTTPAuthenticationSchemeBasic forKey:@"AuthenticationScheme"]; - [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; - } - } - - // Handle response text encoding - [self parseStringEncodingFromHeaders]; - - // Handle cookies - NSArray *newCookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[self responseHeaders] forURL:[self url]]; - [self setResponseCookies:newCookies]; - - if ([self useCookiePersistence]) { - - // Store cookies in global persistent store - [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:newCookies forURL:[self url] mainDocumentURL:nil]; - - // We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later - NSHTTPCookie *cookie; - for (cookie in newCookies) { - [ASIHTTPRequest addSessionCookie:cookie]; - } - } - - // Do we need to redirect? - // Note that ASIHTTPRequest does not currently support 305 Use Proxy - if ([self shouldRedirect] && [responseHeaders valueForKey:@"Location"]) { - if (([self responseStatusCode] > 300 && [self responseStatusCode] < 304) || [self responseStatusCode] == 307) { - - // By default, we redirect 301 and 302 response codes as GET requests - // According to RFC 2616 this is wrong, but this is what most browsers do, so it's probably what you're expecting to happen - // See also: - // http://allseeing-i.lighthouseapp.com/projects/27881/tickets/27-302-redirection-issue - - if ([self responseStatusCode] != 307 && (![self shouldUseRFC2616RedirectBehaviour] || [self responseStatusCode] == 303)) { - [self setRequestMethod:@"GET"]; - [self setPostBody:nil]; - [self setPostLength:0]; - - // Perhaps there are other headers we should be preserving, but it's hard to know what we need to keep and what to throw away. - NSString *userAgent = [[self requestHeaders] objectForKey:@"User-Agent"]; - if (userAgent) { - [self setRequestHeaders:[NSMutableDictionary dictionaryWithObject:userAgent forKey:@"User-Agent"]]; - } else { - [self setRequestHeaders:nil]; - } - [self setHaveBuiltRequestHeaders:NO]; - } else { - - // Force rebuild the cookie header incase we got some new cookies from this request - // All other request headers will remain as they are for 301 / 302 redirects - [self applyCookieHeader]; - } - - // Force the redirected request to rebuild the request headers (if not a 303, it will re-use old ones, and add any new ones) - - [self setURL:[[NSURL URLWithString:[responseHeaders valueForKey:@"Location"] relativeToURL:[self url]] absoluteURL]]; - [self setNeedsRedirect:YES]; - - // Clear the request cookies - // This means manually added cookies will not be added to the redirect request - only those stored in the global persistent store - // But, this is probably the safest option - we might be redirecting to a different domain - [self setRequestCookies:[NSMutableArray array]]; - - #if DEBUG_REQUEST_STATUS - NSLog(@"Request will redirect (code: %hi): %@",[self responseStatusCode],self); - #endif - - } - } - - if (![self needsRedirect]) { - // See if we got a Content-length header - NSString *cLength = [responseHeaders valueForKey:@"Content-Length"]; - ASIHTTPRequest *theRequest = self; - if ([self mainRequest]) { - theRequest = [self mainRequest]; - } - - if (cLength) { - SInt32 length = CFStringGetIntValue((CFStringRef)cLength); - - // Workaround for Apache HEAD requests for dynamically generated content returning the wrong Content-Length when using gzip - if ([self mainRequest] && [self allowCompressedResponse] && length == 20 && [self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [[self mainRequest] setShowAccurateProgress:NO]; - [[self mainRequest] incrementDownloadSizeBy:1]; - - } else { - [theRequest setContentLength:length]; - if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [theRequest incrementDownloadSizeBy:[theRequest contentLength]+[theRequest partialDownloadSize]]; - } - } - - } else if ([self showAccurateProgress] && [self shouldResetDownloadProgress]) { - [theRequest setShowAccurateProgress:NO]; - [theRequest incrementDownloadSizeBy:1]; - } - } - - // Handle connection persistence - if ([self shouldAttemptPersistentConnection]) { - - NSString *connectionHeader = [[[self responseHeaders] objectForKey:@"Connection"] lowercaseString]; - NSString *httpVersion = NSMakeCollectable([(NSString *)CFHTTPMessageCopyVersion(message) autorelease]); - - // Don't re-use the connection if the server is HTTP 1.0 and didn't send Connection: Keep-Alive - if (![httpVersion isEqualToString:(NSString *)kCFHTTPVersion1_0] || [connectionHeader isEqualToString:@"keep-alive"]) { - - // See if server explicitly told us to close the connection - if (![connectionHeader isEqualToString:@"close"]) { - - NSString *keepAliveHeader = [[self responseHeaders] objectForKey:@"Keep-Alive"]; - - // If we got a keep alive header, we'll reuse the connection for as long as the server tells us - if (keepAliveHeader) { - int timeout = 0; - int max = 0; - NSScanner *scanner = [NSScanner scannerWithString:keepAliveHeader]; - [scanner scanString:@"timeout=" intoString:NULL]; - [scanner scanInt:&timeout]; - [scanner scanUpToString:@"max=" intoString:NULL]; - [scanner scanString:@"max=" intoString:NULL]; - [scanner scanInt:&max]; - if (max > 5) { - [self setConnectionCanBeReused:YES]; - [self setPersistentConnectionTimeoutSeconds:timeout]; - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Got a keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); - #endif - } - - // Otherwise, we'll assume we can keep this connection open - } else { - [self setConnectionCanBeReused:YES]; - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Got no keep-alive header, will keep this connection open for %f seconds", [self persistentConnectionTimeoutSeconds]); - #endif - } - } - } - } - - CFRelease(message); - [self requestReceivedResponseHeaders]; -} - -// Handle response text encoding -// If the Content-Type header specified an encoding, we'll use that, otherwise we use defaultStringEncoding (which defaults to NSISOLatin1StringEncoding) -- (void)parseStringEncodingFromHeaders -{ - NSString *contentType = [[self responseHeaders] objectForKey:@"Content-Type"]; - NSStringEncoding encoding = [self defaultResponseEncoding]; - if (contentType) { - - NSString *charsetSeparator = @"charset="; - NSScanner *charsetScanner = [NSScanner scannerWithString: contentType]; - NSString *IANAEncoding = nil; - - if ([charsetScanner scanUpToString: charsetSeparator intoString: NULL] && [charsetScanner scanLocation] < [contentType length]) { - [charsetScanner setScanLocation: [charsetScanner scanLocation] + [charsetSeparator length]]; - [charsetScanner scanUpToString: @";" intoString: &IANAEncoding]; - } - - if (IANAEncoding) { - CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)IANAEncoding); - if (cfEncoding != kCFStringEncodingInvalidId) { - encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding); - } - } - } - [self setResponseEncoding:encoding]; -} - -#pragma mark http authentication - -- (void)saveProxyCredentialsToKeychain:(NSDictionary *)newCredentials -{ - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; - if (authenticationCredentials) { - [ASIHTTPRequest saveCredentials:authenticationCredentials forProxy:[self proxyHost] port:[self proxyPort] realm:[self proxyAuthenticationRealm]]; - } -} - - -- (void)saveCredentialsToKeychain:(NSDictionary *)newCredentials -{ - NSURLCredential *authenticationCredentials = [NSURLCredential credentialWithUser:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationUsername] password:[newCredentials objectForKey:(NSString *)kCFHTTPAuthenticationPassword] persistence:NSURLCredentialPersistencePermanent]; - - if (authenticationCredentials) { - [ASIHTTPRequest saveCredentials:authenticationCredentials forHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; - } -} - -- (BOOL)applyProxyCredentials:(NSDictionary *)newCredentials -{ - [self setProxyAuthenticationRetryCount:[self proxyAuthenticationRetryCount]+1]; - - if (newCredentials && proxyAuthentication && request) { - - // Apply whatever credentials we've built up to the old request - if (CFHTTPMessageApplyCredentialDictionary(request, proxyAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { - - //If we have credentials and they're ok, let's save them to the keychain - if (useKeychainPersistence) { - [self saveProxyCredentialsToKeychain:newCredentials]; - } - if (useSessionPersistence) { - NSMutableDictionary *sessionProxyCredentials = [NSMutableDictionary dictionary]; - [sessionProxyCredentials setObject:(id)proxyAuthentication forKey:@"Authentication"]; - [sessionProxyCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionProxyCredentials setObject:[self proxyHost] forKey:@"Host"]; - [sessionProxyCredentials setObject:[NSNumber numberWithInt:[self proxyPort]] forKey:@"Port"]; - [sessionProxyCredentials setObject:[self proxyAuthenticationScheme] forKey:@"AuthenticationScheme"]; - [[self class] storeProxyAuthenticationCredentialsInSessionStore:sessionProxyCredentials]; - } - [self setProxyCredentials:newCredentials]; - return YES; - } else { - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:newCredentials]; - } - } - return NO; -} - -- (BOOL)applyCredentials:(NSDictionary *)newCredentials -{ - [self setAuthenticationRetryCount:[self authenticationRetryCount]+1]; - - if (newCredentials && requestAuthentication && request) { - // Apply whatever credentials we've built up to the old request - if (CFHTTPMessageApplyCredentialDictionary(request, requestAuthentication, (CFMutableDictionaryRef)newCredentials, NULL)) { - - //If we have credentials and they're ok, let's save them to the keychain - if (useKeychainPersistence) { - [self saveCredentialsToKeychain:newCredentials]; - } - if (useSessionPersistence) { - - NSMutableDictionary *sessionCredentials = [NSMutableDictionary dictionary]; - [sessionCredentials setObject:(id)requestAuthentication forKey:@"Authentication"]; - [sessionCredentials setObject:newCredentials forKey:@"Credentials"]; - [sessionCredentials setObject:[self url] forKey:@"URL"]; - [sessionCredentials setObject:[self authenticationScheme] forKey:@"AuthenticationScheme"]; - if ([self authenticationRealm]) { - [sessionCredentials setObject:[self authenticationRealm] forKey:@"AuthenticationRealm"]; - } - [[self class] storeAuthenticationCredentialsInSessionStore:sessionCredentials]; - - } - [self setRequestCredentials:newCredentials]; - return YES; - } else { - [[self class] removeAuthenticationCredentialsFromSessionStore:newCredentials]; - } - } - return NO; -} - -- (NSMutableDictionary *)findProxyCredentials -{ - NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; - - // Is an account domain needed? (used currently for NTLM only) - if (CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { - if (![self proxyDomain]) { - [self setProxyDomain:@""]; - } - [newCredentials setObject:[self proxyDomain] forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; - } - - NSString *user = nil; - NSString *pass = nil; - - - // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request - if ([self mainRequest] && [[self mainRequest] proxyUsername] && [[self mainRequest] proxyPassword]) { - user = [[self mainRequest] proxyUsername]; - pass = [[self mainRequest] proxyPassword]; - - // Let's try to use the ones set in this object - } else if ([self proxyUsername] && [self proxyPassword]) { - user = [self proxyUsername]; - pass = [self proxyPassword]; - } - - - // Ok, that didn't work, let's try the keychain - // For authenticating proxies, we'll look in the keychain regardless of the value of useKeychainPersistence - if ((!user || !pass)) { - NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForProxy:[self proxyHost] port:[self proxyPort] protocol:[[self url] scheme] realm:[self proxyAuthenticationRealm]]; - if (authenticationCredentials) { - user = [authenticationCredentials user]; - pass = [authenticationCredentials password]; - } - - } - - // If we have a username and password, let's apply them to the request and continue - if (user && pass) { - - [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; - return newCredentials; - } - return nil; -} - - -- (NSMutableDictionary *)findCredentials -{ - NSMutableDictionary *newCredentials = [[[NSMutableDictionary alloc] init] autorelease]; - - // Is an account domain needed? (used currently for NTLM only) - if (CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { - if (!domain) { - [self setDomain:@""]; - } - [newCredentials setObject:domain forKey:(NSString *)kCFHTTPAuthenticationAccountDomain]; - } - - // First, let's look at the url to see if the username and password were included - NSString *user = [[self url] user]; - NSString *pass = [[self url] password]; - - // If the username and password weren't in the url - if (!user || !pass) { - - // If this is a HEAD request generated by an ASINetworkQueue, we'll try to use the details from the main request - if ([self mainRequest] && [[self mainRequest] username] && [[self mainRequest] password]) { - user = [[self mainRequest] username]; - pass = [[self mainRequest] password]; - - // Let's try to use the ones set in this object - } else if ([self username] && [self password]) { - user = [self username]; - pass = [self password]; - } - - } - - // Ok, that didn't work, let's try the keychain - if ((!user || !pass) && useKeychainPersistence) { - NSURLCredential *authenticationCredentials = [ASIHTTPRequest savedCredentialsForHost:[[self url] host] port:[[[self url] port] intValue] protocol:[[self url] scheme] realm:[self authenticationRealm]]; - if (authenticationCredentials) { - user = [authenticationCredentials user]; - pass = [authenticationCredentials password]; - } - - } - - // If we have a username and password, let's apply them to the request and continue - if (user && pass) { - - [newCredentials setObject:user forKey:(NSString *)kCFHTTPAuthenticationUsername]; - [newCredentials setObject:pass forKey:(NSString *)kCFHTTPAuthenticationPassword]; - return newCredentials; - } - return nil; -} - -// Called by delegate or authentication dialog to resume loading once authentication info has been populated -- (void)retryUsingSuppliedCredentials -{ - [self attemptToApplyCredentialsAndResume]; -} - -// Called by delegate or authentication dialog to cancel authentication -- (void)cancelAuthentication -{ - [self failWithError:ASIAuthenticationError]; -} - -- (BOOL)showProxyAuthenticationDialog -{ -// Mac authentication dialog coming soon! -#if TARGET_OS_IPHONE - if ([self shouldPresentProxyAuthenticationDialog]) { - [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; -#else - return NO; -#endif -} - - -- (BOOL)askDelegateForProxyCredentials -{ - - // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. - // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - - if ([authenticationDelegate respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - [authenticationDelegate performSelectorOnMainThread:@selector(proxyAuthenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; -} - -- (void)attemptToApplyProxyCredentialsAndResume -{ - - if ([self error] || [self isCancelled]) { - return; - } - - // Read authentication data - if (!proxyAuthentication) { - CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); - proxyAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); - CFRelease(responseHeader); - [self setProxyAuthenticationScheme:[(NSString *)CFHTTPAuthenticationCopyMethod(proxyAuthentication) autorelease]]; - } - - // If we haven't got a CFHTTPAuthenticationRef by now, something is badly wrong, so we'll have to give up - if (!proxyAuthentication) { - [self cancelLoad]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; - return; - } - - // Get the authentication realm - [self setProxyAuthenticationRealm:nil]; - if (!CFHTTPAuthenticationRequiresAccountDomain(proxyAuthentication)) { - [self setProxyAuthenticationRealm:[(NSString *)CFHTTPAuthenticationCopyRealm(proxyAuthentication) autorelease]]; - } - - // See if authentication is valid - CFStreamError err; - if (!CFHTTPAuthenticationIsValid(proxyAuthentication, &err)) { - - CFRelease(proxyAuthentication); - proxyAuthentication = NULL; - - // check for bad credentials, so we can give the delegate a chance to replace them - if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // We know the credentials we just presented are bad, we should remove them from the session store too - [[self class] removeProxyAuthenticationCredentialsFromSessionStore:proxyCredentials]; - [self setProxyCredentials:nil]; - - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - [self setLastActivityTime:nil]; - - if ([self askDelegateForProxyCredentials]) { - [self attemptToApplyProxyCredentialsAndResume]; - [delegateAuthenticationLock unlock]; - return; - } - if ([self showProxyAuthenticationDialog]) { - [self attemptToApplyProxyCredentialsAndResume]; - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - } - [self cancelLoad]; - [self failWithError:ASIAuthenticationError]; - return; - } - - [self cancelLoad]; - - if (proxyCredentials) { - - // We use startRequest rather than starting all over again in load request because NTLM requires we reuse the request - if ((([self proxyAuthenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self proxyAuthenticationRetryCount] < 2) && [self applyProxyCredentials:proxyCredentials]) { - [self startRequest]; - - // We've failed NTLM authentication twice, we should assume our credentials are wrong - } else if ([self proxyAuthenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self proxyAuthenticationRetryCount] == 2) { - [self failWithError:ASIAuthenticationError]; - - // Something went wrong, we'll have to give up - } else { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - // Are a user name & password needed? - } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(proxyAuthentication)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionProxyAuthenticationCredentials]; - if (credentials && [self applyProxyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - NSMutableDictionary *newCredentials = [self findProxyCredentials]; - - //If we have some credentials to use let's apply them to the request and continue - if (newCredentials) { - - if ([self applyProxyCredentials:newCredentials]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - } else { - [delegateAuthenticationLock unlock]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply proxy credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - return; - } - - if ([self askDelegateForProxyCredentials]) { - [delegateAuthenticationLock unlock]; - return; - } - - if ([self showProxyAuthenticationDialog]) { - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - - // The delegate isn't interested and we aren't showing the authentication dialog, we'll have to give up - [self failWithError:ASIAuthenticationError]; - return; - } - -} - -- (BOOL)showAuthenticationDialog -{ -// Mac authentication dialog coming soon! -#if TARGET_OS_IPHONE - if ([self shouldPresentAuthenticationDialog]) { - [ASIAuthenticationDialog performSelectorOnMainThread:@selector(presentAuthenticationDialogForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; -#else - return NO; -#endif -} - -- (BOOL)askDelegateForCredentials -{ - // If we have a delegate, we'll see if it can handle proxyAuthenticationNeededForRequest:. - // Otherwise, we'll try the queue (if this request is part of one) and it will pass the message on to its own delegate - id authenticationDelegate = [self delegate]; - if (!authenticationDelegate) { - authenticationDelegate = [self queue]; - } - - if ([authenticationDelegate respondsToSelector:@selector(authenticationNeededForRequest:)]) { - [authenticationDelegate performSelectorOnMainThread:@selector(authenticationNeededForRequest:) withObject:self waitUntilDone:[NSThread isMainThread]]; - return YES; - } - return NO; -} - -- (void)attemptToApplyCredentialsAndResume -{ - if ([self error] || [self isCancelled]) { - return; - } - - if ([self authenticationNeeded] == ASIProxyAuthenticationNeeded) { - [self attemptToApplyProxyCredentialsAndResume]; - return; - } - - // Read authentication data - if (!requestAuthentication) { - CFHTTPMessageRef responseHeader = (CFHTTPMessageRef) CFReadStreamCopyProperty((CFReadStreamRef)[self readStream],kCFStreamPropertyHTTPResponseHeader); - requestAuthentication = CFHTTPAuthenticationCreateFromResponse(NULL, responseHeader); - CFRelease(responseHeader); - [self setAuthenticationScheme:[(NSString *)CFHTTPAuthenticationCopyMethod(requestAuthentication) autorelease]]; - } - - if (!requestAuthentication) { - [self cancelLoad]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to get authentication object from response headers",NSLocalizedDescriptionKey,nil]]]; - return; - } - - // Get the authentication realm - [self setAuthenticationRealm:nil]; - if (!CFHTTPAuthenticationRequiresAccountDomain(requestAuthentication)) { - [self setAuthenticationRealm:[(NSString *)CFHTTPAuthenticationCopyRealm(requestAuthentication) autorelease]]; - } - - // See if authentication is valid - CFStreamError err; - if (!CFHTTPAuthenticationIsValid(requestAuthentication, &err)) { - - CFRelease(requestAuthentication); - requestAuthentication = NULL; - - // check for bad credentials, so we can give the delegate a chance to replace them - if (err.domain == kCFStreamErrorDomainHTTP && (err.error == kCFStreamErrorHTTPAuthenticationBadUserName || err.error == kCFStreamErrorHTTPAuthenticationBadPassword)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // We know the credentials we just presented are bad, we should remove them from the session store too - [[self class] removeAuthenticationCredentialsFromSessionStore:requestCredentials]; - [self setRequestCredentials:nil]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionAuthenticationCredentials]; - if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - - - [self setLastActivityTime:nil]; - - if ([self askDelegateForCredentials]) { - [delegateAuthenticationLock unlock]; - return; - } - if ([self showAuthenticationDialog]) { - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - } - [self cancelLoad]; - [self failWithError:ASIAuthenticationError]; - return; - } - - [self cancelLoad]; - - if (requestCredentials) { - - if ((([self authenticationScheme] != (NSString *)kCFHTTPAuthenticationSchemeNTLM) || [self authenticationRetryCount] < 2) && [self applyCredentials:requestCredentials]) { - [self startRequest]; - - // We've failed NTLM authentication twice, we should assume our credentials are wrong - } else if ([self authenticationScheme] == (NSString *)kCFHTTPAuthenticationSchemeNTLM && [self authenticationRetryCount ] == 2) { - [self failWithError:ASIAuthenticationError]; - - } else { - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - - // Are a user name & password needed? - } else if (CFHTTPAuthenticationRequiresUserNameAndPassword(requestAuthentication)) { - - // Prevent more than one request from asking for credentials at once - [delegateAuthenticationLock lock]; - - // If the user cancelled authentication via a dialog presented by another request, our queue may have cancelled us - if ([self error] || [self isCancelled]) { - [delegateAuthenticationLock unlock]; - return; - } - - // Now we've acquired the lock, it may be that the session contains credentials we can re-use for this request - if ([self useSessionPersistence]) { - NSDictionary *credentials = [self findSessionAuthenticationCredentials]; - if (credentials && [self applyCredentials:[credentials objectForKey:@"Credentials"]]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - return; - } - } - - - NSMutableDictionary *newCredentials = [self findCredentials]; - - //If we have some credentials to use let's apply them to the request and continue - if (newCredentials) { - - if ([self applyCredentials:newCredentials]) { - [delegateAuthenticationLock unlock]; - [self startRequest]; - } else { - [delegateAuthenticationLock unlock]; - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIInternalErrorWhileApplyingCredentialsType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"Failed to apply credentials to request",NSLocalizedDescriptionKey,nil]]]; - } - return; - } - if ([self askDelegateForCredentials]) { - [delegateAuthenticationLock unlock]; - return; - } - - if ([self showAuthenticationDialog]) { - [delegateAuthenticationLock unlock]; - return; - } - [delegateAuthenticationLock unlock]; - - [self failWithError:ASIAuthenticationError]; - - return; - } - -} - -- (void)addBasicAuthenticationHeaderWithUsername:(NSString *)theUsername andPassword:(NSString *)thePassword -{ - [self addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:@"%@:%@",theUsername,thePassword] dataUsingEncoding:NSUTF8StringEncoding]]]]; -} - - -#pragma mark stream status handlers - -- (void)handleNetworkEvent:(CFStreamEventType)type -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - [[self retain] autorelease]; - - [[self cancelledLock] lock]; - - if ([self complete] || [self isCancelled]) { - [[self cancelledLock] unlock]; - [pool release]; - return; - } - - // Dispatch the stream events. - switch (type) { - case kCFStreamEventHasBytesAvailable: - [self handleBytesAvailable]; - break; - - case kCFStreamEventEndEncountered: - [self handleStreamComplete]; - break; - - case kCFStreamEventErrorOccurred: - [self handleStreamError]; - break; - - default: - break; - } - - [self performThrottling]; - - [[self cancelledLock] unlock]; - - if ([self downloadComplete] && [self needsRedirect]) { - [self performRedirect]; - } else if ([self downloadComplete] && [self authenticationNeeded]) { - [self attemptToApplyCredentialsAndResume]; - } - [pool release]; -} - - -- (void)handleBytesAvailable -{ - if (![self responseHeaders]) { - [self readResponseHeaders]; - } - - // If we've cancelled the load part way through (for example, after deciding to use a cached version) - if ([self complete]) { - return; - } - - // In certain (presumably very rare) circumstances, handleBytesAvailable seems to be called when there isn't actually any data available - // We'll check that there is actually data available to prevent blocking on CFReadStreamRead() - // So far, I've only seen this in the stress tests, so it might never happen in real-world situations. - if (!CFReadStreamHasBytesAvailable((CFReadStreamRef)[self readStream])) { - return; - } - - long long bufferSize = 16384; - if (contentLength > 262144) { - bufferSize = 262144; - } else if (contentLength > 65536) { - bufferSize = 65536; - } - - // Reduce the buffer size if we're receiving data too quickly when bandwidth throttling is active - // This just augments the throttling done in measureBandwidthUsage to reduce the amount we go over the limit - - if ([[self class] isBandwidthThrottled]) { - [bandwidthThrottlingLock lock]; - if (maxBandwidthPerSecond > 0) { - long long maxiumumSize = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; - if (maxiumumSize < 0) { - // We aren't supposed to read any more data right now, but we'll read a single byte anyway so the CFNetwork's buffer isn't full - bufferSize = 1; - } else if (maxiumumSize/4 < bufferSize) { - // We were going to fetch more data that we should be allowed, so we'll reduce the size of our read - bufferSize = maxiumumSize/4; - } - } - if (bufferSize < 1) { - bufferSize = 1; - } - [bandwidthThrottlingLock unlock]; - } - - - UInt8 buffer[bufferSize]; - NSInteger bytesRead = [[self readStream] read:buffer maxLength:sizeof(buffer)]; - - // Less than zero is an error - if (bytesRead < 0) { - [self handleStreamError]; - - // If zero bytes were read, wait for the EOF to come. - } else if (bytesRead) { - - [self setTotalBytesRead:[self totalBytesRead]+bytesRead]; - [self setLastActivityTime:[NSDate date]]; - - // For bandwidth measurement / throttling - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:bytesRead]; - - // If we need to redirect, and have automatic redirect on, and might be resuming a download, let's do nothing with the content - if ([self needsRedirect] && [self shouldRedirect] && [self allowResumeForFileDownloads]) { - return; - } - - // Does the delegate want to handle the data manually? - if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) { - NSMethodSignature *signature = [[[self delegate] class] instanceMethodSignatureForSelector:[self didReceiveDataSelector]]; - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; - [invocation setTarget:[self delegate]]; - [invocation setSelector:[self didReceiveDataSelector]]; - [invocation setArgument:&self atIndex:2]; - NSData *data = [NSData dataWithBytes:buffer length:bytesRead]; - [invocation setArgument:&data atIndex:3]; - [invocation retainArguments]; - [invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:[self delegate] waitUntilDone:[NSThread isMainThread]]; - - // Are we downloading to a file? - } else if ([self downloadDestinationPath]) { - if (![self fileDownloadOutputStream]) { - BOOL append = NO; - if (![self temporaryFileDownloadPath]) { - [self setTemporaryFileDownloadPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]]; - } else if ([self allowResumeForFileDownloads]) { - append = YES; - } - - [self setFileDownloadOutputStream:[[[NSOutputStream alloc] initToFileAtPath:[self temporaryFileDownloadPath] append:append] autorelease]]; - [[self fileDownloadOutputStream] open]; - } - [[self fileDownloadOutputStream] write:buffer maxLength:bytesRead]; - - //Otherwise, let's add the data to our in-memory store - } else { - [rawResponseData appendBytes:buffer length:bytesRead]; - } - } - -} - -- (void)handleStreamComplete -{ -#if DEBUG_REQUEST_STATUS - NSLog(@"Request %@ finished downloading data",self); -#endif - - [self setDownloadComplete:YES]; - - if (![self responseHeaders]) { - [self readResponseHeaders]; - } - - [progressLock lock]; - // Find out how much data we've uploaded so far - [self setLastBytesSent:totalBytesSent]; - [self setTotalBytesSent:[NSMakeCollectable([(NSNumber *)CFReadStreamCopyProperty((CFReadStreamRef)[self readStream], kCFStreamPropertyHTTPRequestBytesWrittenCount) autorelease]) unsignedLongLongValue]]; - [self setComplete:YES]; - [self updateProgressIndicators]; - - - [[self postBodyReadStream] close]; - - NSError *fileError = nil; - - // Delete up the request body temporary file, if it exists - if ([self didCreateTemporaryPostDataFile] && ![self authenticationNeeded]) { - [self removePostDataFile]; - } - - // Close the output stream as we're done writing to the file - if ([self temporaryFileDownloadPath]) { - [[self fileDownloadOutputStream] close]; - [self setFileDownloadOutputStream:nil]; - - // If we are going to redirect and we are resuming, let's ignore this download - if ([self shouldRedirect] && [self needsRedirect] && [self allowResumeForFileDownloads]) { - - // Decompress the file (if necessary) directly to the destination path - } else if ([self isResponseCompressed]) { - int decompressionStatus = [ASIHTTPRequest uncompressZippedDataFromFile:[self temporaryFileDownloadPath] toFile:[self downloadDestinationPath]]; - if (decompressionStatus != Z_OK) { - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Decompression of %@ failed with code %hi",[self temporaryFileDownloadPath],decompressionStatus],NSLocalizedDescriptionKey,nil]]; - } - [self removeTemporaryDownloadFile]; - } else { - - - //Remove any file at the destination path - NSError *moveError = nil; - if ([[NSFileManager defaultManager] fileExistsAtPath:[self downloadDestinationPath]]) { - [[NSFileManager defaultManager] removeItemAtPath:[self downloadDestinationPath] error:&moveError]; - if (moveError) { - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Unable to remove file at path '%@'",[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; - } - } - - //Move the temporary file to the destination path - if (!fileError) { - [[NSFileManager defaultManager] moveItemAtPath:[self temporaryFileDownloadPath] toPath:[self downloadDestinationPath] error:&moveError]; - if (moveError) { - fileError = [NSError errorWithDomain:NetworkRequestErrorDomain code:ASIFileManagementError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"Failed to move file from '%@' to '%@'",[self temporaryFileDownloadPath],[self downloadDestinationPath]],NSLocalizedDescriptionKey,moveError,NSUnderlyingErrorKey,nil]]; - } - [self setTemporaryFileDownloadPath:nil]; - } - - } - } - - // Save to the cache - if ([self downloadCache]) { - [[self downloadCache] storeResponseForRequest:self maxAge:[self secondsToCache]]; - } - - [progressLock unlock]; - - - [connectionsLock lock]; - if (![self connectionCanBeReused]) { - [self unscheduleReadStream]; - } - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Request #%@ finished using connection #%@",[self requestID], [[self connectionInfo] objectForKey:@"id"]); - #endif - [[self connectionInfo] removeObjectForKey:@"request"]; - [[self connectionInfo] setObject:[NSDate dateWithTimeIntervalSinceNow:[self persistentConnectionTimeoutSeconds]] forKey:@"expires"]; - [connectionsLock unlock]; - - if (![self authenticationNeeded]) { - [self destroyReadStream]; - } - - if (![self needsRedirect] && ![self authenticationNeeded]) { - - if (fileError) { - [self failWithError:fileError]; - } else { - [self requestFinished]; - } - - [self markAsFinished]; - - // If request has asked delegate or ASIAuthenticationDialog for credentials - } else if ([self authenticationNeeded]) { - [self setStatusTimer:nil]; - CFRunLoopStop(CFRunLoopGetCurrent()); - } -} - -- (void)markAsFinished -{ - // Autoreleased requests may well be dealloced here otherwise - [self retain]; - - // dealloc won't be called when running with GC, so we'll clean these up now - if (request) { - CFMakeCollectable(request); - } - if (requestAuthentication) { - CFMakeCollectable(requestAuthentication); - } - if (proxyAuthentication) { - CFMakeCollectable(proxyAuthentication); - } - - [self willChangeValueForKey:@"isFinished"]; - [self willChangeValueForKey:@"isExecuting"]; - [self setInProgress:NO]; - [self setStatusTimer:nil]; - - [self didChangeValueForKey:@"isExecuting"]; - [self didChangeValueForKey:@"isFinished"]; - - CFRunLoopStop(CFRunLoopGetCurrent()); - - [self release]; -} - -- (BOOL)useDataFromCache -{ - NSDictionary *headers = [[self downloadCache] cachedHeadersForRequest:self]; - if (!headers) { - return NO; - } - NSString *dataPath = [[self downloadCache] pathToCachedResponseDataForRequest:self]; - if (!dataPath) { - return NO; - } - - if ([self cachePolicy] == ASIReloadIfDifferentCachePolicy) { - if (![[self downloadCache] isCachedDataCurrentForRequest:self]) { - [[self downloadCache] removeCachedDataForRequest:self]; - return NO; - } - } - - [self setDidUseCachedResponse:YES]; - - ASIHTTPRequest *theRequest = self; - if ([self mainRequest]) { - theRequest = [self mainRequest]; - } - [theRequest setResponseHeaders:headers]; - if ([theRequest downloadDestinationPath]) { - [theRequest setDownloadDestinationPath:dataPath]; - } else { - [theRequest setRawResponseData:[NSMutableData dataWithContentsOfFile:dataPath]]; - } - [theRequest setContentLength:[[[self responseHeaders] objectForKey:@"Content-Length"] longLongValue]]; - [theRequest setTotalBytesRead:[self contentLength]]; - - [theRequest parseStringEncodingFromHeaders]; - - [theRequest setResponseCookies:[NSHTTPCookie cookiesWithResponseHeaderFields:headers forURL:[self url]]]; - - [theRequest setComplete:YES]; - [theRequest setDownloadComplete:YES]; - - [theRequest updateProgressIndicators]; - [theRequest requestFinished]; - [theRequest markAsFinished]; - if ([self mainRequest]) { - [self markAsFinished]; - } - return YES; -} - -- (BOOL)retryUsingNewConnection -{ - if ([self retryCount] == 0) { - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Request attempted to use connection #%@, but it has been closed - will retry with a new connection", [[self connectionInfo] objectForKey:@"id"]); - #endif - [connectionsLock lock]; - [[self connectionInfo] removeObjectForKey:@"request"]; - [persistentConnectionsPool removeObject:[self connectionInfo]]; - [self setConnectionInfo:nil]; - [connectionsLock unlock]; - [self setRetryCount:[self retryCount]+1]; - [self startRequest]; - return YES; - } - #if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Request attempted to use connection #%@, but it has been closed - we have already retried with a new connection, so we must give up", [[self connectionInfo] objectForKey:@"id"]); - #endif - return NO; -} - -- (void)handleStreamError - -{ - NSError *underlyingError = NSMakeCollectable([(NSError *)CFReadStreamCopyError((CFReadStreamRef)[self readStream]) autorelease]); - - [self cancelLoad]; - - if (![self error]) { // We may already have handled this error - - // First, check for a 'socket not connected', 'broken pipe' or 'connection lost' error - // This may occur when we've attempted to reuse a connection that should have been closed - // If we get this, we need to retry the request - // We'll only do this once - if it happens again on retry, we'll give up - // -1005 = kCFURLErrorNetworkConnectionLost - this doesn't seem to be declared on Mac OS 10.5 - if (([[underlyingError domain] isEqualToString:NSPOSIXErrorDomain] && ([underlyingError code] == ENOTCONN || [underlyingError code] == EPIPE)) - || ([[underlyingError domain] isEqualToString:(NSString *)kCFErrorDomainCFNetwork] && [underlyingError code] == -1005)) { - if ([self retryUsingNewConnection]) { - return; - } - } - - NSString *reason = @"A connection failure occurred"; - - // We'll use a custom error message for SSL errors, but you should always check underlying error if you want more details - // For some reason SecureTransport.h doesn't seem to be available on iphone, so error codes hard-coded - // Also, iPhone seems to handle errors differently from Mac OS X - a self-signed certificate returns a different error code on each platform, so we'll just provide a general error - if ([[underlyingError domain] isEqualToString:NSOSStatusErrorDomain]) { - if ([underlyingError code] <= -9800 && [underlyingError code] >= -9818) { - reason = [NSString stringWithFormat:@"%@: SSL problem (possibly a bad/expired/self-signed certificate)",reason]; - } - } - - [self failWithError:[NSError errorWithDomain:NetworkRequestErrorDomain code:ASIConnectionFailureErrorType userInfo:[NSDictionary dictionaryWithObjectsAndKeys:reason,NSLocalizedDescriptionKey,underlyingError,NSUnderlyingErrorKey,nil]]]; - } - [self checkRequestStatus]; -} - -#pragma mark managing the read stream - - - -- (void)destroyReadStream -{ - if ([self readStream]) { - CFReadStreamSetClient((CFReadStreamRef)[self readStream], kCFStreamEventNone, NULL, NULL); - [connectionsLock lock]; - - if ([self readStreamIsScheduled]) { - runningRequestCount--; - #if TARGET_OS_IPHONE - if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) { - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; - } - #endif - } - - [self setReadStreamIsScheduled:NO]; - - if (![self connectionCanBeReused]) { - [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [[self readStream] close]; - } - [self setReadStream:nil]; - [connectionsLock unlock]; - } -} - -- (void)scheduleReadStream -{ - if ([self readStream] && ![self readStreamIsScheduled]) { - - [connectionsLock lock]; - runningRequestCount++; - #if TARGET_OS_IPHONE - if (shouldUpdateNetworkActivityIndicator) { - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; - } - #endif - [connectionsLock unlock]; - - // Reset the timeout - [self setLastActivityTime:[NSDate date]]; - [[self readStream] scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [self setReadStreamIsScheduled:YES]; - } -} - -- (void)unscheduleReadStream -{ - if ([self readStream] && [self readStreamIsScheduled]) { - - [connectionsLock lock]; - runningRequestCount--; - #if TARGET_OS_IPHONE - if (shouldUpdateNetworkActivityIndicator && runningRequestCount == 0) { - [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; - } - #endif - [connectionsLock unlock]; - - [[self readStream] removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:[self runLoopMode]]; - [self setReadStreamIsScheduled:NO]; - } -} - -#pragma mark persistent connections - -- (NSNumber *)connectionID -{ - return [[self connectionInfo] objectForKey:@"id"]; -} - -+ (void)expirePersistentConnections -{ - [connectionsLock lock]; - NSUInteger i; - for (i=0; i<[persistentConnectionsPool count]; i++) { - NSDictionary *existingConnection = [persistentConnectionsPool objectAtIndex:i]; - if (![existingConnection objectForKey:@"request"] && [[existingConnection objectForKey:@"expires"] timeIntervalSinceNow] <= 0) { -#if DEBUG_PERSISTENT_CONNECTIONS - NSLog(@"Closing connection #%hi because it has expired",[[existingConnection objectForKey:@"id"] intValue]); -#endif - NSInputStream *stream = [existingConnection objectForKey:@"stream"]; - if (stream) { - [stream close]; - } - [persistentConnectionsPool removeObject:existingConnection]; - i--; - } - } - [connectionsLock unlock]; -} - -#pragma mark NSCopying - -- (id)copyWithZone:(NSZone *)zone -{ - // Don't forget - this will return a retained copy! - ASIHTTPRequest *newRequest = [[[self class] alloc] initWithURL:[self url]]; - [newRequest setDelegate:[self delegate]]; - [newRequest setRequestMethod:[self requestMethod]]; - [newRequest setPostBody:[self postBody]]; - [newRequest setShouldStreamPostDataFromDisk:[self shouldStreamPostDataFromDisk]]; - [newRequest setPostBodyFilePath:[self postBodyFilePath]]; - [newRequest setRequestHeaders:[[[self requestHeaders] mutableCopyWithZone:zone] autorelease]]; - [newRequest setRequestCookies:[[[self requestCookies] mutableCopyWithZone:zone] autorelease]]; - [newRequest setUseCookiePersistence:[self useCookiePersistence]]; - [newRequest setUseKeychainPersistence:[self useKeychainPersistence]]; - [newRequest setUseSessionPersistence:[self useSessionPersistence]]; - [newRequest setAllowCompressedResponse:[self allowCompressedResponse]]; - [newRequest setDownloadDestinationPath:[self downloadDestinationPath]]; - [newRequest setTemporaryFileDownloadPath:[self temporaryFileDownloadPath]]; - [newRequest setUsername:[self username]]; - [newRequest setPassword:[self password]]; - [newRequest setDomain:[self domain]]; - [newRequest setProxyUsername:[self proxyUsername]]; - [newRequest setProxyPassword:[self proxyPassword]]; - [newRequest setProxyDomain:[self proxyDomain]]; - [newRequest setProxyHost:[self proxyHost]]; - [newRequest setProxyPort:[self proxyPort]]; - [newRequest setProxyType:[self proxyType]]; - [newRequest setUploadProgressDelegate:[self uploadProgressDelegate]]; - [newRequest setDownloadProgressDelegate:[self downloadProgressDelegate]]; - [newRequest setShouldPresentAuthenticationDialog:[self shouldPresentAuthenticationDialog]]; - [newRequest setShouldPresentProxyAuthenticationDialog:[self shouldPresentProxyAuthenticationDialog]]; - [newRequest setPostLength:[self postLength]]; - [newRequest setHaveBuiltPostBody:[self haveBuiltPostBody]]; - [newRequest setDidStartSelector:[self didStartSelector]]; - [newRequest setDidFinishSelector:[self didFinishSelector]]; - [newRequest setDidFailSelector:[self didFailSelector]]; - [newRequest setTimeOutSeconds:[self timeOutSeconds]]; - [newRequest setShouldResetDownloadProgress:[self shouldResetDownloadProgress]]; - [newRequest setShouldResetUploadProgress:[self shouldResetUploadProgress]]; - [newRequest setShowAccurateProgress:[self showAccurateProgress]]; - [newRequest setDefaultResponseEncoding:[self defaultResponseEncoding]]; - [newRequest setAllowResumeForFileDownloads:[self allowResumeForFileDownloads]]; - [newRequest setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; - [newRequest setUseHTTPVersionOne:[self useHTTPVersionOne]]; - [newRequest setShouldRedirect:[self shouldRedirect]]; - [newRequest setValidatesSecureCertificate:[self validatesSecureCertificate]]; - [newRequest setPACurl:[self PACurl]]; - [newRequest setShouldPresentCredentialsBeforeChallenge:[self shouldPresentCredentialsBeforeChallenge]]; - [newRequest setNumberOfTimesToRetryOnTimeout:[self numberOfTimesToRetryOnTimeout]]; - [newRequest setShouldUseRFC2616RedirectBehaviour:[self shouldUseRFC2616RedirectBehaviour]]; - [newRequest setShouldAttemptPersistentConnection:[self shouldAttemptPersistentConnection]]; - [newRequest setPersistentConnectionTimeoutSeconds:[self persistentConnectionTimeoutSeconds]]; - return newRequest; -} - -#pragma mark default time out - -+ (NSTimeInterval)defaultTimeOutSeconds -{ - return defaultTimeOutSeconds; -} - -+ (void)setDefaultTimeOutSeconds:(NSTimeInterval)newTimeOutSeconds -{ - defaultTimeOutSeconds = newTimeOutSeconds; -} - -#pragma mark session credentials - -+ (NSMutableArray *)sessionProxyCredentialsStore -{ - [sessionCredentialsLock lock]; - if (!sessionProxyCredentialsStore) { - sessionProxyCredentialsStore = [[NSMutableArray alloc] init]; - } - [sessionCredentialsLock unlock]; - return sessionProxyCredentialsStore; -} - -+ (NSMutableArray *)sessionCredentialsStore -{ - [sessionCredentialsLock lock]; - if (!sessionCredentialsStore) { - sessionCredentialsStore = [[NSMutableArray alloc] init]; - } - [sessionCredentialsLock unlock]; - return sessionCredentialsStore; -} - -+ (void)storeProxyAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - [self removeProxyAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - [[[self class] sessionProxyCredentialsStore] addObject:credentials]; - [sessionCredentialsLock unlock]; -} - -+ (void)storeAuthenticationCredentialsInSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - [self removeAuthenticationCredentialsFromSessionStore:[credentials objectForKey:@"Credentials"]]; - [[[self class] sessionCredentialsStore] addObject:credentials]; - [sessionCredentialsLock unlock]; -} - -+ (void)removeProxyAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; - NSUInteger i; - for (i=0; i<[sessionCredentialsList count]; i++) { - NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; - if ([theCredentials objectForKey:@"Credentials"] == credentials) { - [sessionCredentialsList removeObjectAtIndex:i]; - [sessionCredentialsLock unlock]; - return; - } - } - [sessionCredentialsLock unlock]; -} - -+ (void)removeAuthenticationCredentialsFromSessionStore:(NSDictionary *)credentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; - NSUInteger i; - for (i=0; i<[sessionCredentialsList count]; i++) { - NSDictionary *theCredentials = [sessionCredentialsList objectAtIndex:i]; - if ([theCredentials objectForKey:@"Credentials"] == credentials) { - [sessionCredentialsList removeObjectAtIndex:i]; - [sessionCredentialsLock unlock]; - return; - } - } - [sessionCredentialsLock unlock]; -} - -- (NSDictionary *)findSessionProxyAuthenticationCredentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionProxyCredentialsStore]; - for (NSDictionary *theCredentials in sessionCredentialsList) { - if ([[theCredentials objectForKey:@"Host"] isEqualToString:[self proxyHost]] && [[theCredentials objectForKey:@"Port"] intValue] == [self proxyPort]) { - [sessionCredentialsLock unlock]; - return theCredentials; - } - } - [sessionCredentialsLock unlock]; - return nil; -} - - -- (NSDictionary *)findSessionAuthenticationCredentials -{ - [sessionCredentialsLock lock]; - NSMutableArray *sessionCredentialsList = [[self class] sessionCredentialsStore]; - // Find an exact match (same url) - for (NSDictionary *theCredentials in sessionCredentialsList) { - if ([[theCredentials objectForKey:@"URL"] isEqual:[self url]]) { - // /Just a sanity check to ensure we never choose credentials from a different realm. Can't really do more than that, as either this request or the stored credentials may not have a realm when the other does - if (![self responseStatusCode] || (![theCredentials objectForKey:@"AuthenticationRealm"] || [[theCredentials objectForKey:@"AuthenticationRealm"] isEqualToString:[self authenticationRealm]])) { - [sessionCredentialsLock unlock]; - return theCredentials; - } - } - } - // Find a rough match (same host, port, scheme) - NSURL *requestURL = [self url]; - for (NSDictionary *theCredentials in sessionCredentialsList) { - NSURL *theURL = [theCredentials objectForKey:@"URL"]; - - // Port can be nil! - if ([[theURL host] isEqualToString:[requestURL host]] && ([theURL port] == [requestURL port] || ([requestURL port] && [[theURL port] isEqualToNumber:[requestURL port]])) && [[theURL scheme] isEqualToString:[requestURL scheme]]) { - if (![self responseStatusCode] || (![theCredentials objectForKey:@"AuthenticationRealm"] || [[theCredentials objectForKey:@"AuthenticationRealm"] isEqualToString:[self authenticationRealm]])) { - [sessionCredentialsLock unlock]; - return theCredentials; - } - } - } - [sessionCredentialsLock unlock]; - return nil; -} - -#pragma mark keychain storage - -+ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; -} - -+ (void)saveCredentials:(NSURLCredential *)credentials forProxy:(NSString *)host port:(int)port realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credentials forProtectionSpace:protectionSpace]; -} - -+ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; -} - -+ (NSURLCredential *)savedCredentialsForProxy:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - return [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; -} - -+ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; - if (credential) { - [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; - } -} - -+ (void)removeCredentialsForProxy:(NSString *)host port:(int)port realm:(NSString *)realm -{ - NSURLProtectionSpace *protectionSpace = [[[NSURLProtectionSpace alloc] initWithProxyHost:host port:port type:NSURLProtectionSpaceHTTPProxy realm:realm authenticationMethod:NSURLAuthenticationMethodDefault] autorelease]; - NSURLCredential *credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:protectionSpace]; - if (credential) { - [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:credential forProtectionSpace:protectionSpace]; - } -} - - -+ (NSMutableArray *)sessionCookies -{ - if (!sessionCookies) { - [ASIHTTPRequest setSessionCookies:[[[NSMutableArray alloc] init] autorelease]]; - } - return sessionCookies; -} - -+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies -{ - [sessionCookiesLock lock]; - // Remove existing cookies from the persistent store - for (NSHTTPCookie *cookie in sessionCookies) { - [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; - } - [sessionCookies release]; - sessionCookies = [newSessionCookies retain]; - [sessionCookiesLock unlock]; -} - -+ (void)addSessionCookie:(NSHTTPCookie *)newCookie -{ - [sessionCookiesLock lock]; - NSHTTPCookie *cookie; - NSUInteger i; - NSUInteger max = [[ASIHTTPRequest sessionCookies] count]; - for (i=0; i= [decompressed length]) { - [decompressed increaseLengthBy: half_length]; - } - strm.next_out = [decompressed mutableBytes] + strm.total_out; - strm.avail_out = (unsigned int)([decompressed length] - strm.total_out); - - // Inflate another chunk. - status = inflate (&strm, Z_SYNC_FLUSH); - if (status == Z_STREAM_END) { - done = YES; - } else if (status != Z_OK) { - break; - } - } - if (inflateEnd (&strm) != Z_OK) return nil; - - // Set real length. - if (done) { - [decompressed setLength: strm.total_out]; - return [NSData dataWithData: decompressed]; - } else { - return nil; - } -} - -// NOTE: To debug this method, turn off Data Formatters in Xcode or you'll crash on closeFile -+ (int)uncompressZippedDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath -{ - // Create an empty file at the destination path - if (![[NSFileManager defaultManager] createFileAtPath:destinationPath contents:[NSData data] attributes:nil]) { - return 1; - } - - // Get a FILE struct for the source file - NSFileHandle *inputFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; - FILE *source = fdopen([inputFileHandle fileDescriptor], "r"); - - // Get a FILE struct for the destination path - NSFileHandle *outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:destinationPath]; - FILE *dest = fdopen([outputFileHandle fileDescriptor], "w"); - - - // Uncompress data in source and save in destination - int status = [ASIHTTPRequest uncompressZippedDataFromSource:source toDestination:dest]; - - // Close the files - fclose(dest); - fclose(source); - [inputFileHandle closeFile]; - [outputFileHandle closeFile]; - return status; -} - -// -// From the zlib sample code by Mark Adler, code here: -// http://www.zlib.net/zpipe.c -// -#define CHUNK 16384 - -+ (int)uncompressZippedDataFromSource:(FILE *)source toDestination:(FILE *)dest -{ - int ret; - unsigned have; - z_stream strm; - unsigned char in[CHUNK]; - unsigned char out[CHUNK]; - - /* allocate inflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.avail_in = 0; - strm.next_in = Z_NULL; - ret = inflateInit2(&strm, (15+32)); - if (ret != Z_OK) - return ret; - - /* decompress until deflate stream ends or end of file */ - do { - strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source); - if (ferror(source)) { - (void)inflateEnd(&strm); - return Z_ERRNO; - } - if (strm.avail_in == 0) - break; - strm.next_in = in; - - /* run inflate() on input until output buffer not full */ - do { - strm.avail_out = CHUNK; - strm.next_out = out; - ret = inflate(&strm, Z_NO_FLUSH); - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ - switch (ret) { - case Z_NEED_DICT: - ret = Z_DATA_ERROR; /* and fall through */ - case Z_DATA_ERROR: - case Z_MEM_ERROR: - (void)inflateEnd(&strm); - return ret; - } - have = CHUNK - strm.avail_out; - if (fwrite(&out, 1, have, dest) != have || ferror(dest)) { - (void)inflateEnd(&strm); - return Z_ERRNO; - } - } while (strm.avail_out == 0); - - /* done when inflate() says it's done */ - } while (ret != Z_STREAM_END); - - /* clean up and return */ - (void)inflateEnd(&strm); - return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; -} - - -#pragma mark gzip compression - -// Based on this from Robbie Hanson: http://deusty.blogspot.com/2007/07/gzip-compressiondecompression.html - -+ (NSData *)compressData:(NSData*)uncompressedData -{ - if ([uncompressedData length] == 0) return uncompressedData; - - z_stream strm; - - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - strm.total_out = 0; - strm.next_in=(Bytef *)[uncompressedData bytes]; - strm.avail_in = (unsigned int)[uncompressedData length]; - - // Compresssion Levels: - // Z_NO_COMPRESSION - // Z_BEST_SPEED - // Z_BEST_COMPRESSION - // Z_DEFAULT_COMPRESSION - - if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil; - - NSMutableData *compressed = [NSMutableData dataWithLength:16384]; // 16K chunks for expansion - - do { - - if (strm.total_out >= [compressed length]) - [compressed increaseLengthBy: 16384]; - - strm.next_out = [compressed mutableBytes] + strm.total_out; - strm.avail_out = (unsigned int)([compressed length] - strm.total_out); - - deflate(&strm, Z_FINISH); - - } while (strm.avail_out == 0); - - deflateEnd(&strm); - - [compressed setLength: strm.total_out]; - return [NSData dataWithData:compressed]; -} - -// NOTE: To debug this method, turn off Data Formatters in Xcode or you'll crash on closeFile -+ (int)compressDataFromFile:(NSString *)sourcePath toFile:(NSString *)destinationPath -{ - // Create an empty file at the destination path - [[NSFileManager defaultManager] createFileAtPath:destinationPath contents:[NSData data] attributes:nil]; - - // Get a FILE struct for the source file - NSFileHandle *inputFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePath]; - FILE *source = fdopen([inputFileHandle fileDescriptor], "r"); - - // Get a FILE struct for the destination path - NSFileHandle *outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:destinationPath]; - FILE *dest = fdopen([outputFileHandle fileDescriptor], "w"); - - // compress data in source and save in destination - int status = [ASIHTTPRequest compressDataFromSource:source toDestination:dest]; - - // Close the files - fclose(dest); - fclose(source); - - // We have to close both of these explictly because CFReadStreamCreateForStreamedHTTPRequest() seems to go bonkers otherwise - [inputFileHandle closeFile]; - [outputFileHandle closeFile]; - - return status; -} - -// -// Also from the zlib sample code at http://www.zlib.net/zpipe.c -// -+ (int)compressDataFromSource:(FILE *)source toDestination:(FILE *)dest -{ - int ret, flush; - unsigned have; - z_stream strm; - unsigned char in[CHUNK]; - unsigned char out[CHUNK]; - - /* allocate deflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY); - if (ret != Z_OK) - return ret; - - /* compress until end of file */ - do { - strm.avail_in = (unsigned int)fread(in, 1, CHUNK, source); - if (ferror(source)) { - (void)deflateEnd(&strm); - return Z_ERRNO; - } - flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; - strm.next_in = in; - - /* run deflate() on input until output buffer not full, finish - compression if all of source has been read in */ - do { - strm.avail_out = CHUNK; - strm.next_out = out; - ret = deflate(&strm, flush); /* no bad return value */ - assert(ret != Z_STREAM_ERROR); /* state not clobbered */ - have = CHUNK - strm.avail_out; - if (fwrite(out, 1, have, dest) != have || ferror(dest)) { - (void)deflateEnd(&strm); - return Z_ERRNO; - } - } while (strm.avail_out == 0); - assert(strm.avail_in == 0); /* all input will be used */ - - /* done when last data in file processed */ - } while (flush != Z_FINISH); - assert(ret == Z_STREAM_END); /* stream will be complete */ - - /* clean up and return */ - (void)deflateEnd(&strm); - return Z_OK; -} - -#pragma mark get user agent - -+ (NSString *)defaultUserAgentString -{ - NSBundle *bundle = [NSBundle mainBundle]; - - // Attempt to find a name for this application - NSString *appName = [bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; - if (!appName) { - appName = [bundle objectForInfoDictionaryKey:@"CFBundleName"]; - } - // If we couldn't find one, we'll give up (and ASIHTTPRequest will use the standard CFNetwork user agent) - if (!appName) { - return nil; - } - NSString *appVersion = nil; - NSString *marketingVersionNumber = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; - NSString *developmentVersionNumber = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"]; - if (marketingVersionNumber && developmentVersionNumber) { - if ([marketingVersionNumber isEqualToString:developmentVersionNumber]) { - appVersion = marketingVersionNumber; - } else { - appVersion = [NSString stringWithFormat:@"%@ rv:%@",marketingVersionNumber,developmentVersionNumber]; - } - } else { - appVersion = (marketingVersionNumber ? marketingVersionNumber : developmentVersionNumber); - } - - - NSString *deviceName; - NSString *OSName; - NSString *OSVersion; - - NSString *locale = [[NSLocale currentLocale] localeIdentifier]; - -#if TARGET_OS_IPHONE - UIDevice *device = [UIDevice currentDevice]; - deviceName = [device model]; - OSName = [device systemName]; - OSVersion = [device systemVersion]; - -#else - deviceName = @"Macintosh"; - OSName = @"Mac OS X"; - - // From http://www.cocoadev.com/index.pl?DeterminingOSVersion - // We won't bother to check for systems prior to 10.4, since ASIHTTPRequest only works on 10.5+ - OSErr err; - SInt32 versionMajor, versionMinor, versionBugFix; - err = Gestalt(gestaltSystemVersionMajor, &versionMajor); - if (err != noErr) return nil; - err = Gestalt(gestaltSystemVersionMinor, &versionMinor); - if (err != noErr) return nil; - err = Gestalt(gestaltSystemVersionBugFix, &versionBugFix); - if (err != noErr) return nil; - OSVersion = [NSString stringWithFormat:@"%u.%u.%u", versionMajor, versionMinor, versionBugFix]; - -#endif - // Takes the form "My Application 1.0 (Macintosh; Mac OS X 10.5.7; en_GB)" - return [NSString stringWithFormat:@"%@ %@ (%@; %@ %@; %@)", appName, appVersion, deviceName, OSName, OSVersion, locale]; -} - -#pragma mark proxy autoconfiguration - -// Returns an array of proxies to use for a particular url, given the url of a PAC script -+ (NSArray *)proxiesForURL:(NSURL *)theURL fromPAC:(NSURL *)pacScriptURL -{ - // From: http://developer.apple.com/samplecode/CFProxySupportTool/listing1.html - // Work around . This dummy call to - // CFNetworkCopyProxiesForURL initialise some state within CFNetwork - // that is required by CFNetworkCopyProxiesForAutoConfigurationScript. - CFRelease(CFNetworkCopyProxiesForURL((CFURLRef)theURL, NULL)); - - NSStringEncoding encoding; - NSError *err = nil; - NSString *script = [NSString stringWithContentsOfURL:pacScriptURL usedEncoding:&encoding error:&err]; - if (err) { - // If we can't fetch the PAC, we'll assume no proxies - // Some people have a PAC configured that is not always available, so I think this is the best behaviour - return [NSArray array]; - } - // Obtain the list of proxies by running the autoconfiguration script - CFErrorRef err2 = NULL; - NSArray *proxies = NSMakeCollectable([(NSArray *)CFNetworkCopyProxiesForAutoConfigurationScript((CFStringRef)script,(CFURLRef)theURL, &err2) autorelease]); - if (err2) { - return nil; - } - return proxies; -} - -#pragma mark mime-type detection - -+ (NSString *)mimeTypeForFileAtPath:(NSString *)path -{ - if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { - return nil; - } - // Borrowed from http://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database - CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL); - CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType); - CFRelease(UTI); - if (!MIMEType) { - return @"application/octet-stream"; - } - return NSMakeCollectable([(NSString *)MIMEType autorelease]); -} - -#pragma mark bandwidth measurement / throttling - -- (void)performThrottling -{ - if (![self readStream]) { - return; - } - [ASIHTTPRequest measureBandwidthUsage]; - if ([ASIHTTPRequest isBandwidthThrottled]) { - [bandwidthThrottlingLock lock]; - // Handle throttling - if (throttleWakeUpTime) { - if ([throttleWakeUpTime timeIntervalSinceDate:[NSDate date]] > 0) { - if ([self readStreamIsScheduled]) { - [self unscheduleReadStream]; - #if DEBUG_THROTTLING - NSLog(@"Sleeping request %@ until after %@",self,throttleWakeUpTime); - #endif - } - } else { - if (![self readStreamIsScheduled]) { - [self scheduleReadStream]; - #if DEBUG_THROTTLING - NSLog(@"Waking up request %@",self); - #endif - } - } - } - [bandwidthThrottlingLock unlock]; - - // Bandwidth throttling must have been turned off since we last looked, let's re-schedule the stream - } else if (![self readStreamIsScheduled]) { - [self scheduleReadStream]; - } -} - -+ (BOOL)isBandwidthThrottled -{ -#if TARGET_OS_IPHONE - [bandwidthThrottlingLock lock]; - - BOOL throttle = isBandwidthThrottled || (!shouldThrottleBandwithForWWANOnly && (maxBandwidthPerSecond)); - [bandwidthThrottlingLock unlock]; - return throttle; -#else - [bandwidthThrottlingLock lock]; - BOOL throttle = (maxBandwidthPerSecond); - [bandwidthThrottlingLock unlock]; - return throttle; -#endif -} - -+ (unsigned long)maxBandwidthPerSecond -{ - [bandwidthThrottlingLock lock]; - unsigned long amount = maxBandwidthPerSecond; - [bandwidthThrottlingLock unlock]; - return amount; -} - -+ (void)setMaxBandwidthPerSecond:(unsigned long)bytes -{ - [bandwidthThrottlingLock lock]; - maxBandwidthPerSecond = bytes; - [bandwidthThrottlingLock unlock]; -} - -+ (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes -{ - [bandwidthThrottlingLock lock]; - bandwidthUsedInLastSecond += bytes; - [bandwidthThrottlingLock unlock]; -} - -+ (void)recordBandwidthUsage -{ - if (bandwidthUsedInLastSecond == 0) { - [bandwidthUsageTracker removeAllObjects]; - } else { - NSTimeInterval interval = [bandwidthMeasurementDate timeIntervalSinceNow]; - while ((interval < 0 || [bandwidthUsageTracker count] > 5) && [bandwidthUsageTracker count] > 0) { - [bandwidthUsageTracker removeObjectAtIndex:0]; - interval++; - } - } - #if DEBUG_THROTTLING - NSLog(@"===Used: %u bytes of bandwidth in last measurement period===",bandwidthUsedInLastSecond); - #endif - [bandwidthUsageTracker addObject:[NSNumber numberWithUnsignedLong:bandwidthUsedInLastSecond]]; - [bandwidthMeasurementDate release]; - bandwidthMeasurementDate = [[NSDate dateWithTimeIntervalSinceNow:1] retain]; - bandwidthUsedInLastSecond = 0; - - NSUInteger measurements = [bandwidthUsageTracker count]; - unsigned long totalBytes = 0; - for (NSNumber *bytes in bandwidthUsageTracker) { - totalBytes += [bytes unsignedLongValue]; - } - averageBandwidthUsedPerSecond = totalBytes/measurements; -} - -+ (unsigned long)averageBandwidthUsedPerSecond -{ - [bandwidthThrottlingLock lock]; - unsigned long amount = averageBandwidthUsedPerSecond; - [bandwidthThrottlingLock unlock]; - return amount; -} - -+ (void)measureBandwidthUsage -{ - // Other requests may have to wait for this lock if we're sleeping, but this is fine, since in that case we already know they shouldn't be sending or receiving data - [bandwidthThrottlingLock lock]; - - if (!bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { - [ASIHTTPRequest recordBandwidthUsage]; - } - - // Are we performing bandwidth throttling? - if ( - #if TARGET_OS_IPHONE - isBandwidthThrottled || (!shouldThrottleBandwithForWWANOnly && (maxBandwidthPerSecond)) - #else - maxBandwidthPerSecond - #endif - ) { - // How much data can we still send or receive this second? - long long bytesRemaining = (long long)maxBandwidthPerSecond - (long long)bandwidthUsedInLastSecond; - - // Have we used up our allowance? - if (bytesRemaining < 0) { - - // Yes, put this request to sleep until a second is up, with extra added punishment sleeping time for being very naughty (we have used more bandwidth than we were allowed) - double extraSleepyTime = (-bytesRemaining/(maxBandwidthPerSecond*1.0)); - [throttleWakeUpTime release]; - throttleWakeUpTime = [[NSDate alloc] initWithTimeInterval:extraSleepyTime sinceDate:bandwidthMeasurementDate]; - } - } - [bandwidthThrottlingLock unlock]; -} - -+ (unsigned long)maxUploadReadLength -{ - - [bandwidthThrottlingLock lock]; - - // We'll split our bandwidth allowance into 4 (which is the default for an ASINetworkQueue's max concurrent operations count) to give all running requests a fighting chance of reading data this cycle - long long toRead = maxBandwidthPerSecond/4; - if (maxBandwidthPerSecond > 0 && (bandwidthUsedInLastSecond + toRead > maxBandwidthPerSecond)) { - toRead = (long long)maxBandwidthPerSecond-(long long)bandwidthUsedInLastSecond; - if (toRead < 0) { - toRead = 0; - } - } - - if (toRead == 0 || !bandwidthMeasurementDate || [bandwidthMeasurementDate timeIntervalSinceNow] < -0) { - [throttleWakeUpTime release]; - throttleWakeUpTime = [bandwidthMeasurementDate retain]; - } - [bandwidthThrottlingLock unlock]; - return (unsigned long)toRead; -} - - -#if TARGET_OS_IPHONE -+ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle -{ - if (throttle) { - [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount]; - } else { - [ASIHTTPRequest unsubscribeFromNetworkReachabilityNotifications]; - [ASIHTTPRequest setMaxBandwidthPerSecond:0]; - [bandwidthThrottlingLock lock]; - isBandwidthThrottled = NO; - shouldThrottleBandwithForWWANOnly = NO; - [bandwidthThrottlingLock unlock]; - } -} - -+ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit -{ - [bandwidthThrottlingLock lock]; - shouldThrottleBandwithForWWANOnly = YES; - maxBandwidthPerSecond = limit; - [ASIHTTPRequest registerForNetworkReachabilityNotifications]; - [bandwidthThrottlingLock unlock]; - [ASIHTTPRequest reachabilityChanged:nil]; -} - -#pragma mark reachability - -+ (void)registerForNetworkReachabilityNotifications -{ - [[Reachability reachabilityForInternetConnection] startNotifier]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil]; -} - - -+ (void)unsubscribeFromNetworkReachabilityNotifications -{ - [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil]; -} - -+ (BOOL)isNetworkReachableViaWWAN -{ - return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN); -} - -+ (void)reachabilityChanged:(NSNotification *)note -{ - [bandwidthThrottlingLock lock]; - isBandwidthThrottled = [ASIHTTPRequest isNetworkReachableViaWWAN]; - [bandwidthThrottlingLock unlock]; -} -#endif - -#pragma mark cache - -+ (void)setDefaultCache:(id )cache -{ - [defaultCache release]; - defaultCache = [cache retain]; -} - -+ (id )defaultCache -{ - return defaultCache; -} - - -#pragma mark network activity - -+ (BOOL)isNetworkInUse -{ - [connectionsLock lock]; - BOOL inUse = (runningRequestCount > 0); - [connectionsLock unlock]; - return inUse; -} -#if TARGET_OS_IPHONE -+ (void)setShouldUpdateNetworkActivityIndicator:(BOOL)shouldUpdate -{ - [connectionsLock lock]; - shouldUpdateNetworkActivityIndicator = shouldUpdate; - [connectionsLock unlock]; -} -#endif - - -#pragma mark threading behaviour - -// In the default implementation, all requests run in a single background thread -// Advanced users only: Override this method in a subclass for a different threading behaviour -// Eg: return [NSThread mainThread] to run all requests in the main thread -// Alternatively, you can create a thread on demand, or manage a pool of threads -// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun()) -// Requests will stop the runloop when they complete -// If you have multiple requests sharing the thread or you want to re-use the thread, you'll need to restart the runloop -+ (NSThread *)threadForRequest:(ASIHTTPRequest *)request -{ - if (!networkThread) { - networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(runRequests) object:nil]; - [networkThread start]; - } - return networkThread; -} - -+ (void)runRequests -{ - // Should keep the runloop from exiting - CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; - CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); - CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); - - while (1) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - CFRunLoopRun(); - [pool release]; - } - - // Should never be called, but anyway - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); - CFRelease(source); -} - - - -#pragma mark miscellany - -// From: http://www.cocoadev.com/index.pl?BaseSixtyFour - -+ (NSString*)base64forData:(NSData*)theData { - - const uint8_t* input = (const uint8_t*)[theData bytes]; - NSInteger length = [theData length]; - - static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - - NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4]; - uint8_t* output = (uint8_t*)data.mutableBytes; - - NSInteger i; - for (i=0; i < length; i += 3) { - NSInteger value = 0; - NSInteger j; - for (j = i; j < (i + 3); j++) { - value <<= 8; - - if (j < length) { - value |= (0xFF & input[j]); - } - } - - NSInteger theIndex = (i / 3) * 4; - output[theIndex + 0] = table[(value >> 18) & 0x3F]; - output[theIndex + 1] = table[(value >> 12) & 0x3F]; - output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; - output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; - } - - return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]; -} - -// Based on hints from http://stackoverflow.com/questions/1850824/parsing-a-rfc-822-date-with-nsdateformatter -+ (NSDate *)dateFromRFC1123String:(NSString *)string -{ - NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; - [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; - // Does the string include a week day? - NSString *day = @""; - if ([string rangeOfString:@","].location != NSNotFound) { - day = @"EEE, "; - } - // Does the string include seconds? - NSString *seconds = @""; - if ([[string componentsSeparatedByString:@":"] count] == 3) { - seconds = @":ss"; - } - [formatter setDateFormat:[NSString stringWithFormat:@"%@dd MMM yyyy HH:mm%@ z",day,seconds]]; - return [formatter dateFromString:string]; -} - -#pragma mark === - -@synthesize username; -@synthesize password; -@synthesize domain; -@synthesize proxyUsername; -@synthesize proxyPassword; -@synthesize proxyDomain; -@synthesize url; -@synthesize originalURL; -@synthesize delegate; -@synthesize queue; -@synthesize uploadProgressDelegate; -@synthesize downloadProgressDelegate; -@synthesize useKeychainPersistence; -@synthesize useSessionPersistence; -@synthesize useCookiePersistence; -@synthesize downloadDestinationPath; -@synthesize temporaryFileDownloadPath; -@synthesize didStartSelector; -@synthesize didReceiveResponseHeadersSelector; -@synthesize didFinishSelector; -@synthesize didFailSelector; -@synthesize didReceiveDataSelector; -@synthesize authenticationRealm; -@synthesize proxyAuthenticationRealm; -@synthesize error; -@synthesize complete; -@synthesize requestHeaders; -@synthesize responseHeaders; -@synthesize responseCookies; -@synthesize requestCookies; -@synthesize requestCredentials; -@synthesize responseStatusCode; -@synthesize rawResponseData; -@synthesize lastActivityTime; -@synthesize timeOutSeconds; -@synthesize requestMethod; -@synthesize postBody; -@synthesize compressedPostBody; -@synthesize contentLength; -@synthesize partialDownloadSize; -@synthesize postLength; -@synthesize shouldResetDownloadProgress; -@synthesize shouldResetUploadProgress; -@synthesize mainRequest; -@synthesize totalBytesRead; -@synthesize totalBytesSent; -@synthesize showAccurateProgress; -@synthesize uploadBufferSize; -@synthesize defaultResponseEncoding; -@synthesize responseEncoding; -@synthesize allowCompressedResponse; -@synthesize allowResumeForFileDownloads; -@synthesize userInfo; -@synthesize postBodyFilePath; -@synthesize compressedPostBodyFilePath; -@synthesize postBodyWriteStream; -@synthesize postBodyReadStream; -@synthesize shouldStreamPostDataFromDisk; -@synthesize didCreateTemporaryPostDataFile; -@synthesize useHTTPVersionOne; -@synthesize lastBytesRead; -@synthesize lastBytesSent; -@synthesize cancelledLock; -@synthesize haveBuiltPostBody; -@synthesize fileDownloadOutputStream; -@synthesize authenticationRetryCount; -@synthesize proxyAuthenticationRetryCount; -@synthesize updatedProgress; -@synthesize shouldRedirect; -@synthesize validatesSecureCertificate; -@synthesize needsRedirect; -@synthesize redirectCount; -@synthesize shouldCompressRequestBody; -@synthesize proxyCredentials; -@synthesize proxyHost; -@synthesize proxyPort; -@synthesize proxyType; -@synthesize PACurl; -@synthesize authenticationScheme; -@synthesize proxyAuthenticationScheme; -@synthesize shouldPresentAuthenticationDialog; -@synthesize shouldPresentProxyAuthenticationDialog; -@synthesize authenticationNeeded; -@synthesize responseStatusMessage; -@synthesize shouldPresentCredentialsBeforeChallenge; -@synthesize haveBuiltRequestHeaders; -@synthesize inProgress; -@synthesize numberOfTimesToRetryOnTimeout; -@synthesize retryCount; -@synthesize shouldAttemptPersistentConnection; -@synthesize persistentConnectionTimeoutSeconds; -@synthesize connectionCanBeReused; -@synthesize connectionInfo; -@synthesize readStream; -@synthesize readStreamIsScheduled; -@synthesize shouldUseRFC2616RedirectBehaviour; -@synthesize downloadComplete; -@synthesize requestID; -@synthesize runLoopMode; -@synthesize statusTimer; -@synthesize downloadCache; -@synthesize cachePolicy; -@synthesize cacheStoragePolicy; -@synthesize didUseCachedResponse; -@synthesize secondsToCache; -@end diff --git a/ASIHTTP/ASIHTTPRequestConfig.h b/ASIHTTP/ASIHTTPRequestConfig.h deleted file mode 100644 index 52cbfcb..0000000 --- a/ASIHTTP/ASIHTTPRequestConfig.h +++ /dev/null @@ -1,32 +0,0 @@ -// -// ASIHTTPRequestConfig.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 14/12/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - - -// ====== -// Debug output configuration options -// ====== - -// When set to 1 ASIHTTPRequests will print information about what a request is doing -#ifndef DEBUG_REQUEST_STATUS - #define DEBUG_REQUEST_STATUS 0 -#endif - -// When set to 1, ASIFormDataRequests will print information about the request body to the console -#ifndef DEBUG_FORM_DATA_REQUEST - #define DEBUG_FORM_DATA_REQUEST 0 -#endif - -// When set to 1, ASIHTTPRequests will print information about bandwidth throttling to the console -#ifndef DEBUG_THROTTLING - #define DEBUG_THROTTLING 0 -#endif - -// When set to 1, ASIHTTPRequests will print information about persistent connections to the console -#ifndef DEBUG_PERSISTENT_CONNECTIONS - #define DEBUG_PERSISTENT_CONNECTIONS 0 -#endif diff --git a/ASIHTTP/ASIHTTPRequestDelegate.h b/ASIHTTP/ASIHTTPRequestDelegate.h deleted file mode 100644 index 82ce352..0000000 --- a/ASIHTTP/ASIHTTPRequestDelegate.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// ASIHTTPRequestDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 13/04/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -@class ASIHTTPRequest; - -@protocol ASIHTTPRequestDelegate - -@optional - -// These are the default delegate methods for request status -// You can use different ones by setting didStartSelector / didFinishSelector / didFailSelector -- (void)requestStarted:(ASIHTTPRequest *)request; -- (void)requestReceivedResponseHeaders:(ASIHTTPRequest *)request; -- (void)requestFinished:(ASIHTTPRequest *)request; -- (void)requestFailed:(ASIHTTPRequest *)request; - -// When a delegate implements this method, it is expected to process all incoming data itself -// This means that responseData / responseString / downloadDestinationPath etc are ignored -// You can have the request call a different method by setting didReceiveDataSelector -- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data; - -// If a delegate implements one of these, it will be asked to supply credentials when none are available -// The delegate can then either restart the request ([request retryUsingSuppliedCredentials]) once credentials have been set -// or cancel it ([request cancelAuthentication]) -- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request; -- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request; - -@end diff --git a/ASIHTTP/ASIInputStream.h b/ASIHTTP/ASIInputStream.h deleted file mode 100644 index 7b9f93e..0000000 --- a/ASIHTTP/ASIInputStream.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// ASIInputStream.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 10/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import - -@class ASIHTTPRequest; - -// This is a wrapper for NSInputStream that pretends to be an NSInputStream itself -// Subclassing NSInputStream seems to be tricky, and may involve overriding undocumented methods, so we'll cheat instead. -// It is used by ASIHTTPRequest whenever we have a request body, and handles measuring and throttling the bandwidth used for uploading - -@interface ASIInputStream : NSObject { - NSInputStream *stream; - ASIHTTPRequest *request; -} -+ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)request; -+ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)request; - -@property (retain, nonatomic) NSInputStream *stream; -@property (assign, nonatomic) ASIHTTPRequest *request; -@end diff --git a/ASIHTTP/ASIInputStream.m b/ASIHTTP/ASIInputStream.m deleted file mode 100644 index bd85e0b..0000000 --- a/ASIHTTP/ASIInputStream.m +++ /dev/null @@ -1,136 +0,0 @@ -// -// ASIInputStream.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 10/08/2009. -// Copyright 2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASIInputStream.h" -#import "ASIHTTPRequest.h" - -// Used to ensure only one request can read data at once -static NSLock *readLock = nil; - -@implementation ASIInputStream - -+ (void)initialize -{ - if (self == [ASIInputStream class]) { - readLock = [[NSLock alloc] init]; - } -} - -+ (id)inputStreamWithFileAtPath:(NSString *)path request:(ASIHTTPRequest *)request -{ - ASIInputStream *stream = [[[self alloc] init] autorelease]; - [stream setRequest:request]; - [stream setStream:[NSInputStream inputStreamWithFileAtPath:path]]; - return stream; -} - -+ (id)inputStreamWithData:(NSData *)data request:(ASIHTTPRequest *)request -{ - ASIInputStream *stream = [[[self alloc] init] autorelease]; - [stream setRequest:request]; - [stream setStream:[NSInputStream inputStreamWithData:data]]; - return stream; -} - -- (void)dealloc -{ - [stream release]; - [super dealloc]; -} - -// Called when CFNetwork wants to read more of our request body -// When throttling is on, we ask ASIHTTPRequest for the maximum amount of data we can read -- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len -{ - [readLock lock]; - unsigned long toRead = len; - if ([ASIHTTPRequest isBandwidthThrottled]) { - toRead = [ASIHTTPRequest maxUploadReadLength]; - if (toRead > len) { - toRead = len; - } else if (toRead == 0) { - toRead = 1; - } - [request performThrottling]; - } - [ASIHTTPRequest incrementBandwidthUsedInLastSecond:toRead]; - [readLock unlock]; - return [stream read:buffer maxLength:toRead]; -} - -/* - * Implement NSInputStream mandatory methods to make sure they are implemented - * (necessary for MacRuby for example) and avoir the overhead of method - * forwarding for these common methods. - */ -- (void)open -{ - [stream open]; -} - -- (void)close -{ - [stream close]; -} - -- (id)delegate -{ - return [stream delegate]; -} - -- (void)setDelegate:(id)delegate -{ - [stream setDelegate:delegate]; -} - -- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode -{ - [stream scheduleInRunLoop:aRunLoop forMode:mode]; -} - -- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop forMode:(NSString *)mode -{ - [stream removeFromRunLoop:aRunLoop forMode:mode]; -} - -- (id)propertyForKey:(NSString *)key -{ - return [stream propertyForKey:key]; -} - -- (BOOL)setProperty:(id)property forKey:(NSString *)key -{ - return [stream setProperty:property forKey:key]; -} - -- (NSStreamStatus)streamStatus -{ - return [stream streamStatus]; -} - -- (NSError *)streamError -{ - return [stream streamError]; -} - -// If we get asked to perform a method we don't have (probably internal ones), -// we'll just forward the message to our stream - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector -{ - return [stream methodSignatureForSelector:aSelector]; -} - -- (void)forwardInvocation:(NSInvocation *)anInvocation -{ - [anInvocation invokeWithTarget:stream]; -} - -@synthesize stream; -@synthesize request; -@end diff --git a/ASIHTTP/ASINetworkQueue.h b/ASIHTTP/ASINetworkQueue.h deleted file mode 100644 index b0398a7..0000000 --- a/ASIHTTP/ASINetworkQueue.h +++ /dev/null @@ -1,102 +0,0 @@ -// -// ASINetworkQueue.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import -#import "ASIHTTPRequestDelegate.h" -#import "ASIProgressDelegate.h" - -@interface ASINetworkQueue : NSOperationQueue { - - // Delegate will get didFail + didFinish messages (if set) - id delegate; - - // Will be called when a request starts with the request as the argument - SEL requestDidStartSelector; - - // Will be called when a request receives response headers with the request as the argument - SEL requestDidReceiveResponseHeadersSelector; - - // Will be called when a request completes with the request as the argument - SEL requestDidFinishSelector; - - // Will be called when a request fails with the request as the argument - SEL requestDidFailSelector; - - // Will be called when the queue finishes with the queue as the argument - SEL queueDidFinishSelector; - - // Upload progress indicator, probably an NSProgressIndicator or UIProgressView - id uploadProgressDelegate; - - // Total amount uploaded so far for all requests in this queue - unsigned long long bytesUploadedSoFar; - - // Total amount to be uploaded for all requests in this queue - requests add to this figure as they work out how much data they have to transmit - unsigned long long totalBytesToUpload; - - // Download progress indicator, probably an NSProgressIndicator or UIProgressView - id downloadProgressDelegate; - - // Total amount downloaded so far for all requests in this queue - unsigned long long bytesDownloadedSoFar; - - // Total amount to be downloaded for all requests in this queue - requests add to this figure as they receive Content-Length headers - unsigned long long totalBytesToDownload; - - // When YES, the queue will cancel all requests when a request fails. Default is YES - BOOL shouldCancelAllRequestsOnFailure; - - //Number of real requests (excludes HEAD requests created to manage showAccurateProgress) - int requestsCount; - - // When NO, this request will only update the progress indicator when it completes - // When YES, this request will update the progress indicator according to how much data it has received so far - // When YES, the queue will first perform HEAD requests for all GET requests in the queue, so it can calculate the total download size before it starts - // NO means better performance, because it skips this step for GET requests, and it won't waste time updating the progress indicator until a request completes - // Set to YES if the size of a requests in the queue varies greatly for much more accurate results - // Default for requests in the queue is NO - BOOL showAccurateProgress; - - // Storage container for additional queue information. - NSDictionary *userInfo; - -} - -// Convenience constructor -+ (id)queue; - -// Call this to reset a queue - it will cancel all operations, clear delegates, and suspend operation -- (void)reset; - -// Used internally to manage HEAD requests when showAccurateProgress is YES, do not use! -- (void)addHEADOperation:(NSOperation *)operation; - -// All ASINetworkQueues are paused when created so that total size can be calculated before the queue starts -// This method will start the queue -- (void)go; - -@property (assign, nonatomic, setter=setUploadProgressDelegate:) id uploadProgressDelegate; -@property (assign, nonatomic, setter=setDownloadProgressDelegate:) id downloadProgressDelegate; - -@property (assign) SEL requestDidStartSelector; -@property (assign) SEL requestDidReceiveResponseHeadersSelector; -@property (assign) SEL requestDidFinishSelector; -@property (assign) SEL requestDidFailSelector; -@property (assign) SEL queueDidFinishSelector; -@property (assign) BOOL shouldCancelAllRequestsOnFailure; -@property (assign) id delegate; -@property (assign) BOOL showAccurateProgress; -@property (assign, readonly) int requestsCount; -@property (retain) NSDictionary *userInfo; - -@property (assign) unsigned long long bytesUploadedSoFar; -@property (assign) unsigned long long totalBytesToUpload; -@property (assign) unsigned long long bytesDownloadedSoFar; -@property (assign) unsigned long long totalBytesToDownload; - -@end diff --git a/ASIHTTP/ASINetworkQueue.m b/ASIHTTP/ASINetworkQueue.m deleted file mode 100644 index bc9c561..0000000 --- a/ASIHTTP/ASINetworkQueue.m +++ /dev/null @@ -1,322 +0,0 @@ -// -// ASINetworkQueue.m -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 07/11/2008. -// Copyright 2008-2009 All-Seeing Interactive. All rights reserved. -// - -#import "ASINetworkQueue.h" -#import "ASIHTTPRequest.h" - -// Private stuff -@interface ASINetworkQueue () - - (void)resetProgressDelegate:(id)progressDelegate; - @property (assign) int requestsCount; -@end - -@implementation ASINetworkQueue - -- (id)init -{ - self = [super init]; - [self setShouldCancelAllRequestsOnFailure:YES]; - [self setMaxConcurrentOperationCount:4]; - [self setSuspended:YES]; - - return self; -} - -+ (id)queue -{ - return [[[self alloc] init] autorelease]; -} - -- (void)dealloc -{ - //We need to clear the queue on any requests that haven't got around to cleaning up yet, as otherwise they'll try to let us know if something goes wrong, and we'll be long gone by then - for (ASIHTTPRequest *request in [self operations]) { - [request setQueue:nil]; - } - [userInfo release]; - [super dealloc]; -} - -- (void)setSuspended:(BOOL)suspend -{ - [super setSuspended:suspend]; -} - -- (void)reset -{ - [self cancelAllOperations]; - [self setDelegate:nil]; - [self setDownloadProgressDelegate:nil]; - [self setUploadProgressDelegate:nil]; - [self setRequestDidStartSelector:NULL]; - [self setRequestDidReceiveResponseHeadersSelector:NULL]; - [self setRequestDidFailSelector:NULL]; - [self setRequestDidFinishSelector:NULL]; - [self setQueueDidFinishSelector:NULL]; - [self setSuspended:YES]; -} - - -- (void)go -{ - [self setSuspended:NO]; -} - -- (void)cancelAllOperations -{ - [self setBytesUploadedSoFar:0]; - [self setTotalBytesToUpload:0]; - [self setBytesDownloadedSoFar:0]; - [self setTotalBytesToDownload:0]; - [super cancelAllOperations]; -} - -- (void)setUploadProgressDelegate:(id)newDelegate -{ - uploadProgressDelegate = newDelegate; - [self resetProgressDelegate:newDelegate]; - -} - -- (void)setDownloadProgressDelegate:(id)newDelegate -{ - downloadProgressDelegate = newDelegate; - [self resetProgressDelegate:newDelegate]; -} - -- (void)resetProgressDelegate:(id)progressDelegate -{ -#if !TARGET_OS_IPHONE - // If the uploadProgressDelegate is an NSProgressIndicator, we set its MaxValue to 1.0 so we can treat it similarly to UIProgressViews - SEL selector = @selector(setMaxValue:); - if ([progressDelegate respondsToSelector:selector]) { - double max = 1.0; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&max]; - } - selector = @selector(setDoubleValue:); - if ([progressDelegate respondsToSelector:selector]) { - double value = 0.0; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value]; - } -#else - SEL selector = @selector(setProgress:); - if ([progressDelegate respondsToSelector:selector]) { - float value = 0.0f; - [ASIHTTPRequest performSelector:selector onTarget:progressDelegate withObject:nil amount:&value]; - } -#endif -} - -- (void)addHEADOperation:(NSOperation *)operation -{ - if ([operation isKindOfClass:[ASIHTTPRequest class]]) { - - ASIHTTPRequest *request = (ASIHTTPRequest *)operation; - [request setRequestMethod:@"HEAD"]; - [request setQueuePriority:10]; - [request setShowAccurateProgress:YES]; - [request setQueue:self]; - - // Important - we are calling NSOperation's add method - we don't want to add this as a normal request! - [super addOperation:request]; - } -} - -// Only add ASIHTTPRequests to this queue!! -- (void)addOperation:(NSOperation *)operation -{ - if (![operation isKindOfClass:[ASIHTTPRequest class]]) { - [NSException raise:@"AttemptToAddInvalidRequest" format:@"Attempted to add an object that was not an ASIHTTPRequest to an ASINetworkQueue"]; - } - - [self setRequestsCount:[self requestsCount]+1]; - - ASIHTTPRequest *request = (ASIHTTPRequest *)operation; - - if ([self showAccurateProgress]) { - - // Force the request to build its body (this may change requestMethod) - [request buildPostBody]; - - // If this is a GET request and we want accurate progress, perform a HEAD request first to get the content-length - // We'll only do this before the queue is started - // If requests are added after the queue is started they will probably move the overall progress backwards anyway, so there's no value performing the HEAD requests first - // Instead, they'll update the total progress if and when they receive a content-length header - if ([[request requestMethod] isEqualToString:@"GET"]) { - if ([self isSuspended]) { - ASIHTTPRequest *HEADRequest = [request HEADRequest]; - [self addHEADOperation:HEADRequest]; - [request addDependency:HEADRequest]; - if ([request shouldResetDownloadProgress]) { - [self resetProgressDelegate:[request downloadProgressDelegate]]; - [request setShouldResetDownloadProgress:NO]; - } - } - } - [request buildPostBody]; - [self request:nil incrementUploadSizeBy:[request postLength]]; - - - } else { - [self request:nil incrementDownloadSizeBy:1]; - [self request:nil incrementUploadSizeBy:1]; - } - // Tell the request not to increment the upload size when it starts, as we've already added its length - if ([request shouldResetUploadProgress]) { - [self resetProgressDelegate:[request uploadProgressDelegate]]; - [request setShouldResetUploadProgress:NO]; - } - - [request setShowAccurateProgress:[self showAccurateProgress]]; - - [request setQueue:self]; - [super addOperation:request]; - -} - -- (void)requestStarted:(ASIHTTPRequest *)request -{ - if ([self requestDidStartSelector]) { - [[self delegate] performSelector:[self requestDidStartSelector] withObject:request]; - } -} - -- (void)requestReceivedResponseHeaders:(ASIHTTPRequest *)request -{ - if ([self requestDidReceiveResponseHeadersSelector]) { - [[self delegate] performSelector:[self requestDidReceiveResponseHeadersSelector] withObject:request]; - } -} - - -- (void)requestFinished:(ASIHTTPRequest *)request -{ - [self setRequestsCount:[self requestsCount]-1]; - if ([self requestDidFinishSelector]) { - [[self delegate] performSelector:[self requestDidFinishSelector] withObject:request]; - } - if ([self requestsCount] == 0) { - if ([self queueDidFinishSelector]) { - [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; - } - } -} - -- (void)requestFailed:(ASIHTTPRequest *)request -{ - [self setRequestsCount:[self requestsCount]-1]; - if ([self requestDidFailSelector]) { - [[self delegate] performSelector:[self requestDidFailSelector] withObject:request]; - } - if ([self requestsCount] == 0) { - if ([self queueDidFinishSelector]) { - [[self delegate] performSelector:[self queueDidFinishSelector] withObject:self]; - } - } - if ([self shouldCancelAllRequestsOnFailure] && [self requestsCount] > 0) { - [self cancelAllOperations]; - } - -} - - -- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes -{ - [self setBytesDownloadedSoFar:[self bytesDownloadedSoFar]+bytes]; - if ([self downloadProgressDelegate]) { - [ASIHTTPRequest updateProgressIndicator:[self downloadProgressDelegate] withProgress:[self bytesDownloadedSoFar] ofTotal:[self totalBytesToDownload]]; - } -} - -- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes -{ - [self setBytesUploadedSoFar:[self bytesUploadedSoFar]+bytes]; - if ([self uploadProgressDelegate]) { - [ASIHTTPRequest updateProgressIndicator:[self uploadProgressDelegate] withProgress:[self bytesUploadedSoFar] ofTotal:[self totalBytesToUpload]]; - } -} - -- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength -{ - [self setTotalBytesToDownload:[self totalBytesToDownload]+newLength]; -} - -- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength -{ - [self setTotalBytesToUpload:[self totalBytesToUpload]+newLength]; -} - - -// Since this queue takes over as the delegate for all requests it contains, it should forward authorisation requests to its own delegate -- (void)authenticationNeededForRequest:(ASIHTTPRequest *)request -{ - if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { - [[self delegate] performSelector:@selector(authenticationNeededForRequest:) withObject:request]; - } -} - -- (void)proxyAuthenticationNeededForRequest:(ASIHTTPRequest *)request -{ - if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - [[self delegate] performSelector:@selector(proxyAuthenticationNeededForRequest:) withObject:request]; - } -} - - -- (BOOL)respondsToSelector:(SEL)selector -{ - if (selector == @selector(authenticationNeededForRequest:)) { - if ([[self delegate] respondsToSelector:@selector(authenticationNeededForRequest:)]) { - return YES; - } - return NO; - } else if (selector == @selector(proxyAuthenticationNeededForRequest:)) { - if ([[self delegate] respondsToSelector:@selector(proxyAuthenticationNeededForRequest:)]) { - return YES; - } - return NO; - } - return [super respondsToSelector:selector]; -} - -#pragma mark NSCopying - -- (id)copyWithZone:(NSZone *)zone -{ - ASINetworkQueue *newQueue = [[[self class] alloc] init]; - [newQueue setDelegate:[self delegate]]; - [newQueue setRequestDidStartSelector:[self requestDidStartSelector]]; - [newQueue setRequestDidFinishSelector:[self requestDidFinishSelector]]; - [newQueue setRequestDidFailSelector:[self requestDidFailSelector]]; - [newQueue setQueueDidFinishSelector:[self queueDidFinishSelector]]; - [newQueue setUploadProgressDelegate:[self uploadProgressDelegate]]; - [newQueue setDownloadProgressDelegate:[self downloadProgressDelegate]]; - [newQueue setShouldCancelAllRequestsOnFailure:[self shouldCancelAllRequestsOnFailure]]; - [newQueue setShowAccurateProgress:[self showAccurateProgress]]; - [newQueue setUserInfo:[[[self userInfo] copyWithZone:zone] autorelease]]; - return newQueue; -} - - -@synthesize requestsCount; -@synthesize bytesUploadedSoFar; -@synthesize totalBytesToUpload; -@synthesize bytesDownloadedSoFar; -@synthesize totalBytesToDownload; -@synthesize shouldCancelAllRequestsOnFailure; -@synthesize uploadProgressDelegate; -@synthesize downloadProgressDelegate; -@synthesize requestDidStartSelector; -@synthesize requestDidReceiveResponseHeadersSelector; -@synthesize requestDidFinishSelector; -@synthesize requestDidFailSelector; -@synthesize queueDidFinishSelector; -@synthesize delegate; -@synthesize showAccurateProgress; -@synthesize userInfo; -@end diff --git a/ASIHTTP/ASIProgressDelegate.h b/ASIHTTP/ASIProgressDelegate.h deleted file mode 100644 index e2bb0cf..0000000 --- a/ASIHTTP/ASIProgressDelegate.h +++ /dev/null @@ -1,38 +0,0 @@ -// -// ASIProgressDelegate.h -// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest -// -// Created by Ben Copsey on 13/04/2010. -// Copyright 2010 All-Seeing Interactive. All rights reserved. -// - -@class ASIHTTPRequest; - -@protocol ASIProgressDelegate - -@optional - -// These methods are used to update UIProgressViews (iPhone OS) or NSProgressIndicators (Mac OS X) -// If you are using a custom progress delegate, you may find it easier to implement didReceiveBytes / didSendBytes instead -#if TARGET_OS_IPHONE -- (void)setProgress:(float)newProgress; -#else -- (void)setDoubleValue:(double)newProgress; -- (void)setMaxValue:(double)newMax; -#endif - -// Called when the request receives some data - bytes is the length of that data -- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes; - -// Called when the request sends some data -// The first 32KB (128KB on older platforms) of data sent is not included in this amount because of limitations with the CFNetwork API -// bytes may be less than zero if a request needs to remove upload progress (probably because the request needs to run again) -- (void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes; - -// Called when a request needs to change the length of the content to download -- (void)request:(ASIHTTPRequest *)request incrementDownloadSizeBy:(long long)newLength; - -// Called when a request needs to change the length of the content to upload -// newLength may be less than zero when a request needs to remove the size of the internal buffer from progress tracking -- (void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength; -@end diff --git a/ASIHTTP/Reachability/.svn/all-wcprops b/ASIHTTP/Reachability/.svn/all-wcprops deleted file mode 100644 index 7e5a6ce..0000000 --- a/ASIHTTP/Reachability/.svn/all-wcprops +++ /dev/null @@ -1,17 +0,0 @@ -K 25 -svn:wc:ra_dav:version-url -V 69 -/repos/!svn/ver/27660/projects/fp2/iOS3/ASIHTTP/External/Reachability -END -Reachability.h -K 25 -svn:wc:ra_dav:version-url -V 84 -/repos/!svn/ver/27660/projects/fp2/iOS3/ASIHTTP/External/Reachability/Reachability.h -END -Reachability.m -K 25 -svn:wc:ra_dav:version-url -V 84 -/repos/!svn/ver/27660/projects/fp2/iOS3/ASIHTTP/External/Reachability/Reachability.m -END diff --git a/ASIHTTP/Reachability/.svn/entries b/ASIHTTP/Reachability/.svn/entries deleted file mode 100644 index f8c72fb..0000000 --- a/ASIHTTP/Reachability/.svn/entries +++ /dev/null @@ -1,96 +0,0 @@ -10 - -dir -31298 -https://svn.echonest.com/repos/projects/fp2/iOS3/ASIHTTP/External/Reachability -https://svn.echonest.com/repos - - - -2011-01-22T18:00:30.135034Z -27660 -brian - - - - - - - - - - - - - - -d21d29e7-c905-0410-a709-e4fc1af1afbc - -Reachability.h -file - - - - -2011-01-24T13:52:03.000000Z -4b29eca6a8051d99252f7509852e6f86 -2011-01-22T18:00:30.135034Z -27660 -brian - - - - - - - - - - - - - - - - - - - - - -7438 - -Reachability.m -file - - - - -2011-01-24T13:52:03.000000Z -9f14a598a3942b9e4e11f9dacb0ee818 -2011-01-22T18:00:30.135034Z -27660 -brian - - - - - - - - - - - - - - - - - - - - - -24061 - diff --git a/ASIHTTP/Reachability/.svn/text-base/Reachability.h.svn-base b/ASIHTTP/Reachability/.svn/text-base/Reachability.h.svn-base deleted file mode 100644 index b49b797..0000000 --- a/ASIHTTP/Reachability/.svn/text-base/Reachability.h.svn-base +++ /dev/null @@ -1,193 +0,0 @@ -/* - - File: Reachability.h - Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. - - Version: 2.0.4ddg - */ - -/* - Significant additions made by Andrew W. Donoho, August 11, 2009. - This is a derived work of Apple's Reachability v2.0 class. - - The below license is the new BSD license with the OSI recommended personalizations. - - - Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C. - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - - -/* - - Apple's Original License on Reachability v2.0 - - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. - ("Apple") in consideration of your agreement to the following terms, and your - use, installation, modification or redistribution of this Apple software - constitutes acceptance of these terms. If you do not agree with these terms, - please do not use, install, modify or redistribute this Apple software. - - In consideration of your agreement to abide by the following terms, and subject - to these terms, Apple grants you a personal, non-exclusive license, under - Apple's copyrights in this original Apple software (the "Apple Software"), to - use, reproduce, modify and redistribute the Apple Software, with or without - modifications, in source and/or binary forms; provided that if you redistribute - the Apple Software in its entirety and without modifications, you must retain - this notice and the following text and disclaimers in all such redistributions - of the Apple Software. - - Neither the name, trademarks, service marks or logos of Apple Inc. may be used - to endorse or promote products derived from the Apple Software without specific - prior written permission from Apple. Except as expressly stated in this notice, - no other rights or licenses, express or implied, are granted by Apple herein, - including but not limited to any patent rights that may be infringed by your - derivative works or by other works in which the Apple Software may be - incorporated. - - The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO - WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED - WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN - COMBINATION WITH YOUR PRODUCTS. - - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR - DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF - CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF - APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - Copyright (C) 2009 Apple Inc. All Rights Reserved. - - */ - - -/* - DDG extensions include: - Each reachability object now has a copy of the key used to store it in a - dictionary. This allows each observer to quickly determine if the event is - important to them. - - -currentReachabilityStatus also has a significantly different decision criteria than - Apple's code. - - A multiple convenience test methods have been added. - */ - -#import -#import - -#define USE_DDG_EXTENSIONS 1 // Use DDG's Extensions to test network criteria. -// Since NSAssert and NSCAssert are used in this code, -// I recommend you set NS_BLOCK_ASSERTIONS=1 in the release versions of your projects. - -enum { - - // DDG NetworkStatus Constant Names. - kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'. - kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi. - kReachableViaWiFi - -}; -typedef uint32_t NetworkStatus; - -enum { - - // Apple NetworkStatus Constant Names. - NotReachable = kNotReachable, - ReachableViaWiFi = kReachableViaWiFi, - ReachableViaWWAN = kReachableViaWWAN - -}; - - -extern NSString *const kInternetConnection; -extern NSString *const kLocalWiFiConnection; -extern NSString *const kReachabilityChangedNotification; - -@interface Reachability: NSObject { - -@private - NSString *key_; - SCNetworkReachabilityRef reachabilityRef; - -} - -@property (copy) NSString *key; // Atomic because network operations are asynchronous. - -// Designated Initializer. -- (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref; - -// Use to check the reachability of a particular host name. -+ (Reachability *) reachabilityWithHostName: (NSString*) hostName; - -// Use to check the reachability of a particular IP address. -+ (Reachability *) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; - -// Use to check whether the default route is available. -// Should be used to, at minimum, establish network connectivity. -+ (Reachability *) reachabilityForInternetConnection; - -// Use to check whether a local wifi connection is available. -+ (Reachability *) reachabilityForLocalWiFi; - -//Start listening for reachability notifications on the current run loop. -- (BOOL) startNotifier; -- (void) stopNotifier; - -// Comparison routines to enable choosing actions in a notification. -- (BOOL) isEqual: (Reachability *) r; - -// These are the status tests. -- (NetworkStatus) currentReachabilityStatus; - -// The main direct test of reachability. -- (BOOL) isReachable; - -// WWAN may be available, but not active until a connection has been established. -// WiFi may require a connection for VPN on Demand. -- (BOOL) isConnectionRequired; // Identical DDG variant. -- (BOOL) connectionRequired; // Apple's routine. - -// Dynamic, on demand connection? -- (BOOL) isConnectionOnDemand; - -// Is user intervention required? -- (BOOL) isInterventionRequired; - -// Routines for specific connection testing by your app. -- (BOOL) isReachableViaWWAN; -- (BOOL) isReachableViaWiFi; - -- (SCNetworkReachabilityFlags) reachabilityFlags; - -@end diff --git a/ASIHTTP/Reachability/.svn/text-base/Reachability.m.svn-base b/ASIHTTP/Reachability/.svn/text-base/Reachability.m.svn-base deleted file mode 100644 index 4082a48..0000000 --- a/ASIHTTP/Reachability/.svn/text-base/Reachability.m.svn-base +++ /dev/null @@ -1,814 +0,0 @@ -/* - - File: Reachability.m - Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. - - Version: 2.0.4ddg - */ - -/* - Significant additions made by Andrew W. Donoho, August 11, 2009. - This is a derived work of Apple's Reachability v2.0 class. - - The below license is the new BSD license with the OSI recommended personalizations. - - - Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C. - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - - -/* - - Apple's Original License on Reachability v2.0 - - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. - ("Apple") in consideration of your agreement to the following terms, and your - use, installation, modification or redistribution of this Apple software - constitutes acceptance of these terms. If you do not agree with these terms, - please do not use, install, modify or redistribute this Apple software. - - In consideration of your agreement to abide by the following terms, and subject - to these terms, Apple grants you a personal, non-exclusive license, under - Apple's copyrights in this original Apple software (the "Apple Software"), to - use, reproduce, modify and redistribute the Apple Software, with or without - modifications, in source and/or binary forms; provided that if you redistribute - the Apple Software in its entirety and without modifications, you must retain - this notice and the following text and disclaimers in all such redistributions - of the Apple Software. - - Neither the name, trademarks, service marks or logos of Apple Inc. may be used - to endorse or promote products derived from the Apple Software without specific - prior written permission from Apple. Except as expressly stated in this notice, - no other rights or licenses, express or implied, are granted by Apple herein, - including but not limited to any patent rights that may be infringed by your - derivative works or by other works in which the Apple Software may be - incorporated. - - The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO - WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED - WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN - COMBINATION WITH YOUR PRODUCTS. - - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR - DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF - CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF - APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - Copyright (C) 2009 Apple Inc. All Rights Reserved. - -*/ - -/* - Each reachability object now has a copy of the key used to store it in a dictionary. - This allows each observer to quickly determine if the event is important to them. -*/ - -#import -#import -#import -#import -#import -#import - -#import - -#import "Reachability.h" - -NSString *const kInternetConnection = @"InternetConnection"; -NSString *const kLocalWiFiConnection = @"LocalWiFiConnection"; -NSString *const kReachabilityChangedNotification = @"NetworkReachabilityChangedNotification"; - -#define CLASS_DEBUG 1 // Turn on logReachabilityFlags. Must also have a project wide defined DEBUG. - -#if (defined DEBUG && defined CLASS_DEBUG) -#define logReachabilityFlags(flags) (logReachabilityFlags_(__PRETTY_FUNCTION__, __LINE__, flags)) - -static NSString *reachabilityFlags_(SCNetworkReachabilityFlags flags) { - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol. - return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c%c", - (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', - (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', - - (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', - (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', - (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', - (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', - (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', - (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', - (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-']; -#else - // Compile out the v3.0 features for v2.2.1 deployment. - return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c", - (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', - (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', - - (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', - (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', - (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', - // v3 kSCNetworkReachabilityFlagsConnectionOnTraffic == v2 kSCNetworkReachabilityFlagsConnectionAutomatic - (flags & kSCNetworkReachabilityFlagsConnectionAutomatic) ? 'C' : '-', - // (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', // No v2 equivalent. - (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', - (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-']; -#endif - -} // reachabilityFlags_() - -static void logReachabilityFlags_(const char *name, int line, SCNetworkReachabilityFlags flags) { - - NSLog(@"%s (%d) \n\t%@", name, line, reachabilityFlags_(flags)); - -} // logReachabilityFlags_() - -#define logNetworkStatus(status) (logNetworkStatus_(__PRETTY_FUNCTION__, __LINE__, status)) - -static void logNetworkStatus_(const char *name, int line, NetworkStatus status) { - - NSString *statusString = nil; - - switch (status) { - case kNotReachable: - statusString = [NSString stringWithString: @"Not Reachable"]; - break; - case kReachableViaWWAN: - statusString = [NSString stringWithString: @"Reachable via WWAN"]; - break; - case kReachableViaWiFi: - statusString = [NSString stringWithString: @"Reachable via WiFi"]; - break; - } - - NSLog(@"%s (%d) \n\tNetwork Status: %@", name, line, statusString); - -} // logNetworkStatus_() - -#else -#define logReachabilityFlags(flags) -#define logNetworkStatus(status) -#endif - -@interface Reachability (private) - -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags; - -@end - -@implementation Reachability - -@synthesize key = key_; - -// Preclude direct access to ivars. -+ (BOOL) accessInstanceVariablesDirectly { - - return NO; - -} // accessInstanceVariablesDirectly - - -- (void) dealloc { - - [self stopNotifier]; - if(reachabilityRef) { - - CFRelease(reachabilityRef); reachabilityRef = NULL; - - } - - self.key = nil; - - [super dealloc]; - -} // dealloc - - -- (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref { - - if (self = [super init]) { - - reachabilityRef = ref; - - } - - return self; - -} // initWithReachabilityRef: - - -#if (defined DEBUG && defined CLASS_DEBUG) -- (NSString *) description { - - NSAssert(reachabilityRef, @"-description called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - - SCNetworkReachabilityGetFlags(reachabilityRef, &flags); - - return [NSString stringWithFormat: @"%@\n\t%@", self.key, reachabilityFlags_(flags)]; - -} // description -#endif - - -#pragma mark - -#pragma mark Notification Management Methods - - -//Start listening for reachability notifications on the current run loop -static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) { - - #pragma unused (target, flags) - NSCAssert(info, @"info was NULL in ReachabilityCallback"); - NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was the wrong class in ReachabilityCallback"); - - //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively - // in case someone uses the Reachablity object in a different thread. - NSAutoreleasePool* pool = [NSAutoreleasePool new]; - - // Post a notification to notify the client that the network reachability changed. - [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification - object: (Reachability *) info]; - - [pool release]; - -} // ReachabilityCallback() - - -- (BOOL) startNotifier { - - SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; - - if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) { - - if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) { - - return YES; - - } - - } - - return NO; - -} // startNotifier - - -- (void) stopNotifier { - - if(reachabilityRef) { - - SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - - } - -} // stopNotifier - - -- (BOOL) isEqual: (Reachability *) r { - - return [r.key isEqualToString: self.key]; - -} // isEqual: - - -#pragma mark - -#pragma mark Reachability Allocation Methods - - -+ (Reachability *) reachabilityWithHostName: (NSString *) hostName { - - SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); - - if (ref) { - - Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease]; - - r.key = hostName; - - return r; - - } - - return nil; - -} // reachabilityWithHostName - - -+ (NSString *) makeAddressKey: (in_addr_t) addr { - // addr is assumed to be in network byte order. - - static const int highShift = 24; - static const int highMidShift = 16; - static const int lowMidShift = 8; - static const in_addr_t mask = 0x000000ff; - - addr = ntohl(addr); - - return [NSString stringWithFormat: @"%d.%d.%d.%d", - (addr >> highShift) & mask, - (addr >> highMidShift) & mask, - (addr >> lowMidShift) & mask, - addr & mask]; - -} // makeAddressKey: - - -+ (Reachability *) reachabilityWithAddress: (const struct sockaddr_in *) hostAddress { - - SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress); - - if (ref) { - - Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease]; - - r.key = [self makeAddressKey: hostAddress->sin_addr.s_addr]; - - return r; - - } - - return nil; - -} // reachabilityWithAddress - - -+ (Reachability *) reachabilityForInternetConnection { - - struct sockaddr_in zeroAddress; - bzero(&zeroAddress, sizeof(zeroAddress)); - zeroAddress.sin_len = sizeof(zeroAddress); - zeroAddress.sin_family = AF_INET; - - Reachability *r = [self reachabilityWithAddress: &zeroAddress]; - - r.key = kInternetConnection; - - return r; - -} // reachabilityForInternetConnection - - -+ (Reachability *) reachabilityForLocalWiFi { - - struct sockaddr_in localWifiAddress; - bzero(&localWifiAddress, sizeof(localWifiAddress)); - localWifiAddress.sin_len = sizeof(localWifiAddress); - localWifiAddress.sin_family = AF_INET; - // IN_LINKLOCALNETNUM is defined in as 169.254.0.0 - localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); - - Reachability *r = [self reachabilityWithAddress: &localWifiAddress]; - - r.key = kLocalWiFiConnection; - - return r; - -} // reachabilityForLocalWiFi - - -#pragma mark - -#pragma mark Network Flag Handling Methods - - -#if USE_DDG_EXTENSIONS -// -// iPhone condition codes as reported by a 3GS running iPhone OS v3.0. -// Airplane Mode turned on: Reachability Flag Status: -- ------- -// WWAN Active: Reachability Flag Status: WR -t----- -// WWAN Connection required: Reachability Flag Status: WR ct----- -// WiFi turned on: Reachability Flag Status: -R ------- Reachable. -// Local WiFi turned on: Reachability Flag Status: -R xxxxxxd Reachable. -// WiFi turned on: Reachability Flag Status: -R ct----- Connection down. (Non-intuitive, empirically determined answer.) -const SCNetworkReachabilityFlags kConnectionDown = kSCNetworkReachabilityFlagsConnectionRequired | - kSCNetworkReachabilityFlagsTransientConnection; -// WiFi turned on: Reachability Flag Status: -R ct-i--- Reachable but it will require user intervention (e.g. enter a WiFi password). -// WiFi turned on: Reachability Flag Status: -R -t----- Reachable via VPN. -// -// In the below method, an 'x' in the flag status means I don't care about its value. -// -// This method differs from Apple's by testing explicitly for empirically observed values. -// This gives me more confidence in it's correct behavior. Apple's code covers more cases -// than mine. My code covers the cases that occur. -// -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags { - - if (flags & kSCNetworkReachabilityFlagsReachable) { - - // Local WiFi -- Test derived from Apple's code: -localWiFiStatusForFlags:. - if (self.key == kLocalWiFiConnection) { - - // Reachability Flag Status: xR xxxxxxd Reachable. - return (flags & kSCNetworkReachabilityFlagsIsDirect) ? kReachableViaWiFi : kNotReachable; - - } - - // Observed WWAN Values: - // WWAN Active: Reachability Flag Status: WR -t----- - // WWAN Connection required: Reachability Flag Status: WR ct----- - // - // Test Value: Reachability Flag Status: WR xxxxxxx - if (flags & kSCNetworkReachabilityFlagsIsWWAN) { return kReachableViaWWAN; } - - // Clear moot bits. - flags &= ~kSCNetworkReachabilityFlagsReachable; - flags &= ~kSCNetworkReachabilityFlagsIsDirect; - flags &= ~kSCNetworkReachabilityFlagsIsLocalAddress; // kInternetConnection is local. - - // Reachability Flag Status: -R ct---xx Connection down. - if (flags == kConnectionDown) { return kNotReachable; } - - // Reachability Flag Status: -R -t---xx Reachable. WiFi + VPN(is up) (Thank you Ling Wang) - if (flags & kSCNetworkReachabilityFlagsTransientConnection) { return kReachableViaWiFi; } - - // Reachability Flag Status: -R -----xx Reachable. - if (flags == 0) { return kReachableViaWiFi; } - - // Apple's code tests for dynamic connection types here. I don't. - // If a connection is required, regardless of whether it is on demand or not, it is a WiFi connection. - // If you care whether a connection needs to be brought up, use -isConnectionRequired. - // If you care about whether user intervention is necessary, use -isInterventionRequired. - // If you care about dynamically establishing the connection, use -isConnectionIsOnDemand. - - // Reachability Flag Status: -R cxxxxxx Reachable. - if (flags & kSCNetworkReachabilityFlagsConnectionRequired) { return kReachableViaWiFi; } - - // Required by the compiler. Should never get here. Default to not connected. -#if (defined DEBUG && defined CLASS_DEBUG) - NSAssert1(NO, @"Uncaught reachability test. Flags: %@", reachabilityFlags_(flags)); -#endif - return kNotReachable; - - } - - // Reachability Flag Status: x- xxxxxxx - return kNotReachable; - -} // networkStatusForFlags: - - -- (NetworkStatus) currentReachabilityStatus { - - NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - -// logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return status; - - } - - return kNotReachable; - -} // currentReachabilityStatus - - -- (BOOL) isReachable { - - NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - -// logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - -// logNetworkStatus(status); - - return (kNotReachable != status); - - } - - return NO; - -} // isReachable - - -- (BOOL) isConnectionRequired { - - NSAssert(reachabilityRef, @"isConnectionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return (flags & kSCNetworkReachabilityFlagsConnectionRequired); - - } - - return NO; - -} // isConnectionRequired - - -- (BOOL) connectionRequired { - - return [self isConnectionRequired]; - -} // connectionRequired -#endif - - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) -static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionOnTraffic | - kSCNetworkReachabilityFlagsConnectionOnDemand; -#else -static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionAutomatic; -#endif - -- (BOOL) isConnectionOnDemand { - - NSAssert(reachabilityRef, @"isConnectionIsOnDemand called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) && - (flags & kOnDemandConnection)); - - } - - return NO; - -} // isConnectionOnDemand - - -- (BOOL) isInterventionRequired { - - NSAssert(reachabilityRef, @"isInterventionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) && - (flags & kSCNetworkReachabilityFlagsInterventionRequired)); - - } - - return NO; - -} // isInterventionRequired - - -- (BOOL) isReachableViaWWAN { - - NSAssert(reachabilityRef, @"isReachableViaWWAN called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return (kReachableViaWWAN == status); - - } - - return NO; - -} // isReachableViaWWAN - - -- (BOOL) isReachableViaWiFi { - - NSAssert(reachabilityRef, @"isReachableViaWiFi called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return (kReachableViaWiFi == status); - - } - - return NO; - -} // isReachableViaWiFi - - -- (SCNetworkReachabilityFlags) reachabilityFlags { - - NSAssert(reachabilityRef, @"reachabilityFlags called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return flags; - - } - - return 0; - -} // reachabilityFlags - - -#pragma mark - -#pragma mark Apple's Network Flag Handling Methods - - -#if !USE_DDG_EXTENSIONS -/* - * - * Apple's Network Status testing code. - * The only changes that have been made are to use the new logReachabilityFlags macro and - * test for local WiFi via the key instead of Apple's boolean. Also, Apple's code was for v3.0 only - * iPhone OS. v2.2.1 and earlier conditional compiling is turned on. Hence, to mirror Apple's behavior, - * set your Base SDK to v3.0 or higher. - * - */ - -- (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags -{ - logReachabilityFlags(flags); - - BOOL retVal = NotReachable; - if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) - { - retVal = ReachableViaWiFi; - } - return retVal; -} - - -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags -{ - logReachabilityFlags(flags); - if (!(flags & kSCNetworkReachabilityFlagsReachable)) - { - // if target host is not reachable - return NotReachable; - } - - BOOL retVal = NotReachable; - - if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired)) - { - // if target host is reachable and no connection is required - // then we'll assume (for now) that your on Wi-Fi - retVal = ReachableViaWiFi; - } - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol. - if ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) || - (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)) -#else - if (flags & kSCNetworkReachabilityFlagsConnectionAutomatic) -#endif - { - // ... and the connection is on-demand (or on-traffic) if the - // calling application is using the CFSocketStream or higher APIs - - if (!(flags & kSCNetworkReachabilityFlagsInterventionRequired)) - { - // ... and no [user] intervention is needed - retVal = ReachableViaWiFi; - } - } - - if (flags & kSCNetworkReachabilityFlagsIsWWAN) - { - // ... but WWAN connections are OK if the calling application - // is using the CFNetwork (CFSocketStream?) APIs. - retVal = ReachableViaWWAN; - } - return retVal; -} - - -- (NetworkStatus) currentReachabilityStatus -{ - NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef"); - - NetworkStatus retVal = NotReachable; - SCNetworkReachabilityFlags flags; - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) - { - if(self.key == kLocalWiFiConnection) - { - retVal = [self localWiFiStatusForFlags: flags]; - } - else - { - retVal = [self networkStatusForFlags: flags]; - } - } - return retVal; -} - - -- (BOOL) isReachable { - - NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - if(self.key == kLocalWiFiConnection) { - - status = [self localWiFiStatusForFlags: flags]; - - } else { - - status = [self networkStatusForFlags: flags]; - - } - - return (kNotReachable != status); - - } - - return NO; - -} // isReachable - - -- (BOOL) isConnectionRequired { - - return [self connectionRequired]; - -} // isConnectionRequired - - -- (BOOL) connectionRequired { - - NSAssert(reachabilityRef, @"connectionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return (flags & kSCNetworkReachabilityFlagsConnectionRequired); - - } - - return NO; - -} // connectionRequired -#endif - -@end diff --git a/ASIHTTP/Reachability/Reachability.h b/ASIHTTP/Reachability/Reachability.h deleted file mode 100644 index b49b797..0000000 --- a/ASIHTTP/Reachability/Reachability.h +++ /dev/null @@ -1,193 +0,0 @@ -/* - - File: Reachability.h - Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. - - Version: 2.0.4ddg - */ - -/* - Significant additions made by Andrew W. Donoho, August 11, 2009. - This is a derived work of Apple's Reachability v2.0 class. - - The below license is the new BSD license with the OSI recommended personalizations. - - - Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C. - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - - -/* - - Apple's Original License on Reachability v2.0 - - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. - ("Apple") in consideration of your agreement to the following terms, and your - use, installation, modification or redistribution of this Apple software - constitutes acceptance of these terms. If you do not agree with these terms, - please do not use, install, modify or redistribute this Apple software. - - In consideration of your agreement to abide by the following terms, and subject - to these terms, Apple grants you a personal, non-exclusive license, under - Apple's copyrights in this original Apple software (the "Apple Software"), to - use, reproduce, modify and redistribute the Apple Software, with or without - modifications, in source and/or binary forms; provided that if you redistribute - the Apple Software in its entirety and without modifications, you must retain - this notice and the following text and disclaimers in all such redistributions - of the Apple Software. - - Neither the name, trademarks, service marks or logos of Apple Inc. may be used - to endorse or promote products derived from the Apple Software without specific - prior written permission from Apple. Except as expressly stated in this notice, - no other rights or licenses, express or implied, are granted by Apple herein, - including but not limited to any patent rights that may be infringed by your - derivative works or by other works in which the Apple Software may be - incorporated. - - The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO - WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED - WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN - COMBINATION WITH YOUR PRODUCTS. - - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR - DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF - CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF - APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - Copyright (C) 2009 Apple Inc. All Rights Reserved. - - */ - - -/* - DDG extensions include: - Each reachability object now has a copy of the key used to store it in a - dictionary. This allows each observer to quickly determine if the event is - important to them. - - -currentReachabilityStatus also has a significantly different decision criteria than - Apple's code. - - A multiple convenience test methods have been added. - */ - -#import -#import - -#define USE_DDG_EXTENSIONS 1 // Use DDG's Extensions to test network criteria. -// Since NSAssert and NSCAssert are used in this code, -// I recommend you set NS_BLOCK_ASSERTIONS=1 in the release versions of your projects. - -enum { - - // DDG NetworkStatus Constant Names. - kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'. - kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi. - kReachableViaWiFi - -}; -typedef uint32_t NetworkStatus; - -enum { - - // Apple NetworkStatus Constant Names. - NotReachable = kNotReachable, - ReachableViaWiFi = kReachableViaWiFi, - ReachableViaWWAN = kReachableViaWWAN - -}; - - -extern NSString *const kInternetConnection; -extern NSString *const kLocalWiFiConnection; -extern NSString *const kReachabilityChangedNotification; - -@interface Reachability: NSObject { - -@private - NSString *key_; - SCNetworkReachabilityRef reachabilityRef; - -} - -@property (copy) NSString *key; // Atomic because network operations are asynchronous. - -// Designated Initializer. -- (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref; - -// Use to check the reachability of a particular host name. -+ (Reachability *) reachabilityWithHostName: (NSString*) hostName; - -// Use to check the reachability of a particular IP address. -+ (Reachability *) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; - -// Use to check whether the default route is available. -// Should be used to, at minimum, establish network connectivity. -+ (Reachability *) reachabilityForInternetConnection; - -// Use to check whether a local wifi connection is available. -+ (Reachability *) reachabilityForLocalWiFi; - -//Start listening for reachability notifications on the current run loop. -- (BOOL) startNotifier; -- (void) stopNotifier; - -// Comparison routines to enable choosing actions in a notification. -- (BOOL) isEqual: (Reachability *) r; - -// These are the status tests. -- (NetworkStatus) currentReachabilityStatus; - -// The main direct test of reachability. -- (BOOL) isReachable; - -// WWAN may be available, but not active until a connection has been established. -// WiFi may require a connection for VPN on Demand. -- (BOOL) isConnectionRequired; // Identical DDG variant. -- (BOOL) connectionRequired; // Apple's routine. - -// Dynamic, on demand connection? -- (BOOL) isConnectionOnDemand; - -// Is user intervention required? -- (BOOL) isInterventionRequired; - -// Routines for specific connection testing by your app. -- (BOOL) isReachableViaWWAN; -- (BOOL) isReachableViaWiFi; - -- (SCNetworkReachabilityFlags) reachabilityFlags; - -@end diff --git a/ASIHTTP/Reachability/Reachability.m b/ASIHTTP/Reachability/Reachability.m deleted file mode 100644 index 4082a48..0000000 --- a/ASIHTTP/Reachability/Reachability.m +++ /dev/null @@ -1,814 +0,0 @@ -/* - - File: Reachability.m - Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs. - - Version: 2.0.4ddg - */ - -/* - Significant additions made by Andrew W. Donoho, August 11, 2009. - This is a derived work of Apple's Reachability v2.0 class. - - The below license is the new BSD license with the OSI recommended personalizations. - - - Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C. - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - */ - - -/* - - Apple's Original License on Reachability v2.0 - - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. - ("Apple") in consideration of your agreement to the following terms, and your - use, installation, modification or redistribution of this Apple software - constitutes acceptance of these terms. If you do not agree with these terms, - please do not use, install, modify or redistribute this Apple software. - - In consideration of your agreement to abide by the following terms, and subject - to these terms, Apple grants you a personal, non-exclusive license, under - Apple's copyrights in this original Apple software (the "Apple Software"), to - use, reproduce, modify and redistribute the Apple Software, with or without - modifications, in source and/or binary forms; provided that if you redistribute - the Apple Software in its entirety and without modifications, you must retain - this notice and the following text and disclaimers in all such redistributions - of the Apple Software. - - Neither the name, trademarks, service marks or logos of Apple Inc. may be used - to endorse or promote products derived from the Apple Software without specific - prior written permission from Apple. Except as expressly stated in this notice, - no other rights or licenses, express or implied, are granted by Apple herein, - including but not limited to any patent rights that may be infringed by your - derivative works or by other works in which the Apple Software may be - incorporated. - - The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO - WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED - WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN - COMBINATION WITH YOUR PRODUCTS. - - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR - CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE - GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR - DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF - CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF - APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - Copyright (C) 2009 Apple Inc. All Rights Reserved. - -*/ - -/* - Each reachability object now has a copy of the key used to store it in a dictionary. - This allows each observer to quickly determine if the event is important to them. -*/ - -#import -#import -#import -#import -#import -#import - -#import - -#import "Reachability.h" - -NSString *const kInternetConnection = @"InternetConnection"; -NSString *const kLocalWiFiConnection = @"LocalWiFiConnection"; -NSString *const kReachabilityChangedNotification = @"NetworkReachabilityChangedNotification"; - -#define CLASS_DEBUG 1 // Turn on logReachabilityFlags. Must also have a project wide defined DEBUG. - -#if (defined DEBUG && defined CLASS_DEBUG) -#define logReachabilityFlags(flags) (logReachabilityFlags_(__PRETTY_FUNCTION__, __LINE__, flags)) - -static NSString *reachabilityFlags_(SCNetworkReachabilityFlags flags) { - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol. - return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c%c", - (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', - (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', - - (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', - (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', - (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', - (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', - (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', - (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', - (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-']; -#else - // Compile out the v3.0 features for v2.2.1 deployment. - return [NSString stringWithFormat:@"Reachability Flags: %c%c %c%c%c%c%c%c", - (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-', - (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-', - - (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', - (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', - (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', - // v3 kSCNetworkReachabilityFlagsConnectionOnTraffic == v2 kSCNetworkReachabilityFlagsConnectionAutomatic - (flags & kSCNetworkReachabilityFlagsConnectionAutomatic) ? 'C' : '-', - // (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', // No v2 equivalent. - (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-', - (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-']; -#endif - -} // reachabilityFlags_() - -static void logReachabilityFlags_(const char *name, int line, SCNetworkReachabilityFlags flags) { - - NSLog(@"%s (%d) \n\t%@", name, line, reachabilityFlags_(flags)); - -} // logReachabilityFlags_() - -#define logNetworkStatus(status) (logNetworkStatus_(__PRETTY_FUNCTION__, __LINE__, status)) - -static void logNetworkStatus_(const char *name, int line, NetworkStatus status) { - - NSString *statusString = nil; - - switch (status) { - case kNotReachable: - statusString = [NSString stringWithString: @"Not Reachable"]; - break; - case kReachableViaWWAN: - statusString = [NSString stringWithString: @"Reachable via WWAN"]; - break; - case kReachableViaWiFi: - statusString = [NSString stringWithString: @"Reachable via WiFi"]; - break; - } - - NSLog(@"%s (%d) \n\tNetwork Status: %@", name, line, statusString); - -} // logNetworkStatus_() - -#else -#define logReachabilityFlags(flags) -#define logNetworkStatus(status) -#endif - -@interface Reachability (private) - -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags; - -@end - -@implementation Reachability - -@synthesize key = key_; - -// Preclude direct access to ivars. -+ (BOOL) accessInstanceVariablesDirectly { - - return NO; - -} // accessInstanceVariablesDirectly - - -- (void) dealloc { - - [self stopNotifier]; - if(reachabilityRef) { - - CFRelease(reachabilityRef); reachabilityRef = NULL; - - } - - self.key = nil; - - [super dealloc]; - -} // dealloc - - -- (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref { - - if (self = [super init]) { - - reachabilityRef = ref; - - } - - return self; - -} // initWithReachabilityRef: - - -#if (defined DEBUG && defined CLASS_DEBUG) -- (NSString *) description { - - NSAssert(reachabilityRef, @"-description called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - - SCNetworkReachabilityGetFlags(reachabilityRef, &flags); - - return [NSString stringWithFormat: @"%@\n\t%@", self.key, reachabilityFlags_(flags)]; - -} // description -#endif - - -#pragma mark - -#pragma mark Notification Management Methods - - -//Start listening for reachability notifications on the current run loop -static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) { - - #pragma unused (target, flags) - NSCAssert(info, @"info was NULL in ReachabilityCallback"); - NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was the wrong class in ReachabilityCallback"); - - //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively - // in case someone uses the Reachablity object in a different thread. - NSAutoreleasePool* pool = [NSAutoreleasePool new]; - - // Post a notification to notify the client that the network reachability changed. - [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification - object: (Reachability *) info]; - - [pool release]; - -} // ReachabilityCallback() - - -- (BOOL) startNotifier { - - SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL}; - - if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) { - - if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) { - - return YES; - - } - - } - - return NO; - -} // startNotifier - - -- (void) stopNotifier { - - if(reachabilityRef) { - - SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); - - } - -} // stopNotifier - - -- (BOOL) isEqual: (Reachability *) r { - - return [r.key isEqualToString: self.key]; - -} // isEqual: - - -#pragma mark - -#pragma mark Reachability Allocation Methods - - -+ (Reachability *) reachabilityWithHostName: (NSString *) hostName { - - SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); - - if (ref) { - - Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease]; - - r.key = hostName; - - return r; - - } - - return nil; - -} // reachabilityWithHostName - - -+ (NSString *) makeAddressKey: (in_addr_t) addr { - // addr is assumed to be in network byte order. - - static const int highShift = 24; - static const int highMidShift = 16; - static const int lowMidShift = 8; - static const in_addr_t mask = 0x000000ff; - - addr = ntohl(addr); - - return [NSString stringWithFormat: @"%d.%d.%d.%d", - (addr >> highShift) & mask, - (addr >> highMidShift) & mask, - (addr >> lowMidShift) & mask, - addr & mask]; - -} // makeAddressKey: - - -+ (Reachability *) reachabilityWithAddress: (const struct sockaddr_in *) hostAddress { - - SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress); - - if (ref) { - - Reachability *r = [[[self alloc] initWithReachabilityRef: ref] autorelease]; - - r.key = [self makeAddressKey: hostAddress->sin_addr.s_addr]; - - return r; - - } - - return nil; - -} // reachabilityWithAddress - - -+ (Reachability *) reachabilityForInternetConnection { - - struct sockaddr_in zeroAddress; - bzero(&zeroAddress, sizeof(zeroAddress)); - zeroAddress.sin_len = sizeof(zeroAddress); - zeroAddress.sin_family = AF_INET; - - Reachability *r = [self reachabilityWithAddress: &zeroAddress]; - - r.key = kInternetConnection; - - return r; - -} // reachabilityForInternetConnection - - -+ (Reachability *) reachabilityForLocalWiFi { - - struct sockaddr_in localWifiAddress; - bzero(&localWifiAddress, sizeof(localWifiAddress)); - localWifiAddress.sin_len = sizeof(localWifiAddress); - localWifiAddress.sin_family = AF_INET; - // IN_LINKLOCALNETNUM is defined in as 169.254.0.0 - localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); - - Reachability *r = [self reachabilityWithAddress: &localWifiAddress]; - - r.key = kLocalWiFiConnection; - - return r; - -} // reachabilityForLocalWiFi - - -#pragma mark - -#pragma mark Network Flag Handling Methods - - -#if USE_DDG_EXTENSIONS -// -// iPhone condition codes as reported by a 3GS running iPhone OS v3.0. -// Airplane Mode turned on: Reachability Flag Status: -- ------- -// WWAN Active: Reachability Flag Status: WR -t----- -// WWAN Connection required: Reachability Flag Status: WR ct----- -// WiFi turned on: Reachability Flag Status: -R ------- Reachable. -// Local WiFi turned on: Reachability Flag Status: -R xxxxxxd Reachable. -// WiFi turned on: Reachability Flag Status: -R ct----- Connection down. (Non-intuitive, empirically determined answer.) -const SCNetworkReachabilityFlags kConnectionDown = kSCNetworkReachabilityFlagsConnectionRequired | - kSCNetworkReachabilityFlagsTransientConnection; -// WiFi turned on: Reachability Flag Status: -R ct-i--- Reachable but it will require user intervention (e.g. enter a WiFi password). -// WiFi turned on: Reachability Flag Status: -R -t----- Reachable via VPN. -// -// In the below method, an 'x' in the flag status means I don't care about its value. -// -// This method differs from Apple's by testing explicitly for empirically observed values. -// This gives me more confidence in it's correct behavior. Apple's code covers more cases -// than mine. My code covers the cases that occur. -// -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags { - - if (flags & kSCNetworkReachabilityFlagsReachable) { - - // Local WiFi -- Test derived from Apple's code: -localWiFiStatusForFlags:. - if (self.key == kLocalWiFiConnection) { - - // Reachability Flag Status: xR xxxxxxd Reachable. - return (flags & kSCNetworkReachabilityFlagsIsDirect) ? kReachableViaWiFi : kNotReachable; - - } - - // Observed WWAN Values: - // WWAN Active: Reachability Flag Status: WR -t----- - // WWAN Connection required: Reachability Flag Status: WR ct----- - // - // Test Value: Reachability Flag Status: WR xxxxxxx - if (flags & kSCNetworkReachabilityFlagsIsWWAN) { return kReachableViaWWAN; } - - // Clear moot bits. - flags &= ~kSCNetworkReachabilityFlagsReachable; - flags &= ~kSCNetworkReachabilityFlagsIsDirect; - flags &= ~kSCNetworkReachabilityFlagsIsLocalAddress; // kInternetConnection is local. - - // Reachability Flag Status: -R ct---xx Connection down. - if (flags == kConnectionDown) { return kNotReachable; } - - // Reachability Flag Status: -R -t---xx Reachable. WiFi + VPN(is up) (Thank you Ling Wang) - if (flags & kSCNetworkReachabilityFlagsTransientConnection) { return kReachableViaWiFi; } - - // Reachability Flag Status: -R -----xx Reachable. - if (flags == 0) { return kReachableViaWiFi; } - - // Apple's code tests for dynamic connection types here. I don't. - // If a connection is required, regardless of whether it is on demand or not, it is a WiFi connection. - // If you care whether a connection needs to be brought up, use -isConnectionRequired. - // If you care about whether user intervention is necessary, use -isInterventionRequired. - // If you care about dynamically establishing the connection, use -isConnectionIsOnDemand. - - // Reachability Flag Status: -R cxxxxxx Reachable. - if (flags & kSCNetworkReachabilityFlagsConnectionRequired) { return kReachableViaWiFi; } - - // Required by the compiler. Should never get here. Default to not connected. -#if (defined DEBUG && defined CLASS_DEBUG) - NSAssert1(NO, @"Uncaught reachability test. Flags: %@", reachabilityFlags_(flags)); -#endif - return kNotReachable; - - } - - // Reachability Flag Status: x- xxxxxxx - return kNotReachable; - -} // networkStatusForFlags: - - -- (NetworkStatus) currentReachabilityStatus { - - NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - -// logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return status; - - } - - return kNotReachable; - -} // currentReachabilityStatus - - -- (BOOL) isReachable { - - NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - -// logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - -// logNetworkStatus(status); - - return (kNotReachable != status); - - } - - return NO; - -} // isReachable - - -- (BOOL) isConnectionRequired { - - NSAssert(reachabilityRef, @"isConnectionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return (flags & kSCNetworkReachabilityFlagsConnectionRequired); - - } - - return NO; - -} // isConnectionRequired - - -- (BOOL) connectionRequired { - - return [self isConnectionRequired]; - -} // connectionRequired -#endif - - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) -static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionOnTraffic | - kSCNetworkReachabilityFlagsConnectionOnDemand; -#else -static const SCNetworkReachabilityFlags kOnDemandConnection = kSCNetworkReachabilityFlagsConnectionAutomatic; -#endif - -- (BOOL) isConnectionOnDemand { - - NSAssert(reachabilityRef, @"isConnectionIsOnDemand called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) && - (flags & kOnDemandConnection)); - - } - - return NO; - -} // isConnectionOnDemand - - -- (BOOL) isInterventionRequired { - - NSAssert(reachabilityRef, @"isInterventionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) && - (flags & kSCNetworkReachabilityFlagsInterventionRequired)); - - } - - return NO; - -} // isInterventionRequired - - -- (BOOL) isReachableViaWWAN { - - NSAssert(reachabilityRef, @"isReachableViaWWAN called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return (kReachableViaWWAN == status); - - } - - return NO; - -} // isReachableViaWWAN - - -- (BOOL) isReachableViaWiFi { - - NSAssert(reachabilityRef, @"isReachableViaWiFi called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - status = [self networkStatusForFlags: flags]; - - return (kReachableViaWiFi == status); - - } - - return NO; - -} // isReachableViaWiFi - - -- (SCNetworkReachabilityFlags) reachabilityFlags { - - NSAssert(reachabilityRef, @"reachabilityFlags called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return flags; - - } - - return 0; - -} // reachabilityFlags - - -#pragma mark - -#pragma mark Apple's Network Flag Handling Methods - - -#if !USE_DDG_EXTENSIONS -/* - * - * Apple's Network Status testing code. - * The only changes that have been made are to use the new logReachabilityFlags macro and - * test for local WiFi via the key instead of Apple's boolean. Also, Apple's code was for v3.0 only - * iPhone OS. v2.2.1 and earlier conditional compiling is turned on. Hence, to mirror Apple's behavior, - * set your Base SDK to v3.0 or higher. - * - */ - -- (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags -{ - logReachabilityFlags(flags); - - BOOL retVal = NotReachable; - if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) - { - retVal = ReachableViaWiFi; - } - return retVal; -} - - -- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags -{ - logReachabilityFlags(flags); - if (!(flags & kSCNetworkReachabilityFlagsReachable)) - { - // if target host is not reachable - return NotReachable; - } - - BOOL retVal = NotReachable; - - if (!(flags & kSCNetworkReachabilityFlagsConnectionRequired)) - { - // if target host is reachable and no connection is required - // then we'll assume (for now) that your on Wi-Fi - retVal = ReachableViaWiFi; - } - -#if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) // Apple advises you to use the magic number instead of a symbol. - if ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) || - (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)) -#else - if (flags & kSCNetworkReachabilityFlagsConnectionAutomatic) -#endif - { - // ... and the connection is on-demand (or on-traffic) if the - // calling application is using the CFSocketStream or higher APIs - - if (!(flags & kSCNetworkReachabilityFlagsInterventionRequired)) - { - // ... and no [user] intervention is needed - retVal = ReachableViaWiFi; - } - } - - if (flags & kSCNetworkReachabilityFlagsIsWWAN) - { - // ... but WWAN connections are OK if the calling application - // is using the CFNetwork (CFSocketStream?) APIs. - retVal = ReachableViaWWAN; - } - return retVal; -} - - -- (NetworkStatus) currentReachabilityStatus -{ - NSAssert(reachabilityRef, @"currentReachabilityStatus called with NULL reachabilityRef"); - - NetworkStatus retVal = NotReachable; - SCNetworkReachabilityFlags flags; - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) - { - if(self.key == kLocalWiFiConnection) - { - retVal = [self localWiFiStatusForFlags: flags]; - } - else - { - retVal = [self networkStatusForFlags: flags]; - } - } - return retVal; -} - - -- (BOOL) isReachable { - - NSAssert(reachabilityRef, @"isReachable called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags = 0; - NetworkStatus status = kNotReachable; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - if(self.key == kLocalWiFiConnection) { - - status = [self localWiFiStatusForFlags: flags]; - - } else { - - status = [self networkStatusForFlags: flags]; - - } - - return (kNotReachable != status); - - } - - return NO; - -} // isReachable - - -- (BOOL) isConnectionRequired { - - return [self connectionRequired]; - -} // isConnectionRequired - - -- (BOOL) connectionRequired { - - NSAssert(reachabilityRef, @"connectionRequired called with NULL reachabilityRef"); - - SCNetworkReachabilityFlags flags; - - if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { - - logReachabilityFlags(flags); - - return (flags & kSCNetworkReachabilityFlagsConnectionRequired); - - } - - return NO; - -} // connectionRequired -#endif - -@end diff --git a/Classes/CodeGenWrapper.h b/Classes/CodeGenWrapper.h new file mode 100644 index 0000000..4484bb6 --- /dev/null +++ b/Classes/CodeGenWrapper.h @@ -0,0 +1,27 @@ +// +// CodeGenWrapper.h +// Echoprint +// +// Created by Виктор Полевой on 04.03.14. +// +// + +#import + +#import +#import + +#import "Codegen.h" + +@interface CodeGenWrapper : NSObject +{ + @private + Codegen *codegen; + + NSString *fingerprint; +} + +- (id) initWithPCM:(float*)pcmFloatValue numberOfSamples:(NSUInteger)numSamples startOffset:(NSInteger)startOffset; + +- (NSString*) codeString; +@end diff --git a/Classes/CodeGenWrapper.mm b/Classes/CodeGenWrapper.mm new file mode 100644 index 0000000..229f12b --- /dev/null +++ b/Classes/CodeGenWrapper.mm @@ -0,0 +1,28 @@ +// +// CodeGenWrapper.m +// Echoprint +// +// Created by Виктор Полевой on 04.03.14. +// +// + +#import "CodeGenWrapper.h" + +@implementation CodeGenWrapper + +- (id) initWithPCM:(float*)pcmFloatValue numberOfSamples:(NSUInteger)numSamples startOffset:(NSInteger)startOffset +{ + if (self = [super init]) + { + self->fingerprint = Codegen::encode(pcmFloatValue, numSamples, startOffset); + } + + return self; +} + +- (NSString*) codeString +{ + return self->fingerprint; +} + +@end diff --git a/Classes/FPGenerator.h b/Classes/FPGenerator.h new file mode 100644 index 0000000..5243ca5 --- /dev/null +++ b/Classes/FPGenerator.h @@ -0,0 +1,19 @@ +// +// FPGenerator.h +// Echoprint +// +// Created by Виктор Полевой on 04.03.14. +// +// + +#import +#include +#include "CAStreamBasicDescription.h" + +#import "CodeGenWrapper.h" + +@interface FPGenerator : NSObject + ++ (NSString*) generateFingerprintForFile:(NSString*)fullFileName; + +@end diff --git a/Classes/FPGenerator.mm b/Classes/FPGenerator.mm new file mode 100644 index 0000000..907dbb0 --- /dev/null +++ b/Classes/FPGenerator.mm @@ -0,0 +1,95 @@ +// +// FPGenerator.m +// Echoprint +// +// Created by Виктор Полевой on 04.03.14. +// +// + +#import "FPGenerator.h" + +@implementation FPGenerator + ++ (NSString*) generateFingerprintForFile:(NSString*)fullFileName +{ + char *filename = (char*) [fullFileName cStringUsingEncoding:NSASCIIStringEncoding]; + CFURLRef audioFileURL = CFURLCreateFromFileSystemRepresentation(NULL,(const UInt8*)filename, strlen(filename), false); + ExtAudioFileRef outExtAudioFile; + int err = ExtAudioFileOpenURL(audioFileURL, &outExtAudioFile); + if (err) { + NSLog(@"open failed"); + } + + + CAStreamBasicDescription clientFormat; + clientFormat.mSampleRate = 11025; + clientFormat.mFormatID = kAudioFormatLinearPCM; + clientFormat.mChannelsPerFrame = 2; + clientFormat.mBitsPerChannel = 32; + clientFormat.mBytesPerPacket = clientFormat.mBytesPerFrame = 4 * clientFormat.mChannelsPerFrame; + clientFormat.mFramesPerPacket = 1; + clientFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked;// | kAudioFormatFlagIsNonInterleaved; + + int size = sizeof(clientFormat); + err = ExtAudioFileSetProperty(outExtAudioFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat); + if (err) + NSLog(@"err on set format %d", err); + + int seconds_to_decode = 30; + int bytes_for_bigbuf = sizeof(float)*11025*seconds_to_decode; + float *bigBuf = (float*) malloc(bytes_for_bigbuf); + if(bigBuf == NULL) { + NSLog(@"Error mallocing bigbuf"); + } + NSUInteger totalFrames = 0; + while (1) { + AudioBufferList fillBufList; + fillBufList.mNumberBuffers = 1; + UInt32 bufferByteSize = 11025 * 4 * 2; // 1s of audio + char srcBuffer[bufferByteSize]; + UInt32 numFrames = clientFormat.BytesToFrames(bufferByteSize); // (bufferByteSize / clientFormat.mBytesPerFrame); + + fillBufList.mBuffers[0].mNumberChannels = clientFormat.NumberChannels(); + fillBufList.mBuffers[0].mDataByteSize = bufferByteSize; + fillBufList.mBuffers[0].mData = srcBuffer; + err = ExtAudioFileRead(outExtAudioFile, &numFrames, &fillBufList); + if (err) { + NSLog(@"err on read %d", err); + totalFrames = 0; + break; + } + if (!numFrames) + break; + + float mono_version[numFrames]; + float* float_buf = (float*) fillBufList.mBuffers[0].mData; + for(int i=0;i bytesLeftInBuffer) { + memcpy(bigBuf + totalFrames, mono_version, bytesLeftInBuffer); + totalFrames = totalFrames + (bytesLeftInBuffer/4); + break; + } else { + memcpy(bigBuf + totalFrames, mono_version, numFrames * sizeof(float)); + totalFrames = totalFrames + numFrames; + } + } + + NSString *fingerprint; + if(totalFrames > 11025) { + NSLog(@"Doing codegen on %d samples...", totalFrames); + + CodeGenWrapper *wrapper = [[CodeGenWrapper alloc] initWithPCM:bigBuf numberOfSamples:totalFrames startOffset:0]; + + fingerprint = [wrapper codeString]; + + NSLog(@"Done with codegen"); + } + free(bigBuf); + + return fingerprint; +} +@end diff --git a/Classes/MicrophoneInput.h b/Classes/MicrophoneInput.h old mode 100644 new mode 100755 diff --git a/Classes/MicrophoneInput.m b/Classes/MicrophoneInput.mm old mode 100644 new mode 100755 similarity index 78% rename from Classes/MicrophoneInput.m rename to Classes/MicrophoneInput.mm index e2dfc8a..193f246 --- a/Classes/MicrophoneInput.m +++ b/Classes/MicrophoneInput.mm @@ -28,16 +28,16 @@ -(IBAction) startRecording [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10]; - [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; - [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey]; - [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; - [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; - [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; - [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; + recordSettings[AVFormatIDKey] = @(kAudioFormatLinearPCM); + recordSettings[AVSampleRateKey] = @44100.0f; + recordSettings[AVNumberOfChannelsKey] = @2; + recordSettings[AVLinearPCMBitDepthKey] = @16; + recordSettings[AVLinearPCMIsBigEndianKey] = @NO; + recordSettings[AVLinearPCMIsFloatKey] = @NO; //set the export session's outputURL to /output.caf NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *documentsDirectory = [paths objectAtIndex:0]; + NSString *documentsDirectory = paths[0]; NSURL* outURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"output.caf"]]; [[NSFileManager defaultManager] removeItemAtURL:outURL error:nil]; NSLog(@"url loc is %@", outURL); diff --git a/Classes/TSLibraryImport.h b/Classes/TSLibraryImport.h old mode 100644 new mode 100755 diff --git a/Classes/TSLibraryImport.m b/Classes/TSLibraryImport.mm old mode 100644 new mode 100755 similarity index 98% rename from Classes/TSLibraryImport.m rename to Classes/TSLibraryImport.mm index 12b1063..b163f30 --- a/Classes/TSLibraryImport.m +++ b/Classes/TSLibraryImport.mm @@ -78,7 +78,7 @@ - (void)doMp3ImportToFile:(NSURL*)destURL completionBlock:(void (^)(TSLibraryImp OSStatus code = noErr; if ([e.name compare:TSUnknownError]) code = kTSUnknownError; else if ([e.name compare:TSFileExistsError]) code = kTSFileExistsError; - NSDictionary* errorDict = [NSDictionary dictionaryWithObject:e.reason forKey:NSLocalizedDescriptionKey]; + NSDictionary* errorDict = @{NSLocalizedDescriptionKey: e.reason}; movieFileErr = [[NSError alloc] initWithDomain:TSLibraryImportErrorDomain code:code userInfo:errorDict]; } diff --git a/Classes/echoprintAppDelegate.h b/Classes/echoprintAppDelegate.h old mode 100644 new mode 100755 diff --git a/Classes/echoprintAppDelegate.m b/Classes/echoprintAppDelegate.mm old mode 100644 new mode 100755 similarity index 100% rename from Classes/echoprintAppDelegate.m rename to Classes/echoprintAppDelegate.mm diff --git a/Classes/echoprintViewController.h b/Classes/echoprintViewController.h old mode 100644 new mode 100755 index 54582e4..6b1a1f4 --- a/Classes/echoprintViewController.h +++ b/Classes/echoprintViewController.h @@ -9,12 +9,13 @@ #import #import #import -#import -#import "ASIFormDataRequest.h" -#import "JSON.h" +#import #import "TSLibraryImport.h" #import "MicrophoneInput.h" +#import "FPGenerator.h" + + // developer.echonest.com #define API_KEY @"GET_YOUR_OWN" #define API_HOST @"developer.echonest.com" @@ -29,7 +30,7 @@ - (IBAction)pickSong:(id)sender; - (IBAction)startMicrophone:(id)sender; -- (void) getSong: (const char*) fpCode; +- (void) getSong: (NSString*) fpCode; @end diff --git a/Classes/echoprintViewController.m b/Classes/echoprintViewController.mm old mode 100644 new mode 100755 similarity index 64% rename from Classes/echoprintViewController.m rename to Classes/echoprintViewController.mm index 0b5cabf..654a458 --- a/Classes/echoprintViewController.m +++ b/Classes/echoprintViewController.mm @@ -7,9 +7,7 @@ // #import "echoprintViewController.h" -extern const char * GetPCMFromFile(char * filename); - - +#import "ASIHTTPRequest.h" @implementation echoprintViewController @@ -17,7 +15,7 @@ - (IBAction)pickSong:(id)sender { NSLog(@"Pick song"); MPMediaPickerController* mediaPicker = [[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic] autorelease]; mediaPicker.delegate = self; - [self presentModalViewController:mediaPicker animated:YES]; + [self presentViewController:mediaPicker animated:YES completion:nil]; } - (IBAction) startMicrophone:(id)sender { @@ -26,12 +24,12 @@ - (IBAction) startMicrophone:(id)sender { [recorder stopRecording]; [recordButton setTitle:@"Record" forState:UIControlStateNormal]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *documentsDirectory = [paths objectAtIndex:0]; + NSString *documentsDirectory = paths[0]; NSString *filePath =[documentsDirectory stringByAppendingPathComponent:@"output.caf"]; [statusLine setText:@"analysing..."]; [statusLine setNeedsDisplay]; [self.view setNeedsDisplay]; - const char * fpCode = GetPCMFromFile((char*) [filePath cStringUsingEncoding:NSASCIIStringEncoding]); + NSString* fpCode = [FPGenerator generateFingerprintForFile:filePath]; [self getSong:fpCode]; } else { [statusLine setText:@"recording..."]; @@ -48,13 +46,13 @@ - (IBAction) startMicrophone:(id)sender { - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection { - [self dismissModalViewControllerAnimated:YES]; + [self dismissViewControllerAnimated:YES completion:nil]; for (MPMediaItem* item in mediaItemCollection.items) { NSString* title = [item valueForProperty:MPMediaItemPropertyTitle]; NSURL* assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL]; NSLog(@"title: %@, url: %@", title, assetURL); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *documentsDirectory = [paths objectAtIndex:0]; + NSString *documentsDirectory = paths[0]; NSURL* destinationURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:@"temp_data"]]; [[NSFileManager defaultManager] removeItemAtURL:destinationURL error:nil]; @@ -65,7 +63,9 @@ - (void)mediaPicker:(MPMediaPickerController *)mediaPicker NSString *outPath = [documentsDirectory stringByAppendingPathComponent:@"temp_data"]; NSLog(@"done now. %@", outPath); [statusLine setText:@"analysing..."]; - const char * fpCode = GetPCMFromFile((char*) [outPath cStringUsingEncoding:NSASCIIStringEncoding]); + + NSString* fpCode = [FPGenerator generateFingerprintForFile:outPath]; + [statusLine setNeedsDisplay]; [self.view setNeedsDisplay]; [self getSong:fpCode]; @@ -75,21 +75,25 @@ - (void)mediaPicker:(MPMediaPickerController *)mediaPicker } -- (void) getSong: (const char*) fpCode { - NSLog(@"Done %s", fpCode); - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/api/v4/song/identify?api_key=%@&version=4.11&code=%s", API_HOST, API_KEY, fpCode]]; - ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url]; +- (void) getSong: (NSString*) fpCode { + NSLog(@"Done %@", fpCode); + + NSString *apiString = [NSString stringWithFormat:@"http://%@/api/v4/song/identify?api_key=%@&version=4.11&code=%@&format=json", API_HOST, API_KEY, fpCode]; + + NSURL *url = [NSURL URLWithString:apiString]; + + ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url]; [request setAllowCompressedResponse:NO]; [request startSynchronous]; NSError *error = [request error]; if (!error) { - NSString *response = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]; - NSDictionary *dictionary = [response JSONValue]; + NSString *response = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding]; + NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[response dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; NSLog(@"%@", dictionary); - NSArray *songList = [[dictionary objectForKey:@"response"] objectForKey:@"songs"]; + NSArray *songList = dictionary[@"response"][@"songs"]; if([songList count]>0) { - NSString * song_title = [[songList objectAtIndex:0] objectForKey:@"title"]; - NSString * artist_name = [[songList objectAtIndex:0] objectForKey:@"artist_name"]; + NSString * song_title = songList[0][@"title"]; + NSString * artist_name = songList[0][@"artist_name"]; [statusLine setText:[NSString stringWithFormat:@"%@ - %@", artist_name, song_title]]; } else { [statusLine setText:@"no match"]; @@ -105,26 +109,9 @@ - (void) getSong: (const char*) fpCode { - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker { - [self dismissModalViewControllerAnimated:YES]; + [self dismissViewControllerAnimated:YES completion:nil]; } -/* -// The designated initializer. Override to perform setup that is required before the view is loaded. -- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { - self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; - if (self) { - // Custom initialization - } - return self; -} -*/ - -/* -// Implement loadView to create a view hierarchy programmatically, without using a nib. -- (void)loadView { -} -*/ - // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { @@ -133,15 +120,6 @@ - (void)viewDidLoad { recording = NO; } - -/* -// Override to allow orientations other than the default portrait orientation. -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { - // Return YES for supported orientations - return (interfaceOrientation == UIInterfaceOrientationPortrait); -} -*/ - - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; diff --git a/Default-568h@2x.png b/Default-568h@2x.png new file mode 100644 index 0000000..0891b7a Binary files /dev/null and b/Default-568h@2x.png differ diff --git a/CADebugMacros.h b/Echoprint Headers/CADebugMacros.h similarity index 100% rename from CADebugMacros.h rename to Echoprint Headers/CADebugMacros.h diff --git a/CAStreamBasicDescription.h b/Echoprint Headers/CAStreamBasicDescription.h similarity index 99% rename from CAStreamBasicDescription.h rename to Echoprint Headers/CAStreamBasicDescription.h index dcbe051..60afb30 100755 --- a/CAStreamBasicDescription.h +++ b/Echoprint Headers/CAStreamBasicDescription.h @@ -88,7 +88,7 @@ typedef Float32 AudioUnitSampleType; // It adds a number of convenience routines, but otherwise adds nothing // to the footprint of the original struct. //============================================================================= -class CAStreamBasicDescription : +class CAStreamBasicDescription : public AudioStreamBasicDescription { diff --git a/Codegen.h b/Echoprint Headers/Codegen.h old mode 100644 new mode 100755 similarity index 81% rename from Codegen.h rename to Echoprint Headers/Codegen.h index f9ec409..015519a --- a/Codegen.h +++ b/Echoprint Headers/Codegen.h @@ -10,6 +10,7 @@ // Entry point for generating codes from PCM data. #define VERSION 4.10 +#include #include #include @@ -24,6 +25,8 @@ class Codegen { string getCodeString(){return _CodeString;} int getNumCodes(){return _NumCodes;} float getVersion() { return VERSION; } + + static NSString* encode(const float* pcm, unsigned int numSamples, int start_offset); private: string _CodeString; diff --git a/Codegen_wrapper.cpp b/Echoprint Headers/Codegen_wrapper.cpp old mode 100644 new mode 100755 similarity index 100% rename from Codegen_wrapper.cpp rename to Echoprint Headers/Codegen_wrapper.cpp diff --git a/Codegen_wrapper.h b/Echoprint Headers/Codegen_wrapper.h old mode 100644 new mode 100755 similarity index 100% rename from Codegen_wrapper.h rename to Echoprint Headers/Codegen_wrapper.h diff --git a/GetPCMFromFile.cpp b/Echoprint Headers/GetPCMFromFile.cpp old mode 100644 new mode 100755 similarity index 100% rename from GetPCMFromFile.cpp rename to Echoprint Headers/GetPCMFromFile.cpp diff --git a/GetPCMFromFile.h b/Echoprint Headers/GetPCMFromFile.h old mode 100644 new mode 100755 similarity index 100% rename from GetPCMFromFile.h rename to Echoprint Headers/GetPCMFromFile.h diff --git a/Echoprint.xcworkspace/contents.xcworkspacedata b/Echoprint.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..fa40a6d --- /dev/null +++ b/Echoprint.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,10 @@ + + + + + + + diff --git a/Echoprint.xcworkspace/xcshareddata/Echoprint.xccheckout b/Echoprint.xcworkspace/xcshareddata/Echoprint.xccheckout new file mode 100644 index 0000000..ecb678d --- /dev/null +++ b/Echoprint.xcworkspace/xcshareddata/Echoprint.xccheckout @@ -0,0 +1,41 @@ + + + + + IDESourceControlProjectFavoriteDictionaryKey + + IDESourceControlProjectIdentifier + B135386C-1F5E-4792-A1B3-BB29D51C9A0D + IDESourceControlProjectName + Echoprint + IDESourceControlProjectOriginsDictionary + + 61B95543-4B66-40A5-A3EF-475F20148F69 + ssh://github.com/rexstjohn/echoprint-ios-sample.git + + IDESourceControlProjectPath + Echoprint.xcworkspace + IDESourceControlProjectRelativeInstallPathDictionary + + 61B95543-4B66-40A5-A3EF-475F20148F69 + .. + + IDESourceControlProjectURL + ssh://github.com/rexstjohn/echoprint-ios-sample.git + IDESourceControlProjectVersion + 110 + IDESourceControlProjectWCCIdentifier + 61B95543-4B66-40A5-A3EF-475F20148F69 + IDESourceControlProjectWCConfigurations + + + IDESourceControlRepositoryExtensionIdentifierKey + public.vcs.git + IDESourceControlWCCIdentifierKey + 61B95543-4B66-40A5-A3EF-475F20148F69 + IDESourceControlWCCName + echoprint-ios-sample + + + + diff --git a/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/UserInterfaceState.xcuserstate b/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..fea6cfe Binary files /dev/null and b/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/WorkspaceSettings.xcsettings b/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..659c876 --- /dev/null +++ b/Echoprint.xcworkspace/xcuserdata/rexstjohn.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,10 @@ + + + + + HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges + + SnapshotAutomaticallyBeforeSignificantChanges + + + diff --git a/JSON/JSON.h b/JSON/JSON.h deleted file mode 100644 index 1e58c9a..0000000 --- a/JSON/JSON.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** - @mainpage A strict JSON parser and generator for Objective-C - - JSON (JavaScript Object Notation) is a lightweight data-interchange - format. This framework provides two apis for parsing and generating - JSON. One standard object-based and a higher level api consisting of - categories added to existing Objective-C classes. - - Learn more on the http://code.google.com/p/json-framework project site. - - This framework does its best to be as strict as possible, both in what it - accepts and what it generates. For example, it does not support trailing commas - in arrays or objects. Nor does it support embedded comments, or - anything else not in the JSON specification. This is considered a feature. - -*/ - -#import "SBJSON.h" -#import "NSObject+SBJSON.h" -#import "NSString+SBJSON.h" - diff --git a/JSON/NSObject+SBJSON.h b/JSON/NSObject+SBJSON.h deleted file mode 100644 index ecf0ee4..0000000 --- a/JSON/NSObject+SBJSON.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import - - -/** - @brief Adds JSON generation to Foundation classes - - This is a category on NSObject that adds methods for returning JSON representations - of standard objects to the objects themselves. This means you can call the - -JSONRepresentation method on an NSArray object and it'll do what you want. - */ -@interface NSObject (NSObject_SBJSON) - -/** - @brief Returns a string containing the receiver encoded as a JSON fragment. - - This method is added as a category on NSObject but is only actually - supported for the following objects: - @li NSDictionary - @li NSArray - @li NSString - @li NSNumber (also used for booleans) - @li NSNull - - @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (NSString *)JSONFragment; - -/** - @brief Returns a string containing the receiver encoded in JSON. - - This method is added as a category on NSObject but is only actually - supported for the following objects: - @li NSDictionary - @li NSArray - */ -- (NSString *)JSONRepresentation; - -@end - diff --git a/JSON/NSObject+SBJSON.m b/JSON/NSObject+SBJSON.m deleted file mode 100644 index 20b084b..0000000 --- a/JSON/NSObject+SBJSON.m +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "NSObject+SBJSON.h" -#import "SBJsonWriter.h" - -@implementation NSObject (NSObject_SBJSON) - -- (NSString *)JSONFragment { - SBJsonWriter *jsonWriter = [SBJsonWriter new]; - NSString *json = [jsonWriter stringWithFragment:self]; - if (!json) - NSLog(@"-JSONFragment failed. Error trace is: %@", [jsonWriter errorTrace]); - [jsonWriter release]; - return json; -} - -- (NSString *)JSONRepresentation { - SBJsonWriter *jsonWriter = [SBJsonWriter new]; - NSString *json = [jsonWriter stringWithObject:self]; - if (!json) - NSLog(@"-JSONRepresentation failed. Error trace is: %@", [jsonWriter errorTrace]); - [jsonWriter release]; - return json; -} - -@end diff --git a/JSON/NSString+SBJSON.h b/JSON/NSString+SBJSON.h deleted file mode 100644 index fad7179..0000000 --- a/JSON/NSString+SBJSON.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import - -/** - @brief Adds JSON parsing methods to NSString - -This is a category on NSString that adds methods for parsing the target string. -*/ -@interface NSString (NSString_SBJSON) - - -/** - @brief Returns the object represented in the receiver, or nil on error. - - Returns a a scalar object represented by the string's JSON fragment representation. - - @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (id)JSONFragmentValue; - -/** - @brief Returns the NSDictionary or NSArray represented by the current string's JSON representation. - - Returns the dictionary or array represented in the receiver, or nil on error. - - Returns the NSDictionary or NSArray represented by the current string's JSON representation. - */ -- (id)JSONValue; - -@end diff --git a/JSON/NSString+SBJSON.m b/JSON/NSString+SBJSON.m deleted file mode 100644 index 41a5a85..0000000 --- a/JSON/NSString+SBJSON.m +++ /dev/null @@ -1,55 +0,0 @@ -/* - Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "NSString+SBJSON.h" -#import "SBJsonParser.h" - -@implementation NSString (NSString_SBJSON) - -- (id)JSONFragmentValue -{ - SBJsonParser *jsonParser = [SBJsonParser new]; - id repr = [jsonParser fragmentWithString:self]; - if (!repr) - NSLog(@"-JSONFragmentValue failed. Error trace is: %@", [jsonParser errorTrace]); - [jsonParser release]; - return repr; -} - -- (id)JSONValue -{ - SBJsonParser *jsonParser = [SBJsonParser new]; - id repr = [jsonParser objectWithString:self]; - if (!repr) - NSLog(@"-JSONValue failed. Error trace is: %@", [jsonParser errorTrace]); - [jsonParser release]; - return repr; -} - -@end diff --git a/JSON/SBJSON.h b/JSON/SBJSON.h deleted file mode 100644 index 43d63c3..0000000 --- a/JSON/SBJSON.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import -#import "SBJsonParser.h" -#import "SBJsonWriter.h" - -/** - @brief Facade for SBJsonWriter/SBJsonParser. - - Requests are forwarded to instances of SBJsonWriter and SBJsonParser. - */ -@interface SBJSON : SBJsonBase { - -@private - SBJsonParser *jsonParser; - SBJsonWriter *jsonWriter; -} - - -/// Return the fragment represented by the given string -- (id)fragmentWithString:(NSString*)jsonrep - error:(NSError**)error; - -/// Return the object represented by the given string -- (id)objectWithString:(NSString*)jsonrep - error:(NSError**)error; - -/// Parse the string and return the represented object (or scalar) -- (id)objectWithString:(id)value - allowScalar:(BOOL)x - error:(NSError**)error; - - -/// Return JSON representation of an array or dictionary -- (NSString*)stringWithObject:(id)value - error:(NSError**)error; - -/// Return JSON representation of any legal JSON value -- (NSString*)stringWithFragment:(id)value - error:(NSError**)error; - -/// Return JSON representation (or fragment) for the given object -- (NSString*)stringWithObject:(id)value - allowScalar:(BOOL)x - error:(NSError**)error; - - -@end diff --git a/JSON/SBJSON.m b/JSON/SBJSON.m deleted file mode 100644 index 2a30f1a..0000000 --- a/JSON/SBJSON.m +++ /dev/null @@ -1,212 +0,0 @@ -/* - Copyright (C) 2007-2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "SBJSON.h" - -@implementation SBJSON - -- (id)init { - self = [super init]; - if (self) { - jsonWriter = [SBJsonWriter new]; - jsonParser = [SBJsonParser new]; - [self setMaxDepth:512]; - - } - return self; -} - -- (void)dealloc { - [jsonWriter release]; - [jsonParser release]; - [super dealloc]; -} - -#pragma mark Writer - - -- (NSString *)stringWithObject:(id)obj { - NSString *repr = [jsonWriter stringWithObject:obj]; - if (repr) - return repr; - - [errorTrace release]; - errorTrace = [[jsonWriter errorTrace] mutableCopy]; - return nil; -} - -/** - Returns a string containing JSON representation of the passed in value, or nil on error. - If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error. - - @param value any instance that can be represented as a JSON fragment - @param allowScalar wether to return json fragments for scalar objects - @param error used to return an error by reference (pass NULL if this is not desired) - -@deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (NSString*)stringWithObject:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error { - - NSString *json = allowScalar ? [jsonWriter stringWithFragment:value] : [jsonWriter stringWithObject:value]; - if (json) - return json; - - [errorTrace release]; - errorTrace = [[jsonWriter errorTrace] mutableCopy]; - - if (error) - *error = [errorTrace lastObject]; - return nil; -} - -/** - Returns a string containing JSON representation of the passed in value, or nil on error. - If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error. - - @param value any instance that can be represented as a JSON fragment - @param error used to return an error by reference (pass NULL if this is not desired) - - @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (NSString*)stringWithFragment:(id)value error:(NSError**)error { - return [self stringWithObject:value - allowScalar:YES - error:error]; -} - -/** - Returns a string containing JSON representation of the passed in value, or nil on error. - If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error. - - @param value a NSDictionary or NSArray instance - @param error used to return an error by reference (pass NULL if this is not desired) - */ -- (NSString*)stringWithObject:(id)value error:(NSError**)error { - return [self stringWithObject:value - allowScalar:NO - error:error]; -} - -#pragma mark Parsing - -- (id)objectWithString:(NSString *)repr { - id obj = [jsonParser objectWithString:repr]; - if (obj) - return obj; - - [errorTrace release]; - errorTrace = [[jsonParser errorTrace] mutableCopy]; - - return nil; -} - -/** - Returns the object represented by the passed-in string or nil on error. The returned object can be - a string, number, boolean, null, array or dictionary. - - @param value the json string to parse - @param allowScalar whether to return objects for JSON fragments - @param error used to return an error by reference (pass NULL if this is not desired) - - @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (id)objectWithString:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error { - - id obj = allowScalar ? [jsonParser fragmentWithString:value] : [jsonParser objectWithString:value]; - if (obj) - return obj; - - [errorTrace release]; - errorTrace = [[jsonParser errorTrace] mutableCopy]; - - if (error) - *error = [errorTrace lastObject]; - return nil; -} - -/** - Returns the object represented by the passed-in string or nil on error. The returned object can be - a string, number, boolean, null, array or dictionary. - - @param repr the json string to parse - @param error used to return an error by reference (pass NULL if this is not desired) - - @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. - */ -- (id)fragmentWithString:(NSString*)repr error:(NSError**)error { - return [self objectWithString:repr - allowScalar:YES - error:error]; -} - -/** - Returns the object represented by the passed-in string or nil on error. The returned object - will be either a dictionary or an array. - - @param repr the json string to parse - @param error used to return an error by reference (pass NULL if this is not desired) - */ -- (id)objectWithString:(NSString*)repr error:(NSError**)error { - return [self objectWithString:repr - allowScalar:NO - error:error]; -} - - - -#pragma mark Properties - parsing - -- (NSUInteger)maxDepth { - return jsonParser.maxDepth; -} - -- (void)setMaxDepth:(NSUInteger)d { - jsonWriter.maxDepth = jsonParser.maxDepth = d; -} - - -#pragma mark Properties - writing - -- (BOOL)humanReadable { - return jsonWriter.humanReadable; -} - -- (void)setHumanReadable:(BOOL)x { - jsonWriter.humanReadable = x; -} - -- (BOOL)sortKeys { - return jsonWriter.sortKeys; -} - -- (void)setSortKeys:(BOOL)x { - jsonWriter.sortKeys = x; -} - -@end diff --git a/JSON/SBJsonBase.h b/JSON/SBJsonBase.h deleted file mode 100644 index 7b10844..0000000 --- a/JSON/SBJsonBase.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import - -extern NSString * SBJSONErrorDomain; - - -enum { - EUNSUPPORTED = 1, - EPARSENUM, - EPARSE, - EFRAGMENT, - ECTRL, - EUNICODE, - EDEPTH, - EESCAPE, - ETRAILCOMMA, - ETRAILGARBAGE, - EEOF, - EINPUT -}; - -/** - @brief Common base class for parsing & writing. - - This class contains the common error-handling code and option between the parser/writer. - */ -@interface SBJsonBase : NSObject { - NSMutableArray *errorTrace; - -@protected - NSUInteger depth, maxDepth; -} - -/** - @brief The maximum recursing depth. - - Defaults to 512. If the input is nested deeper than this the input will be deemed to be - malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can - turn off this security feature by setting the maxDepth value to 0. - */ -@property NSUInteger maxDepth; - -/** - @brief Return an error trace, or nil if there was no errors. - - Note that this method returns the trace of the last method that failed. - You need to check the return value of the call you're making to figure out - if the call actually failed, before you know call this method. - */ - @property(copy,readonly) NSArray* errorTrace; - -/// @internal for use in subclasses to add errors to the stack trace -- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str; - -/// @internal for use in subclasess to clear the error before a new parsing attempt -- (void)clearErrorTrace; - -@end diff --git a/JSON/SBJsonBase.m b/JSON/SBJsonBase.m deleted file mode 100644 index 6684325..0000000 --- a/JSON/SBJsonBase.m +++ /dev/null @@ -1,78 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "SBJsonBase.h" -NSString * SBJSONErrorDomain = @"org.brautaset.JSON.ErrorDomain"; - - -@implementation SBJsonBase - -@synthesize errorTrace; -@synthesize maxDepth; - -- (id)init { - self = [super init]; - if (self) - self.maxDepth = 512; - return self; -} - -- (void)dealloc { - [errorTrace release]; - [super dealloc]; -} - -- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str { - NSDictionary *userInfo; - if (!errorTrace) { - errorTrace = [NSMutableArray new]; - userInfo = [NSDictionary dictionaryWithObject:str forKey:NSLocalizedDescriptionKey]; - - } else { - userInfo = [NSDictionary dictionaryWithObjectsAndKeys: - str, NSLocalizedDescriptionKey, - [errorTrace lastObject], NSUnderlyingErrorKey, - nil]; - } - - NSError *error = [NSError errorWithDomain:SBJSONErrorDomain code:code userInfo:userInfo]; - - [self willChangeValueForKey:@"errorTrace"]; - [errorTrace addObject:error]; - [self didChangeValueForKey:@"errorTrace"]; -} - -- (void)clearErrorTrace { - [self willChangeValueForKey:@"errorTrace"]; - [errorTrace release]; - errorTrace = nil; - [self didChangeValueForKey:@"errorTrace"]; -} - -@end diff --git a/JSON/SBJsonParser.h b/JSON/SBJsonParser.h deleted file mode 100644 index e95304d..0000000 --- a/JSON/SBJsonParser.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import -#import "SBJsonBase.h" - -/** - @brief Options for the parser class. - - This exists so the SBJSON facade can implement the options in the parser without having to re-declare them. - */ -@protocol SBJsonParser - -/** - @brief Return the object represented by the given string. - - Returns the object represented by the passed-in string or nil on error. The returned object can be - a string, number, boolean, null, array or dictionary. - - @param repr the json string to parse - */ -- (id)objectWithString:(NSString *)repr; - -@end - - -/** - @brief The JSON parser class. - - JSON is mapped to Objective-C types in the following way: - - @li Null -> NSNull - @li String -> NSMutableString - @li Array -> NSMutableArray - @li Object -> NSMutableDictionary - @li Boolean -> NSNumber (initialised with -initWithBool:) - @li Number -> NSDecimalNumber - - Since Objective-C doesn't have a dedicated class for boolean values, these turns into NSNumber - instances. These are initialised with the -initWithBool: method, and - round-trip back to JSON properly. (They won't silently suddenly become 0 or 1; they'll be - represented as 'true' and 'false' again.) - - JSON numbers turn into NSDecimalNumber instances, - as we can thus avoid any loss of precision. (JSON allows ridiculously large numbers.) - - */ -@interface SBJsonParser : SBJsonBase { - -@private - const char *c; -} - -@end - -// don't use - exists for backwards compatibility with 2.1.x only. Will be removed in 2.3. -@interface SBJsonParser (Private) -- (id)fragmentWithString:(id)repr; -@end - - diff --git a/JSON/SBJsonParser.m b/JSON/SBJsonParser.m deleted file mode 100644 index eda051a..0000000 --- a/JSON/SBJsonParser.m +++ /dev/null @@ -1,475 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "SBJsonParser.h" - -@interface SBJsonParser () - -- (BOOL)scanValue:(NSObject **)o; - -- (BOOL)scanRestOfArray:(NSMutableArray **)o; -- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o; -- (BOOL)scanRestOfNull:(NSNull **)o; -- (BOOL)scanRestOfFalse:(NSNumber **)o; -- (BOOL)scanRestOfTrue:(NSNumber **)o; -- (BOOL)scanRestOfString:(NSMutableString **)o; - -// Cannot manage without looking at the first digit -- (BOOL)scanNumber:(NSNumber **)o; - -- (BOOL)scanHexQuad:(unichar *)x; -- (BOOL)scanUnicodeChar:(unichar *)x; - -- (BOOL)scanIsAtEnd; - -@end - -#define skipWhitespace(c) while (isspace(*c)) c++ -#define skipDigits(c) while (isdigit(*c)) c++ - - -@implementation SBJsonParser - -static char ctrl[0x22]; - - -+ (void)initialize { - ctrl[0] = '\"'; - ctrl[1] = '\\'; - for (int i = 1; i < 0x20; i++) - ctrl[i+1] = i; - ctrl[0x21] = 0; -} - -/** - @deprecated This exists in order to provide fragment support in older APIs in one more version. - It should be removed in the next major version. - */ -- (id)fragmentWithString:(id)repr { - [self clearErrorTrace]; - - if (!repr) { - [self addErrorWithCode:EINPUT description:@"Input was 'nil'"]; - return nil; - } - - depth = 0; - c = [repr UTF8String]; - - id o; - if (![self scanValue:&o]) { - return nil; - } - - // We found some valid JSON. But did it also contain something else? - if (![self scanIsAtEnd]) { - [self addErrorWithCode:ETRAILGARBAGE description:@"Garbage after JSON"]; - return nil; - } - - NSAssert1(o, @"Should have a valid object from %@", repr); - return o; -} - -- (id)objectWithString:(NSString *)repr { - - id o = [self fragmentWithString:repr]; - if (!o) - return nil; - - // Check that the object we've found is a valid JSON container. - if (![o isKindOfClass:[NSDictionary class]] && ![o isKindOfClass:[NSArray class]]) { - [self addErrorWithCode:EFRAGMENT description:@"Valid fragment, but not JSON"]; - return nil; - } - - return o; -} - -/* - In contrast to the public methods, it is an error to omit the error parameter here. - */ -- (BOOL)scanValue:(NSObject **)o -{ - skipWhitespace(c); - - switch (*c++) { - case '{': - return [self scanRestOfDictionary:(NSMutableDictionary **)o]; - break; - case '[': - return [self scanRestOfArray:(NSMutableArray **)o]; - break; - case '"': - return [self scanRestOfString:(NSMutableString **)o]; - break; - case 'f': - return [self scanRestOfFalse:(NSNumber **)o]; - break; - case 't': - return [self scanRestOfTrue:(NSNumber **)o]; - break; - case 'n': - return [self scanRestOfNull:(NSNull **)o]; - break; - case '-': - case '0'...'9': - c--; // cannot verify number correctly without the first character - return [self scanNumber:(NSNumber **)o]; - break; - case '+': - [self addErrorWithCode:EPARSENUM description: @"Leading + disallowed in number"]; - return NO; - break; - case 0x0: - [self addErrorWithCode:EEOF description:@"Unexpected end of string"]; - return NO; - break; - default: - [self addErrorWithCode:EPARSE description: @"Unrecognised leading character"]; - return NO; - break; - } - - NSAssert(0, @"Should never get here"); - return NO; -} - -- (BOOL)scanRestOfTrue:(NSNumber **)o -{ - if (!strncmp(c, "rue", 3)) { - c += 3; - *o = [NSNumber numberWithBool:YES]; - return YES; - } - [self addErrorWithCode:EPARSE description:@"Expected 'true'"]; - return NO; -} - -- (BOOL)scanRestOfFalse:(NSNumber **)o -{ - if (!strncmp(c, "alse", 4)) { - c += 4; - *o = [NSNumber numberWithBool:NO]; - return YES; - } - [self addErrorWithCode:EPARSE description: @"Expected 'false'"]; - return NO; -} - -- (BOOL)scanRestOfNull:(NSNull **)o { - if (!strncmp(c, "ull", 3)) { - c += 3; - *o = [NSNull null]; - return YES; - } - [self addErrorWithCode:EPARSE description: @"Expected 'null'"]; - return NO; -} - -- (BOOL)scanRestOfArray:(NSMutableArray **)o { - if (maxDepth && ++depth > maxDepth) { - [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; - return NO; - } - - *o = [NSMutableArray arrayWithCapacity:8]; - - for (; *c ;) { - id v; - - skipWhitespace(c); - if (*c == ']' && c++) { - depth--; - return YES; - } - - if (![self scanValue:&v]) { - [self addErrorWithCode:EPARSE description:@"Expected value while parsing array"]; - return NO; - } - - [*o addObject:v]; - - skipWhitespace(c); - if (*c == ',' && c++) { - skipWhitespace(c); - if (*c == ']') { - [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in array"]; - return NO; - } - } - } - - [self addErrorWithCode:EEOF description: @"End of input while parsing array"]; - return NO; -} - -- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o -{ - if (maxDepth && ++depth > maxDepth) { - [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; - return NO; - } - - *o = [NSMutableDictionary dictionaryWithCapacity:7]; - - for (; *c ;) { - id k, v; - - skipWhitespace(c); - if (*c == '}' && c++) { - depth--; - return YES; - } - - if (!(*c == '\"' && c++ && [self scanRestOfString:&k])) { - [self addErrorWithCode:EPARSE description: @"Object key string expected"]; - return NO; - } - - skipWhitespace(c); - if (*c != ':') { - [self addErrorWithCode:EPARSE description: @"Expected ':' separating key and value"]; - return NO; - } - - c++; - if (![self scanValue:&v]) { - NSString *string = [NSString stringWithFormat:@"Object value expected for key: %@", k]; - [self addErrorWithCode:EPARSE description: string]; - return NO; - } - - [*o setObject:v forKey:k]; - - skipWhitespace(c); - if (*c == ',' && c++) { - skipWhitespace(c); - if (*c == '}') { - [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in object"]; - return NO; - } - } - } - - [self addErrorWithCode:EEOF description: @"End of input while parsing object"]; - return NO; -} - -- (BOOL)scanRestOfString:(NSMutableString **)o -{ - *o = [NSMutableString stringWithCapacity:16]; - do { - // First see if there's a portion we can grab in one go. - // Doing this caused a massive speedup on the long string. - size_t len = strcspn(c, ctrl); - if (len) { - // check for - id t = [[NSString alloc] initWithBytesNoCopy:(char*)c - length:len - encoding:NSUTF8StringEncoding - freeWhenDone:NO]; - if (t) { - [*o appendString:t]; - [t release]; - c += len; - } - } - - if (*c == '"') { - c++; - return YES; - - } else if (*c == '\\') { - unichar uc = *++c; - switch (uc) { - case '\\': - case '/': - case '"': - break; - - case 'b': uc = '\b'; break; - case 'n': uc = '\n'; break; - case 'r': uc = '\r'; break; - case 't': uc = '\t'; break; - case 'f': uc = '\f'; break; - - case 'u': - c++; - if (![self scanUnicodeChar:&uc]) { - [self addErrorWithCode:EUNICODE description: @"Broken unicode character"]; - return NO; - } - c--; // hack. - break; - default: - [self addErrorWithCode:EESCAPE description: [NSString stringWithFormat:@"Illegal escape sequence '0x%x'", uc]]; - return NO; - break; - } - CFStringAppendCharacters((CFMutableStringRef)*o, &uc, 1); - c++; - - } else if (*c < 0x20) { - [self addErrorWithCode:ECTRL description: [NSString stringWithFormat:@"Unescaped control character '0x%x'", *c]]; - return NO; - - } else { - NSLog(@"should not be able to get here"); - } - } while (*c); - - [self addErrorWithCode:EEOF description:@"Unexpected EOF while parsing string"]; - return NO; -} - -- (BOOL)scanUnicodeChar:(unichar *)x -{ - unichar hi, lo; - - if (![self scanHexQuad:&hi]) { - [self addErrorWithCode:EUNICODE description: @"Missing hex quad"]; - return NO; - } - - if (hi >= 0xd800) { // high surrogate char? - if (hi < 0xdc00) { // yes - expect a low char - - if (!(*c == '\\' && ++c && *c == 'u' && ++c && [self scanHexQuad:&lo])) { - [self addErrorWithCode:EUNICODE description: @"Missing low character in surrogate pair"]; - return NO; - } - - if (lo < 0xdc00 || lo >= 0xdfff) { - [self addErrorWithCode:EUNICODE description:@"Invalid low surrogate char"]; - return NO; - } - - hi = (hi - 0xd800) * 0x400 + (lo - 0xdc00) + 0x10000; - - } else if (hi < 0xe000) { - [self addErrorWithCode:EUNICODE description:@"Invalid high character in surrogate pair"]; - return NO; - } - } - - *x = hi; - return YES; -} - -- (BOOL)scanHexQuad:(unichar *)x -{ - *x = 0; - for (int i = 0; i < 4; i++) { - unichar uc = *c; - c++; - int d = (uc >= '0' && uc <= '9') - ? uc - '0' : (uc >= 'a' && uc <= 'f') - ? (uc - 'a' + 10) : (uc >= 'A' && uc <= 'F') - ? (uc - 'A' + 10) : -1; - if (d == -1) { - [self addErrorWithCode:EUNICODE description:@"Missing hex digit in quad"]; - return NO; - } - *x *= 16; - *x += d; - } - return YES; -} - -- (BOOL)scanNumber:(NSNumber **)o -{ - const char *ns = c; - - // The logic to test for validity of the number formatting is relicensed - // from JSON::XS with permission from its author Marc Lehmann. - // (Available at the CPAN: http://search.cpan.org/dist/JSON-XS/ .) - - if ('-' == *c) - c++; - - if ('0' == *c && c++) { - if (isdigit(*c)) { - [self addErrorWithCode:EPARSENUM description: @"Leading 0 disallowed in number"]; - return NO; - } - - } else if (!isdigit(*c) && c != ns) { - [self addErrorWithCode:EPARSENUM description: @"No digits after initial minus"]; - return NO; - - } else { - skipDigits(c); - } - - // Fractional part - if ('.' == *c && c++) { - - if (!isdigit(*c)) { - [self addErrorWithCode:EPARSENUM description: @"No digits after decimal point"]; - return NO; - } - skipDigits(c); - } - - // Exponential part - if ('e' == *c || 'E' == *c) { - c++; - - if ('-' == *c || '+' == *c) - c++; - - if (!isdigit(*c)) { - [self addErrorWithCode:EPARSENUM description: @"No digits after exponent"]; - return NO; - } - skipDigits(c); - } - - id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns - length:c - ns - encoding:NSUTF8StringEncoding - freeWhenDone:NO]; - [str autorelease]; - if (str && (*o = [NSDecimalNumber decimalNumberWithString:str])) - return YES; - - [self addErrorWithCode:EPARSENUM description: @"Failed creating decimal instance"]; - return NO; -} - -- (BOOL)scanIsAtEnd -{ - skipWhitespace(c); - return !*c; -} - - -@end diff --git a/JSON/SBJsonWriter.h b/JSON/SBJsonWriter.h deleted file mode 100644 index f6f5e17..0000000 --- a/JSON/SBJsonWriter.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import -#import "SBJsonBase.h" - -/** - @brief Options for the writer class. - - This exists so the SBJSON facade can implement the options in the writer without having to re-declare them. - */ -@protocol SBJsonWriter - -/** - @brief Whether we are generating human-readable (multiline) JSON. - - Set whether or not to generate human-readable JSON. The default is NO, which produces - JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable - JSON with linebreaks after each array value and dictionary key/value pair, indented two - spaces per nesting level. - */ -@property BOOL humanReadable; - -/** - @brief Whether or not to sort the dictionary keys in the output. - - If this is set to YES, the dictionary keys in the JSON output will be in sorted order. - (This is useful if you need to compare two structures, for example.) The default is NO. - */ -@property BOOL sortKeys; - -/** - @brief Return JSON representation (or fragment) for the given object. - - Returns a string containing JSON representation of the passed in value, or nil on error. - If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error. - - @param value any instance that can be represented as a JSON fragment - - */ -- (NSString*)stringWithObject:(id)value; - -@end - - -/** - @brief The JSON writer class. - - Objective-C types are mapped to JSON types in the following way: - - @li NSNull -> Null - @li NSString -> String - @li NSArray -> Array - @li NSDictionary -> Object - @li NSNumber (-initWithBool:) -> Boolean - @li NSNumber -> Number - - In JSON the keys of an object must be strings. NSDictionary keys need - not be, but attempting to convert an NSDictionary with non-string keys - into JSON will throw an exception. - - NSNumber instances created with the +initWithBool: method are - converted into the JSON boolean "true" and "false" values, and vice - versa. Any other NSNumber instances are converted to a JSON number the - way you would expect. - - */ -@interface SBJsonWriter : SBJsonBase { - -@private - BOOL sortKeys, humanReadable; -} - -@end - -// don't use - exists for backwards compatibility. Will be removed in 2.3. -@interface SBJsonWriter (Private) -- (NSString*)stringWithFragment:(id)value; -@end - -/** - @brief Allows generation of JSON for otherwise unsupported classes. - - If you have a custom class that you want to create a JSON representation for you can implement - this method in your class. It should return a representation of your object defined - in terms of objects that can be translated into JSON. For example, a Person - object might implement it like this: - - @code - - (id)jsonProxyObject { - return [NSDictionary dictionaryWithObjectsAndKeys: - name, @"name", - phone, @"phone", - email, @"email", - nil]; - } - @endcode - - */ -@interface NSObject (SBProxyForJson) -- (id)proxyForJson; -@end - diff --git a/JSON/SBJsonWriter.m b/JSON/SBJsonWriter.m deleted file mode 100644 index 0f32904..0000000 --- a/JSON/SBJsonWriter.m +++ /dev/null @@ -1,237 +0,0 @@ -/* - Copyright (C) 2009 Stig Brautaset. All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name of the author nor the names of its contributors may be used - to endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#import "SBJsonWriter.h" - -@interface SBJsonWriter () - -- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json; -- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json; -- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json; -- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json; - -- (NSString*)indent; - -@end - -@implementation SBJsonWriter - -static NSMutableCharacterSet *kEscapeChars; - -+ (void)initialize { - kEscapeChars = [[NSMutableCharacterSet characterSetWithRange: NSMakeRange(0,32)] retain]; - [kEscapeChars addCharactersInString: @"\"\\"]; -} - - -@synthesize sortKeys; -@synthesize humanReadable; - -/** - @deprecated This exists in order to provide fragment support in older APIs in one more version. - It should be removed in the next major version. - */ -- (NSString*)stringWithFragment:(id)value { - [self clearErrorTrace]; - depth = 0; - NSMutableString *json = [NSMutableString stringWithCapacity:128]; - - if ([self appendValue:value into:json]) - return json; - - return nil; -} - - -- (NSString*)stringWithObject:(id)value { - - if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) { - return [self stringWithFragment:value]; - } - - if ([value respondsToSelector:@selector(proxyForJson)]) { - NSString *tmp = [self stringWithObject:[value proxyForJson]]; - if (tmp) - return tmp; - } - - - [self clearErrorTrace]; - [self addErrorWithCode:EFRAGMENT description:@"Not valid type for JSON"]; - return nil; -} - - -- (NSString*)indent { - return [@"\n" stringByPaddingToLength:1 + 2 * depth withString:@" " startingAtIndex:0]; -} - -- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json { - if ([fragment isKindOfClass:[NSDictionary class]]) { - if (![self appendDictionary:fragment into:json]) - return NO; - - } else if ([fragment isKindOfClass:[NSArray class]]) { - if (![self appendArray:fragment into:json]) - return NO; - - } else if ([fragment isKindOfClass:[NSString class]]) { - if (![self appendString:fragment into:json]) - return NO; - - } else if ([fragment isKindOfClass:[NSNumber class]]) { - if ('c' == *[fragment objCType]) - [json appendString:[fragment boolValue] ? @"true" : @"false"]; - else - [json appendString:[fragment stringValue]]; - - } else if ([fragment isKindOfClass:[NSNull class]]) { - [json appendString:@"null"]; - } else if ([fragment respondsToSelector:@selector(proxyForJson)]) { - [self appendValue:[fragment proxyForJson] into:json]; - - } else { - [self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"JSON serialisation not supported for %@", [fragment class]]]; - return NO; - } - return YES; -} - -- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json { - if (maxDepth && ++depth > maxDepth) { - [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; - return NO; - } - [json appendString:@"["]; - - BOOL addComma = NO; - for (id value in fragment) { - if (addComma) - [json appendString:@","]; - else - addComma = YES; - - if ([self humanReadable]) - [json appendString:[self indent]]; - - if (![self appendValue:value into:json]) { - return NO; - } - } - - depth--; - if ([self humanReadable] && [fragment count]) - [json appendString:[self indent]]; - [json appendString:@"]"]; - return YES; -} - -- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json { - if (maxDepth && ++depth > maxDepth) { - [self addErrorWithCode:EDEPTH description: @"Nested too deep"]; - return NO; - } - [json appendString:@"{"]; - - NSString *colon = [self humanReadable] ? @" : " : @":"; - BOOL addComma = NO; - NSArray *keys = [fragment allKeys]; - if (self.sortKeys) - keys = [keys sortedArrayUsingSelector:@selector(compare:)]; - - for (id value in keys) { - if (addComma) - [json appendString:@","]; - else - addComma = YES; - - if ([self humanReadable]) - [json appendString:[self indent]]; - - if (![value isKindOfClass:[NSString class]]) { - [self addErrorWithCode:EUNSUPPORTED description: @"JSON object key must be string"]; - return NO; - } - - if (![self appendString:value into:json]) - return NO; - - [json appendString:colon]; - if (![self appendValue:[fragment objectForKey:value] into:json]) { - [self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"Unsupported value for key %@ in object", value]]; - return NO; - } - } - - depth--; - if ([self humanReadable] && [fragment count]) - [json appendString:[self indent]]; - [json appendString:@"}"]; - return YES; -} - -- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json { - - [json appendString:@"\""]; - - NSRange esc = [fragment rangeOfCharacterFromSet:kEscapeChars]; - if ( !esc.length ) { - // No special chars -- can just add the raw string: - [json appendString:fragment]; - - } else { - NSUInteger length = [fragment length]; - for (NSUInteger i = 0; i < length; i++) { - unichar uc = [fragment characterAtIndex:i]; - switch (uc) { - case '"': [json appendString:@"\\\""]; break; - case '\\': [json appendString:@"\\\\"]; break; - case '\t': [json appendString:@"\\t"]; break; - case '\n': [json appendString:@"\\n"]; break; - case '\r': [json appendString:@"\\r"]; break; - case '\b': [json appendString:@"\\b"]; break; - case '\f': [json appendString:@"\\f"]; break; - default: - if (uc < 0x20) { - [json appendFormat:@"\\u%04x", uc]; - } else { - CFStringAppendCharacters((CFMutableStringRef)json, &uc, 1); - } - break; - - } - } - } - - [json appendString:@"\""]; - return YES; -} - - -@end diff --git a/MainWindow.xib b/MainWindow.xib old mode 100644 new mode 100755 diff --git a/Podfile b/Podfile new file mode 100644 index 0000000..9a2dc2c --- /dev/null +++ b/Podfile @@ -0,0 +1,2 @@ +platform :ios, '7.0' +pod 'ASIHTTPRequest', '= 1.8.1' diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 88292e4..7c43273 --- a/README.md +++ b/README.md @@ -1,6 +1,38 @@ -# echoprint-ios-sample +***** UPDATE ***** -Very brief sample of how to use echoprint in an iOS app. This will choose a file from your library, compute the code with Echoprint and send it to the Echo Nest API for matching. +March 10th, 2014 + +Turns out many of the .h headers were not set to C++ and we needed to link against the C++ dylib. I have successfully used this to both select a song from my library and record an audio segment and correctly identify it now. + +(March 6th, 2014) + +I just merged a pull request with a fix to the issues people were having previously, hopefully these are now resolved. + There is a thread on Google Groups: https://groups.google.com/forum/#!topic/echoprint/Zi_ip49Xgds. + + +OVERVIEW + +This is an upgraded version of the Echoprint Sample iOS Project. + +- Works in XCode 5.0 + iOS 7 +- Modernized Objective-C +- Modernized UIKit and other legacy project files +- Updated ASIHTTP to more current version +- Added Cocoapods +- Builds in the echoprint-codegen-ios library as a sub-project +- Added project dependency on lbstdc++.6.0.9.dylib that didn't seem to be required before + +INSTALL / SETUP INSTRUCTIONS + +- Sign up for an Echoprint API key on developer.echoprint.com and replace "API_KEY" in "echoprintViewController" with yours +- The project requires a depdendency on libechoprint-codegen-ios.a and thus needs to build the sub-project (see below) +- Run pod install to setup the pod files in the project root +- Download Boost 1.5+ and unzip it someplace useful like /Users/YOUR_USER_GOES_HERE_REPLACE_THIS/Development/boost +- Open the project workspace created by Cocoapods (not the main project) +- In "echoprint-codegen-ios.xcconfig" in the "echoprint-codegen-ios" sub-project, make sure this line points to your boost directory: + +HEADER_SEARCH_PATHS = /Users/YOUR_USER_GOES_HERE_REPLACE_THIS/Development/boost + +It should build for you. -You will need to have compiled libechoprint-codegen-ios.a, which you can compile using the Xcode project in echoprint-codegen. diff --git a/echoprint-Info.plist b/echoprint-Info.plist old mode 100644 new mode 100755 diff --git a/echoprint-codegen-master/._AUTHORS b/echoprint-codegen-master/._AUTHORS new file mode 100644 index 0000000..fdbb6a1 Binary files /dev/null and b/echoprint-codegen-master/._AUTHORS differ diff --git a/echoprint-codegen-master/._LICENSE b/echoprint-codegen-master/._LICENSE new file mode 100644 index 0000000..fdbb6a1 Binary files /dev/null and b/echoprint-codegen-master/._LICENSE differ diff --git a/echoprint-codegen-master/._README.md b/echoprint-codegen-master/._README.md new file mode 100644 index 0000000..fdbb6a1 Binary files /dev/null and b/echoprint-codegen-master/._README.md differ diff --git a/echoprint-codegen-master/._echoprint-codegen b/echoprint-codegen-master/._echoprint-codegen new file mode 100644 index 0000000..fdbb6a1 Binary files /dev/null and b/echoprint-codegen-master/._echoprint-codegen differ diff --git a/echoprint-codegen-master/._examples b/echoprint-codegen-master/._examples new file mode 100644 index 0000000..eb84ce7 Binary files /dev/null and b/echoprint-codegen-master/._examples differ diff --git a/echoprint-codegen-master/._src b/echoprint-codegen-master/._src new file mode 100644 index 0000000..eb84ce7 Binary files /dev/null and b/echoprint-codegen-master/._src differ diff --git a/echoprint-codegen-master/._windows b/echoprint-codegen-master/._windows new file mode 100644 index 0000000..eb84ce7 Binary files /dev/null and b/echoprint-codegen-master/._windows differ diff --git a/echoprint-codegen-master/AUTHORS b/echoprint-codegen-master/AUTHORS new file mode 100755 index 0000000..250aaf6 --- /dev/null +++ b/echoprint-codegen-master/AUTHORS @@ -0,0 +1,26 @@ +See the LICENSE file for important license information. + +Whitening, SubbandAnalysis, Fingerprint +Dan Ellis +Brian Whitman + +AudioBufferInput, AudioStreamInput, Codegen, Common, File, MatrixUtility, Metadata +Tristan Jehan +Paul Lamere +Jason Sundram +Brian Whitman + +Murmurhash2 +Austin Appleby + +Base64 +Rene Nyffenegger + +Contributors +Alastair Porter +efsavage +alsuren +artgillespie +yhorng +divan + diff --git a/echoprint-codegen-master/LICENSE b/echoprint-codegen-master/LICENSE new file mode 100755 index 0000000..ce44614 --- /dev/null +++ b/echoprint-codegen-master/LICENSE @@ -0,0 +1,44 @@ +echoprint-codegen is open source software licensed under the "MIT License" +More information about the MIT License: http://en.wikipedia.org/wiki/MIT_License + +Copyright (c) 2011 The Echo Nest Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +libcodegen makes use of the following pieces of software: + +- Murmurhash by Austin Appleby (Public Domain / MIT) +http://sites.google.com/site/murmurhash/ + +- Boost (Boost Software License) +http://www.boost.org/users/license.html + +- Base64.cpp and Base64.h, see source files for license +Copyright (C) 2004-2008 René Nyffenegger + + +codegen (the example binary that the makefile also builds) makes use of libcodegen and: + +- Taglib (LGPL) +http://developer.kde.org/~wheeler/taglib.html + +- ffmpeg (via system shell, you must install ffmpeg on your own) +http://www.ffmpeg.org/legal.html + diff --git a/echoprint-codegen-master/README.md b/echoprint-codegen-master/README.md new file mode 100755 index 0000000..97c721e --- /dev/null +++ b/echoprint-codegen-master/README.md @@ -0,0 +1,99 @@ +# Codegen for Echoprint + +Echoprint is an open source music fingerprint and resolving framework powered by the [The Echo Nest](http://the.echonest.com/ "The Echo Nest"). The [code generator](http://github.com/echonest/echoprint-codegen "echoprint-codegen") (library to convert PCM samples from a microphone or file into Echoprint codes) is open source (MIT licensed) and free for any use. The [server component](http://github.com/echonest/echoprint-server "echoprint-server") that stores and resolves queries is open source (Apache 2 licensed) and free for any use. The [data for resolving to millions of songs](http://echoprint.me/data "Echoprint Data") is free for any use provided any changes or additions are merged back to the community. + +[Read more about Echoprint here](http://echoprint.me) + +There are two modes of operation of the Echoprint codegen: + +1. the codegen library (libcodegen) is meant to be linked into code that passes it a buffer of PCM data and will output a code string. + +2. the codegen binary runs standalone, accepts filenames as inputs and runs in a multithreaded worker mode. + +## Requirements + +### For libcodegen + +* Boost >= 1.35 +* zlib + +### Additional requirements for the codegen binary + +* [TagLib](http://developer.kde.org/~wheeler/taglib.html "TagLib") +* ffmpeg - this is called via shell and is not linked into codegen + +On Ubuntu or Debian you can install these dependencies with: + + sudo apt-get install ffmpeg libboost1.42-dev libtag1-dev zlib1g-dev +On OS-X with [homebrew](http://mxcl.github.io/homebrew/) you can use: + + brew install ffmpeg boost taglib + +On Windows: + +Refer to the documentation under the windows folder for more specifics. + +The binary generated in Windows will be named codegen by default where as on Linux or Mac OS-X it is named echoprint-codegen. + +## Notes about libcodegen: + +Code generation takes a buffer of floating point PCM data sampled at 11025 Hz and mono. + + Codegen * pCodegen = new Codegen(const float* pcm, uint numSamples, int start_offset); + + pcm: a buffer of floats, mono, 11025 Hz + numSamples: the number of samples + start_offset: creates a hint to the server on where the sample is taken from in the original file if known + + string code = pCodegen->getCodeString(); + +The code string is just a base64 encoding of a zlib compression of the original code string, which is a hex encoded series of ASCII numbers. See API/fp.py in echoprint-server for decoding help. + +You only need to query for 20 seconds of audio to get a result. + +## Notes about the codegen binary + +The makefile builds an example code generator that uses libcodegen, called "codegen." This code generator has more features -- it will output ID3 tag information and uses ffmpeg to decode any type of file. If you don't need to compile libcodegen into your app you can rely on this. Note that you need to have ffmpeg installed and accessible on your path for this to work. + + ./echoprint-codegen billie_jean.mp3 10 30 + +Will take 30 seconds of audio from 10 seconds into the file and output JSON suitable for querying: + + {"metadata":{"artist":"Michael jackson", "release":"800 chansons des annes 80", "title":"Billie jean", "genre":"", "bitrate":192, "sample_rate":44100, "seconds":294, "filename":"billie_jean.mp3", "samples_decoded":220598, "given_duration":30, "start_offset":10, "version":4.00}, "code_count":846, "code":"JxVlIuNwzAMQ1fxCDL133+xo1rnGqNAEcWy/ERa2aKeZmW... + +You can POST this JSON directly to the Echo Nest's [song/identify](http://developer.echonest.com/docs/v4/song.html#identify "song/identify") (who has an Echoprint server booted), for example: + + curl -F "query=@post_string" http://developer.echonest.com/api/v4/song/identify?api_key=YOUR_KEY + {"fp_lookup_time_ms": 21, "results": [{"songID": "SOAFVGQ1280ED4E371", "match_type": "fp", "title": "Billie Jean", "artist": "Michael Jackson", "artistID": "ARXPPEY1187FB51DF4", "score": 63, "release": "Thriller"}] + (you can also use GET, see the API description) + +Or you can host your own [Echoprint server](http://github.com/echonest/echoprint-server "echoprint-server") and ingest or query to that. + +Codegen also runs in a multithreaded mode for bulk resolving: + + ./echoprint-codegen -s 10 30 < file_list + +Will compute codes for every file in file_list for 30 seconds starting at 10 seconds. (It tries to be smart about the number of threads to use.) It will output a JSON list. Note that song/identify can accept lists in the JSON, which will be faster than sending each code one at a time. The "tag" parameter is added to each code dictionary to match the resolving material. + +## Statistics + +### Speed + +Codegen scans audio at roughly 250x real time per processor after decoding and resampling to 11025 Hz. This means a full song can be scanned in less than 0.5s on an average computer, and an amount of audio suitable for querying (30s) can be scanned in less than 0.04s. + +Decoding from MP3 will be the bottleneck for most implementations. Decoders like mpg123 or ffmpeg can decode 30s mp3 audio to 11025 PCM in under 0.10s. + + clump:echoprint-codegen bwhitman$ time mpg123 -q -s -4 -n 1200 song.mp3 > /dev/null + real 0m0.079s + user 0m0.067s + sys 0m0.007s + +### Accuracy + +Look at http://echoprint.me for information on the accuracy of the echoprint system. + +## FAQ + +Q: I get "Couldn't decode any samples with: ffmpeg" when running codegen + +A: When running the example code generator (echoprint-codegen) make sure ffmpeg is accessible to your path. Try running ffmpeg filename.mp3 on the file you are testing the code generator with. If it doesn't work, codegen won't work. diff --git a/echoprint-codegen-master/echoprint-codegen b/echoprint-codegen-master/echoprint-codegen new file mode 100755 index 0000000..093f5fd Binary files /dev/null and b/echoprint-codegen-master/echoprint-codegen differ diff --git a/echoprint-codegen-master/examples/._lookup.py b/echoprint-codegen-master/examples/._lookup.py new file mode 100644 index 0000000..fdbb6a1 Binary files /dev/null and b/echoprint-codegen-master/examples/._lookup.py differ diff --git a/echoprint-codegen-master/examples/lookup.py b/echoprint-codegen-master/examples/lookup.py new file mode 100755 index 0000000..d325dc0 --- /dev/null +++ b/echoprint-codegen-master/examples/lookup.py @@ -0,0 +1,39 @@ +#!/usr/bin/python + +# This script takes an audio file and performs an echoprint lookup on it. +# Requirements: pyechonest >= 4.2.15 http://code.google.com/p/pyechonest/ +# The echoprint-codegen binary (run make from ../src) +# an Echo Nest API key + +import sys +import os + +import pyechonest.config as config +import pyechonest.song as song + +config.CODEGEN_BINARY_OVERRIDE = os.path.abspath("../echoprint-codegen") + +# Put your API key in a shell variable ECHO_NEST_API_KEY, or put it here +# config.ECHO_NEST_API_KEY='KEY HERE' + +def lookup(file): + # Note that song.identify reads just the first 30 seconds of the file + fp = song.util.codegen(file) + if len(fp) and "code" in fp[0]: + # The version parameter to song/identify indicates the use of echoprint + result = song.identify(query_obj=fp, version="4.11") + print "Got result:", result + if len(result): + print "Artist: %s (%s)" % (result[0].artist_name, result[0].artist_id) + print "Song: %s (%s)" % (result[0].title, result[0].id) + else: + print "No match. This track may not be in the database yet." + else: + print "Couldn't decode", file + + +if __name__ == "__main__": + if len(sys.argv) < 2: + print >>sys.stderr, "Usage: %s