appjam 0.1.8.6 → 0.1.8.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/lib/appjam.rb +1 -1
  2. data/lib/appjam/generators/blank.rb +133 -0
  3. data/lib/appjam/generators/help.rb +5 -3
  4. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/project.pbxproj +855 -0
  5. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  6. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  7. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/EiffelApplication.xcscheme +96 -0
  8. data/lib/appjam/generators/templates/blank/EiffelApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +27 -0
  9. data/lib/appjam/generators/templates/blank/EiffelApplication/AppDelegate.h.tt +22 -0
  10. data/lib/appjam/generators/templates/blank/EiffelApplication/AppDelegate.m.tt +156 -0
  11. data/lib/appjam/generators/templates/blank/EiffelApplication/EiffelApplication-Info.plist +45 -0
  12. data/lib/appjam/generators/templates/blank/EiffelApplication/EiffelApplication-Prefix.pch.tt +30 -0
  13. data/lib/appjam/generators/templates/blank/EiffelApplication/en.lproj/InfoPlist.strings +2 -0
  14. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFHTTPClient.h +636 -0
  15. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFHTTPClient.m +1359 -0
  16. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFHTTPRequestOperation.h +133 -0
  17. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFHTTPRequestOperation.m +318 -0
  18. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFImageRequestOperation.h +108 -0
  19. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFImageRequestOperation.m +234 -0
  20. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFJSONRequestOperation.h +71 -0
  21. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFJSONRequestOperation.m +142 -0
  22. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
  23. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFNetworkActivityIndicatorManager.m +145 -0
  24. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFNetworking.h +43 -0
  25. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFPropertyListRequestOperation.h +68 -0
  26. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFPropertyListRequestOperation.m +142 -0
  27. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFURLConnectionOperation.h +379 -0
  28. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFURLConnectionOperation.m +818 -0
  29. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFXMLRequestOperation.h +89 -0
  30. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/AFXMLRequestOperation.m +166 -0
  31. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/UIImageView+AFNetworking.h +78 -0
  32. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/AFNetworking/UIImageView+AFNetworking.m +184 -0
  33. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/JSONKit/JSONKit.h +251 -0
  34. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/JSONKit/JSONKit.m +3067 -0
  35. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabase.h +155 -0
  36. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabase.m +1162 -0
  37. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabaseAdditions.h +37 -0
  38. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabaseAdditions.m +163 -0
  39. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabasePool.h +75 -0
  40. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabasePool.m +244 -0
  41. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabaseQueue.h +38 -0
  42. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMDatabaseQueue.m +176 -0
  43. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMResultSet.h +104 -0
  44. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/frameworks/fmdb/FMResultSet.m +413 -0
  45. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/EGOCache/EGOCache.h +78 -0
  46. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/EGOCache/EGOCache.m +370 -0
  47. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDASLLogger.h +41 -0
  48. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDASLLogger.m +99 -0
  49. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDAbstractDatabaseLogger.h +102 -0
  50. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDAbstractDatabaseLogger.m +727 -0
  51. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDFileLogger.h +334 -0
  52. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDFileLogger.m +1353 -0
  53. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDLog.h +601 -0
  54. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDLog.m +1083 -0
  55. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDTTYLogger.h +167 -0
  56. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/DDTTYLogger.m +1479 -0
  57. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/Extensions/ContextFilterLogFormatter.h +65 -0
  58. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/Extensions/ContextFilterLogFormatter.m +191 -0
  59. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/Extensions/DispatchQueueLogFormatter.h +116 -0
  60. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Lumberjack/Extensions/DispatchQueueLogFormatter.m +251 -0
  61. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/ObjectiveMixin/Mixin.h +33 -0
  62. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/ObjectiveMixin/Mixin.m +122 -0
  63. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/USArrayWrapper.h +72 -0
  64. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/USArrayWrapper.m +305 -0
  65. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/USConstants.h +38 -0
  66. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/USDictionaryWrapper.h +57 -0
  67. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/USDictionaryWrapper.m +188 -0
  68. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/Underscore+Functional.h +89 -0
  69. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/Underscore+Functional.m +261 -0
  70. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/Underscore-Prefix.pch +7 -0
  71. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/Underscore.h +50 -0
  72. data/lib/appjam/generators/templates/blank/EiffelApplication/libs/toolkit/Underscore/Underscore.m +100 -0
  73. data/lib/appjam/generators/templates/blank/EiffelApplication/main.m.tt +18 -0
  74. data/lib/appjam/generators/templates/blank/EiffelApplicationTests/EiffelApplicationTests-Info.plist +22 -0
  75. data/lib/appjam/generators/templates/blank/EiffelApplicationTests/EiffelApplicationTests.h.tt +13 -0
  76. data/lib/appjam/generators/templates/blank/EiffelApplicationTests/EiffelApplicationTests.m.tt +32 -0
  77. data/lib/appjam/generators/templates/blank/EiffelApplicationTests/en.lproj/InfoPlist.strings +2 -0
  78. data/lib/appjam/generators/templates/resources/Default-568h@2x.png +0 -0
  79. data/lib/appjam/generators/templates/resources/Default.png +0 -0
  80. data/lib/appjam/generators/templates/resources/Default@2x.png +0 -0
  81. data/lib/appjam/generators/templates/resources/contents.tt +4 -0
  82. data/lib/appjam/version.rb +1 -1
  83. metadata +462 -326
  84. data/test/helper.rb +0 -132
  85. data/test/test_model_generator.rb +0 -28
  86. data/test/test_project_generator.rb +0 -38
