rejigger things to do callbacks and end-of-stream read properly
This commit is contained in:
parent
d434de241f
commit
c7f6fb09bf
2 changed files with 383 additions and 387 deletions
|
|
@ -42,10 +42,10 @@ extern NSString * const AFNetworkingReachabilityDidChangeNotification;
|
||||||
*/
|
*/
|
||||||
#ifdef _SYSTEMCONFIGURATION_H
|
#ifdef _SYSTEMCONFIGURATION_H
|
||||||
typedef enum {
|
typedef enum {
|
||||||
AFNetworkReachabilityStatusUnknown = -1,
|
AFNetworkReachabilityStatusUnknown = -1,
|
||||||
AFNetworkReachabilityStatusNotReachable = 0,
|
AFNetworkReachabilityStatusNotReachable = 0,
|
||||||
AFNetworkReachabilityStatusReachableViaWWAN = 1,
|
AFNetworkReachabilityStatusReachableViaWWAN = 1,
|
||||||
AFNetworkReachabilityStatusReachableViaWiFi = 2,
|
AFNetworkReachabilityStatusReachableViaWiFi = 2,
|
||||||
} AFNetworkReachabilityStatus;
|
} AFNetworkReachabilityStatus;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -53,9 +53,9 @@ typedef enum {
|
||||||
Specifies the method used to encode parameters into request body.
|
Specifies the method used to encode parameters into request body.
|
||||||
*/
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
AFFormURLParameterEncoding,
|
AFFormURLParameterEncoding,
|
||||||
AFJSONParameterEncoding,
|
AFJSONParameterEncoding,
|
||||||
AFPropertyListParameterEncoding,
|
AFPropertyListParameterEncoding,
|
||||||
} AFHTTPClientParameterEncoding;
|
} AFHTTPClientParameterEncoding;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -116,13 +116,13 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
|
||||||
|
|
||||||
Both `requestWithMethod:path:parameters` and `multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:` construct URLs from the path relative to the `baseURL`, using `NSURL +URLWithString:relativeToURL:`. Below are a few examples of how `baseURL` and relative paths interract:
|
Both `requestWithMethod:path:parameters` and `multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock:` construct URLs from the path relative to the `baseURL`, using `NSURL +URLWithString:relativeToURL:`. Below are a few examples of how `baseURL` and relative paths interract:
|
||||||
|
|
||||||
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
|
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/v1/"];
|
||||||
[NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
|
[NSURL URLWithString:@"foo" relativeToURL:baseURL]; // http://example.com/v1/foo
|
||||||
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
|
[NSURL URLWithString:@"foo?bar=baz" relativeToURL:baseURL]; // http://example.com/v1/foo?bar=baz
|
||||||
[NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
|
[NSURL URLWithString:@"/foo" relativeToURL:baseURL]; // http://example.com/foo
|
||||||
[NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
|
[NSURL URLWithString:@"foo/" relativeToURL:baseURL]; // http://example.com/v1/foo
|
||||||
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
|
[NSURL URLWithString:@"/foo/" relativeToURL:baseURL]; // http://example.com/foo/
|
||||||
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
|
[NSURL URLWithString:@"http://example2.com/" relativeToURL:baseURL]; // http://example2.com/
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@interface AFHTTPClient : NSObject
|
@interface AFHTTPClient : NSObject
|
||||||
|
|
@ -156,7 +156,7 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
|
||||||
/**
|
/**
|
||||||
The reachability status from the device to the current `baseURL` of the `AFHTTPClient`.
|
The reachability status from the device to the current `baseURL` of the `AFHTTPClient`.
|
||||||
|
|
||||||
@warning This property requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import <SystemConfiguration/SystemConfiguration.h>` to the header prefix of the project (Prefix.pch).
|
@warning This property requires the `SystemConfiguration` framework. Add it in the active target's "Link Binary With Library" build phase, and add `#import <SystemConfiguration/SystemConfiguration.h>` to the header prefix of the project (Prefix.pch).
|
||||||
*/
|
*/
|
||||||
#ifdef _SYSTEMCONFIGURATION_H
|
#ifdef _SYSTEMCONFIGURATION_H
|
||||||
@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
|
@property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
|
||||||
|
|
|
||||||
|
|
@ -80,36 +80,36 @@ typedef void (^AFCompletionBlock)(void);
|
||||||
static NSUInteger const kAFHTTPClientDefaultMaxConcurrentOperationCount = 4;
|
static NSUInteger const kAFHTTPClientDefaultMaxConcurrentOperationCount = 4;
|
||||||
|
|
||||||
static NSString * AFBase64EncodedStringFromString(NSString *string) {
|
static NSString * AFBase64EncodedStringFromString(NSString *string) {
|
||||||
NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
|
NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
|
||||||
NSUInteger length = [data length];
|
NSUInteger length = [data length];
|
||||||
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
|
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
|
||||||
|
|
||||||
uint8_t *input = (uint8_t *)[data bytes];
|
uint8_t *input = (uint8_t *)[data bytes];
|
||||||
uint8_t *output = (uint8_t *)[mutableData mutableBytes];
|
uint8_t *output = (uint8_t *)[mutableData mutableBytes];
|
||||||
|
|
||||||
for (NSUInteger i = 0; i < length; i += 3) {
|
for (NSUInteger i = 0; i < length; i += 3) {
|
||||||
NSUInteger value = 0;
|
NSUInteger value = 0;
|
||||||
for (NSUInteger j = i; j < (i + 3); j++) {
|
for (NSUInteger j = i; j < (i + 3); j++) {
|
||||||
value <<= 8;
|
value <<= 8;
|
||||||
if (j < length) {
|
if (j < length) {
|
||||||
value |= (0xFF & input[j]);
|
value |= (0xFF & input[j]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
|
NSUInteger idx = (i / 3) * 4;
|
||||||
|
output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
|
||||||
|
output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
|
||||||
|
output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
|
||||||
|
output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
return [[[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding] autorelease];
|
||||||
|
|
||||||
NSUInteger idx = (i / 3) * 4;
|
|
||||||
output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
|
|
||||||
output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
|
|
||||||
output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
|
|
||||||
output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
|
|
||||||
}
|
|
||||||
|
|
||||||
return [[[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding] autorelease];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
|
NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
|
||||||
static NSString * const kAFLegalCharactersToBeEscaped = @"?!@#$^&%*+=,:;'\"`<>()[]{}/\\|~ ";
|
static NSString * const kAFLegalCharactersToBeEscaped = @"?!@#$^&%*+=,:;'\"`<>()[]{}/\\|~ ";
|
||||||
|
|
||||||
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, (CFStringRef)kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];
|
return [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, (CFStringRef)kAFLegalCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding)) autorelease];
|
||||||
}
|
}
|
||||||
|
|
@ -118,8 +118,8 @@ NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEn
|
||||||
|
|
||||||
@interface AFQueryStringComponent : NSObject {
|
@interface AFQueryStringComponent : NSObject {
|
||||||
@private
|
@private
|
||||||
NSString *_key;
|
NSString *_key;
|
||||||
NSString *_value;
|
NSString *_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@property (readwrite, nonatomic, retain) id key;
|
@property (readwrite, nonatomic, retain) id key;
|
||||||
|
|
@ -135,25 +135,25 @@ NSString * AFURLEncodedStringFromStringWithEncoding(NSString *string, NSStringEn
|
||||||
@synthesize value = _value;
|
@synthesize value = _value;
|
||||||
|
|
||||||
- (id)initWithKey:(id)key value:(id)value {
|
- (id)initWithKey:(id)key value:(id)value {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.key = key;
|
self.key = key;
|
||||||
self.value = value;
|
self.value = value;
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc {
|
- (void)dealloc {
|
||||||
[_key release];
|
[_key release];
|
||||||
[_value release];
|
[_value release];
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding {
|
- (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding {
|
||||||
return [NSString stringWithFormat:@"%@=%@", self.key, AFURLEncodedStringFromStringWithEncoding([self.value description], stringEncoding)];
|
return [NSString stringWithFormat:@"%@=%@", self.key, AFURLEncodedStringFromStringWithEncoding([self.value description], stringEncoding)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
@ -165,69 +165,69 @@ extern NSArray * AFQueryStringComponentsFromKeyAndDictionaryValue(NSString *key,
|
||||||
extern NSArray * AFQueryStringComponentsFromKeyAndArrayValue(NSString *key, NSArray *value);
|
extern NSArray * AFQueryStringComponentsFromKeyAndArrayValue(NSString *key, NSArray *value);
|
||||||
|
|
||||||
NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) {
|
NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) {
|
||||||
NSMutableArray *mutableComponents = [NSMutableArray array];
|
NSMutableArray *mutableComponents = [NSMutableArray array];
|
||||||
for (AFQueryStringComponent *component in AFQueryStringComponentsFromKeyAndValue(nil, parameters)) {
|
for (AFQueryStringComponent *component in AFQueryStringComponentsFromKeyAndValue(nil, parameters)) {
|
||||||
[mutableComponents addObject:[component URLEncodedStringValueWithEncoding:stringEncoding]];
|
[mutableComponents addObject:[component URLEncodedStringValueWithEncoding:stringEncoding]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [mutableComponents componentsJoinedByString:@"&"];
|
return [mutableComponents componentsJoinedByString:@"&"];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSArray * AFQueryStringComponentsFromKeyAndValue(NSString *key, id value) {
|
NSArray * AFQueryStringComponentsFromKeyAndValue(NSString *key, id value) {
|
||||||
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
||||||
|
|
||||||
if([value isKindOfClass:[NSDictionary class]]) {
|
if([value isKindOfClass:[NSDictionary class]]) {
|
||||||
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndDictionaryValue(key, value)];
|
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndDictionaryValue(key, value)];
|
||||||
} else if([value isKindOfClass:[NSArray class]]) {
|
} else if([value isKindOfClass:[NSArray class]]) {
|
||||||
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndArrayValue(key, value)];
|
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndArrayValue(key, value)];
|
||||||
} else {
|
} else {
|
||||||
[mutableQueryStringComponents addObject:[[[AFQueryStringComponent alloc] initWithKey:key value:value] autorelease]];
|
[mutableQueryStringComponents addObject:[[[AFQueryStringComponent alloc] initWithKey:key value:value] autorelease]];
|
||||||
}
|
}
|
||||||
|
|
||||||
return mutableQueryStringComponents;
|
return mutableQueryStringComponents;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSArray * AFQueryStringComponentsFromKeyAndDictionaryValue(NSString *key, NSDictionary *value){
|
NSArray * AFQueryStringComponentsFromKeyAndDictionaryValue(NSString *key, NSDictionary *value){
|
||||||
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
||||||
|
|
||||||
[value enumerateKeysAndObjectsUsingBlock:^(id nestedKey, id nestedValue, BOOL *stop) {
|
[value enumerateKeysAndObjectsUsingBlock:^(id nestedKey, id nestedValue, BOOL *stop) {
|
||||||
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
|
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
return mutableQueryStringComponents;
|
return mutableQueryStringComponents;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSArray * AFQueryStringComponentsFromKeyAndArrayValue(NSString *key, NSArray *value) {
|
NSArray * AFQueryStringComponentsFromKeyAndArrayValue(NSString *key, NSArray *value) {
|
||||||
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
|
||||||
|
|
||||||
[value enumerateObjectsUsingBlock:^(id nestedValue, NSUInteger idx, BOOL *stop) {
|
[value enumerateObjectsUsingBlock:^(id nestedValue, NSUInteger idx, BOOL *stop) {
|
||||||
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
|
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringComponentsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
return mutableQueryStringComponents;
|
return mutableQueryStringComponents;
|
||||||
}
|
}
|
||||||
|
|
||||||
static NSString * AFJSONStringFromParameters(NSDictionary *parameters) {
|
static NSString * AFJSONStringFromParameters(NSDictionary *parameters) {
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
NSData *JSONData = AFJSONEncode(parameters, &error);
|
NSData *JSONData = AFJSONEncode(parameters, &error);
|
||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
return [[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding] autorelease];
|
return [[[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding] autorelease];
|
||||||
} else {
|
} else {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
||||||
NSString *propertyListString = nil;
|
NSString *propertyListString = nil;
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
|
|
||||||
NSData *propertyListData = [NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
|
NSData *propertyListData = [NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
|
||||||
if (!error) {
|
if (!error) {
|
||||||
propertyListString = [[[NSString alloc] initWithData:propertyListData encoding:NSUTF8StringEncoding] autorelease];
|
propertyListString = [[[NSString alloc] initWithData:propertyListData encoding:NSUTF8StringEncoding] autorelease];
|
||||||
}
|
}
|
||||||
|
|
||||||
return propertyListString;
|
return propertyListString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@interface AFHTTPClient ()
|
@interface AFHTTPClient ()
|
||||||
|
|
@ -261,21 +261,21 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
+ (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
|
+ (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
|
||||||
return [[[self alloc] initWithBaseURL:url] autorelease];
|
return [[[self alloc] initWithBaseURL:url] autorelease];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id)initWithBaseURL:(NSURL *)url {
|
- (id)initWithBaseURL:(NSURL *)url {
|
||||||
self = [super init];
|
self = [super init];
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.baseURL = url;
|
self.baseURL = url;
|
||||||
|
|
||||||
self.stringEncoding = NSUTF8StringEncoding;
|
self.stringEncoding = NSUTF8StringEncoding;
|
||||||
self.parameterEncoding = AFFormURLParameterEncoding;
|
self.parameterEncoding = AFFormURLParameterEncoding;
|
||||||
|
|
||||||
self.registeredHTTPOperationClassNames = [NSMutableArray array];
|
self.registeredHTTPOperationClassNames = [NSMutableArray array];
|
||||||
|
|
||||||
self.defaultHeaders = [NSMutableDictionary dictionary];
|
self.defaultHeaders = [NSMutableDictionary dictionary];
|
||||||
|
|
||||||
|
|
@ -287,151 +287,151 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) {
|
||||||
[self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]];
|
[self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]];
|
||||||
|
|
||||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||||
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
|
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
|
||||||
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@, %@ %@, %@, Scale/%f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], @"unknown", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion], [[UIDevice currentDevice] model], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0)]];
|
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@, %@ %@, %@, Scale/%f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], @"unknown", [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion], [[UIDevice currentDevice] model], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0)]];
|
||||||
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
|
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
|
||||||
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], @"unknown"]];
|
[self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], @"unknown"]];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _SYSTEMCONFIGURATION_H
|
#ifdef _SYSTEMCONFIGURATION_H
|
||||||
self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
|
self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
|
||||||
[self startMonitoringNetworkReachability];
|
[self startMonitoringNetworkReachability];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
|
self.operationQueue = [[[NSOperationQueue alloc] init] autorelease];
|
||||||
[self.operationQueue setMaxConcurrentOperationCount:kAFHTTPClientDefaultMaxConcurrentOperationCount];
|
[self.operationQueue setMaxConcurrentOperationCount:kAFHTTPClientDefaultMaxConcurrentOperationCount];
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)dealloc {
|
- (void)dealloc {
|
||||||
#ifdef _SYSTEMCONFIGURATION_H
|
#ifdef _SYSTEMCONFIGURATION_H
|
||||||
[self stopMonitoringNetworkReachability];
|
[self stopMonitoringNetworkReachability];
|
||||||
[_networkReachabilityStatusBlock release];
|
[_networkReachabilityStatusBlock release];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[_baseURL release];
|
[_baseURL release];
|
||||||
[_registeredHTTPOperationClassNames release];
|
[_registeredHTTPOperationClassNames release];
|
||||||
[_defaultHeaders release];
|
[_defaultHeaders release];
|
||||||
[_operationQueue release];
|
[_operationQueue release];
|
||||||
|
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSString *)description {
|
- (NSString *)description {
|
||||||
return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue];
|
return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
||||||
#ifdef _SYSTEMCONFIGURATION_H
|
#ifdef _SYSTEMCONFIGURATION_H
|
||||||
static BOOL AFURLHostIsIPAddress(NSURL *url) {
|
static BOOL AFURLHostIsIPAddress(NSURL *url) {
|
||||||
struct sockaddr_in sa_in;
|
struct sockaddr_in sa_in;
|
||||||
struct sockaddr_in6 sa_in6;
|
struct sockaddr_in6 sa_in6;
|
||||||
|
|
||||||
return [url host] && (inet_pton(AF_INET, [[url host] UTF8String], &sa_in) == 1 || inet_pton(AF_INET6, [[url host] UTF8String], &sa_in6) == 1);
|
return [url host] && (inet_pton(AF_INET, [[url host] UTF8String], &sa_in) == 1 || inet_pton(AF_INET6, [[url host] UTF8String], &sa_in6) == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
|
static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
|
||||||
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
|
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
|
||||||
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
|
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
|
||||||
BOOL isNetworkReachable = (isReachable && !needsConnection);
|
BOOL isNetworkReachable = (isReachable && !needsConnection);
|
||||||
|
|
||||||
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
|
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
|
||||||
if(isNetworkReachable == NO){
|
if(isNetworkReachable == NO){
|
||||||
status = AFNetworkReachabilityStatusNotReachable;
|
status = AFNetworkReachabilityStatusNotReachable;
|
||||||
}
|
}
|
||||||
#if TARGET_OS_IPHONE
|
#if TARGET_OS_IPHONE
|
||||||
else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
|
else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
|
||||||
status = AFNetworkReachabilityStatusReachableViaWWAN;
|
status = AFNetworkReachabilityStatusReachableViaWWAN;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
status = AFNetworkReachabilityStatusReachableViaWiFi;
|
status = AFNetworkReachabilityStatusReachableViaWiFi;
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
|
static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
|
||||||
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
||||||
AFNetworkReachabilityStatusBlock block = (AFNetworkReachabilityStatusBlock)info;
|
AFNetworkReachabilityStatusBlock block = (AFNetworkReachabilityStatusBlock)info;
|
||||||
if (block) {
|
if (block) {
|
||||||
block(status);
|
block(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:[NSNumber numberWithInt:status]];
|
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:[NSNumber numberWithInt:status]];
|
||||||
}
|
}
|
||||||
|
|
||||||
static const void * AFNetworkReachabilityRetainCallback(const void *info) {
|
static const void * AFNetworkReachabilityRetainCallback(const void *info) {
|
||||||
return [(AFNetworkReachabilityStatusBlock)info copy];
|
return [(AFNetworkReachabilityStatusBlock)info copy];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
[(AFNetworkReachabilityStatusBlock)info release];
|
[(AFNetworkReachabilityStatusBlock)info release];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)startMonitoringNetworkReachability {
|
- (void)startMonitoringNetworkReachability {
|
||||||
[self stopMonitoringNetworkReachability];
|
[self stopMonitoringNetworkReachability];
|
||||||
|
|
||||||
if (!self.baseURL) {
|
if (!self.baseURL) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
|
|
||||||
|
|
||||||
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
|
|
||||||
self.networkReachabilityStatus = status;
|
|
||||||
if (self.networkReachabilityStatusBlock) {
|
|
||||||
self.networkReachabilityStatusBlock(status);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
SCNetworkReachabilityContext context = {0, callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
|
self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
|
||||||
SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context);
|
|
||||||
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
|
||||||
|
|
||||||
/* Network reachability monitoring does not establish a baseline for IP addresses as it does for hostnames, so if the base URL host is an IP address, the initial reachability callback is manually triggered.
|
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
|
||||||
*/
|
self.networkReachabilityStatus = status;
|
||||||
if (AFURLHostIsIPAddress(self.baseURL)) {
|
if (self.networkReachabilityStatusBlock) {
|
||||||
SCNetworkReachabilityFlags flags;
|
self.networkReachabilityStatusBlock(status);
|
||||||
SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
|
}
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
};
|
||||||
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
|
||||||
callback(status);
|
SCNetworkReachabilityContext context = {0, callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
|
||||||
});
|
SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context);
|
||||||
}
|
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
||||||
|
|
||||||
|
/* Network reachability monitoring does not establish a baseline for IP addresses as it does for hostnames, so if the base URL host is an IP address, the initial reachability callback is manually triggered.
|
||||||
|
*/
|
||||||
|
if (AFURLHostIsIPAddress(self.baseURL)) {
|
||||||
|
SCNetworkReachabilityFlags flags;
|
||||||
|
SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
||||||
|
callback(status);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)stopMonitoringNetworkReachability {
|
- (void)stopMonitoringNetworkReachability {
|
||||||
if (_networkReachability) {
|
if (_networkReachability) {
|
||||||
SCNetworkReachabilityUnscheduleFromRunLoop(_networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
SCNetworkReachabilityUnscheduleFromRunLoop(_networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
||||||
CFRelease(_networkReachability);
|
CFRelease(_networkReachability);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
|
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
|
||||||
self.networkReachabilityStatusBlock = block;
|
self.networkReachabilityStatusBlock = block;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
||||||
- (BOOL)registerHTTPOperationClass:(Class)operationClass {
|
- (BOOL)registerHTTPOperationClass:(Class)operationClass {
|
||||||
if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
|
if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *className = NSStringFromClass(operationClass);
|
NSString *className = NSStringFromClass(operationClass);
|
||||||
[self.registeredHTTPOperationClassNames removeObject:className];
|
[self.registeredHTTPOperationClassNames removeObject:className];
|
||||||
[self.registeredHTTPOperationClassNames insertObject:className atIndex:0];
|
[self.registeredHTTPOperationClassNames insertObject:className atIndex:0];
|
||||||
|
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)unregisterHTTPOperationClass:(Class)operationClass {
|
- (void)unregisterHTTPOperationClass:(Class)operationClass {
|
||||||
NSString *className = NSStringFromClass(operationClass);
|
NSString *className = NSStringFromClass(operationClass);
|
||||||
[self.registeredHTTPOperationClassNames removeObject:className];
|
[self.registeredHTTPOperationClassNames removeObject:className];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
@ -446,11 +446,11 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
|
|
||||||
- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
|
- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
|
||||||
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
|
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
|
||||||
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]];
|
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
|
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
|
||||||
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
|
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)clearAuthorizationHeader {
|
- (void)clearAuthorizationHeader {
|
||||||
|
|
@ -463,33 +463,33 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
path:(NSString *)path
|
path:(NSString *)path
|
||||||
parameters:(NSDictionary *)parameters
|
parameters:(NSDictionary *)parameters
|
||||||
{
|
{
|
||||||
NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
|
NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
|
||||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
|
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];
|
||||||
[request setHTTPMethod:method];
|
[request setHTTPMethod:method];
|
||||||
[request setAllHTTPHeaderFields:self.defaultHeaders];
|
[request setAllHTTPHeaderFields:self.defaultHeaders];
|
||||||
|
|
||||||
if (parameters) {
|
if (parameters) {
|
||||||
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
|
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
|
||||||
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
|
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
|
||||||
[request setURL:url];
|
[request setURL:url];
|
||||||
} else {
|
} else {
|
||||||
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
|
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
|
||||||
switch (self.parameterEncoding) {
|
switch (self.parameterEncoding) {
|
||||||
case AFFormURLParameterEncoding:;
|
case AFFormURLParameterEncoding:;
|
||||||
[request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
[request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
||||||
[request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
|
[request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
|
||||||
break;
|
break;
|
||||||
case AFJSONParameterEncoding:;
|
case AFJSONParameterEncoding:;
|
||||||
[request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
[request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
||||||
[request setHTTPBody:[AFJSONStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
|
[request setHTTPBody:[AFJSONStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
|
||||||
break;
|
break;
|
||||||
case AFPropertyListParameterEncoding:;
|
case AFPropertyListParameterEncoding:;
|
||||||
[request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
[request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
||||||
[request setHTTPBody:[AFPropertyListStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
|
[request setHTTPBody:[AFPropertyListStringFromParameters(parameters) dataUsingEncoding:self.stringEncoding]];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
@ -499,125 +499,125 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
parameters:(NSDictionary *)parameters
|
parameters:(NSDictionary *)parameters
|
||||||
constructingBodyWithBlock:(void (^)(id <AFStreamingMultipartFormData>formData))block
|
constructingBodyWithBlock:(void (^)(id <AFStreamingMultipartFormData>formData))block
|
||||||
{
|
{
|
||||||
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
|
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
|
||||||
|
|
||||||
__block AFStreamingMultipartFormData * formData = [[[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding] autorelease];
|
__block AFStreamingMultipartFormData * formData = [[[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding] autorelease];
|
||||||
|
|
||||||
// __block AFMultipartFormData *formData = [[[AFMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding] autorelease];
|
// __block AFMultipartFormData *formData = [[[AFMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding] autorelease];
|
||||||
|
|
||||||
for (AFQueryStringComponent *component in AFQueryStringComponentsFromKeyAndValue(nil, parameters)) {
|
for (AFQueryStringComponent *component in AFQueryStringComponentsFromKeyAndValue(nil, parameters)) {
|
||||||
NSData *data = nil;
|
NSData *data = nil;
|
||||||
if ([component.value isKindOfClass:[NSData class]]) {
|
if ([component.value isKindOfClass:[NSData class]]) {
|
||||||
data = component.value;
|
data = component.value;
|
||||||
} else {
|
} else {
|
||||||
data = [[component.value description] dataUsingEncoding:self.stringEncoding];
|
data = [[component.value description] dataUsingEncoding:self.stringEncoding];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
[formData appendPartWithFormData:data name:[component.key description]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data) {
|
if (block) {
|
||||||
[formData appendPartWithFormData:data name:[component.key description]];
|
block(formData);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (block) {
|
return [formData requestByFinalizingMultipartFormData];
|
||||||
block(formData);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [formData requestByFinalizingMultipartFormData];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
AFHTTPRequestOperation *operation = nil;
|
AFHTTPRequestOperation *operation = nil;
|
||||||
NSString *className = nil;
|
NSString *className = nil;
|
||||||
NSEnumerator *enumerator = [self.registeredHTTPOperationClassNames reverseObjectEnumerator];
|
NSEnumerator *enumerator = [self.registeredHTTPOperationClassNames reverseObjectEnumerator];
|
||||||
while (!operation && (className = [enumerator nextObject])) {
|
while (!operation && (className = [enumerator nextObject])) {
|
||||||
Class op_class = NSClassFromString(className);
|
Class op_class = NSClassFromString(className);
|
||||||
if (op_class && [op_class canProcessRequest:urlRequest]) {
|
if (op_class && [op_class canProcessRequest:urlRequest]) {
|
||||||
operation = [[(AFHTTPRequestOperation *)[op_class alloc] initWithRequest:urlRequest] autorelease];
|
operation = [[(AFHTTPRequestOperation *)[op_class alloc] initWithRequest:urlRequest] autorelease];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!operation) {
|
if (!operation) {
|
||||||
operation = [[[AFHTTPRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
operation = [[[AFHTTPRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
||||||
}
|
}
|
||||||
|
|
||||||
[operation setCompletionBlockWithSuccess:success failure:failure];
|
[operation setCompletionBlockWithSuccess:success failure:failure];
|
||||||
|
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
||||||
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
|
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
|
||||||
[self.operationQueue addOperation:operation];
|
[self.operationQueue addOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path {
|
- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path {
|
||||||
for (NSOperation *operation in [self.operationQueue operations]) {
|
for (NSOperation *operation in [self.operationQueue operations]) {
|
||||||
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
|
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]) && [path isEqualToString:[[[(AFHTTPRequestOperation *)operation request] URL] path]]) {
|
if ((!method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]]) && [path isEqualToString:[[[(AFHTTPRequestOperation *)operation request] URL] path]]) {
|
||||||
[operation cancel];
|
[operation cancel];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)requests
|
- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)requests
|
||||||
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
||||||
completionBlock:(void (^)(NSArray *operations))completionBlock
|
completionBlock:(void (^)(NSArray *operations))completionBlock
|
||||||
{
|
{
|
||||||
NSMutableArray *mutableOperations = [NSMutableArray array];
|
NSMutableArray *mutableOperations = [NSMutableArray array];
|
||||||
for (NSURLRequest *request in requests) {
|
for (NSURLRequest *request in requests) {
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
||||||
[mutableOperations addObject:operation];
|
[mutableOperations addObject:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
[self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock];
|
[self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
|
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
|
||||||
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
||||||
completionBlock:(void (^)(NSArray *operations))completionBlock
|
completionBlock:(void (^)(NSArray *operations))completionBlock
|
||||||
{
|
{
|
||||||
__block dispatch_group_t dispatchGroup = dispatch_group_create();
|
__block dispatch_group_t dispatchGroup = dispatch_group_create();
|
||||||
NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
|
NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
|
||||||
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
|
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
|
||||||
if (completionBlock) {
|
if (completionBlock) {
|
||||||
completionBlock(operations);
|
completionBlock(operations);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
dispatch_release(dispatchGroup);
|
dispatch_release(dispatchGroup);
|
||||||
}];
|
}];
|
||||||
|
|
||||||
NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"];
|
NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"];
|
||||||
|
|
||||||
for (AFHTTPRequestOperation *operation in operations) {
|
for (AFHTTPRequestOperation *operation in operations) {
|
||||||
AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease];
|
AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease];
|
||||||
operation.completionBlock = ^{
|
operation.completionBlock = ^{
|
||||||
dispatch_queue_t queue = operation.successCallbackQueue ? operation.successCallbackQueue : dispatch_get_main_queue();
|
dispatch_queue_t queue = operation.successCallbackQueue ? operation.successCallbackQueue : dispatch_get_main_queue();
|
||||||
dispatch_group_async(dispatchGroup, queue, ^{
|
dispatch_group_async(dispatchGroup, queue, ^{
|
||||||
if (originalCompletionBlock) {
|
if (originalCompletionBlock) {
|
||||||
originalCompletionBlock();
|
originalCompletionBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (progressBlock) {
|
if (progressBlock) {
|
||||||
progressBlock([[operations filteredArrayUsingPredicate:finishedOperationPredicate] count], [operations count]);
|
progressBlock([[operations filteredArrayUsingPredicate:finishedOperationPredicate] count], [operations count]);
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_group_leave(dispatchGroup);
|
dispatch_group_leave(dispatchGroup);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
dispatch_group_enter(dispatchGroup);
|
dispatch_group_enter(dispatchGroup);
|
||||||
[batchedOperation addDependency:operation];
|
[batchedOperation addDependency:operation];
|
||||||
|
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
[self.operationQueue addOperation:batchedOperation];
|
[self.operationQueue addOperation:batchedOperation];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
@ -628,8 +628,8 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)postPath:(NSString *)path
|
- (void)postPath:(NSString *)path
|
||||||
|
|
@ -639,7 +639,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
{
|
{
|
||||||
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)putPath:(NSString *)path
|
- (void)putPath:(NSString *)path
|
||||||
|
|
@ -649,7 +649,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
{
|
{
|
||||||
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)deletePath:(NSString *)path
|
- (void)deletePath:(NSString *)path
|
||||||
|
|
@ -659,7 +659,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
{
|
{
|
||||||
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)patchPath:(NSString *)path
|
- (void)patchPath:(NSString *)path
|
||||||
|
|
@ -667,9 +667,9 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters];
|
NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters];
|
||||||
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
||||||
[self enqueueHTTPRequestOperation:operation];
|
[self enqueueHTTPRequestOperation:operation];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
@ -679,18 +679,18 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
static NSString * const kAFMultipartTemporaryFileDirectoryName = @"com.alamofire.uploads";
|
static NSString * const kAFMultipartTemporaryFileDirectoryName = @"com.alamofire.uploads";
|
||||||
|
|
||||||
static NSString * AFMultipartTemporaryFileDirectoryPath() {
|
static NSString * AFMultipartTemporaryFileDirectoryPath() {
|
||||||
static NSString *multipartTemporaryFilePath = nil;
|
static NSString *multipartTemporaryFilePath = nil;
|
||||||
static dispatch_once_t onceToken;
|
static dispatch_once_t onceToken;
|
||||||
dispatch_once(&onceToken, ^{
|
dispatch_once(&onceToken, ^{
|
||||||
multipartTemporaryFilePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:kAFMultipartTemporaryFileDirectoryName] copy];
|
multipartTemporaryFilePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:kAFMultipartTemporaryFileDirectoryName] copy];
|
||||||
|
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
if(![[NSFileManager defaultManager] createDirectoryAtPath:multipartTemporaryFilePath withIntermediateDirectories:YES attributes:nil error:&error]) {
|
if(![[NSFileManager defaultManager] createDirectoryAtPath:multipartTemporaryFilePath withIntermediateDirectories:YES attributes:nil error:&error]) {
|
||||||
NSLog(@"Failed to create multipary temporary file directory at %@", multipartTemporaryFilePath);
|
NSLog(@"Failed to create multipary temporary file directory at %@", multipartTemporaryFilePath);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return multipartTemporaryFilePath;
|
return multipartTemporaryFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
|
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
|
||||||
|
|
@ -700,15 +700,15 @@ static NSString * const kAFMultipartFormCRLF = @"\r\n";
|
||||||
static NSInteger const kAFStreamToStreamBufferSize = 1024*1024; //1 meg default
|
static NSInteger const kAFStreamToStreamBufferSize = 1024*1024; //1 meg default
|
||||||
|
|
||||||
static inline NSString * AFMultipartFormInitialBoundary() {
|
static inline NSString * AFMultipartFormInitialBoundary() {
|
||||||
return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline NSString * AFMultipartFormEncapsulationBoundary() {
|
static inline NSString * AFMultipartFormEncapsulationBoundary() {
|
||||||
return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline NSString * AFMultipartFormFinalBoundary() {
|
static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark --
|
#pragma mark --
|
||||||
|
|
@ -813,7 +813,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
stringEncoding = NSUTF8StringEncoding;
|
stringEncoding = NSUTF8StringEncoding;
|
||||||
streamStatus = NSStreamStatusNotOpen;
|
streamStatus = NSStreamStatusNotOpen;
|
||||||
[self resetCursors];
|
[self resetCursors];
|
||||||
[self setDelegate:self];
|
// [self setDelegate:self];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -938,7 +938,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSInteger)readData:(NSData *)data intoBuffer:(uint8_t *)buffer maxLength:(NSUInteger)len offsetCursor:(NSUInteger*)offsetCursorPtr {
|
-(NSInteger)readData:(NSData *)data intoBuffer:(uint8_t *)buffer maxLength:(NSUInteger)len offsetCursor:(NSUInteger*)offsetCursorPtr {
|
||||||
NSUInteger bytesAvailable = [data length] - *offsetCursorPtr;
|
NSInteger bytesAvailable = [data length] - *offsetCursorPtr;
|
||||||
if (len > bytesAvailable) {
|
if (len > bytesAvailable) {
|
||||||
[data getBytes:buffer range:NSMakeRange(*offsetCursorPtr, bytesAvailable)];
|
[data getBytes:buffer range:NSMakeRange(*offsetCursorPtr, bytesAvailable)];
|
||||||
*offsetCursorPtr += bytesAvailable;
|
*offsetCursorPtr += bytesAvailable;
|
||||||
|
|
@ -979,7 +979,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSUInteger)totalElements {
|
-(NSUInteger)totalElements {
|
||||||
return [formDatas count] + [fileURLs count];
|
return [formDatas count] + [fileURLs count] + 1; //one extra for final boundary
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSData *)finalBoundaryData {
|
-(NSData *)finalBoundaryData {
|
||||||
|
|
@ -1036,10 +1036,14 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
#pragma mark - NSInputStream subclass overrides
|
#pragma mark - NSInputStream subclass overrides
|
||||||
|
|
||||||
-(NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
|
-(NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
|
||||||
|
if ([self streamStatus] == NSStreamStatusClosed) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
assert ([self streamStatus] == NSStreamStatusOpen);
|
assert ([self streamStatus] == NSStreamStatusOpen);
|
||||||
|
|
||||||
NSUInteger bytesRead = 0;
|
NSInteger bytesRead = 0;
|
||||||
NSUInteger readFileCursor = (readElementCursor - [formNames count]);
|
NSInteger readFileCursor = (readElementCursor - [formNames count]);
|
||||||
|
|
||||||
if (readElementCursor < [formNames count]) {
|
if (readElementCursor < [formNames count]) {
|
||||||
//reading from formDatas
|
//reading from formDatas
|
||||||
|
|
@ -1056,7 +1060,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
[self nextElement];
|
[self nextElement];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (readElementCursor >= [formNames count] && readFileCursor < [fileNames count]) {
|
else if (readFileCursor >= 0 && readFileCursor < [fileNames count]) {
|
||||||
//reading from files
|
//reading from files
|
||||||
NSString * currentFileName = [fileNames objectAtIndex:readFileCursor];
|
NSString * currentFileName = [fileNames objectAtIndex:readFileCursor];
|
||||||
NSURL * currentFileURL = [fileURLs objectForKey:currentFileName];
|
NSURL * currentFileURL = [fileURLs objectForKey:currentFileName];
|
||||||
|
|
@ -1069,7 +1073,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
} else {
|
} else {
|
||||||
if (!currentFileStream) {
|
if (!currentFileStream) {
|
||||||
currentFileStream = [[NSInputStream inputStreamWithURL:currentFileURL] retain];
|
currentFileStream = [[NSInputStream inputStreamWithURL:currentFileURL] retain];
|
||||||
currentFileStream.delegate = self;
|
// currentFileStream.delegate = self;
|
||||||
[currentFileStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
[currentFileStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
||||||
[currentFileStream open];
|
[currentFileStream open];
|
||||||
}
|
}
|
||||||
|
|
@ -1081,44 +1085,37 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else if (readElementCursor < [self totalElements]) {
|
||||||
//add final boundary
|
//add final boundary
|
||||||
bytesRead = [self readData:[self finalBoundaryData] intoBuffer:buffer maxLength:len offsetCursor:&readOffsetCursor];
|
bytesRead = [self readData:[self finalBoundaryData] intoBuffer:buffer maxLength:len offsetCursor:&readOffsetCursor];
|
||||||
if (readOffsetCursor == [[self finalBoundaryData] length]) {
|
if (readOffsetCursor == [[self finalBoundaryData] length]) {
|
||||||
[self nextElement];
|
[self nextElement];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
if (bytesRead < len && readElementCursor <= [self totalElements]) {
|
[self nextElement];
|
||||||
//recurse to fill out the buffer, this is critical if the above read operations produce a zero length buffer for some reason (empty form data, end of file) because returning zero from this method will result in the stream being closed.
|
|
||||||
bytesRead += [self read:buffer+bytesRead maxLength:len-bytesRead];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (readElementCursor <= [self totalElements]) {
|
||||||
|
if (bytesRead < len) {
|
||||||
|
bytesRead += [self read:buffer+bytesRead maxLength:len-bytesRead];
|
||||||
|
} else {
|
||||||
|
// no deeper recursion so call callback if necessary
|
||||||
|
if (copiedCallback && (requestedEvents & kCFStreamEventHasBytesAvailable)) {
|
||||||
|
copiedCallback((CFReadStreamRef)self, kCFStreamEventHasBytesAvailable, &copiedContext);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//doesn't seem to make a diff if we do the callbacks or not for the HTTP request use anyway,
|
|
||||||
//and it sometimes crashes if the callback reciever or context have been released.
|
|
||||||
// if (CFReadStreamGetStatus((CFReadStreamRef)self) == kCFStreamStatusOpen) {
|
|
||||||
// double delayInSeconds = 0;
|
|
||||||
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
|
|
||||||
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
|
||||||
// if (copiedCallback && (requestedEvents & kCFStreamEventHasBytesAvailable)) {
|
|
||||||
// NSLog(@"callback kCFStreamEventHasBytesAvailable... bytes read: %d", bytesRead);
|
|
||||||
// copiedCallback((CFReadStreamRef)self, kCFStreamEventHasBytesAvailable, &copiedContext);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
assert(bytesRead > 0); //really should never return zero from this call
|
|
||||||
return bytesRead;
|
return bytesRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL)hasBytesAvailable {
|
-(BOOL)hasBytesAvailable {
|
||||||
if ([self totalElements] == 0) {
|
if ([self streamStatus] != NSStreamStatusOpen) {
|
||||||
return NO;
|
return NO;
|
||||||
} else if (readElementCursor < ([self totalElements] + 1)) {
|
}
|
||||||
|
else {
|
||||||
return YES;
|
return YES;
|
||||||
} else {
|
|
||||||
return NO;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1161,7 +1158,6 @@ static inline NSString * AFMultipartFormFinalBoundary() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue