afmotion 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +16 -0
- data/AFMotion.gemspec +19 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +53 -0
- data/README.md +135 -0
- data/Rakefile +14 -0
- data/app/app_delegate.rb +6 -0
- data/examples/AppDotNet/.gitignore +13 -0
- data/examples/AppDotNet/Gemfile +4 -0
- data/examples/AppDotNet/Gemfile.lock +48 -0
- data/examples/AppDotNet/Rakefile +10 -0
- data/examples/AppDotNet/app/app_delegate.rb +28 -0
- data/examples/AppDotNet/app/global_timeline_view_controller.rb +84 -0
- data/examples/AppDotNet/app/post.rb +23 -0
- data/examples/AppDotNet/app/post_table_view_cell.rb +44 -0
- data/examples/AppDotNet/app/user.rb +9 -0
- data/examples/AppDotNet/resources/profile-image-placeholder.png +0 -0
- data/examples/AppDotNet/resources/profile-image-placeholder@2x.png +0 -0
- data/examples/AppDotNet/spec/main_spec.rb +9 -0
- data/examples/AppDotNet/vendor/Podfile.lock +11 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.h +574 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.m +1187 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.m +341 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.h +108 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.m +237 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.m +136 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.m +131 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFNetworking.h +44 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.m +145 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.m +652 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.m +169 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.m +180 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/AFNetworking.podspec +28 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/LICENSE +19 -0
- data/examples/AppDotNet/vendor/Pods/AFNetworking/README.md +179 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPClient.h +574 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFImageRequestOperation.h +108 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFNetworking.h +44 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/examples/AppDotNet/vendor/Pods/BuildHeaders/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFHTTPClient.h +574 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFImageRequestOperation.h +108 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFNetworking.h +44 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/examples/AppDotNet/vendor/Pods/Headers/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/examples/AppDotNet/vendor/Pods/Pods-Acknowledgements.markdown +26 -0
- data/examples/AppDotNet/vendor/Pods/Pods-Acknowledgements.plist +56 -0
- data/examples/AppDotNet/vendor/Pods/Pods-prefix.pch +12 -0
- data/examples/AppDotNet/vendor/Pods/Pods-resources.sh +19 -0
- data/examples/AppDotNet/vendor/Pods/Pods.bridgesupport +617 -0
- data/examples/AppDotNet/vendor/Pods/Pods.xcconfig +7 -0
- data/examples/AppDotNet/vendor/Pods/Pods.xcodeproj/project.pbxproj +998 -0
- data/examples/AppDotNet/vendor/Pods/PodsDummy_Pods.m +4 -0
- data/lib/afmotion/http.rb +55 -0
- data/lib/afmotion/http_client.rb +121 -0
- data/lib/afmotion/http_result.rb +25 -0
- data/lib/afmotion/image.rb +10 -0
- data/lib/afmotion/operation.rb +65 -0
- data/lib/afmotion/patch/NSString_NSUrl.rb +11 -0
- data/lib/afmotion/version.rb +5 -0
- data/lib/afmotion.rb +16 -0
- data/pkg/afmotion-0.0.1.gem +0 -0
- data/spec/http_client_spec.rb +104 -0
- data/spec/http_spec.rb +26 -0
- data/spec/operation_spec.rb +41 -0
- data/vendor/Podfile.lock +11 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.h +574 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.m +1187 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.m +341 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.h +108 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.m +237 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.m +136 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.m +131 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworking.h +44 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.m +145 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.m +652 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.m +169 -0
- data/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.m +180 -0
- data/vendor/Pods/AFNetworking/AFNetworking.podspec +28 -0
- data/vendor/Pods/AFNetworking/LICENSE +19 -0
- data/vendor/Pods/AFNetworking/README.md +179 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPClient.h +574 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFImageRequestOperation.h +108 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFNetworking.h +44 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/vendor/Pods/BuildHeaders/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/vendor/Pods/Headers/AFNetworking/AFHTTPClient.h +574 -0
- data/vendor/Pods/Headers/AFNetworking/AFHTTPRequestOperation.h +133 -0
- data/vendor/Pods/Headers/AFNetworking/AFImageRequestOperation.h +108 -0
- data/vendor/Pods/Headers/AFNetworking/AFJSONRequestOperation.h +71 -0
- data/vendor/Pods/Headers/AFNetworking/AFNetworkActivityIndicatorManager.h +75 -0
- data/vendor/Pods/Headers/AFNetworking/AFNetworking.h +44 -0
- data/vendor/Pods/Headers/AFNetworking/AFPropertyListRequestOperation.h +68 -0
- data/vendor/Pods/Headers/AFNetworking/AFURLConnectionOperation.h +295 -0
- data/vendor/Pods/Headers/AFNetworking/AFXMLRequestOperation.h +89 -0
- data/vendor/Pods/Headers/AFNetworking/UIImageView+AFNetworking.h +78 -0
- data/vendor/Pods/Pods-Acknowledgements.markdown +26 -0
- data/vendor/Pods/Pods-Acknowledgements.plist +56 -0
- data/vendor/Pods/Pods-prefix.pch +12 -0
- data/vendor/Pods/Pods-resources.sh +19 -0
- data/vendor/Pods/Pods.bridgesupport +617 -0
- data/vendor/Pods/Pods.xcconfig +7 -0
- data/vendor/Pods/PodsDummy_Pods.m +4 -0
- metadata +218 -0
@@ -0,0 +1,1187 @@
|
|
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 __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, stringEncoding);
|
120
|
+
} else {
|
121
|
+
return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(self.field, 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
|
+
[value enumerateKeysAndObjectsUsingBlock:^(id nestedKey, id nestedValue, BOOL *stop) {
|
150
|
+
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
|
151
|
+
}];
|
152
|
+
} else if([value isKindOfClass:[NSArray class]]) {
|
153
|
+
[value enumerateObjectsUsingBlock:^(id nestedValue, NSUInteger idx, BOOL *stop) {
|
154
|
+
[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
|
155
|
+
}];
|
156
|
+
} else {
|
157
|
+
[mutableQueryStringComponents addObject:[[AFQueryStringPair alloc] initWithField:key value:value]];
|
158
|
+
}
|
159
|
+
|
160
|
+
return mutableQueryStringComponents;
|
161
|
+
}
|
162
|
+
|
163
|
+
@interface AFStreamingMultipartFormData : NSObject <AFMultipartFormData>
|
164
|
+
- (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
|
165
|
+
stringEncoding:(NSStringEncoding)encoding;
|
166
|
+
|
167
|
+
- (NSMutableURLRequest *)requestByFinalizingMultipartFormData;
|
168
|
+
@end
|
169
|
+
|
170
|
+
#pragma mark -
|
171
|
+
|
172
|
+
@interface AFHTTPClient ()
|
173
|
+
@property (readwrite, nonatomic) NSURL *baseURL;
|
174
|
+
@property (readwrite, nonatomic) NSMutableArray *registeredHTTPOperationClassNames;
|
175
|
+
@property (readwrite, nonatomic) NSMutableDictionary *defaultHeaders;
|
176
|
+
@property (readwrite, nonatomic) NSOperationQueue *operationQueue;
|
177
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
178
|
+
@property (readwrite, nonatomic, assign) AFNetworkReachabilityRef networkReachability;
|
179
|
+
@property (readwrite, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
|
180
|
+
@property (readwrite, nonatomic, copy) AFNetworkReachabilityStatusBlock networkReachabilityStatusBlock;
|
181
|
+
#endif
|
182
|
+
|
183
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
184
|
+
- (void)startMonitoringNetworkReachability;
|
185
|
+
- (void)stopMonitoringNetworkReachability;
|
186
|
+
#endif
|
187
|
+
@end
|
188
|
+
|
189
|
+
@implementation AFHTTPClient
|
190
|
+
@synthesize baseURL = _baseURL;
|
191
|
+
@synthesize stringEncoding = _stringEncoding;
|
192
|
+
@synthesize parameterEncoding = _parameterEncoding;
|
193
|
+
@synthesize registeredHTTPOperationClassNames = _registeredHTTPOperationClassNames;
|
194
|
+
@synthesize defaultHeaders = _defaultHeaders;
|
195
|
+
@synthesize operationQueue = _operationQueue;
|
196
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
197
|
+
@synthesize networkReachability = _networkReachability;
|
198
|
+
@synthesize networkReachabilityStatus = _networkReachabilityStatus;
|
199
|
+
@synthesize networkReachabilityStatusBlock = _networkReachabilityStatusBlock;
|
200
|
+
#endif
|
201
|
+
|
202
|
+
+ (AFHTTPClient *)clientWithBaseURL:(NSURL *)url {
|
203
|
+
return [[self alloc] initWithBaseURL:url];
|
204
|
+
}
|
205
|
+
|
206
|
+
- (id)initWithBaseURL:(NSURL *)url {
|
207
|
+
NSParameterAssert(url);
|
208
|
+
|
209
|
+
self = [super init];
|
210
|
+
if (!self) {
|
211
|
+
return nil;
|
212
|
+
}
|
213
|
+
|
214
|
+
// Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
|
215
|
+
if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
|
216
|
+
url = [url URLByAppendingPathComponent:@""];
|
217
|
+
}
|
218
|
+
|
219
|
+
self.baseURL = url;
|
220
|
+
|
221
|
+
self.stringEncoding = NSUTF8StringEncoding;
|
222
|
+
self.parameterEncoding = AFFormURLParameterEncoding;
|
223
|
+
|
224
|
+
self.registeredHTTPOperationClassNames = [NSMutableArray array];
|
225
|
+
|
226
|
+
self.defaultHeaders = [NSMutableDictionary dictionary];
|
227
|
+
|
228
|
+
// Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
229
|
+
NSString *preferredLanguageCodes = [[NSLocale preferredLanguages] componentsJoinedByString:@", "];
|
230
|
+
[self setDefaultHeader:@"Accept-Language" value:[NSString stringWithFormat:@"%@, en-us;q=0.8", preferredLanguageCodes]];
|
231
|
+
|
232
|
+
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
233
|
+
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
|
234
|
+
[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)]];
|
235
|
+
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
|
236
|
+
[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]]];
|
237
|
+
#endif
|
238
|
+
|
239
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
240
|
+
self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
|
241
|
+
[self startMonitoringNetworkReachability];
|
242
|
+
#endif
|
243
|
+
|
244
|
+
self.operationQueue = [[NSOperationQueue alloc] init];
|
245
|
+
[self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
|
246
|
+
|
247
|
+
return self;
|
248
|
+
}
|
249
|
+
|
250
|
+
- (void)dealloc {
|
251
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
252
|
+
[self stopMonitoringNetworkReachability];
|
253
|
+
#endif
|
254
|
+
}
|
255
|
+
|
256
|
+
- (NSString *)description {
|
257
|
+
return [NSString stringWithFormat:@"<%@: %p, baseURL: %@, defaultHeaders: %@, registeredOperationClasses: %@, operationQueue: %@>", NSStringFromClass([self class]), self, [self.baseURL absoluteString], self.defaultHeaders, self.registeredHTTPOperationClassNames, self.operationQueue];
|
258
|
+
}
|
259
|
+
|
260
|
+
#pragma mark -
|
261
|
+
|
262
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
263
|
+
static BOOL AFURLHostIsIPAddress(NSURL *url) {
|
264
|
+
struct sockaddr_in sa_in;
|
265
|
+
struct sockaddr_in6 sa_in6;
|
266
|
+
|
267
|
+
return [url host] && (inet_pton(AF_INET, [[url host] UTF8String], &sa_in) == 1 || inet_pton(AF_INET6, [[url host] UTF8String], &sa_in6) == 1);
|
268
|
+
}
|
269
|
+
|
270
|
+
static AFNetworkReachabilityStatus AFNetworkReachabilityStatusForFlags(SCNetworkReachabilityFlags flags) {
|
271
|
+
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
|
272
|
+
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
|
273
|
+
BOOL isNetworkReachable = (isReachable && !needsConnection);
|
274
|
+
|
275
|
+
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusUnknown;
|
276
|
+
if(isNetworkReachable == NO){
|
277
|
+
status = AFNetworkReachabilityStatusNotReachable;
|
278
|
+
}
|
279
|
+
#if TARGET_OS_IPHONE
|
280
|
+
else if((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0){
|
281
|
+
status = AFNetworkReachabilityStatusReachableViaWWAN;
|
282
|
+
}
|
283
|
+
#endif
|
284
|
+
else {
|
285
|
+
status = AFNetworkReachabilityStatusReachableViaWiFi;
|
286
|
+
}
|
287
|
+
|
288
|
+
return status;
|
289
|
+
}
|
290
|
+
|
291
|
+
static void AFNetworkReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkReachabilityFlags flags, void *info) {
|
292
|
+
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
293
|
+
AFNetworkReachabilityStatusBlock block = (__bridge AFNetworkReachabilityStatusBlock)info;
|
294
|
+
if (block) {
|
295
|
+
block(status);
|
296
|
+
}
|
297
|
+
|
298
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingReachabilityDidChangeNotification object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:status] forKey:AFNetworkingReachabilityNotificationStatusItem]];
|
299
|
+
}
|
300
|
+
|
301
|
+
static const void * AFNetworkReachabilityRetainCallback(const void *info) {
|
302
|
+
return (__bridge_retained const void *)([(__bridge AFNetworkReachabilityStatusBlock)info copy]);
|
303
|
+
}
|
304
|
+
|
305
|
+
static void AFNetworkReachabilityReleaseCallback(const void *info) {}
|
306
|
+
|
307
|
+
- (void)startMonitoringNetworkReachability {
|
308
|
+
[self stopMonitoringNetworkReachability];
|
309
|
+
|
310
|
+
if (!self.baseURL) {
|
311
|
+
return;
|
312
|
+
}
|
313
|
+
|
314
|
+
self.networkReachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [[self.baseURL host] UTF8String]);
|
315
|
+
|
316
|
+
__weak __typeof(&*self)weakSelf = self;
|
317
|
+
AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status){
|
318
|
+
__strong __typeof(&*weakSelf)strongSelf = weakSelf;
|
319
|
+
if (!strongSelf) {
|
320
|
+
return;
|
321
|
+
}
|
322
|
+
|
323
|
+
strongSelf.networkReachabilityStatus = status;
|
324
|
+
if (strongSelf.networkReachabilityStatusBlock) {
|
325
|
+
strongSelf.networkReachabilityStatusBlock(status);
|
326
|
+
}
|
327
|
+
};
|
328
|
+
|
329
|
+
SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
|
330
|
+
SCNetworkReachabilitySetCallback(self.networkReachability, AFNetworkReachabilityCallback, &context);
|
331
|
+
SCNetworkReachabilityScheduleWithRunLoop(self.networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
332
|
+
|
333
|
+
/* 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.
|
334
|
+
*/
|
335
|
+
if (AFURLHostIsIPAddress(self.baseURL)) {
|
336
|
+
SCNetworkReachabilityFlags flags;
|
337
|
+
SCNetworkReachabilityGetFlags(self.networkReachability, &flags);
|
338
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
339
|
+
AFNetworkReachabilityStatus status = AFNetworkReachabilityStatusForFlags(flags);
|
340
|
+
callback(status);
|
341
|
+
});
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
- (void)stopMonitoringNetworkReachability {
|
346
|
+
if (_networkReachability) {
|
347
|
+
SCNetworkReachabilityUnscheduleFromRunLoop(_networkReachability, CFRunLoopGetMain(), (CFStringRef)NSRunLoopCommonModes);
|
348
|
+
CFRelease(_networkReachability);
|
349
|
+
_networkReachability = NULL;
|
350
|
+
}
|
351
|
+
}
|
352
|
+
|
353
|
+
- (void)setReachabilityStatusChangeBlock:(void (^)(AFNetworkReachabilityStatus status))block {
|
354
|
+
self.networkReachabilityStatusBlock = block;
|
355
|
+
}
|
356
|
+
#endif
|
357
|
+
|
358
|
+
#pragma mark -
|
359
|
+
|
360
|
+
- (BOOL)registerHTTPOperationClass:(Class)operationClass {
|
361
|
+
if (![operationClass isSubclassOfClass:[AFHTTPRequestOperation class]]) {
|
362
|
+
return NO;
|
363
|
+
}
|
364
|
+
|
365
|
+
NSString *className = NSStringFromClass(operationClass);
|
366
|
+
[self.registeredHTTPOperationClassNames removeObject:className];
|
367
|
+
[self.registeredHTTPOperationClassNames insertObject:className atIndex:0];
|
368
|
+
|
369
|
+
return YES;
|
370
|
+
}
|
371
|
+
|
372
|
+
- (void)unregisterHTTPOperationClass:(Class)operationClass {
|
373
|
+
NSString *className = NSStringFromClass(operationClass);
|
374
|
+
[self.registeredHTTPOperationClassNames removeObject:className];
|
375
|
+
}
|
376
|
+
|
377
|
+
#pragma mark -
|
378
|
+
|
379
|
+
- (NSString *)defaultValueForHeader:(NSString *)header {
|
380
|
+
return [self.defaultHeaders valueForKey:header];
|
381
|
+
}
|
382
|
+
|
383
|
+
- (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
|
384
|
+
[self.defaultHeaders setValue:value forKey:header];
|
385
|
+
}
|
386
|
+
|
387
|
+
- (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
|
388
|
+
NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
|
389
|
+
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)]];
|
390
|
+
}
|
391
|
+
|
392
|
+
- (void)setAuthorizationHeaderWithToken:(NSString *)token {
|
393
|
+
[self setDefaultHeader:@"Authorization" value:[NSString stringWithFormat:@"Token token=\"%@\"", token]];
|
394
|
+
}
|
395
|
+
|
396
|
+
- (void)clearAuthorizationHeader {
|
397
|
+
[self.defaultHeaders removeObjectForKey:@"Authorization"];
|
398
|
+
}
|
399
|
+
|
400
|
+
#pragma mark -
|
401
|
+
|
402
|
+
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
|
403
|
+
path:(NSString *)path
|
404
|
+
parameters:(NSDictionary *)parameters
|
405
|
+
{
|
406
|
+
NSParameterAssert(method);
|
407
|
+
|
408
|
+
if (!path) {
|
409
|
+
path = @"";
|
410
|
+
}
|
411
|
+
|
412
|
+
NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
|
413
|
+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
|
414
|
+
[request setHTTPMethod:method];
|
415
|
+
[request setAllHTTPHeaderFields:self.defaultHeaders];
|
416
|
+
|
417
|
+
if (parameters) {
|
418
|
+
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) {
|
419
|
+
url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@", AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding)]];
|
420
|
+
[request setURL:url];
|
421
|
+
} else {
|
422
|
+
NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
|
423
|
+
NSError *error = nil;
|
424
|
+
|
425
|
+
switch (self.parameterEncoding) {
|
426
|
+
case AFFormURLParameterEncoding:;
|
427
|
+
[request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
428
|
+
[request setHTTPBody:[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) dataUsingEncoding:self.stringEncoding]];
|
429
|
+
break;
|
430
|
+
case AFJSONParameterEncoding:;
|
431
|
+
[request setValue:[NSString stringWithFormat:@"application/json; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
432
|
+
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error]];
|
433
|
+
break;
|
434
|
+
case AFPropertyListParameterEncoding:;
|
435
|
+
[request setValue:[NSString stringWithFormat:@"application/x-plist; charset=%@", charset] forHTTPHeaderField:@"Content-Type"];
|
436
|
+
[request setHTTPBody:[NSPropertyListSerialization dataWithPropertyList:parameters format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]];
|
437
|
+
break;
|
438
|
+
}
|
439
|
+
|
440
|
+
if (error) {
|
441
|
+
NSLog(@"%@ %@: %@", [self class], NSStringFromSelector(_cmd), error);
|
442
|
+
}
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
return request;
|
447
|
+
}
|
448
|
+
|
449
|
+
- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
|
450
|
+
path:(NSString *)path
|
451
|
+
parameters:(NSDictionary *)parameters
|
452
|
+
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
|
453
|
+
{
|
454
|
+
NSParameterAssert(method);
|
455
|
+
NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
|
456
|
+
|
457
|
+
NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
|
458
|
+
|
459
|
+
__block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:request stringEncoding:self.stringEncoding];
|
460
|
+
|
461
|
+
if (parameters) {
|
462
|
+
for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
|
463
|
+
NSData *data = nil;
|
464
|
+
if ([pair.value isKindOfClass:[NSData class]]) {
|
465
|
+
data = pair.value;
|
466
|
+
} else if ([pair.value isEqual:[NSNull null]]) {
|
467
|
+
data = [NSData data];
|
468
|
+
} else {
|
469
|
+
data = [[pair.value description] dataUsingEncoding:self.stringEncoding];
|
470
|
+
}
|
471
|
+
|
472
|
+
if (data) {
|
473
|
+
[formData appendPartWithFormData:data name:[pair.field description]];
|
474
|
+
}
|
475
|
+
}
|
476
|
+
}
|
477
|
+
|
478
|
+
if (block) {
|
479
|
+
block(formData);
|
480
|
+
}
|
481
|
+
|
482
|
+
return [formData requestByFinalizingMultipartFormData];
|
483
|
+
}
|
484
|
+
|
485
|
+
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
486
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
487
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
488
|
+
{
|
489
|
+
AFHTTPRequestOperation *operation = nil;
|
490
|
+
NSString *className = nil;
|
491
|
+
NSEnumerator *enumerator = [self.registeredHTTPOperationClassNames reverseObjectEnumerator];
|
492
|
+
while (!operation && (className = [enumerator nextObject])) {
|
493
|
+
Class op_class = NSClassFromString(className);
|
494
|
+
if (op_class && [op_class canProcessRequest:urlRequest]) {
|
495
|
+
operation = [(AFHTTPRequestOperation *)[op_class alloc] initWithRequest:urlRequest];
|
496
|
+
}
|
497
|
+
}
|
498
|
+
|
499
|
+
if (!operation) {
|
500
|
+
operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
|
501
|
+
}
|
502
|
+
|
503
|
+
[operation setCompletionBlockWithSuccess:success failure:failure];
|
504
|
+
|
505
|
+
return operation;
|
506
|
+
}
|
507
|
+
|
508
|
+
#pragma mark -
|
509
|
+
|
510
|
+
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
|
511
|
+
[self.operationQueue addOperation:operation];
|
512
|
+
}
|
513
|
+
|
514
|
+
- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method
|
515
|
+
path:(NSString *)path
|
516
|
+
{
|
517
|
+
NSString *URLStringToMatched = [[[self requestWithMethod:(method ?: @"GET") path:path parameters:nil] URL] absoluteString];
|
518
|
+
|
519
|
+
for (NSOperation *operation in [self.operationQueue operations]) {
|
520
|
+
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
|
521
|
+
continue;
|
522
|
+
}
|
523
|
+
|
524
|
+
BOOL hasMatchingMethod = !method || [method isEqualToString:[[(AFHTTPRequestOperation *)operation request] HTTPMethod]];
|
525
|
+
BOOL hasMatchingURL = [[[[(AFHTTPRequestOperation *)operation request] URL] absoluteString] isEqualToString:URLStringToMatched];
|
526
|
+
|
527
|
+
if (hasMatchingMethod && hasMatchingURL) {
|
528
|
+
[operation cancel];
|
529
|
+
}
|
530
|
+
}
|
531
|
+
}
|
532
|
+
|
533
|
+
- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
|
534
|
+
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
535
|
+
completionBlock:(void (^)(NSArray *operations))completionBlock
|
536
|
+
{
|
537
|
+
NSMutableArray *mutableOperations = [NSMutableArray array];
|
538
|
+
for (NSURLRequest *request in urlRequests) {
|
539
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:nil failure:nil];
|
540
|
+
[mutableOperations addObject:operation];
|
541
|
+
}
|
542
|
+
|
543
|
+
[self enqueueBatchOfHTTPRequestOperations:mutableOperations progressBlock:progressBlock completionBlock:completionBlock];
|
544
|
+
}
|
545
|
+
|
546
|
+
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
|
547
|
+
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
|
548
|
+
completionBlock:(void (^)(NSArray *operations))completionBlock
|
549
|
+
{
|
550
|
+
__block dispatch_group_t dispatchGroup = dispatch_group_create();
|
551
|
+
NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
|
552
|
+
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
|
553
|
+
if (completionBlock) {
|
554
|
+
completionBlock(operations);
|
555
|
+
}
|
556
|
+
});
|
557
|
+
#if !OS_OBJECT_USE_OBJC
|
558
|
+
dispatch_release(dispatchGroup);
|
559
|
+
#endif
|
560
|
+
}];
|
561
|
+
|
562
|
+
for (AFHTTPRequestOperation *operation in operations) {
|
563
|
+
AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy];
|
564
|
+
operation.completionBlock = ^{
|
565
|
+
dispatch_queue_t queue = operation.successCallbackQueue ?: dispatch_get_main_queue();
|
566
|
+
dispatch_group_async(dispatchGroup, queue, ^{
|
567
|
+
if (originalCompletionBlock) {
|
568
|
+
originalCompletionBlock();
|
569
|
+
}
|
570
|
+
|
571
|
+
__block NSUInteger numberOfFinishedOperations = 0;
|
572
|
+
[operations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
573
|
+
if ([(NSOperation *)obj isFinished]) {
|
574
|
+
numberOfFinishedOperations++;
|
575
|
+
}
|
576
|
+
}];
|
577
|
+
|
578
|
+
if (progressBlock) {
|
579
|
+
progressBlock(numberOfFinishedOperations, [operations count]);
|
580
|
+
}
|
581
|
+
|
582
|
+
dispatch_group_leave(dispatchGroup);
|
583
|
+
});
|
584
|
+
};
|
585
|
+
|
586
|
+
dispatch_group_enter(dispatchGroup);
|
587
|
+
[batchedOperation addDependency:operation];
|
588
|
+
|
589
|
+
[self enqueueHTTPRequestOperation:operation];
|
590
|
+
}
|
591
|
+
[self.operationQueue addOperation:batchedOperation];
|
592
|
+
}
|
593
|
+
|
594
|
+
#pragma mark -
|
595
|
+
|
596
|
+
- (void)getPath:(NSString *)path
|
597
|
+
parameters:(NSDictionary *)parameters
|
598
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
599
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
600
|
+
{
|
601
|
+
NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];
|
602
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
603
|
+
[self enqueueHTTPRequestOperation:operation];
|
604
|
+
}
|
605
|
+
|
606
|
+
- (void)postPath:(NSString *)path
|
607
|
+
parameters:(NSDictionary *)parameters
|
608
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
609
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
610
|
+
{
|
611
|
+
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];
|
612
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
613
|
+
[self enqueueHTTPRequestOperation:operation];
|
614
|
+
}
|
615
|
+
|
616
|
+
- (void)putPath:(NSString *)path
|
617
|
+
parameters:(NSDictionary *)parameters
|
618
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
619
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
620
|
+
{
|
621
|
+
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters];
|
622
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
623
|
+
[self enqueueHTTPRequestOperation:operation];
|
624
|
+
}
|
625
|
+
|
626
|
+
- (void)deletePath:(NSString *)path
|
627
|
+
parameters:(NSDictionary *)parameters
|
628
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
629
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
630
|
+
{
|
631
|
+
NSURLRequest *request = [self requestWithMethod:@"DELETE" path:path parameters:parameters];
|
632
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
633
|
+
[self enqueueHTTPRequestOperation:operation];
|
634
|
+
}
|
635
|
+
|
636
|
+
- (void)patchPath:(NSString *)path
|
637
|
+
parameters:(NSDictionary *)parameters
|
638
|
+
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
639
|
+
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
640
|
+
{
|
641
|
+
NSURLRequest *request = [self requestWithMethod:@"PATCH" path:path parameters:parameters];
|
642
|
+
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
|
643
|
+
[self enqueueHTTPRequestOperation:operation];
|
644
|
+
}
|
645
|
+
|
646
|
+
#pragma mark - NSCoding
|
647
|
+
|
648
|
+
- (id)initWithCoder:(NSCoder *)aDecoder {
|
649
|
+
NSURL *baseURL = [aDecoder decodeObjectForKey:@"baseURL"];
|
650
|
+
|
651
|
+
self = [self initWithBaseURL:baseURL];
|
652
|
+
if (!self) {
|
653
|
+
return nil;
|
654
|
+
}
|
655
|
+
|
656
|
+
self.stringEncoding = [aDecoder decodeIntegerForKey:@"stringEncoding"];
|
657
|
+
self.parameterEncoding = [aDecoder decodeIntegerForKey:@"parameterEncoding"];
|
658
|
+
self.registeredHTTPOperationClassNames = [aDecoder decodeObjectForKey:@"registeredHTTPOperationClassNames"];
|
659
|
+
self.defaultHeaders = [aDecoder decodeObjectForKey:@"defaultHeaders"];
|
660
|
+
|
661
|
+
return self;
|
662
|
+
}
|
663
|
+
|
664
|
+
- (void)encodeWithCoder:(NSCoder *)aCoder {
|
665
|
+
[aCoder encodeObject:self.baseURL forKey:@"baseURL"];
|
666
|
+
[aCoder encodeInteger:self.stringEncoding forKey:@"stringEncoding"];
|
667
|
+
[aCoder encodeInteger:self.parameterEncoding forKey:@"parameterEncoding"];
|
668
|
+
[aCoder encodeObject:self.registeredHTTPOperationClassNames forKey:@"registeredHTTPOperationClassNames"];
|
669
|
+
[aCoder encodeObject:self.defaultHeaders forKey:@"defaultHeaders"];
|
670
|
+
}
|
671
|
+
|
672
|
+
#pragma mark - NSCopying
|
673
|
+
|
674
|
+
- (id)copyWithZone:(NSZone *)zone {
|
675
|
+
AFHTTPClient *HTTPClient = [[[self class] allocWithZone:zone] initWithBaseURL:self.baseURL];
|
676
|
+
|
677
|
+
HTTPClient.stringEncoding = self.stringEncoding;
|
678
|
+
HTTPClient.parameterEncoding = self.parameterEncoding;
|
679
|
+
HTTPClient.registeredHTTPOperationClassNames = [self.registeredHTTPOperationClassNames copyWithZone:zone];
|
680
|
+
HTTPClient.defaultHeaders = [self.defaultHeaders copyWithZone:zone];
|
681
|
+
#ifdef _SYSTEMCONFIGURATION_H
|
682
|
+
HTTPClient.networkReachabilityStatusBlock = self.networkReachabilityStatusBlock;
|
683
|
+
#endif
|
684
|
+
return HTTPClient;
|
685
|
+
}
|
686
|
+
|
687
|
+
@end
|
688
|
+
|
689
|
+
#pragma mark -
|
690
|
+
|
691
|
+
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";
|
692
|
+
|
693
|
+
static NSString * const kAFMultipartFormCRLF = @"\r\n";
|
694
|
+
|
695
|
+
static NSInteger const kAFStreamToStreamBufferSize = 1024*1024; //1 meg default
|
696
|
+
|
697
|
+
static inline NSString * AFMultipartFormInitialBoundary() {
|
698
|
+
return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
699
|
+
}
|
700
|
+
|
701
|
+
static inline NSString * AFMultipartFormEncapsulationBoundary() {
|
702
|
+
return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
703
|
+
}
|
704
|
+
|
705
|
+
static inline NSString * AFMultipartFormFinalBoundary() {
|
706
|
+
return [NSString stringWithFormat:@"%@--%@--%@", kAFMultipartFormCRLF, kAFMultipartFormBoundary, kAFMultipartFormCRLF];
|
707
|
+
}
|
708
|
+
|
709
|
+
static inline NSString * AFContentTypeForPathExtension(NSString *extension) {
|
710
|
+
#ifdef __UTTYPE__
|
711
|
+
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
|
712
|
+
return (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
|
713
|
+
#else
|
714
|
+
return @"application/octet-stream";
|
715
|
+
#endif
|
716
|
+
}
|
717
|
+
|
718
|
+
NSUInteger const kAFUploadStream3GSuggestedPacketSize = 1024 * 16;
|
719
|
+
NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
|
720
|
+
|
721
|
+
@interface AFHTTPBodyPart : NSObject
|
722
|
+
@property (nonatomic, assign) NSStringEncoding stringEncoding;
|
723
|
+
@property (nonatomic, strong) NSDictionary *headers;
|
724
|
+
@property (nonatomic, strong) NSInputStream *inputStream;
|
725
|
+
@property (nonatomic, assign) unsigned long long bodyContentLength;
|
726
|
+
|
727
|
+
@property (nonatomic, assign) BOOL hasInitialBoundary;
|
728
|
+
@property (nonatomic, assign) BOOL hasFinalBoundary;
|
729
|
+
|
730
|
+
@property (readonly, getter = hasBytesAvailable) BOOL bytesAvailable;
|
731
|
+
@property (readonly) unsigned long long contentLength;
|
732
|
+
|
733
|
+
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length;
|
734
|
+
@end
|
735
|
+
|
736
|
+
@interface AFMultipartBodyStream : NSInputStream <NSStreamDelegate>
|
737
|
+
@property (nonatomic, assign) NSUInteger numberOfBytesInPacket;
|
738
|
+
@property (nonatomic, assign) NSTimeInterval delay;
|
739
|
+
@property (readonly) unsigned long long contentLength;
|
740
|
+
@property (readonly, getter = isEmpty) BOOL empty;
|
741
|
+
|
742
|
+
- (id)initWithStringEncoding:(NSStringEncoding)encoding;
|
743
|
+
- (void)setInitialAndFinalBoundaries;
|
744
|
+
- (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart;
|
745
|
+
@end
|
746
|
+
|
747
|
+
#pragma mark -
|
748
|
+
|
749
|
+
@interface AFStreamingMultipartFormData ()
|
750
|
+
@property (readwrite, nonatomic, copy) NSMutableURLRequest *request;
|
751
|
+
@property (readwrite, nonatomic, strong) AFMultipartBodyStream *bodyStream;
|
752
|
+
@property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding;
|
753
|
+
@end
|
754
|
+
|
755
|
+
@implementation AFStreamingMultipartFormData
|
756
|
+
@synthesize request = _request;
|
757
|
+
@synthesize bodyStream = _bodyStream;
|
758
|
+
@synthesize stringEncoding = _stringEncoding;
|
759
|
+
|
760
|
+
- (id)initWithURLRequest:(NSMutableURLRequest *)urlRequest
|
761
|
+
stringEncoding:(NSStringEncoding)encoding
|
762
|
+
{
|
763
|
+
self = [super init];
|
764
|
+
if (!self) {
|
765
|
+
return nil;
|
766
|
+
}
|
767
|
+
|
768
|
+
self.request = urlRequest;
|
769
|
+
self.stringEncoding = encoding;
|
770
|
+
self.bodyStream = [[AFMultipartBodyStream alloc] initWithStringEncoding:encoding];
|
771
|
+
|
772
|
+
return self;
|
773
|
+
}
|
774
|
+
|
775
|
+
- (BOOL)appendPartWithFileURL:(NSURL *)fileURL
|
776
|
+
name:(NSString *)name
|
777
|
+
error:(NSError * __autoreleasing *)error
|
778
|
+
{
|
779
|
+
NSParameterAssert(fileURL);
|
780
|
+
NSParameterAssert(name);
|
781
|
+
|
782
|
+
if (![fileURL isFileURL]) {
|
783
|
+
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"Expected URL to be a file URL", nil) forKey:NSLocalizedFailureReasonErrorKey];
|
784
|
+
if (error != NULL) {
|
785
|
+
*error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
|
786
|
+
}
|
787
|
+
|
788
|
+
return NO;
|
789
|
+
} else if ([fileURL checkResourceIsReachableAndReturnError:error] == NO) {
|
790
|
+
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"File URL not reachable.", nil) forKey:NSLocalizedFailureReasonErrorKey];
|
791
|
+
if (error != NULL) {
|
792
|
+
*error = [[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo];
|
793
|
+
}
|
794
|
+
|
795
|
+
return NO;
|
796
|
+
}
|
797
|
+
|
798
|
+
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
|
799
|
+
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, [fileURL lastPathComponent]] forKey:@"Content-Disposition"];
|
800
|
+
[mutableHeaders setValue:AFContentTypeForPathExtension([fileURL pathExtension]) forKey:@"Content-Type"];
|
801
|
+
|
802
|
+
AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
|
803
|
+
bodyPart.stringEncoding = self.stringEncoding;
|
804
|
+
bodyPart.headers = mutableHeaders;
|
805
|
+
bodyPart.inputStream = [NSInputStream inputStreamWithURL:fileURL];
|
806
|
+
|
807
|
+
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
|
808
|
+
bodyPart.bodyContentLength = [[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
|
809
|
+
|
810
|
+
[self.bodyStream appendHTTPBodyPart:bodyPart];
|
811
|
+
|
812
|
+
return YES;
|
813
|
+
}
|
814
|
+
|
815
|
+
- (void)appendPartWithFileData:(NSData *)data
|
816
|
+
name:(NSString *)name
|
817
|
+
fileName:(NSString *)fileName
|
818
|
+
mimeType:(NSString *)mimeType
|
819
|
+
{
|
820
|
+
NSParameterAssert(name);
|
821
|
+
NSParameterAssert(fileName);
|
822
|
+
NSParameterAssert(mimeType);
|
823
|
+
|
824
|
+
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
|
825
|
+
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
|
826
|
+
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];
|
827
|
+
|
828
|
+
[self appendPartWithHeaders:mutableHeaders body:data];
|
829
|
+
}
|
830
|
+
|
831
|
+
- (void)appendPartWithFormData:(NSData *)data
|
832
|
+
name:(NSString *)name
|
833
|
+
{
|
834
|
+
NSParameterAssert(name);
|
835
|
+
|
836
|
+
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
|
837
|
+
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"", name] forKey:@"Content-Disposition"];
|
838
|
+
|
839
|
+
[self appendPartWithHeaders:mutableHeaders body:data];
|
840
|
+
}
|
841
|
+
|
842
|
+
- (void)appendPartWithHeaders:(NSDictionary *)headers
|
843
|
+
body:(NSData *)body
|
844
|
+
{
|
845
|
+
AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];
|
846
|
+
bodyPart.stringEncoding = self.stringEncoding;
|
847
|
+
bodyPart.headers = headers;
|
848
|
+
bodyPart.bodyContentLength = [body length];
|
849
|
+
bodyPart.inputStream = [NSInputStream inputStreamWithData:body];
|
850
|
+
|
851
|
+
[self.bodyStream appendHTTPBodyPart:bodyPart];
|
852
|
+
}
|
853
|
+
|
854
|
+
- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
|
855
|
+
delay:(NSTimeInterval)delay
|
856
|
+
{
|
857
|
+
self.bodyStream.numberOfBytesInPacket = numberOfBytes;
|
858
|
+
self.bodyStream.delay = delay;
|
859
|
+
}
|
860
|
+
|
861
|
+
- (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
|
862
|
+
if ([self.bodyStream isEmpty]) {
|
863
|
+
return self.request;
|
864
|
+
}
|
865
|
+
|
866
|
+
// Reset the initial and final boundaries to ensure correct Content-Length
|
867
|
+
[self.bodyStream setInitialAndFinalBoundaries];
|
868
|
+
|
869
|
+
[self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"];
|
870
|
+
[self.request setValue:[NSString stringWithFormat:@"%llu", [self.bodyStream contentLength]] forHTTPHeaderField:@"Content-Length"];
|
871
|
+
[self.request setHTTPBodyStream:self.bodyStream];
|
872
|
+
|
873
|
+
return self.request;
|
874
|
+
}
|
875
|
+
|
876
|
+
@end
|
877
|
+
|
878
|
+
#pragma mark -
|
879
|
+
|
880
|
+
@interface AFMultipartBodyStream ()
|
881
|
+
@property (nonatomic, assign) NSStreamStatus streamStatus;
|
882
|
+
@property (nonatomic, strong) NSError *streamError;
|
883
|
+
|
884
|
+
@property (nonatomic, assign) NSStringEncoding stringEncoding;
|
885
|
+
@property (nonatomic, strong) NSMutableArray *HTTPBodyParts;
|
886
|
+
@property (nonatomic, strong) NSEnumerator *HTTPBodyPartEnumerator;
|
887
|
+
@property (nonatomic, strong) AFHTTPBodyPart *currentHTTPBodyPart;
|
888
|
+
@end
|
889
|
+
|
890
|
+
@implementation AFMultipartBodyStream
|
891
|
+
@synthesize streamStatus = _streamStatus;
|
892
|
+
@synthesize streamError = _streamError;
|
893
|
+
@synthesize stringEncoding = _stringEncoding;
|
894
|
+
@synthesize HTTPBodyParts = _HTTPBodyParts;
|
895
|
+
@synthesize HTTPBodyPartEnumerator = _HTTPBodyPartEnumerator;
|
896
|
+
@synthesize currentHTTPBodyPart = _currentHTTPBodyPart;
|
897
|
+
@synthesize numberOfBytesInPacket = _numberOfBytesInPacket;
|
898
|
+
@synthesize delay = _delay;
|
899
|
+
|
900
|
+
- (id)initWithStringEncoding:(NSStringEncoding)encoding {
|
901
|
+
self = [super init];
|
902
|
+
if (!self) {
|
903
|
+
return nil;
|
904
|
+
}
|
905
|
+
|
906
|
+
self.stringEncoding = encoding;
|
907
|
+
self.HTTPBodyParts = [NSMutableArray array];
|
908
|
+
self.numberOfBytesInPacket = NSIntegerMax;
|
909
|
+
|
910
|
+
return self;
|
911
|
+
}
|
912
|
+
|
913
|
+
- (void)setInitialAndFinalBoundaries {
|
914
|
+
if ([self.HTTPBodyParts count] > 0) {
|
915
|
+
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
|
916
|
+
bodyPart.hasInitialBoundary = NO;
|
917
|
+
bodyPart.hasFinalBoundary = NO;
|
918
|
+
}
|
919
|
+
|
920
|
+
[[self.HTTPBodyParts objectAtIndex:0] setHasInitialBoundary:YES];
|
921
|
+
[[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
|
922
|
+
}
|
923
|
+
}
|
924
|
+
|
925
|
+
- (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart {
|
926
|
+
[self.HTTPBodyParts addObject:bodyPart];
|
927
|
+
}
|
928
|
+
|
929
|
+
- (BOOL)isEmpty {
|
930
|
+
return [self.HTTPBodyParts count] == 0;
|
931
|
+
}
|
932
|
+
|
933
|
+
#pragma mark - NSInputStream
|
934
|
+
|
935
|
+
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
936
|
+
if ([self streamStatus] == NSStreamStatusClosed) {
|
937
|
+
return 0;
|
938
|
+
}
|
939
|
+
|
940
|
+
NSInteger bytesRead = 0;
|
941
|
+
|
942
|
+
while ((NSUInteger)bytesRead < MIN(length, self.numberOfBytesInPacket)) {
|
943
|
+
if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) {
|
944
|
+
if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) {
|
945
|
+
break;
|
946
|
+
}
|
947
|
+
} else {
|
948
|
+
bytesRead += [self.currentHTTPBodyPart read:&buffer[bytesRead] maxLength:length - bytesRead];
|
949
|
+
if (self.delay > 0.0f) {
|
950
|
+
[NSThread sleepForTimeInterval:self.delay];
|
951
|
+
}
|
952
|
+
}
|
953
|
+
}
|
954
|
+
|
955
|
+
return bytesRead;
|
956
|
+
}
|
957
|
+
|
958
|
+
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
|
959
|
+
return NO;
|
960
|
+
}
|
961
|
+
|
962
|
+
- (BOOL)hasBytesAvailable {
|
963
|
+
return [self streamStatus] == NSStreamStatusOpen;
|
964
|
+
}
|
965
|
+
|
966
|
+
#pragma mark - NSStream
|
967
|
+
|
968
|
+
- (void)open {
|
969
|
+
if (self.streamStatus == NSStreamStatusOpen) {
|
970
|
+
return;
|
971
|
+
}
|
972
|
+
|
973
|
+
self.streamStatus = NSStreamStatusOpen;
|
974
|
+
|
975
|
+
[self setInitialAndFinalBoundaries];
|
976
|
+
self.HTTPBodyPartEnumerator = [self.HTTPBodyParts objectEnumerator];
|
977
|
+
}
|
978
|
+
|
979
|
+
- (void)close {
|
980
|
+
self.streamStatus = NSStreamStatusClosed;
|
981
|
+
}
|
982
|
+
|
983
|
+
- (id)propertyForKey:(NSString *)key {
|
984
|
+
return nil;
|
985
|
+
}
|
986
|
+
|
987
|
+
- (BOOL)setProperty:(id)property forKey:(NSString *)key {
|
988
|
+
return NO;
|
989
|
+
}
|
990
|
+
|
991
|
+
- (void)scheduleInRunLoop:(NSRunLoop *)aRunLoop
|
992
|
+
forMode:(NSString *)mode
|
993
|
+
{}
|
994
|
+
|
995
|
+
- (void)removeFromRunLoop:(NSRunLoop *)aRunLoop
|
996
|
+
forMode:(NSString *)mode
|
997
|
+
{}
|
998
|
+
|
999
|
+
- (unsigned long long)contentLength {
|
1000
|
+
unsigned long long length = 0;
|
1001
|
+
for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
|
1002
|
+
length += [bodyPart contentLength];
|
1003
|
+
}
|
1004
|
+
|
1005
|
+
return length;
|
1006
|
+
}
|
1007
|
+
|
1008
|
+
#pragma mark - Undocumented CFReadStream Bridged Methods
|
1009
|
+
|
1010
|
+
- (void)_scheduleInCFRunLoop:(CFRunLoopRef)aRunLoop
|
1011
|
+
forMode:(CFStringRef)aMode
|
1012
|
+
{}
|
1013
|
+
|
1014
|
+
- (void)_unscheduleFromCFRunLoop:(CFRunLoopRef)aRunLoop
|
1015
|
+
forMode:(CFStringRef)aMode
|
1016
|
+
{}
|
1017
|
+
|
1018
|
+
- (BOOL)_setCFClientFlags:(CFOptionFlags)inFlags
|
1019
|
+
callback:(CFReadStreamClientCallBack)inCallback
|
1020
|
+
context:(CFStreamClientContext *)inContext {
|
1021
|
+
return NO;
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
@end
|
1025
|
+
|
1026
|
+
#pragma mark -
|
1027
|
+
|
1028
|
+
typedef enum {
|
1029
|
+
AFEncapsulationBoundaryPhase = 1,
|
1030
|
+
AFHeaderPhase = 2,
|
1031
|
+
AFBodyPhase = 3,
|
1032
|
+
AFFinalBoundaryPhase = 4,
|
1033
|
+
} AFHTTPBodyPartReadPhase;
|
1034
|
+
|
1035
|
+
@interface AFHTTPBodyPart () {
|
1036
|
+
AFHTTPBodyPartReadPhase _phase;
|
1037
|
+
unsigned long long _phaseReadOffset;
|
1038
|
+
}
|
1039
|
+
|
1040
|
+
- (BOOL)transitionToNextPhase;
|
1041
|
+
@end
|
1042
|
+
|
1043
|
+
@implementation AFHTTPBodyPart
|
1044
|
+
@synthesize stringEncoding = _stringEncoding;
|
1045
|
+
@synthesize headers = _headers;
|
1046
|
+
@synthesize bodyContentLength = _bodyContentLength;
|
1047
|
+
@synthesize inputStream = _inputStream;
|
1048
|
+
@synthesize hasInitialBoundary = _hasInitialBoundary;
|
1049
|
+
@synthesize hasFinalBoundary = _hasFinalBoundary;
|
1050
|
+
|
1051
|
+
- (id)init {
|
1052
|
+
self = [super init];
|
1053
|
+
if (!self) {
|
1054
|
+
return nil;
|
1055
|
+
}
|
1056
|
+
|
1057
|
+
[self transitionToNextPhase];
|
1058
|
+
|
1059
|
+
return self;
|
1060
|
+
}
|
1061
|
+
|
1062
|
+
- (void)dealloc {
|
1063
|
+
if (_inputStream) {
|
1064
|
+
[_inputStream close];
|
1065
|
+
_inputStream = nil;
|
1066
|
+
}
|
1067
|
+
}
|
1068
|
+
|
1069
|
+
- (NSString *)stringForHeaders {
|
1070
|
+
NSMutableString *headerString = [NSMutableString string];
|
1071
|
+
for (NSString *field in [self.headers allKeys]) {
|
1072
|
+
[headerString appendString:[NSString stringWithFormat:@"%@: %@%@", field, [self.headers valueForKey:field], kAFMultipartFormCRLF]];
|
1073
|
+
}
|
1074
|
+
[headerString appendString:kAFMultipartFormCRLF];
|
1075
|
+
|
1076
|
+
return [NSString stringWithString:headerString];
|
1077
|
+
}
|
1078
|
+
|
1079
|
+
- (unsigned long long)contentLength {
|
1080
|
+
unsigned long long length = 0;
|
1081
|
+
|
1082
|
+
NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
|
1083
|
+
length += [encapsulationBoundaryData length];
|
1084
|
+
|
1085
|
+
NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding];
|
1086
|
+
length += [headersData length];
|
1087
|
+
|
1088
|
+
length += _bodyContentLength;
|
1089
|
+
|
1090
|
+
NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
|
1091
|
+
length += [closingBoundaryData length];
|
1092
|
+
|
1093
|
+
return length;
|
1094
|
+
}
|
1095
|
+
|
1096
|
+
- (BOOL)hasBytesAvailable {
|
1097
|
+
switch (self.inputStream.streamStatus) {
|
1098
|
+
case NSStreamStatusNotOpen:
|
1099
|
+
case NSStreamStatusOpening:
|
1100
|
+
case NSStreamStatusOpen:
|
1101
|
+
case NSStreamStatusReading:
|
1102
|
+
case NSStreamStatusWriting:
|
1103
|
+
return YES;
|
1104
|
+
case NSStreamStatusAtEnd:
|
1105
|
+
case NSStreamStatusClosed:
|
1106
|
+
case NSStreamStatusError:
|
1107
|
+
return NO;
|
1108
|
+
}
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)length {
|
1112
|
+
NSInteger bytesRead = 0;
|
1113
|
+
|
1114
|
+
if (_phase == AFEncapsulationBoundaryPhase) {
|
1115
|
+
NSData *encapsulationBoundaryData = [([self hasInitialBoundary] ? AFMultipartFormInitialBoundary() : AFMultipartFormEncapsulationBoundary()) dataUsingEncoding:self.stringEncoding];
|
1116
|
+
bytesRead += [self readData:encapsulationBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - bytesRead)];
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
if (_phase == AFHeaderPhase) {
|
1120
|
+
NSData *headersData = [[self stringForHeaders] dataUsingEncoding:self.stringEncoding];
|
1121
|
+
bytesRead += [self readData:headersData intoBuffer:&buffer[bytesRead] maxLength:(length - bytesRead)];
|
1122
|
+
}
|
1123
|
+
|
1124
|
+
if (_phase == AFBodyPhase) {
|
1125
|
+
if ([self.inputStream hasBytesAvailable]) {
|
1126
|
+
bytesRead += [self.inputStream read:&buffer[bytesRead] maxLength:(length - bytesRead)];
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
if (![self.inputStream hasBytesAvailable]) {
|
1130
|
+
[self transitionToNextPhase];
|
1131
|
+
}
|
1132
|
+
}
|
1133
|
+
|
1134
|
+
if (_phase == AFFinalBoundaryPhase) {
|
1135
|
+
NSData *closingBoundaryData = ([self hasFinalBoundary] ? [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding] : [NSData data]);
|
1136
|
+
bytesRead += [self readData:closingBoundaryData intoBuffer:&buffer[bytesRead] maxLength:(length - bytesRead)];
|
1137
|
+
}
|
1138
|
+
|
1139
|
+
return bytesRead;
|
1140
|
+
}
|
1141
|
+
|
1142
|
+
- (NSInteger)readData:(NSData *)data
|
1143
|
+
intoBuffer:(uint8_t *)buffer
|
1144
|
+
maxLength:(NSUInteger)length
|
1145
|
+
{
|
1146
|
+
NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length));
|
1147
|
+
[data getBytes:buffer range:range];
|
1148
|
+
|
1149
|
+
_phaseReadOffset += range.length;
|
1150
|
+
|
1151
|
+
if (((NSUInteger)_phaseReadOffset) >= [data length]) {
|
1152
|
+
[self transitionToNextPhase];
|
1153
|
+
}
|
1154
|
+
|
1155
|
+
return range.length;
|
1156
|
+
}
|
1157
|
+
|
1158
|
+
- (BOOL)transitionToNextPhase {
|
1159
|
+
if (![[NSThread currentThread] isMainThread]) {
|
1160
|
+
[self performSelectorOnMainThread:@selector(transitionToNextPhase) withObject:nil waitUntilDone:YES];
|
1161
|
+
return YES;
|
1162
|
+
}
|
1163
|
+
|
1164
|
+
switch (_phase) {
|
1165
|
+
case AFEncapsulationBoundaryPhase:
|
1166
|
+
_phase = AFHeaderPhase;
|
1167
|
+
break;
|
1168
|
+
case AFHeaderPhase:
|
1169
|
+
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
1170
|
+
[self.inputStream open];
|
1171
|
+
_phase = AFBodyPhase;
|
1172
|
+
break;
|
1173
|
+
case AFBodyPhase:
|
1174
|
+
[self.inputStream close];
|
1175
|
+
_phase = AFFinalBoundaryPhase;
|
1176
|
+
break;
|
1177
|
+
default:
|
1178
|
+
_phase = AFEncapsulationBoundaryPhase;
|
1179
|
+
break;
|
1180
|
+
}
|
1181
|
+
|
1182
|
+
_phaseReadOffset = 0;
|
1183
|
+
|
1184
|
+
return YES;
|
1185
|
+
}
|
1186
|
+
|
1187
|
+
@end
|