@@ -0,0 +1,1359 @@
1
+ // AFHTTPClient.m
2
+ //
3
+ // Copyright (c) 2011 Gowalla (http://gowalla.com/)
4
+ //
5
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ // of this software and associated documentation files (the "Software"), to deal
7
+ // in the Software without restriction, including without limitation the rights
8
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ // copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+ //
12
+ // The above copyright notice and this permission notice shall be included in
13
+ // all copies or substantial portions of the Software.
14
+ //
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ // THE SOFTWARE.
22
+
23
+ #import <Foundation/Foundation.h>
24
+
25
+ #import "AFHTTPClient.h"
26
+ #import "AFHTTPRequestOperation.h"
27
+
28
+ #import <Availability.h>
29
+
30
+ #ifdef _SYSTEMCONFIGURATION_H
31
+ #import <netinet/in.h>
32
+ #import <netinet6/in6.h>
33
+ #import <arpa/inet.h>
34
+ #import <ifaddrs.h>
35
+ #import <netdb.h>
36
+ #endif
37
+
38
+ #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
39
+ #import <UIKit/UIKit.h>
40
+ #endif
41
+
42
+ #ifdef _SYSTEMCONFIGURATION_H
43
+ NSString * const AFNetworkingReachabilityDidChangeNotification = @"com.alamofire.networking.reachability.change";
44
+ NSString * const AFNetworkingReachabilityNotificationStatusItem = @"AFNetworkingReachabilityNotificationStatusItem";
45
+
46
+ typedef SCNetworkReachabilityRef AFNetworkReachabilityRef;
47
+ typedef void (^AFNetworkReachabilityStatusBlock)(AFNetworkReachabilityStatus status);
48
+ #else
49
+ typedef id AFNetworkReachabilityRef;
50
+ #endif
51
+
52
+ typedef void (^AFCompletionBlock)(void);
53
+
54
+ static NSString * AFBase64EncodedStringFromString(NSString *string) {
55
+ NSData *data = [NSData dataWithBytes:[string UTF8String] length:[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
56
+ NSUInteger length = [data length];
57
+ NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
58
+
59
+ uint8_t *input = (uint8_t *)[data bytes];
60
+ uint8_t *output = (uint8_t *)[mutableData mutableBytes];
61
+
62
+ for (NSUInteger i = 0; i < length; i += 3) {
63
+ NSUInteger value = 0;
64
+ for (NSUInteger j = i; j < (i + 3); j++) {
65
+ value <<= 8;
66
+ if (j < length) {
67
+ value |= (0xFF & input[j]);
68
+ }
69
+ }
70
+
71
+ static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72
+
73
+ NSUInteger idx = (i / 3) * 4;
74
+ output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
75
+ output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
76
+ output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
77
+ output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
78
+ }
79
+
80
+ return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
81
+ }
82
+
83
+ static NSString * AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(NSString *string, NSStringEncoding encoding) {
84
+ static NSString * const kAFCharactersToBeEscaped = @":/?&=;+!@#$()~',*";
85
+ static NSString * const kAFCharactersToLeaveUnescaped = @"[].";
86
+
87
+ return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, (__bridge CFStringRef)kAFCharactersToLeaveUnescaped, (__bridge CFStringRef)kAFCharactersToBeEscaped, CFStringConvertNSStringEncodingToEncoding(encoding));
88
+ }
89
+
90
+ #pragma mark -
91
+
92
+ @interface AFQueryStringPair : NSObject
93
+ @property (readwrite, nonatomic, strong) id field;
94
+ @property (readwrite, nonatomic, strong) id value;
95
+
96
+ - (id)initWithField:(id)field value:(id)value;
97
+
98
+ - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding;
99
+ @end
100
+
101
+ @implementation AFQueryStringPair
102
+ @synthesize field = _field;
103
+ @synthesize value = _value;
104
+
105
+ - (id)initWithField:(id)field value:(id)value {
106
+ self = [super init];
107
+ if (!self) {
108
+ return nil;
109
+ }
110
+
111
+ self.field = field;
112
+ self.value = value;
113
+
114
+ return self;
115
+ }
116
+
117
+ - (NSString *)URLEncodedStringValueWithEncoding:(NSStringEncoding)stringEncoding {
118
+ if (!self.value || [self.value isEqual:[NSNull null]]) {
119
+ return AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding);
120
+ } else {
121
+ return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.field description], stringEncoding), AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([self.value description], stringEncoding)];
122
+ }
123
+ }
124
+
125
+ @end
126
+
127
+ #pragma mark -
128
+
129
+ extern NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary);
130
+ extern NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value);
131
+
132
+ NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *parameters, NSStringEncoding stringEncoding) {
133
+ NSMutableArray *mutablePairs = [NSMutableArray array];
134
+ for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
135
+ [mutablePairs addObject:[pair URLEncodedStringValueWithEncoding:stringEncoding]];
136
+ }
137
+
138
+ return [mutablePairs componentsJoinedByString:@"&"];
139
+ }
140
+
141
+ NSArray * AFQueryStringPairsFromDictionary(NSDictionary *dictionary) {
142
+ return AFQueryStringPairsFromKeyAndValue(nil, dictionary);
143
+ }
144
+
145
+ NSArray * AFQueryStringPairsFromKeyAndValue(NSString *key, id value) {
146
+ NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
147
+
148
+ if ([value isKindOfClass:[NSDictionary class]]) {
149
+ NSDictionary *dictionary = value;
150
+ // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
151
+ NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(caseInsensitiveCompare:)];
152
+ [[[dictionary allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]] enumerateObjectsUsingBlock:^(id nestedKey, __unused NSUInteger idx, __unused BOOL *stop) {
153
+ id nestedValue = [dictionary objectForKey:nestedKey];
154
+ if (nestedValue) {
155
+ [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
156
+ }
157
+ }];
158
+ } else if ([value isKindOfClass:[NSArray class]]) {
159
+ NSArray *array = value;
160
+ [array enumerateObjectsUsingBlock:^(id nestedValue, __unused NSUInteger idx, __unused BOOL *stop) {
161
+ [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
162
+ }];
163
+ } else if ([value isKindOfClass:[NSSet class]]) {
164
+ NSSet *set = value;
165
+ [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
166
+ [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue(key, obj)];
167
+ }];
168
+ } else {
169
+ [mutableQueryStringComponents addObject:[[AFQueryStringPair alloc] initWithField:key value:value]];
170
+ }
171
+
172
+ return mutableQueryStringComponents;
173
+ }
174
+
175
+ @interface AFStreamingMultipartFormData : NSObject <AFMultipartFormData>
176
+ - (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
177
+ stringEncoding:(NSStringEncoding)encoding;
178
+
179
+ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData;
180
+ @end
181
+
182
+ #pragma mark -
183
+
184
+ @interface AFHTTPClient ()
185
+ @property (readwrite, nonatomic, strong) NSURL *baseURL;
186
+ @property (readwrite, nonatomic, strong) NSMutableArray *registeredHTTPOperationClassNames;
187
+ @property (readwrite, nonatomic, strong) NSMutableDictionary *defaultHeaders;
188
+ @property (readwrite, nonatomic, strong) NSURLCredential *defaultCredential;
189
+ @property (readwrite, nonatomic, strong) NSOperationQueue *operationQueue;
190
+ #ifdef _SYSTEMCONFIGURATION_H
191
+ @property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability;
192
+ @property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
193
+ @property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock;
194
+ #endif
195
+
196
+ #ifdef _SYSTEMCONFIGURATION_H
197
+ - (void)startMonitoringNetworkReachability;
198
+ - (void)stopMonitoringNetworkReachability;
199
+ #endif
200
+ @end
201
+
202
+ @implementation AFHTTPClient
203
+ @synthesize baseURL = _baseURL;
204
+ @synthesize stringEncoding = _stringEncoding;
205
+ @synthesize parameterEncoding = _parameterEncoding;
206
+ @synthesize registeredHTTPOperationClassNames = _registeredHTTPOperationClassNames;
207
+ @synthesize defaultHeaders = _defaultHeaders;
208
+ @synthesize defaultCredential = _defaultCredential;
209
+ @synthesize operationQueue = _operationQueue;
210
+ #ifdef _SYSTEMCONFIGURATION_H
211
+ @synthesize networkReachability = _networkReachability;
212
+ @synthesize networkReachabilityStatus = _networkReachabilityStatus;
213
+ @synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock;
214
+ #endif
215
+
216
+ + (instancetype)clientWithBaseURL:(NSURL *)url {
217
+ return [[self alloc] initWithBaseURL:url];
218
+ }
219
+
220
+ - (id)initWithBaseURL:(NSURL *)url {
221
+ NSParameterAssert(url);
222
+
223
+ self = [super init];
224
+ if (!self) {
225
+ return nil;
226
+ }
227
+
228
+ // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
229
+ if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
230
+ url = [url URLByAppendingPathComponent:@""];
231
+ }
232
+
233
+ self.baseURL = url;
234
+
235
+ self.stringEncoding = NSUTF8StringEncoding;
236
+ self.parameterEncoding = AFFormURLParameterEncoding;
237
+
238
+ self.registeredHTTPOperationClassNames = [NSMutableArray array];
239
+
240
+ self.defaultHeaders = [NSMutableDictionary dictionary];
241
+
242
+ // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
243
+ NSMutableArray *acceptLanguagesComponents = [NSMutableArray array];
244
+ [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
245
+ float q = 1.0f - (idx * 0.1f);
246
+ [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g", obj, q]];
247
+ *stop = q <= 0.5f;
248
+ }];
249
+ [self setDefaultHeader:@"Accept-Language" value:[acceptLanguagesComponents componentsJoinedByString:@", "]];
250
+
251
+ #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
252
+ // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
253
+ [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)]];
254
+ #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
255
+ [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]]];
256
+ #endif
257
+
258
+ #ifdef _SYSTEMCONFIGURATION_H
259
+ self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
260
+ [self startMonitoringNetworkReachability];
261
+ #endif
262
+
263
+ self.operationQueue = [[NSOperationQueue alloc] init];
264
+ [self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
265
+
266
+ return self;
267
+ }
268
+
269
+ - (void)dealloc {
270
+ #ifdef _SYSTEMCONFIGURATION_H
271
+ [self stopMonitoringNetworkReachability];
272
+ #endif
273
+ }
274
+
275
+ - (NSString *)description {
276
+ return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue];
277
+ }
278
+
279
+ #pragma mark -
280
+
281
+ #ifdef _SYSTEMCONFIGURATION_H
282
+ static BOOL AFURLHostIsIPAddress(NSURL *url) {
283
+ struct sockaddr_in sa_in;
284
+ struct sockaddr_in6 sa_in6;
285
+
286
+ return [url host] && (inet_pton(AF_INET, [[url host] UTF8String], &sa_in) == 1 || inet_pton(AF_INET6, [[url host] UTF8String], &sa_in6) == 1);
287
+ }
288
+
289
+ static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
290
+ BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
291
+ BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
292
+ BOOL isNetworkReachable = (isReachable && !needsConnection);
293
+
294
+ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
295
+ if (isNetworkReachable == NO) {
296
+ status = AFNetworkReachabilityStatusNotReachable;
297
+ }
298
+ #if TARGET_OS_IPHONE
299
+ else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
300
+ status = AFNetworkReachabilityStatusReachableViaWWAN;
301
+ }
302
+ #endif
303
+ else {
304
+ status = AFNetworkReachabilityStatusReachableViaWiFi;
305
+ }
306
+
307
+ return status;
308
+ }
309
+
310
+ static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
311
+ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
312
+ AFNetworkReachabilityStatusBlock block = (__bridge AFNetworkReachabilityStatusBlock)info;
313
+ if (block) {
314
+ block(status);
315
+ }
316
+
317
+ dispatch_async(dispatch_get_main_queue(), ^{
318
+ NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
319
+ [notificationCenter postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:status] forKey:AFNetworkingReachabilityNotificationStatusItem]];
320
+ });
321
+ }
322
+
323
+ static const void * AFNetworkReachabilityRetainCallback(const void *info) {
324
+ return (__bridge_retained const void *)([(__bridge AFNetworkReachabilityStatusBlock)info copy]);
325
+ }
326
+
327
+ static void AFNetworkReachabilityReleaseCallback(const void *info) {
328
+ if (info) {
329
+ CFRelease(info);
330
+ }
331
+ }
332
+
333
+ - (void)startMonitoringNetworkReachability {
334
+ [self stopMonitoringNetworkReachability];
335
+
336
+ if (!self.baseURL) {
337
+ return;
338
+ }
339
+
340
+ self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
341
+
342
+ if (!self.networkReachability) {
343
+ return;
344
+ }
345
+
346
+ __weak __typeof(&*self)weakSelf = self;
347
+ AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
348
+ __strong __typeof(&*weakSelf)strongSelf = weakSelf;
349
+ if (!strongSelf) {
350
+ return;
351
+ }
352
+
353
+ strongSelf.networkReachabilityStatus = status;
354
+ if (strongSelf.networkReachabilityStatusBlock) {
355
+ strongSelf.networkReachabilityStatusBlock(status);
356
+ }
357
+ };
358
+
359
+ SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
360
+ SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context);
361
+ SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
362
+
363
+ /* 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.
364
+ */
365
+ if (AFURLHostIsIPAddress(self.baseURL)) {
366
+ SCNetworkReachabilityFlags flags;
367
+ SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
368
+ dispatch_async(dispatch_get_main_queue(), ^{
369
+ AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
370
+ callback(status);
371
+ });
372
+ }
373
+ }
374
+
375
+ - (void)stopMonitoringNetworkReachability {
376
+ if (_networkReachability) {
377
+ SCNetworkReachabilityUnscheduleFromRunLoop(_networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
378
+ CFRelease(_networkReachability);
379
+ _networkReachability = NULL;
380
+ }
381
+ }
382
+
383
+ - (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
384
+ self.networkReachabilityStatusBlock = block;
385
+ }
386
+ #endif
387
+
388
+ #pragma mark -
389
+
390
+ - (BOOL)registerHTTPOperationClass:(Class)operationClass {
391
+ if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
392
+ return NO;
393
+ }
394
+
395
+ NSString *className = NSStringFromClass(operationClass);
396
+ [self.registeredHTTPOperationClassNames removeObject:className];
397
+ [self.registeredHTTPOperationClassNames insertObject:className atIndex:0];
398
+
399
+ return YES;
400
+ }
401
+
402
+ - (void)unregisterHTTPOperationClass:(Class)operationClass {
403
+ NSString *className = NSStringFromClass(operationClass);
404
+ [self.registeredHTTPOperationClassNames removeObject:className];
405
+ }
406
+
407
+ #pragma mark -
408
+
409
+ - (NSString *)defaultValueForHeader:(NSString *)header {
410
+ return [self.defaultHeaders valueForKey:header];
411
+ }
412
+
413
+ - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
414
+ [self.defaultHeaders setValue:value forKey:header];
415
+ }
416
+
417
+ - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
418
+ NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
419
+ [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]];
420
+ }
421
+
422
+ - (void)setAuthorizationHeaderWithToken:(NSString *)token {
423
+ [self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
424
+ }
425
+
426
+ - (void)clearAuthorizationHeader {
427
+ [self.defaultHeaders removeObjectForKey:@"Authorization"];
428
+ }
429
+
430
+ #pragma mark -
431
+
432
+ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
433
+ path:(NSString *)path
434
+ parameters:(NSDictionary *)parameters
435
+ {
436
+ NSParameterAssert(method);
437
+
438
+ if (!path) {
439
+ path = @"";
440
+ }
441
+
442
+ NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
443
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
444
+ [request setHTTPMethod:method];
445
+ [request setAllHTTPHeaderFields:self.defaultHeaders];
446
+
447
+ if (parameters) {
448
+ if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
449
+ url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
450
+ [request setURL:url];
451
+ } else {
452
+ NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
453
+ NSError *error = nil;
454
+
455
+ switch (self.parameterEncoding) {
456
+ case AFFormURLParameterEncoding:;
457
+ [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
458
+ [request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
459
+ break;
460
+ case AFJSONParameterEncoding:;
461
+ [request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
462
+ [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
463
+ break;
464
+ case AFPropertyListParameterEncoding:;
465
+ [request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
466
+ [request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]];
467
+ break;
468
+ }
469
+
470
+ if (error) {
471
+ NSLog(@"%@ %@: %@", [self class], NSStringFromSelector(_cmd), error);
472
+ }
473
+ }
474
+ }
475
+
476
+ return request;
477
+ }
478
+
479
+ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
480
+ path:(NSString *)path
481
+ parameters:(NSDictionary *)parameters
482
+ constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
483
+ {
484
+ NSParameterAssert(method);
485
+ NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
486
+
487
+ NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
488
+
489
+ __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding];
490
+
491
+ if (parameters) {
492
+ for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
493
+ NSData *data = nil;
494
+ if ([pair.value isKindOfClass:[NSData class]]) {
495
+ data = pair.value;
496
+ } else if ([pair.value isEqual:[NSNull null]]) {
497
+ data = [NSData data];
498
+ } else {
499
+ data = [[pair.value description] dataUsingEncoding:self.stringEncoding];
500
+ }
501
+
502
+ if (data) {
503
+ [formData appendPartWithFormData:data name:[pair.field description]];
504
+ }
505
+ }
506
+ }
507
+
508
+ if (block) {
509
+ block(formData);
510
+ }
511
+
512
+ return [formData requestByFinalizingMultipartFormData];
513
+ }
514
+
515
+ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
516
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
517
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
518
+ {
519
+ AFHTTPRequestOperation *operation = nil;
520
+
521
+ for (NSString *className in self.registeredHTTPOperationClassNames) {
522
+ Class operationClass = NSClassFromString(className);
523
+ if (operationClass && [operationClass canProcessRequest:urlRequest]) {
524
+ operation = [(AFHTTPRequestOperation *)[operationClass alloc] initWithRequest:urlRequest];
525
+ break;
526
+ }
527
+ }
528
+
529
+ if (!operation) {
530
+ operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
531
+ }
532
+
533
+ [operation setCompletionBlockWithSuccess:success failure:failure];
534
+
535
+ operation.credential = self.defaultCredential;
536
+ #ifdef _AFNETWORKING_PIN_SSL_CERTIFICATES_
537
+ operation.SSLPinningMode = self.defaultSSLPinningMode;
538
+ #endif
539
+
540
+ return operation;
541
+ }
542
+
543
+ #pragma mark -
544
+
545
+ - (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
546
+ [self.operationQueue addOperation:operation];
547
+ }
548
+
549
+ - (void)cancelAllHTTPOperationsWithMethod:(NSString *)method
550
+ path:(NSString *)path
551
+ {
552
+ NSString *pathToBeMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] path];
553
+
554
+ for (NSOperation *operation in [self.operationQueue operations]) {
555
+ if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
556
+ continue;
557
+ }
558
+
559
+ BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]];
560
+ BOOL hasMatchingPath = [[[[(AFHTTPRequestOperation *)operation request] URL] path] isEqual:pathToBeMatched];
561
+
562
+ if (hasMatchingMethod && hasMatchingPath) {
563
+ [operation cancel];
564
+ }
565
+ }
566
+ }
567
+
568
+ - (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
569
+ progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
570
+ completionBlock:(void (^)(NSArray *operations))completionBlock
571
+ {
572
+ NSMutableArray *mutableOperations = [NSMutableArray array];
573
+ for (NSURLRequest *request in urlRequests) {
574
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
575
+ [mutableOperations addObject:operation];
576
+ }
577
+
578
+ [self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock];
579
+ }
580
+
581
+ - (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
582
+ progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
583
+ completionBlock:(void (^)(NSArray *operations))completionBlock
584
+ {
585
+ __block dispatch_group_t dispatchGroup = dispatch_group_create();
586
+ NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
587
+ dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
588
+ if (completionBlock) {
589
+ completionBlock(operations);
590
+ }
591
+ });
592
+ #if !OS_OBJECT_USE_OBJC
593
+ dispatch_release(dispatchGroup);
594
+ #endif
595
+ }];
596
+
597
+ for (AFHTTPRequestOperation *operation in operations) {
598
+ AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy];
599
+ __weak __typeof(&*operation)weakOperation = operation;
600
+ operation.completionBlock = ^{
601
+ __strong __typeof(&*weakOperation)strongOperation = weakOperation;
602
+ dispatch_queue_t queue = strongOperation.successCallbackQueue ?: dispatch_get_main_queue();
603
+ dispatch_group_async(dispatchGroup, queue, ^{
604
+ if (originalCompletionBlock) {
605
+ originalCompletionBlock();
606
+ }
607
+
608
+ __block NSUInteger numberOfFinishedOperations = 0;
609
+ [operations enumerateObjectsUsingBlock:^(id obj, __unused NSUInteger idx, __unused BOOL *stop) {
610
+ if ([(NSOperation *)obj isFinished]) {
611
+ numberOfFinishedOperations++;
612
+ }
613
+ }];
614
+
615
+ if (progressBlock) {
616
+ progressBlock(numberOfFinishedOperations, [operations count]);
617
+ }
618
+
619
+ dispatch_group_leave(dispatchGroup);
620
+ });
621
+ };
622
+
623
+ dispatch_group_enter(dispatchGroup);
624
+ [batchedOperation addDependency:operation];
625
+ }
626
+ [self.operationQueue addOperations:operations waitUntilFinished:NO];
627
+ [self.operationQueue addOperation:batchedOperation];
628
+ }
629
+
630
+ #pragma mark -
631
+
632
+ - (void)getPath:(NSString *)path
633
+ parameters:(NSDictionary *)parameters
634
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
635
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
636
+ {
637
+ NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
638
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
639
+ [self enqueueHTTPRequestOperation:operation];
640
+ }
641
+
642
+ - (void)postPath:(NSString *)path
643
+ parameters:(NSDictionary *)parameters
644
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
645
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
646
+ {
647
+ NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
648
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
649
+ [self enqueueHTTPRequestOperation:operation];
650
+ }
651
+
652
+ - (void)putPath:(NSString *)path
653
+ parameters:(NSDictionary *)parameters
654
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
655
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
656
+ {
657
+ NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
658
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
659
+ [self enqueueHTTPRequestOperation:operation];
660
+ }
661
+
662
+ - (void)deletePath:(NSString *)path
663
+ parameters:(NSDictionary *)parameters
664
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
665
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
666
+ {
667
+ NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
668
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
669
+ [self enqueueHTTPRequestOperation:operation];
670
+ }
671
+
672
+ - (void)patchPath:(NSString *)path
673
+ parameters:(NSDictionary *)parameters
674
+ success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
675
+ failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
676
+ {
677
+ NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters];
678
+ AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
679
+ [self enqueueHTTPRequestOperation:operation];
680
+ }
681
+
682
+ #pragma mark - NSCoding
683
+
684
+ - (id)initWithCoder:(NSCoder *)aDecoder {
685
+ NSURL *baseURL = [aDecoder decodeObjectForKey:@"baseURL"];
686
+
687
+ self = [self initWithBaseURL:baseURL];
688
+ if (!self) {
689
+ return nil;
690
+ }
691
+
692
+ self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"];
693
+ self.parameterEncoding = [aDecoder decodeIntegerForKey:@"parameterEncoding"];
694
+ self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"];
695
+ self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"];
696
+
697
+ return self;
698
+ }
699
+
700
+ - (void)encodeWithCoder:(NSCoder *)aCoder {
701
+ [aCoder encodeObject:self.baseURL forKey:@"baseURL"];
702
+ [aCoder encodeInteger:(NSInteger)self.stringEncoding forKey:@"stringEncoding"];
703
+ [aCoder encodeInteger:self.parameterEncoding forKey:@"parameterEncoding"];
704
+ [aCoder encodeObject:self.registeredHTTPOperationClassNames forKey:@"registeredHTTPOperationClassNames"];
705
+ [aCoder encodeObject:self.defaultHeaders forKey:@"defaultHeaders"];
706
+ }
707
+
708
+ #pragma mark - NSCopying
709
+
710
+ - (id)copyWithZone:(NSZone *)zone {
711
+ AFHTTPClient *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
712
+
713
+ HTTPClient.stringEncoding = self.stringEncoding;
714
+ HTTPClient.parameterEncoding = self.parameterEncoding;
715
+ HTTPClient.registeredHTTPOperationClassNames = [self.registeredHTTPOperationClassNames copyWithZone:zone];
716
+ HTTPClient.defaultHeaders = [self.defaultHeaders copyWithZone:zone];
717
+ #ifdef _SYSTEMCONFIGURATION_H
718
+ HTTPClient.networkReachabilityStatusBlock = self.networkReachabilityStatusBlock;
719
+ #endif
720
+ return HTTPClient;
721
+ }
722
+
723
+ @end
724
+
725
+ #pragma mark -
726
+
727
+ static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
728
+
729
+ static NSString * const kAFMultipartFormCRLF = @"\r\n";
730
+
731
+ static NSInteger const kAFStreamToStreamBufferSize = 1024 * 1024; //1 meg default
732
+
733
+ static inline NSString * AFMultipartFormInitialBoundary() {
734
+ return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
735
+ }
736
+
737
+ static inline NSString * AFMultipartFormEncapsulationBoundary() {
738
+ return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
739
+ }
740
+
741
+ static inline NSString * AFMultipartFormFinalBoundary() {
742
+ return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
743
+ }
744
+
745
+ static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
746
+ #ifdef __UTTYPE__
747
+ NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
748
+ NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
749
+ if (!contentType) {
750
+ return @"application/octet-stream";
751
+ } else {
752
+ return contentType;
753
+ }
754
+ #else
755
+ return @"application/octet-stream";
756
+ #endif
757
+ }
758
+
759
+ NSUInteger const kAFUploadStream3GSuggestedPacketSize = 1024 * 16;
760
+ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
761
+
762
+ @interface AFHTTPBodyPart : NSObject
763
+ @property (nonatomic, assign) NSStringEncoding stringEncoding;
764
+ @property (nonatomic, strong) NSDictionary *headers;
765
+ @property (nonatomic, strong) id body;
766
+ @property (nonatomic, assign) unsigned long long bodyContentLength;
767
+ @property (nonatomic, readonly) NSInputStream *inputStream;
768
+
769
+ @property (nonatomic, assign) BOOL hasInitialBoundary;
770
+ @property (nonatomic, assign) BOOL hasFinalBoundary;
771
+
772
+ @property (nonatomic, readonly, getter = hasBytesAvailable) BOOL bytesAvailable;
773
+ @property (nonatomic, readonly) unsigned long long contentLength;
774
+
775
+ - (NSInteger)read:(uint8_t *)buffer
776
+ maxLength:(NSUInteger)length;
777
+ @end
778
+
779
+ @interface AFMultipartBodyStreamProvider : NSObject
780
+ @property (nonatomic, assign) NSUInteger bufferLength;
781
+ @property (nonatomic, assign) NSTimeInterval delay;
782
+ @property (nonatomic, readonly) NSInputStream *inputStream;
783
+ @property (nonatomic, readonly) unsigned long long contentLength;
784
+ @property (nonatomic, readonly, getter = isEmpty) BOOL empty;
785
+
786
+ - (id)initWithStringEncoding:(NSStringEncoding)encoding;
787
+ - (void)setInitialAndFinalBoundaries;
788
+ - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart;
789
+ @end
790
+
791
+ #pragma mark -
792
+
793
+ @interface AFStreamingMultipartFormData ()
794
+ @property (readwrite, nonatomic, copy) NSMutableURLRequest *request;
795
+ @property (readwrite, nonatomic, strong) AFMultipartBodyStreamProvider *bodyStream;
796
+ @property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding;
797
+ @end
798
+
799
+ @implementation AFStreamingMultipartFormData
800
+ @synthesize request = _request;
801
+ @synthesize bodyStream = _bodyStream;
802
+ @synthesize stringEncoding = _stringEncoding;
803
+
804
+ - (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
805
+ stringEncoding:(NSStringEncoding)encoding
806
+ {
807
+ self = [super init];
808
+ if (!self) {
809
+ return nil;
810
+ }
811
+
812
+ self.request = urlRequest;
813
+ self.stringEncoding = encoding;
814
+ self.bodyStream = [[AFMultipartBodyStreamProvider alloc] initWithStringEncoding:encoding];
815
+
816
+ return self;
817
+ }
818
+
819
+ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
820
+ name:(NSString *)name
821
+ error:(NSError * __autoreleasing *)error
822
+ {
823
+ NSParameterAssert(fileURL);
824
+ NSParameterAssert(name);
825
+
826
+ NSString *fileName = [fileURL lastPathComponent];
827
+ NSString *mimeType = AFContentTypeForPathExtension([fileURL pathExtension]);
828
+
829
+ return [self appendPartWithFileURL:fileURL name:name fileName:fileName mimeType:mimeType error:error];
830
+ }
831
+
832
+ - (BOOL)appendPartWithFileURL:(NSURL *)fileURL
833
+ name:(NSString *)name
834
+ fileName:(NSString *)fileName
835
+ mimeType:(NSString *)mimeType
836
+ error:(NSError * __autoreleasing *)error
837
+ {
838
+ NSParameterAssert(fileURL);
839
+ NSParameterAssert(name);
840
+ NSParameterAssert(fileName);
841
+ NSParameterAssert(mimeType);
842
+
843
+ if (![fileURL isFileURL]) {
844
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"Expected URL to be a file URL", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey];
845
+ if (error != NULL) {
846
+ *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
847
+ }
848
+
849
+ return NO;
850
+ } else if ([fileURL checkResourceIsReachableAndReturnError:error] == NO) {
851
+ NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedStringFromTable(@"File URL not reachable.", @"AFNetworking", nil) forKey:NSLocalizedFailureReasonErrorKey];
852
+ if (error != NULL) {
853
+ *error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
854
+ }
855
+
856
+ return NO;
857
+ }
858
+
859
+ NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
860
+ [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
861
+ [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
862
+
863
+ AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
864
+ bodyPart.stringEncoding = self.stringEncoding;
865
+ bodyPart.headers = mutableHeaders;
866
+ bodyPart.body = fileURL;
867
+
868
+ NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
869
+ bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
870
+
871
+ [self.bodyStream appendHTTPBodyPart:bodyPart];
872
+
873
+ return YES;
874
+ }
875
+
876
+
877
+ - (void)appendPartWithInputStream:(NSInputStream *)inputStream
878
+ name:(NSString *)name
879
+ fileName:(NSString *)fileName
880
+ length:(unsigned long long)length
881
+ mimeType:(NSString *)mimeType
882
+ {
883
+ NSParameterAssert(name);
884
+ NSParameterAssert(fileName);
885
+ NSParameterAssert(mimeType);
886
+
887
+ NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
888
+ [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
889
+ [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
890
+
891
+
892
+ AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
893
+ bodyPart.stringEncoding = self.stringEncoding;
894
+ bodyPart.headers = mutableHeaders;
895
+ bodyPart.body = inputStream;
896
+
897
+ bodyPart.bodyContentLength = length;
898
+
899
+ [self.bodyStream appendHTTPBodyPart:bodyPart];
900
+ }
901
+
902
+ - (void)appendPartWithFileData:(NSData *)data
903
+ name:(NSString *)name
904
+ fileName:(NSString *)fileName
905
+ mimeType:(NSString *)mimeType
906
+ {
907
+ NSParameterAssert(name);
908
+ NSParameterAssert(fileName);
909
+ NSParameterAssert(mimeType);
910
+
911
+ NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
912
+ [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
913
+ [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
914
+
915
+ [self appendPartWithHeaders:mutableHeaders body:data];
916
+ }
917
+
918
+ - (void)appendPartWithFormData:(NSData *)data
919
+ name:(NSString *)name
920
+ {
921
+ NSParameterAssert(name);
922
+
923
+ NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
924
+ [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", name] forKey:@"Content-Disposition"];
925
+
926
+ [self appendPartWithHeaders:mutableHeaders body:data];
927
+ }
928
+
929
+ - (void)appendPartWithHeaders:(NSDictionary *)headers
930
+ body:(NSData *)body
931
+ {
932
+ NSParameterAssert(body);
933
+
934
+ AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
935
+ bodyPart.stringEncoding = self.stringEncoding;
936
+ bodyPart.headers = headers;
937
+ bodyPart.bodyContentLength = [body length];
938
+ bodyPart.body = body;
939
+
940
+ [self.bodyStream appendHTTPBodyPart:bodyPart];
941
+ }
942
+
943
+ - (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
944
+ delay:(NSTimeInterval)delay
945
+ {
946
+ self.bodyStream.bufferLength = numberOfBytes;
947
+ self.bodyStream.delay = delay;
948
+ }
949
+
950
+ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
951
+ if ([self.bodyStream isEmpty]) {
952
+ return self.request;
953
+ }
954
+
955
+ // Reset the initial and final boundaries to ensure correct Content-Length
956
+ [self.bodyStream setInitialAndFinalBoundaries];
957
+
958
+ [self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"];
959
+ [self.request setValue:[NSString stringWithFormat:@"%llu", [self.bodyStream contentLength]] forHTTPHeaderField:@"Content-Length"];
960
+ [self.request setHTTPBodyStream:self.bodyStream.inputStream];
961
+
962
+ return self.request;
963
+ }
964
+
965
+ @end
966
+
967
+ #pragma mark -
968
+
969
+ @interface AFMultipartBodyStreamProvider () <NSCopying, NSStreamDelegate>
970
+ @property (nonatomic, assign) NSStringEncoding stringEncoding;
971
+ @property (nonatomic, strong) NSMutableArray *HTTPBodyParts;
972
+ @property (nonatomic, strong) NSEnumerator *HTTPBodyPartEnumerator;
973
+ @property (nonatomic, strong) AFHTTPBodyPart *currentHTTPBodyPart;
974
+ @property (nonatomic, strong) NSInputStream *inputStream;
975
+ @property (nonatomic, strong) NSOutputStream *outputStream;
976
+ @property (nonatomic, strong) NSMutableData *buffer;
977
+ @end
978
+
979
+ static const NSUInteger AFMultipartBodyStreamProviderDefaultBufferLength = 4096;
980
+
981
+ @implementation AFMultipartBodyStreamProvider {
982
+ @private
983
+ // Workaround for stream delegates being weakly referenced, but otherwise unowned
984
+ __strong id _self;
985
+ }
986
+ @synthesize stringEncoding = _stringEncoding;
987
+ @synthesize HTTPBodyParts = _HTTPBodyParts;
988
+ @synthesize HTTPBodyPartEnumerator = _HTTPBodyPartEnumerator;
989
+ @synthesize currentHTTPBodyPart = _currentHTTPBodyPart;
990
+ @synthesize inputStream = _inputStream;
991
+ @synthesize outputStream = _outputStream;
992
+ @synthesize buffer = _buffer;
993
+ @synthesize bufferLength = _numberOfBytesInPacket;
994
+ @synthesize delay = _delay;
995
+
996
+ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
997
+ self = [super init];
998
+ if (!self) {
999
+ return nil;
1000
+ }
1001
+
1002
+ self.stringEncoding = encoding;
1003
+ self.HTTPBodyParts = [NSMutableArray array];
1004
+ self.bufferLength = NSIntegerMax;
1005
+
1006
+ self.buffer = [[NSMutableData alloc] init];
1007
+ self.bufferLength = AFMultipartBodyStreamProviderDefaultBufferLength;
1008
+
1009
+ return self;
1010
+ }
1011
+
1012
+ - (void)dealloc {
1013
+ _outputStream.delegate = nil;
1014
+ }
1015
+
1016
+ - (void)setInitialAndFinalBoundaries {
1017
+ if ([self.HTTPBodyParts count] > 0) {
1018
+ for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
1019
+ bodyPart.hasInitialBoundary = NO;
1020
+ bodyPart.hasFinalBoundary = NO;
1021
+ }
1022
+
1023
+ [[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
1024
+ [[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
1025
+ }
1026
+ }
1027
+
1028
+ - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart {
1029
+ [self.HTTPBodyParts addObject:bodyPart];
1030
+ }
1031
+
1032
+ - (NSInputStream *)inputStream {
1033
+ if (_inputStream == nil) {
1034
+ CFReadStreamRef readStream;
1035
+ CFWriteStreamRef writeStream;
1036
+ CFStreamCreateBoundPair(NULL, &readStream, &writeStream, self.bufferLength);
1037
+ _inputStream = CFBridgingRelease(readStream);
1038
+ _outputStream = CFBridgingRelease(writeStream);
1039
+
1040
+ _outputStream.delegate = self;
1041
+ if ([NSThread isMainThread]) {
1042
+ [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
1043
+ } else {
1044
+ dispatch_sync(dispatch_get_main_queue(), ^{
1045
+ [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
1046
+ });
1047
+ }
1048
+ [_outputStream open];
1049
+
1050
+ _self = self;
1051
+ }
1052
+
1053
+ return _inputStream;
1054
+ }
1055
+
1056
+ - (BOOL)isEmpty {
1057
+ return [self.HTTPBodyParts count] == 0;
1058
+ }
1059
+
1060
+ #pragma mark - NSStreamDelegate
1061
+
1062
+ - (void)stream:(NSStream *)stream
1063
+ handleEvent:(NSStreamEvent)eventCode
1064
+ {
1065
+ if (eventCode & NSStreamEventHasSpaceAvailable) {
1066
+ [self handleOutputStreamSpaceAvailable];
1067
+ }
1068
+ }
1069
+
1070
+ - (void)handleOutputStreamSpaceAvailable {
1071
+ while ([_outputStream hasSpaceAvailable]) {
1072
+ if ([_buffer length] > 0) {
1073
+ NSInteger numberOfBytesWritten = [_outputStream write:[_buffer bytes] maxLength:[_buffer length]];
1074
+ if (numberOfBytesWritten < 0) {
1075
+ [self close];
1076
+ return;
1077
+ }
1078
+
1079
+ [_buffer replaceBytesInRange:NSMakeRange(0, numberOfBytesWritten) withBytes:NULL length:0];
1080
+ } else {
1081
+ if (!self.currentHTTPBodyPart) {
1082
+ if (!self.HTTPBodyPartEnumerator) {
1083
+ self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
1084
+ }
1085
+ self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject];
1086
+ }
1087
+
1088
+ if (!self.currentHTTPBodyPart) {
1089
+ [self close];
1090
+ return;
1091
+ }
1092
+
1093
+ [_buffer setLength:self.bufferLength];
1094
+
1095
+ NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:[_buffer mutableBytes] maxLength:[_buffer length]];
1096
+ if (numberOfBytesRead < 0) {
1097
+ [self close];
1098
+ return;
1099
+ }
1100
+
1101
+ [_buffer setLength:numberOfBytesRead];
1102
+
1103
+ if(numberOfBytesRead == 0) {
1104
+ self.currentHTTPBodyPart = nil;
1105
+ }
1106
+
1107
+ if (self.delay > 0.0f) {
1108
+ [NSThread sleepForTimeInterval:self.delay];
1109
+ }
1110
+ }
1111
+ }
1112
+ }
1113
+
1114
+ - (void)close {
1115
+ [_outputStream close];
1116
+ _outputStream.delegate = nil;
1117
+
1118
+ _self = nil;
1119
+ }
1120
+
1121
+ - (unsigned long long)contentLength {
1122
+ unsigned long long length = 0;
1123
+ for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
1124
+ length += [bodyPart contentLength];
1125
+ }
1126
+
1127
+ return length;
1128
+ }
1129
+
1130
+ #pragma mark - NSCopying
1131
+
1132
+ - (id)copyWithZone:(NSZone *)zone {
1133
+ AFMultipartBodyStreamProvider *bodyStreamCopy = [[[self class] allocWithZone:zone] initWithStringEncoding:self.stringEncoding];
1134
+
1135
+ for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
1136
+ [bodyStreamCopy appendHTTPBodyPart:[bodyPart copy]];
1137
+ }
1138
+
1139
+ [bodyStreamCopy setInitialAndFinalBoundaries];
1140
+
1141
+ return bodyStreamCopy;
1142
+ }
1143
+
1144
+ @end
1145
+
1146
+ #pragma mark -
1147
+
1148
+ typedef enum {
1149
+ AFInitialPhase = 0,
1150
+ AFEncapsulationBoundaryPhase = 1,
1151
+ AFHeaderPhase = 2,
1152
+ AFBodyPhase = 3,
1153
+ AFFinalBoundaryPhase = 4,
1154
+ AFCompletedPhase = 5,
1155
+ } AFHTTPBodyPartReadPhase;
1156
+
1157
+ @interface AFHTTPBodyPart () <NSCopying> {
1158
+ AFHTTPBodyPartReadPhase _phase;
1159
+ NSInputStream *_inputStream;
1160
+ unsigned long long _phaseReadOffset;
1161
+ }
1162
+
1163
+ - (BOOL)transitionToNextPhase;
1164
+ - (NSInteger)readData:(NSData *)data
1165
+ intoBuffer:(uint8_t *)buffer
1166
+ maxLength:(NSUInteger)length;
1167
+ @end
1168
+
1169
+ @implementation AFHTTPBodyPart
1170
+ @synthesize stringEncoding = _stringEncoding;
1171
+ @synthesize headers = _headers;
1172
+ @synthesize body = _body;
1173
+ @synthesize bodyContentLength = _bodyContentLength;
1174
+ @synthesize hasInitialBoundary = _hasInitialBoundary;
1175
+ @synthesize hasFinalBoundary = _hasFinalBoundary;
1176
+
1177
+ - (id)init {
1178
+ self = [super init];
1179
+ if (!self) {
1180
+ return nil;
1181
+ }
1182
+
1183
+ [self transitionToNextPhase];
1184
+
1185
+ return self;
1186
+ }
1187
+
1188
+ - (void)dealloc {
1189
+ if (_inputStream) {
1190
+ [_inputStream close];
1191
+ _inputStream = nil;
1192
+ }
1193
+ }
1194
+
1195
+ - (NSInputStream *)inputStream {
1196
+ if (!_inputStream) {
1197
+ if ([self.body isKindOfClass:[NSData class]]) {
1198
+ _inputStream = [NSInputStream inputStreamWithData:self.body];
1199
+ } else if ([self.body isKindOfClass:[NSURL class]]) {
1200
+ _inputStream = [NSInputStream inputStreamWithURL:self.body];
1201
+ } else if ([self.body isKindOfClass:[NSInputStream class]]) {
1202
+ _inputStream = self.body;
1203
+ }
1204
+ }
1205
+
1206
+ return _inputStream;
1207
+ }
1208
+
1209
+ - (NSString *)stringForHeaders {
1210
+ NSMutableString *headerString = [NSMutableString string];
1211
+ for (NSString *field in [self.headers allKeys]) {
1212
+ [headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]];
1213
+ }
1214
+ [headerString appendString:kAFMultipartFormCRLF];
1215
+
1216
+ return [NSString stringWithString:headerString];
1217
+ }
1218
+
1219
+ - (unsigned long long)contentLength {
1220
+ unsigned long long length = 0;
1221
+
1222
+ NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
1223
+ length += [encapsulationBoundaryData length];
1224
+
1225
+ NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding];
1226
+ length += [headersData length];
1227
+
1228
+ length += _bodyContentLength;
1229
+
1230
+ NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
1231
+ length += [closingBoundaryData length];
1232
+
1233
+ return length;
1234
+ }
1235
+
1236
+ - (BOOL)hasBytesAvailable {
1237
+ // Allows `read:maxLength:` to be called again if `AFMultipartFormFinalBoundary` doesn't fit into the available buffer
1238
+ if (_phase == AFFinalBoundaryPhase) {
1239
+ return YES;
1240
+ }
1241
+
1242
+ #pragma clang diagnostic push
1243
+ #pragma clang diagnostic ignored "-Wcovered-switch-default"
1244
+ switch (self.inputStream.streamStatus) {
1245
+ case NSStreamStatusNotOpen:
1246
+ case NSStreamStatusOpening:
1247
+ case NSStreamStatusOpen:
1248
+ case NSStreamStatusReading:
1249
+ case NSStreamStatusWriting:
1250
+ return YES;
1251
+ case NSStreamStatusAtEnd:
1252
+ case NSStreamStatusClosed:
1253
+ case NSStreamStatusError:
1254
+ default:
1255
+ return NO;
1256
+ }
1257
+ #pragma clang diagnostic pop
1258
+
1259
+ }
1260
+
1261
+ - (NSInteger)read:(uint8_t *)buffer
1262
+ maxLength:(NSUInteger)length
1263
+ {
1264
+ NSInteger bytesRead = 0;
1265
+
1266
+ if (_phase == AFEncapsulationBoundaryPhase) {
1267
+ NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
1268
+ bytesRead += [self readData:encapsulationBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)];
1269
+ }
1270
+
1271
+ if (_phase == AFHeaderPhase) {
1272
+ NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding];
1273
+ bytesRead += [self readData:headersData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)];
1274
+ }
1275
+
1276
+ if (_phase == AFBodyPhase) {
1277
+ if ([self.inputStream hasBytesAvailable]) {
1278
+ bytesRead += [self.inputStream read:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)];
1279
+ }
1280
+
1281
+ if (![self.inputStream hasBytesAvailable]) {
1282
+ [self transitionToNextPhase];
1283
+ }
1284
+ }
1285
+
1286
+ if (_phase == AFFinalBoundaryPhase) {
1287
+ NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
1288
+ bytesRead += [self readData:closingBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - (NSUInteger)bytesRead)];
1289
+ }
1290
+
1291
+ return bytesRead;
1292
+ }
1293
+
1294
+ - (NSInteger)readData:(NSData *)data
1295
+ intoBuffer:(uint8_t *)buffer
1296
+ maxLength:(NSUInteger)length
1297
+ {
1298
+ NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length));
1299
+ [data getBytes:buffer range:range];
1300
+
1301
+ _phaseReadOffset += range.length;
1302
+
1303
+ if (((NSUInteger)_phaseReadOffset) >= [data length]) {
1304
+ [self transitionToNextPhase];
1305
+ }
1306
+
1307
+ return (NSInteger)range.length;
1308
+ }
1309
+
1310
+ - (BOOL)transitionToNextPhase {
1311
+ if (![[NSThread currentThread] isMainThread]) {
1312
+ [self performSelectorOnMainThread:@selector(transitionToNextPhase) withObject:nil waitUntilDone:YES];
1313
+ return YES;
1314
+ }
1315
+
1316
+ #pragma clang diagnostic push
1317
+ #pragma clang diagnostic ignored "-Wcovered-switch-default"
1318
+ switch (_phase) {
1319
+ case AFInitialPhase:
1320
+ _phase = AFEncapsulationBoundaryPhase;
1321
+ break;
1322
+ case AFEncapsulationBoundaryPhase:
1323
+ _phase = AFHeaderPhase;
1324
+ break;
1325
+ case AFHeaderPhase:
1326
+ [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
1327
+ [self.inputStream open];
1328
+ _phase = AFBodyPhase;
1329
+ break;
1330
+ case AFBodyPhase:
1331
+ [self.inputStream close];
1332
+ _phase = AFFinalBoundaryPhase;
1333
+ break;
1334
+ case AFFinalBoundaryPhase:
1335
+ case AFCompletedPhase:
1336
+ default:
1337
+ _phase = AFCompletedPhase;
1338
+ break;
1339
+ }
1340
+ _phaseReadOffset = 0;
1341
+ #pragma clang diagnostic pop
1342
+
1343
+ return YES;
1344
+ }
1345
+
1346
+ #pragma mark - NSCopying
1347
+
1348
+ - (id)copyWithZone:(NSZone *)zone {
1349
+ AFHTTPBodyPart *bodyPart = [[[self class] allocWithZone:zone] init];
1350
+
1351
+ bodyPart.stringEncoding = self.stringEncoding;
1352
+ bodyPart.headers = self.headers;
1353
+ bodyPart.bodyContentLength = self.bodyContentLength;
1354
+ bodyPart.body = self.body;
1355
+
1356
+ return bodyPart;
1357
+ }
1358
+
1359
+ @end