/Pods/AFNetworking/AFNetworking/AFHTTPClient.m
Objective C | 1270 lines | 985 code | 254 blank | 31 comment | 124 complexity | 1e57708a12c681927ffa06628925e660 MD5 | raw file
- // AFHTTPClient.m
- //
- // Copyright (c) 2011 Gowalla (http://gowalla.com/)
- //
- // 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.
- #import <Foundation/Foundation.h>
- #import "AFHTTPClient.h"
- #import "AFHTTPRequestOperation.h"
- #import <Availability.h>
- #ifdef _SYSTEMCONFIGURATION_H
- #import <netinet/in.h>
- #import <netinet6/in6.h>
- #import <arpa/inet.h>
- #import <ifaddrs.h>
- #import <netdb.h>
- #endif
- #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
- #import <UIKit/UIKit.h>
- #endif
- #ifdef _SYSTEMCONFIGURATION_H
- NSString * const AFNetworkingReachabilityDidChangeNotification = @"com.alamofire.networking.reachability.change";
- NSString * const AFNetworkingReachabilityNotificationStatusItem = @"AFNetworkingReachabilityNotificationStatusItem";
- typedef SCNetworkReachabilityRef AFNetworkReachabilityRef;
- typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
- #else
- typedef id AFNetworkReachabilityRef;
- #endif
- typedef void (^AFCompletionBlock)(void);
- static NSString * AFBase64EncodedStringFromString(NSString *string) {
- NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
- NSUInteger length = [data length];
- NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
- uint8_t *input = (uint8_t *)[data bytes];
- uint8_t *output = (uint8_t *)[mutableData mutableBytes];
- for (NSUInteger i = 0; i < length; i += 3) {
- NSUInteger value = 0;
- for (NSUInteger j = i; j < (i + 3); j++) {
- value <<= 8;
- if (j < length) {
- 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] : '=';
- }
- return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
- }
- static NSString * AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
- static NSString * const kAFCharactersToBeEscaped = @":/?&=;+!@#$()~',*";
- static NSString * const kAFCharactersToLeaveUnescaped = @"[].";
- return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding));
- }
- #pragma mark -
- @interface AFQueryStringPair : NSObject
- @property (readwrite, nonatomic, strong) id field;
- @property (readwrite, nonatomic, strong) id value;
- - (id)initWithField:(id)field value:(id)value;
- - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding;
- @end
- @implementation AFQueryStringPair
- @synthesize field = _field;
- @synthesize value = _value;
- - (id)initWithField:(id)field value:(id)value {
- self = [super init];
- if (!self) {
- return nil;
- }
- self.field = field;
- self.value = value;
- return self;
- }
- - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding {
- if (!self.value || [self.value isEqual:[NSNull null]]) {
- return AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding);
- } else {
- return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding), AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.value description], stringEncoding)];
- }
- }
- @end
- #pragma mark -
- extern NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary);
- extern NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value);
- NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) {
- NSMutableArray *mutablePairs = [NSMutableArray array];
- for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
- [mutablePairs addObject:[pair URLEncodedStringValueWithEncoding:stringEncoding]];
- }
- return [mutablePairs componentsJoinedByString:@"&"];
- }
- NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary) {
- return AFQueryStringPairsFromKeyAndValue(nil, dictionary);
- }
- NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) {
- NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
- if ([value isKindOfClass:[NSDictionary class]]) {
- NSDictionary *dictionary = value;
- // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
- NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveCompare:)];
- for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors:@[ sortDescriptor ]]) {
- id nestedValue = dictionary[nestedKey];
- if (nestedValue) {
- [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
- }
- }
- } else if ([value isKindOfClass:[NSArray class]]) {
- NSArray *array = value;
- for (id nestedValue in array) {
- [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
- }
- } else if ([value isKindOfClass:[NSSet class]]) {
- NSSet *set = value;
- for (id obj in set) {
- [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue(key, obj)];
- }
- } else {
- [mutableQueryStringComponents addObject:[[AFQueryStringPair alloc] initWithField:key value:value]];
- }
- return mutableQueryStringComponents;
- }
- @interface AFStreamingMultipartFormData : NSObject <AFMultipartFormData>
- - (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
- stringEncoding:(NSStringEncoding)encoding;
- - (NSMutableURLRequest *)requestByFinalizingMultipartFormData;
- @end
- #pragma mark -
- @interface AFHTTPClient ()
- @property (readwrite, nonatomic, strong) NSURL *baseURL;
- @property (readwrite, nonatomic, strong) NSMutableArray *registeredHTTPOperationClassNames;
- @property (readwrite, nonatomic, strong) NSMutableDictionary *defaultHeaders;
- @property (readwrite, nonatomic, strong) NSURLCredential *defaultCredential;
- @property (readwrite, nonatomic, strong) NSOperationQueue *operationQueue;
- #ifdef _SYSTEMCONFIGURATION_H
- @property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability;
- @property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
- @property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock;
- #endif
- #ifdef _SYSTEMCONFIGURATION_H
- - (void)startMonitoringNetworkReachability;
- - (void)stopMonitoringNetworkReachability;
- #endif
- @end
- @implementation AFHTTPClient
- @synthesize baseURL = _baseURL;
- @synthesize stringEncoding = _stringEncoding;
- @synthesize parameterEncoding = _parameterEncoding;
- @synthesize registeredHTTPOperationClassNames = _registeredHTTPOperationClassNames;
- @synthesize defaultHeaders = _defaultHeaders;
- @synthesize defaultCredential = _defaultCredential;
- @synthesize operationQueue = _operationQueue;
- #ifdef _SYSTEMCONFIGURATION_H
- @synthesize networkReachability = _networkReachability;
- @synthesize networkReachabilityStatus = _networkReachabilityStatus;
- @synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock;
- #endif
- + (instancetype)clientWithBaseURL:(NSURL *)url {
- return [[self alloc] initWithBaseURL:url];
- }
- - (id)initWithBaseURL:(NSURL *)url {
- NSParameterAssert(url);
- self = [super init];
- if (!self) {
- return nil;
- }
- // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
- if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
- url = [url URLByAppendingPathComponent:@""];
- }
- self.baseURL = url;
- self.stringEncoding = NSUTF8StringEncoding;
- self.parameterEncoding = AFFormURLParameterEncoding;
- self.registeredHTTPOperationClassNames = [NSMutableArray array];
- self.defaultHeaders = [NSMutableDictionary dictionary];
- // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
- NSMutableArray *acceptLanguagesComponents = [NSMutableArray array];
- [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- float q = 1.0f - (idx * 0.1f);
- [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g", obj, q]];
- *stop = q <= 0.5f;
- }];
- [self setDefaultHeader:@"Accept-Language" value:[acceptLanguagesComponents componentsJoinedByString:@", "]];
-
- #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
- // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
- [self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], (__bridge id)CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey) ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] ? [[UIScreen mainScreen] scale] : 1.0f)]];
- #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
- [self setDefaultHeader:@"User-Agent" value:[NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleIdentifierKey], [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] ?: [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]]];
- #endif
- #ifdef _SYSTEMCONFIGURATION_H
- self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
- [self startMonitoringNetworkReachability];
- #endif
- self.operationQueue = [[NSOperationQueue alloc] init];
- [self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
- // #ifdef included for backwards-compatibility
- #ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
- self.allowsInvalidSSLCertificate = YES;
- #endif
-
- return self;
- }
- - (void)dealloc {
- #ifdef _SYSTEMCONFIGURATION_H
- [self stopMonitoringNetworkReachability];
- #endif
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue];
- }
- #pragma mark -
- #ifdef _SYSTEMCONFIGURATION_H
- static BOOL AFURLHostIsIPAddress(NSURL *url) {
- struct sockaddr_in sa_in;
- 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);
- }
- static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
- BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
- BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
- BOOL isNetworkReachable = (isReachable && !needsConnection);
- AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
- if (isNetworkReachable == NO) {
- status = AFNetworkReachabilityStatusNotReachable;
- }
- #if TARGET_OS_IPHONE
- else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
- status = AFNetworkReachabilityStatusReachableViaWWAN;
- }
- #endif
- else {
- status = AFNetworkReachabilityStatusReachableViaWiFi;
- }
- return status;
- }
- static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
- AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
- AFNetworkReachabilityStatusBlock block = (__bridge AFNetworkReachabilityStatusBlock)info;
- if (block) {
- block(status);
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
- [notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:status] forKey:AFNetworkingReachabilityNotificationStatusItem]];
- });
- }
- static const void * AFNetworkReachabilityRetainCallback(const void *info) {
- return (__bridge_retained const void *)([(__bridge AFNetworkReachabilityStatusBlock)info copy]);
- }
- static void AFNetworkReachabilityReleaseCallback(const void *info) {
- if (info) {
- CFRelease(info);
- }
- }
- - (void)startMonitoringNetworkReachability {
- [self stopMonitoringNetworkReachability];
- if (!self.baseURL) {
- return;
- }
- self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
- if (!self.networkReachability) {
- return;
- }
- __weak __typeof(&*self)weakSelf = self;
- AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
- __strong __typeof(&*weakSelf)strongSelf = weakSelf;
- if (!strongSelf) {
- return;
- }
- strongSelf.networkReachabilityStatus = status;
- if (strongSelf.networkReachabilityStatusBlock) {
- strongSelf.networkReachabilityStatusBlock(status);
- }
- };
- SCNetworkReachabilityContext context = {0, (__bridge void *)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 {
- if (_networkReachability) {
- SCNetworkReachabilityUnscheduleFromRunLoop(_networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
- CFRelease(_networkReachability);
- _networkReachability = NULL;
- }
- }
- - (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
- self.networkReachabilityStatusBlock = block;
- }
- #endif
- #pragma mark -
- - (BOOL)registerHTTPOperationClass:(Class)operationClass {
- if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
- return NO;
- }
- NSString *className = NSStringFromClass(operationClass);
- [self.registeredHTTPOperationClassNames removeObject:className];
- [self.registeredHTTPOperationClassNames insertObject:className atIndex:0];
- return YES;
- }
- - (void)unregisterHTTPOperationClass:(Class)operationClass {
- NSString *className = NSStringFromClass(operationClass);
- [self.registeredHTTPOperationClassNames removeObject:className];
- }
- #pragma mark -
- - (NSString *)defaultValueForHeader:(NSString *)header {
- return [self.defaultHeaders valueForKey:header];
- }
- - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
- [self.defaultHeaders setValue:value forKey:header];
- }
- - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
- NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
- [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]];
- }
- - (void)setAuthorizationHeaderWithToken:(NSString *)token {
- [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
- }
- - (void)clearAuthorizationHeader {
- [self.defaultHeaders removeObjectForKey:@"Authorization"];
- }
- #pragma mark -
- - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
- path:(NSString *)path
- parameters:(NSDictionary *)parameters
- {
- NSParameterAssert(method);
- if (!path) {
- path = @"";
- }
- NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
- [request setHTTPMethod:method];
- [request setAllHTTPHeaderFields:self.defaultHeaders];
- if (parameters) {
- if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
- url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
- [request setURL:url];
- } else {
- NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
- NSError *error = nil;
- switch (self.parameterEncoding) {
- case AFFormURLParameterEncoding:;
- [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
- break;
- case AFJSONParameterEncoding:;
- [request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
- break;
- case AFPropertyListParameterEncoding:;
- [request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]];
- break;
- }
- if (error) {
- NSLog(@"%@ %@: %@", [self class], NSStringFromSelector(_cmd), error);
- }
- }
- }
- return request;
- }
- - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
- path:(NSString *)path
- parameters:(NSDictionary *)parameters
- constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
- {
- NSParameterAssert(method);
- NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
- NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
- __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding];
- if (parameters) {
- for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
- NSData *data = nil;
- if ([pair.value isKindOfClass:[NSData class]]) {
- data = pair.value;
- } else if ([pair.value isEqual:[NSNull null]]) {
- data = [NSData data];
- } else {
- data = [[pair.value description] dataUsingEncoding:self.stringEncoding];
- }
- if (data) {
- [formData appendPartWithFormData:data name:[pair.field description]];
- }
- }
- }
- if (block) {
- block(formData);
- }
- return [formData requestByFinalizingMultipartFormData];
- }
- - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- AFHTTPRequestOperation *operation = nil;
-
- for (NSString *className in self.registeredHTTPOperationClassNames) {
- Class operationClass = NSClassFromString(className);
- if (operationClass && [operationClass canProcessRequest:urlRequest]) {
- operation = [(AFHTTPRequestOperation *)[operationClass alloc] initWithRequest:urlRequest];
- break;
- }
- }
- if (!operation) {
- operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
- }
- [operation setCompletionBlockWithSuccess:success failure:failure];
- operation.credential = self.defaultCredential;
- #ifdef _AFNETWORKING_PIN_SSL_CERTIFICATES_
- operation.SSLPinningMode = self.defaultSSLPinningMode;
- #endif
- operation.allowsInvalidSSLCertificate = self.allowsInvalidSSLCertificate;
- return operation;
- }
- #pragma mark -
- - (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
- [self.operationQueue addOperation:operation];
- }
- - (void)cancelAllHTTPOperationsWithMethod:(NSString *)method
- path:(NSString *)path
- {
- NSString *pathToBeMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] path];
- for (NSOperation *operation in [self.operationQueue operations]) {
- if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
- continue;
- }
- BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]];
- BOOL hasMatchingPath = [[[[(AFHTTPRequestOperation *)operation request] URL] path] isEqual:pathToBeMatched];
- if (hasMatchingMethod && hasMatchingPath) {
- [operation cancel];
- }
- }
- }
- - (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
- progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
- completionBlock:(void (^)(NSArray *operations))completionBlock
- {
- NSMutableArray *mutableOperations = [NSMutableArray array];
- for (NSURLRequest *request in urlRequests) {
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
- [mutableOperations addObject:operation];
- }
- [self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock];
- }
- - (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
- progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
- completionBlock:(void (^)(NSArray *operations))completionBlock
- {
- __block dispatch_group_t dispatchGroup = dispatch_group_create();
- NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
- dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
- if (completionBlock) {
- completionBlock(operations);
- }
- });
- #if !OS_OBJECT_USE_OBJC
- dispatch_release(dispatchGroup);
- #endif
- }];
- for (AFHTTPRequestOperation *operation in operations) {
- AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy];
- __weak __typeof(&*operation)weakOperation = operation;
- operation.completionBlock = ^{
- __strong __typeof(&*weakOperation)strongOperation = weakOperation;
- dispatch_queue_t queue = strongOperation.successCallbackQueue ?: dispatch_get_main_queue();
- dispatch_group_async(dispatchGroup, queue, ^{
- if (originalCompletionBlock) {
- originalCompletionBlock();
- }
- NSInteger numberOfFinishedOperations = [[operations indexesOfObjectsPassingTest:^BOOL(id op, NSUInteger __unused idx, BOOL __unused *stop) {
- return [op isFinished];
- }] count];
- if (progressBlock) {
- progressBlock(numberOfFinishedOperations, [operations count]);
- }
- dispatch_group_leave(dispatchGroup);
- });
- };
- dispatch_group_enter(dispatchGroup);
- [batchedOperation addDependency:operation];
- }
- [self.operationQueue addOperations:operations waitUntilFinished:NO];
- [self.operationQueue addOperation:batchedOperation];
- }
- #pragma mark -
- - (void)getPath:(NSString *)path
- parameters:(NSDictionary *)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
- [self enqueueHTTPRequestOperation:operation];
- }
- - (void)postPath:(NSString *)path
- parameters:(NSDictionary *)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
- [self enqueueHTTPRequestOperation:operation];
- }
- - (void)putPath:(NSString *)path
- parameters:(NSDictionary *)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
- [self enqueueHTTPRequestOperation:operation];
- }
- - (void)deletePath:(NSString *)path
- parameters:(NSDictionary *)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
- [self enqueueHTTPRequestOperation:operation];
- }
- - (void)patchPath:(NSString *)path
- parameters:(NSDictionary *)parameters
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters];
- AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
- [self enqueueHTTPRequestOperation:operation];
- }
- #pragma mark - NSCoding
- - (id)initWithCoder:(NSCoder *)aDecoder {
- NSURL *baseURL = [aDecoder decodeObjectForKey:@"baseURL"];
- self = [self initWithBaseURL:baseURL];
- if (!self) {
- return nil;
- }
- self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"];
- self.parameterEncoding = [aDecoder decodeIntegerForKey:@"parameterEncoding"];
- self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"];
- self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeObject:self.baseURL forKey:@"baseURL"];
- [aCoder encodeInteger:(NSInteger)self.stringEncoding forKey:@"stringEncoding"];
- [aCoder encodeInteger:self.parameterEncoding forKey:@"parameterEncoding"];
- [aCoder encodeObject:self.registeredHTTPOperationClassNames forKey:@"registeredHTTPOperationClassNames"];
- [aCoder encodeObject:self.defaultHeaders forKey:@"defaultHeaders"];
- }
- #pragma mark - NSCopying
- - (id)copyWithZone:(NSZone *)zone {
- AFHTTPClient *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
- HTTPClient.stringEncoding = self.stringEncoding;
- HTTPClient.parameterEncoding = self.parameterEncoding;
- HTTPClient.registeredHTTPOperationClassNames = [self.registeredHTTPOperationClassNames copyWithZone:zone];
- HTTPClient.defaultHeaders = [self.defaultHeaders copyWithZone:zone];
- #ifdef _SYSTEMCONFIGURATION_H
- HTTPClient.networkReachabilityStatusBlock = self.networkReachabilityStatusBlock;
- #endif
- return HTTPClient;
- }
- @end
- #pragma mark -
- static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
- static NSString * const kAFMultipartFormCRLF = @"\r\n";
- static NSInteger const kAFStreamToStreamBufferSize = 1024 * 1024; //1 meg default
- static inline NSString * AFMultipartFormInitialBoundary() {
- return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
- }
- static inline NSString * AFMultipartFormEncapsulationBoundary() {
- return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
- }
- static inline NSString * AFMultipartFormFinalBoundary() {
- return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
- }
- static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
- #ifdef __UTTYPE__
- NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
- NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
- if (!contentType) {
- return @"application/octet-stream";
- } else {
- return contentType;
- }
- #else
- return @"application/octet-stream";
- #endif
- }
- NSUInteger const kAFUploadStream3GSuggestedPacketSize = 1024 * 16;
- NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
- @interface AFHTTPBodyPart : NSObject
- @property (nonatomic, assign) NSStringEncoding stringEncoding;
- @property (nonatomic, strong) NSDictionary *headers;
- @property (nonatomic, strong) id body;
- @property (nonatomic, assign) unsigned long long bodyContentLength;
- @property (nonatomic, strong) NSInputStream *inputStream;
- @property (nonatomic, assign) BOOL hasInitialBoundary;
- @property (nonatomic, assign) BOOL hasFinalBoundary;
- @property (nonatomic, readonly, getter = hasBytesAvailable) BOOL bytesAvailable;
- @property (nonatomic, readonly) unsigned long long contentLength;
- - (NSInteger)read:(uint8_t *)buffer
- maxLength:(NSUInteger)length;
- @end
- @interface AFMultipartBodyStreamProvider : NSObject
- @property (nonatomic, assign) NSUInteger bufferLength;
- @property (nonatomic, assign) NSTimeInterval delay;
- @property (nonatomic, strong) NSInputStream *inputStream;
- @property (nonatomic, readonly) unsigned long long contentLength;
- @property (nonatomic, readonly, getter = isEmpty) BOOL empty;
- - (id)initWithStringEncoding:(NSStringEncoding)encoding;
- - (void)setInitialAndFinalBoundaries;
- - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart;
- @end
- #pragma mark -
- @interface AFStreamingMultipartFormData ()
- @property (readwrite, nonatomic, copy) NSMutableURLRequest *request;
- @property (readwrite, nonatomic, strong) AFMultipartBodyStreamProvider *bodyStream;
- @property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding;
- @end
- @implementation AFStreamingMultipartFormData
- @synthesize request = _request;
- @synthesize bodyStream = _bodyStream;
- @synthesize stringEncoding = _stringEncoding;
- - (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
- stringEncoding:(NSStringEncoding)encoding
- {
- self = [super init];
- if (!self) {
- return nil;
- }
- self.request = urlRequest;
- self.stringEncoding = encoding;
- self.bodyStream = [[AFMultipartBodyStreamProvider alloc] initWithStringEncoding:encoding];
- return self;
- }
- - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
- name:(NSString *)name
- error:(NSError * __autoreleasing *)error
- {
- NSParameterAssert(fileURL);
- NSParameterAssert(name);
- NSString *fileName = [fileURL lastPathComponent];
- NSString *mimeType = AFContentTypeForPathExtension([fileURL pathExtension]);
-
- return [self appendPartWithFileURL:fileURL name:name fileName:fileName mimeType:mimeType error:error];
- }
- - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
- name:(NSString *)name
- fileName:(NSString *)fileName
- mimeType:(NSString *)mimeType
- error:(NSError * __autoreleasing *)error
- {
- NSParameterAssert(fileURL);
- NSParameterAssert(name);
- NSParameterAssert(fileName);
- NSParameterAssert(mimeType);
- if (![fileURL isFileURL]) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"Expected URL to be a file URL", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey];
- if (error != NULL) {
- *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
- }
- return NO;
- } else if ([fileURL checkResourceIsReachableAndReturnError:error] == NO) {
- NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"File URL not reachable.", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey];
- if (error != NULL) {
- *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
- }
- return NO;
- }
- NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
- [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
- [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
- AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
- bodyPart.stringEncoding = self.stringEncoding;
- bodyPart.headers = mutableHeaders;
- bodyPart.body = fileURL;
- NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
- bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
- [self.bodyStream appendHTTPBodyPart:bodyPart];
- return YES;
- }
- - (void)appendPartWithInputStream:(NSInputStream *)inputStream
- name:(NSString *)name
- fileName:(NSString *)fileName
- length:(unsigned long long)length
- mimeType:(NSString *)mimeType
- {
- NSParameterAssert(name);
- NSParameterAssert(fileName);
- NSParameterAssert(mimeType);
- NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
- [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
- [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
- AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
- bodyPart.stringEncoding = self.stringEncoding;
- bodyPart.headers = mutableHeaders;
- bodyPart.body = inputStream;
- bodyPart.bodyContentLength = length;
- [self.bodyStream appendHTTPBodyPart:bodyPart];
- }
- - (void)appendPartWithFileData:(NSData *)data
- name:(NSString *)name
- fileName:(NSString *)fileName
- mimeType:(NSString *)mimeType
- {
- NSParameterAssert(name);
- NSParameterAssert(fileName);
- NSParameterAssert(mimeType);
- NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
- [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
- [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
- [self appendPartWithHeaders:mutableHeaders body:data];
- }
- - (void)appendPartWithFormData:(NSData *)data
- name:(NSString *)name
- {
- NSParameterAssert(name);
- NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
- [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", name] forKey:@"Content-Disposition"];
- [self appendPartWithHeaders:mutableHeaders body:data];
- }
- - (void)appendPartWithHeaders:(NSDictionary *)headers
- body:(NSData *)body
- {
- NSParameterAssert(body);
- AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
- bodyPart.stringEncoding = self.stringEncoding;
- bodyPart.headers = headers;
- bodyPart.bodyContentLength = [body length];
- bodyPart.body = body;
- [self.bodyStream appendHTTPBodyPart:bodyPart];
- }
- - (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
- delay:(NSTimeInterval)delay
- {
- self.bodyStream.bufferLength = numberOfBytes;
- self.bodyStream.delay = delay;
- }
- - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
- if ([self.bodyStream isEmpty]) {
- return self.request;
- }
- // Reset the initial and final boundaries to ensure correct Content-Length
- [self.bodyStream setInitialAndFinalBoundaries];
- [self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"];
- [self.request setValue:[NSString stringWithFormat:@"%llu", [self.bodyStream contentLength]] forHTTPHeaderField:@"Content-Length"];
- [self.request setHTTPBodyStream:self.bodyStream.inputStream];
- return self.request;
- }
- @end
- #pragma mark -
- @interface AFMultipartBodyStreamProvider () <NSCopying, NSStreamDelegate>
- @property (nonatomic, assign) NSStringEncoding stringEncoding;
- @property (nonatomic, strong) NSMutableArray *HTTPBodyParts;
- @property (nonatomic, strong) NSEnumerator *HTTPBodyPartEnumerator;
- @property (nonatomic, strong) AFHTTPBodyPart *currentHTTPBodyPart;
- @property (nonatomic, strong) NSOutputStream *outputStream;
- @property (nonatomic, strong) NSMutableData *buffer;
- @end
- static const NSUInteger AFMultipartBodyStreamProviderDefaultBufferLength = 4096;
- @implementation AFMultipartBodyStreamProvider {
- @private
- // Workaround for stream delegates being weakly referenced, but otherwise unowned
- __strong id _self;
- }
- @synthesize stringEncoding = _stringEncoding;
- @synthesize HTTPBodyParts = _HTTPBodyParts;
- @synthesize HTTPBodyPartEnumerator = _HTTPBodyPartEnumerator;
- @synthesize currentHTTPBodyPart = _currentHTTPBodyPart;
- @synthesize inputStream = _inputStream;
- @synthesize outputStream = _outputStream;
- @synthesize buffer = _buffer;
- @synthesize bufferLength = _numberOfBytesInPacket;
- @synthesize delay = _delay;
- - (id)initWithStringEncoding:(NSStringEncoding)encoding {
- self = [super init];
- if (!self) {
- return nil;
- }
- self.stringEncoding = encoding;
- self.HTTPBodyParts = [NSMutableArray array];
- self.bufferLength = NSIntegerMax;
-
- self.buffer = [[NSMutableData alloc] init];
- self.bufferLength = AFMultipartBodyStreamProviderDefaultBufferLength;
-
- return self;
- }
- - (void)dealloc {
- _outputStream.delegate = nil;
- }
- - (void)setInitialAndFinalBoundaries {
- if ([self.HTTPBodyParts count] > 0) {
- for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
- bodyPart.hasInitialBoundary = NO;
- bodyPart.hasFinalBoundary = NO;
- }
- [[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
- [[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
- }
- }
- - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart {
- [self.HTTPBodyParts addObject:bodyPart];
- }
- - (NSInputStream *)inputStream {
- if (_inputStream == nil) {
- CFReadStreamRef readStream;
- CFWriteStreamRef writeStream;
- CFStreamCreateBoundPair(NULL, &readStream, &writeStream, self.bufferLength);
- _inputStream = CFBridgingRelease(readStream);
- _outputStream = CFBridgingRelease(writeStream);
-
- _outputStream.delegate = self;
- if ([NSThread isMainThread]) {
- [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
- } else {
- dispatch_sync(dispatch_get_main_queue(), ^{
- [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
- });
- }
- [_outputStream open];
- _self = self;
- }
-
- return _inputStream;
- }
- - (BOOL)isEmpty {
- return [self.HTTPBodyParts count] == 0;
- }
- #pragma mark - NSStreamDelegate
- - (void)stream:(NSStream *)stream
- handleEvent:(NSStreamEvent)eventCode
- {
- if (eventCode & NSStreamEventHasSpaceAvailable) {
- [self handleOutputStreamSpaceAvailable];
- }
- }
- - (void)handleOutputStreamSpaceAvailable {
- while ([_outputStream hasSpaceAvailable]) {
- if ([_buffer length] > 0) {
- NSInteger numberOfBytesWritten = [_outputStream write:[_buffer bytes] maxLength:[_buffer length]];
- if (numberOfBytesWritten < 0) {
- [self close];
- return;
- }
- [_buffer replaceBytesInRange:NSMakeRange(0, numberOfBytesWritten) withBytes:NULL length:0];
- } else {
- if (!self.currentHTTPBodyPart) {
- if (!self.HTTPBodyPartEnumerator) {
- self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
- }
- self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject];
- }
-
- if (!self.currentHTTPBodyPart) {
- [self close];
- return;
- }
-
- [_buffer setLength:self.bufferLength];
-
- NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:[_buffer mutableBytes] maxLength:[_buffer length]];
- if (numberOfBytesRead < 0) {
- [self close];
- return;
- }
-
- [_buffer setLength:numberOfBytesRead];
- if (numberOfBytesRead == 0) {
- self.currentHTTPBodyPart = nil;
- }
- if (self.delay > 0.0f) {
- [NSThread sleepForTimeInterval:self.delay];
- }
- }
- }
- }
- - (void)close {
- [_outputStream close];
- _outputStream.delegate = nil;
-
- _self = nil;
- }
- - (unsigned long long)contentLength {
- unsigned long long length = 0;
- for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
- length += [bodyPart contentLength];
- }
- return length;
- }
- #pragma mark - NSCopying
- - (id)copyWithZone:(NSZone *)zone {
- AFMultipartBodyStreamProvider *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding];
- for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
- [bodyStreamCopy appendHTTPBodyPart:[bodyPart copy]];
- }
- [bodyStreamCopy setInitialAndFinalBoundaries];
- return bodyStreamCopy;
- }
- @end
- #pragma mark -
- typedef enum {
- AFInitialPhase = 0,
- AFEncapsulationBoundaryPhase = 1,
- AFHeaderPhase = 2,
- AFBodyPhase = 3,
- AFFinalBoundaryPhase = 4,
- AFCompletedPhase = 5,
- } AFHTTPBodyPartReadPhase;
- @interface AFHTTPBodyPart () <NSCopying> {
- AFHTTPBodyPartReadPhase _phase;
- NSInputStream *_inputStream;
- unsigned long long _phaseReadOffset;
- }
- - (BOOL)transitionToNextPhase;
- - (NSInteger)readData:(NSData *)data
- intoBuffer:(uint8_t *)buffer
- maxLength:(NSUInteger)length;
- @end
- @implementation AFHTTPBodyPart
- @synthesize stringEncoding = _stringEncoding;
- @synthesize headers = _headers;
- @synthesize body = _body;
- @synthesize bodyContentLength = _bodyContentLength;
- @synthesize hasInitialBoundary = _hasInitialBoundary;
- @synthesize hasFinalBoundary = _hasFinalBoundary;
- - (id)init {
- self = [super init];
- if (!self) {
- return nil;
- }
- [self transitionToNextPhase];
- return self;
- }
- - (void)dealloc {
- if (_inputStream) {
- [_inputStream close];
- _inputStream = nil;
- }
- }
- - (NSInputStream *)inputStream {
- if (!_inputStream) {
- if ([self.body isKindOfClass:[NSData class]]) {
- _inputStream = [NSInputStream inputStreamWithData:self.body];
- } else if ([self.body isKindOfClass:[NSURL class]]) {
- _inputStream = [NSInputStream inputStreamWithURL:self.body];
- } else if ([self.body isKindOfClass:[NSInputStream class]]) {
- _inputStream = self.body;
- }
- }
- return _inputStream;
- }
- - (NSString *)stringForHeaders {
- NSMutableString *headerString = [NSMutableString string];
- for (NSString *field in [self.headers allKeys]) {
- [headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]];
- }
- [headerString appendString:kAFMultipartFormCRLF];
- return [NSString stringWithString:headerString];
- }
- - (unsigned long long)contentLength {
- unsigned long long length = 0;
- NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
- length += [encapsulationBoundaryData length];
- NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding];
- length += [headersData length];
- length += _bodyContentLength;
- NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
- length += [closingBoundaryData length];
- return length;
- }
- - (BOOL)hasBytesAvailable {
- // Allows `read:maxLength:` to be called again if `AFMultipartFormFinalBoundary` doesn't fit into the available buffer
- if (_phase == AFFinalBoundaryPhase) {
- return YES;
- }
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wcovered-switch-default"
- switch (self.inputStream.streamStatus) {
- case NSStreamStatusNotOpen:
- case NSStreamStatusOpening:
- case NSStreamStatusOpen:
- case NSStreamStatusReading:
- case NSStreamStatusWriting:
- return YES;
- case NSStreamStatusAtEnd:
- case NSStreamStatusClosed:
- case NSStreamStatusError:
- default:
- return NO;
- }
- #pragma clang diagnostic pop
- }
- - (NSInteger)read:(uint8_t *)buffer
- maxLength:(NSUInteger)length
- {
- NSInteger bytesRead = 0;
- if (_phase == AFEncapsulationBoundaryPhase) {
- NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
- bytesRead += [self readData:encapsulationBoundaryData intoBuf