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,652 @@
|
|
1
|
+
// AFURLConnectionOperation.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 "AFURLConnectionOperation.h"
|
24
|
+
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
25
|
+
#import <UIKit/UIKit.h>
|
26
|
+
#endif
|
27
|
+
|
28
|
+
#if !__has_feature(objc_arc)
|
29
|
+
#error AFNetworking must be built with ARC.
|
30
|
+
// You can turn on ARC for only AFNetworking files by adding -fobjc-arc to the build phase for each of its files.
|
31
|
+
#endif
|
32
|
+
|
33
|
+
typedef enum {
|
34
|
+
AFOperationPausedState = -1,
|
35
|
+
AFOperationReadyState = 1,
|
36
|
+
AFOperationExecutingState = 2,
|
37
|
+
AFOperationFinishedState = 3,
|
38
|
+
} _AFOperationState;
|
39
|
+
|
40
|
+
typedef signed short AFOperationState;
|
41
|
+
|
42
|
+
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
43
|
+
typedef UIBackgroundTaskIdentifier AFBackgroundTaskIdentifier;
|
44
|
+
#else
|
45
|
+
typedef id AFBackgroundTaskIdentifier;
|
46
|
+
#endif
|
47
|
+
|
48
|
+
static NSString * const kAFNetworkingLockName = @"com.alamofire.networking.operation.lock";
|
49
|
+
|
50
|
+
NSString * const AFNetworkingErrorDomain = @"AFNetworkingErrorDomain";
|
51
|
+
NSString * const AFNetworkingOperationFailingURLRequestErrorKey = @"AFNetworkingOperationFailingURLRequestErrorKey";
|
52
|
+
NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"AFNetworkingOperationFailingURLResponseErrorKey";
|
53
|
+
|
54
|
+
NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.networking.operation.start";
|
55
|
+
NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish";
|
56
|
+
|
57
|
+
typedef void (^AFURLConnectionOperationProgressBlock)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected);
|
58
|
+
typedef BOOL (^AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace);
|
59
|
+
typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge);
|
60
|
+
typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse);
|
61
|
+
typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse);
|
62
|
+
|
63
|
+
static inline NSString * AFKeyPathFromOperationState(AFOperationState state) {
|
64
|
+
switch (state) {
|
65
|
+
case AFOperationReadyState:
|
66
|
+
return @"isReady";
|
67
|
+
case AFOperationExecutingState:
|
68
|
+
return @"isExecuting";
|
69
|
+
case AFOperationFinishedState:
|
70
|
+
return @"isFinished";
|
71
|
+
case AFOperationPausedState:
|
72
|
+
return @"isPaused";
|
73
|
+
default:
|
74
|
+
return @"state";
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperationState toState, BOOL isCancelled) {
|
79
|
+
switch (fromState) {
|
80
|
+
case AFOperationReadyState:
|
81
|
+
switch (toState) {
|
82
|
+
case AFOperationPausedState:
|
83
|
+
case AFOperationExecutingState:
|
84
|
+
return YES;
|
85
|
+
case AFOperationFinishedState:
|
86
|
+
return isCancelled;
|
87
|
+
default:
|
88
|
+
return NO;
|
89
|
+
}
|
90
|
+
case AFOperationExecutingState:
|
91
|
+
switch (toState) {
|
92
|
+
case AFOperationPausedState:
|
93
|
+
case AFOperationFinishedState:
|
94
|
+
return YES;
|
95
|
+
default:
|
96
|
+
return NO;
|
97
|
+
}
|
98
|
+
case AFOperationFinishedState:
|
99
|
+
return NO;
|
100
|
+
case AFOperationPausedState:
|
101
|
+
return toState == AFOperationReadyState;
|
102
|
+
default:
|
103
|
+
return YES;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
@interface AFURLConnectionOperation ()
|
108
|
+
@property (readwrite, nonatomic, assign) AFOperationState state;
|
109
|
+
@property (readwrite, nonatomic, assign, getter = isCancelled) BOOL cancelled;
|
110
|
+
@property (readwrite, nonatomic, strong) NSRecursiveLock *lock;
|
111
|
+
@property (readwrite, nonatomic, strong) NSURLConnection *connection;
|
112
|
+
@property (readwrite, nonatomic, strong) NSURLRequest *request;
|
113
|
+
@property (readwrite, nonatomic, strong) NSURLResponse *response;
|
114
|
+
@property (readwrite, nonatomic, strong) NSError *error;
|
115
|
+
@property (readwrite, nonatomic, strong) NSData *responseData;
|
116
|
+
@property (readwrite, nonatomic, copy) NSString *responseString;
|
117
|
+
@property (readwrite, nonatomic, assign) long long totalBytesRead;
|
118
|
+
@property (readwrite, nonatomic, assign) AFBackgroundTaskIdentifier backgroundTaskIdentifier;
|
119
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress;
|
120
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress;
|
121
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock authenticationAgainstProtectionSpace;
|
122
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge;
|
123
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationCacheResponseBlock cacheResponse;
|
124
|
+
@property (readwrite, nonatomic, copy) AFURLConnectionOperationRedirectResponseBlock redirectResponse;
|
125
|
+
|
126
|
+
- (void)operationDidStart;
|
127
|
+
- (void)finish;
|
128
|
+
- (void)cancelConnection;
|
129
|
+
@end
|
130
|
+
|
131
|
+
@implementation AFURLConnectionOperation
|
132
|
+
@synthesize state = _state;
|
133
|
+
@synthesize cancelled = _cancelled;
|
134
|
+
@synthesize connection = _connection;
|
135
|
+
@synthesize runLoopModes = _runLoopModes;
|
136
|
+
@synthesize request = _request;
|
137
|
+
@synthesize response = _response;
|
138
|
+
@synthesize error = _error;
|
139
|
+
@synthesize responseData = _responseData;
|
140
|
+
@synthesize responseString = _responseString;
|
141
|
+
@synthesize totalBytesRead = _totalBytesRead;
|
142
|
+
@dynamic inputStream;
|
143
|
+
@synthesize outputStream = _outputStream;
|
144
|
+
@synthesize backgroundTaskIdentifier = _backgroundTaskIdentifier;
|
145
|
+
@synthesize uploadProgress = _uploadProgress;
|
146
|
+
@synthesize downloadProgress = _downloadProgress;
|
147
|
+
@synthesize authenticationAgainstProtectionSpace = _authenticationAgainstProtectionSpace;
|
148
|
+
@synthesize authenticationChallenge = _authenticationChallenge;
|
149
|
+
@synthesize cacheResponse = _cacheResponse;
|
150
|
+
@synthesize redirectResponse = _redirectResponse;
|
151
|
+
@synthesize lock = _lock;
|
152
|
+
|
153
|
+
+ (void) __attribute__((noreturn)) networkRequestThreadEntryPoint:(id)__unused object {
|
154
|
+
do {
|
155
|
+
@autoreleasepool {
|
156
|
+
[[NSRunLoop currentRunLoop] run];
|
157
|
+
}
|
158
|
+
} while (YES);
|
159
|
+
}
|
160
|
+
|
161
|
+
+ (NSThread *)networkRequestThread {
|
162
|
+
static NSThread *_networkRequestThread = nil;
|
163
|
+
static dispatch_once_t oncePredicate;
|
164
|
+
|
165
|
+
dispatch_once(&oncePredicate, ^{
|
166
|
+
_networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil];
|
167
|
+
[_networkRequestThread start];
|
168
|
+
});
|
169
|
+
|
170
|
+
return _networkRequestThread;
|
171
|
+
}
|
172
|
+
|
173
|
+
- (id)initWithRequest:(NSURLRequest *)urlRequest {
|
174
|
+
self = [super init];
|
175
|
+
if (!self) {
|
176
|
+
return nil;
|
177
|
+
}
|
178
|
+
|
179
|
+
self.lock = [[NSRecursiveLock alloc] init];
|
180
|
+
self.lock.name = kAFNetworkingLockName;
|
181
|
+
|
182
|
+
self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
|
183
|
+
|
184
|
+
self.request = urlRequest;
|
185
|
+
|
186
|
+
self.outputStream = [NSOutputStream outputStreamToMemory];
|
187
|
+
|
188
|
+
self.state = AFOperationReadyState;
|
189
|
+
|
190
|
+
return self;
|
191
|
+
}
|
192
|
+
|
193
|
+
- (void)dealloc {
|
194
|
+
if (_outputStream) {
|
195
|
+
[_outputStream close];
|
196
|
+
_outputStream = nil;
|
197
|
+
}
|
198
|
+
|
199
|
+
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
200
|
+
if (_backgroundTaskIdentifier) {
|
201
|
+
[[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
|
202
|
+
_backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
203
|
+
}
|
204
|
+
#endif
|
205
|
+
}
|
206
|
+
|
207
|
+
- (NSString *)description {
|
208
|
+
return [NSString stringWithFormat:@"<%@: %p, state: %@, cancelled: %@ request: %@, response: %@>", NSStringFromClass([self class]), self, AFKeyPathFromOperationState(self.state), ([self isCancelled] ? @"YES" : @"NO"), self.request, self.response];
|
209
|
+
}
|
210
|
+
|
211
|
+
- (void)setCompletionBlock:(void (^)(void))block {
|
212
|
+
[self.lock lock];
|
213
|
+
if (!block) {
|
214
|
+
[super setCompletionBlock:nil];
|
215
|
+
} else {
|
216
|
+
__weak __typeof(&*self)weakSelf = self;
|
217
|
+
[super setCompletionBlock:^ {
|
218
|
+
block();
|
219
|
+
[weakSelf setCompletionBlock:nil];
|
220
|
+
}];
|
221
|
+
}
|
222
|
+
[self.lock unlock];
|
223
|
+
}
|
224
|
+
|
225
|
+
- (NSInputStream *)inputStream {
|
226
|
+
return self.request.HTTPBodyStream;
|
227
|
+
}
|
228
|
+
|
229
|
+
- (void)setInputStream:(NSInputStream *)inputStream {
|
230
|
+
[self willChangeValueForKey:@"inputStream"];
|
231
|
+
NSMutableURLRequest *mutableRequest = [self.request mutableCopy];
|
232
|
+
mutableRequest.HTTPBodyStream = inputStream;
|
233
|
+
self.request = mutableRequest;
|
234
|
+
[self didChangeValueForKey:@"inputStream"];
|
235
|
+
}
|
236
|
+
|
237
|
+
- (void)setOutputStream:(NSOutputStream *)outputStream {
|
238
|
+
if (outputStream == _outputStream) {
|
239
|
+
return;
|
240
|
+
}
|
241
|
+
|
242
|
+
[self willChangeValueForKey:@"outputStream"];
|
243
|
+
if (_outputStream) {
|
244
|
+
[_outputStream close];
|
245
|
+
}
|
246
|
+
_outputStream = outputStream;
|
247
|
+
[self didChangeValueForKey:@"outputStream"];
|
248
|
+
}
|
249
|
+
|
250
|
+
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
251
|
+
- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler {
|
252
|
+
[self.lock lock];
|
253
|
+
if (!self.backgroundTaskIdentifier) {
|
254
|
+
UIApplication *application = [UIApplication sharedApplication];
|
255
|
+
__weak __typeof(&*self)weakSelf = self;
|
256
|
+
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
|
257
|
+
__strong __typeof(&*weakSelf)strongSelf = weakSelf;
|
258
|
+
|
259
|
+
if (handler) {
|
260
|
+
handler();
|
261
|
+
}
|
262
|
+
|
263
|
+
if (strongSelf) {
|
264
|
+
[strongSelf cancel];
|
265
|
+
|
266
|
+
[application endBackgroundTask:strongSelf.backgroundTaskIdentifier];
|
267
|
+
strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
268
|
+
}
|
269
|
+
}];
|
270
|
+
}
|
271
|
+
[self.lock unlock];
|
272
|
+
}
|
273
|
+
#endif
|
274
|
+
|
275
|
+
- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block {
|
276
|
+
self.uploadProgress = block;
|
277
|
+
}
|
278
|
+
|
279
|
+
- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block {
|
280
|
+
self.downloadProgress = block;
|
281
|
+
}
|
282
|
+
|
283
|
+
- (void)setAuthenticationAgainstProtectionSpaceBlock:(BOOL (^)(NSURLConnection *, NSURLProtectionSpace *))block {
|
284
|
+
self.authenticationAgainstProtectionSpace = block;
|
285
|
+
}
|
286
|
+
|
287
|
+
- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block {
|
288
|
+
self.authenticationChallenge = block;
|
289
|
+
}
|
290
|
+
|
291
|
+
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block {
|
292
|
+
self.cacheResponse = block;
|
293
|
+
}
|
294
|
+
|
295
|
+
- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block {
|
296
|
+
self.redirectResponse = block;
|
297
|
+
}
|
298
|
+
|
299
|
+
- (void)setState:(AFOperationState)state {
|
300
|
+
[self.lock lock];
|
301
|
+
if (AFStateTransitionIsValid(self.state, state, [self isCancelled])) {
|
302
|
+
NSString *oldStateKey = AFKeyPathFromOperationState(self.state);
|
303
|
+
NSString *newStateKey = AFKeyPathFromOperationState(state);
|
304
|
+
|
305
|
+
[self willChangeValueForKey:newStateKey];
|
306
|
+
[self willChangeValueForKey:oldStateKey];
|
307
|
+
_state = state;
|
308
|
+
[self didChangeValueForKey:oldStateKey];
|
309
|
+
[self didChangeValueForKey:newStateKey];
|
310
|
+
|
311
|
+
switch (state) {
|
312
|
+
case AFOperationExecutingState:
|
313
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self];
|
314
|
+
break;
|
315
|
+
case AFOperationFinishedState:
|
316
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self];
|
317
|
+
break;
|
318
|
+
default:
|
319
|
+
break;
|
320
|
+
}
|
321
|
+
}
|
322
|
+
[self.lock unlock];
|
323
|
+
}
|
324
|
+
|
325
|
+
- (NSString *)responseString {
|
326
|
+
[self.lock lock];
|
327
|
+
if (!_responseString && self.response && self.responseData) {
|
328
|
+
NSStringEncoding stringEncoding = NSUTF8StringEncoding;
|
329
|
+
if (self.response.textEncodingName) {
|
330
|
+
CFStringEncoding IANAEncoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)self.response.textEncodingName);
|
331
|
+
if (IANAEncoding != kCFStringEncodingInvalidId) {
|
332
|
+
stringEncoding = CFStringConvertEncodingToNSStringEncoding(IANAEncoding);
|
333
|
+
}
|
334
|
+
}
|
335
|
+
|
336
|
+
self.responseString = [[NSString alloc] initWithData:self.responseData encoding:stringEncoding];
|
337
|
+
}
|
338
|
+
[self.lock unlock];
|
339
|
+
|
340
|
+
return _responseString;
|
341
|
+
}
|
342
|
+
|
343
|
+
- (void)pause {
|
344
|
+
if ([self isPaused] || [self isFinished] || [self isCancelled]) {
|
345
|
+
return;
|
346
|
+
}
|
347
|
+
|
348
|
+
[self.lock lock];
|
349
|
+
|
350
|
+
if ([self isExecuting]) {
|
351
|
+
[self.connection performSelector:@selector(cancel) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
|
352
|
+
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self];
|
353
|
+
}
|
354
|
+
|
355
|
+
self.state = AFOperationPausedState;
|
356
|
+
|
357
|
+
[self.lock unlock];
|
358
|
+
}
|
359
|
+
|
360
|
+
- (BOOL)isPaused {
|
361
|
+
return self.state == AFOperationPausedState;
|
362
|
+
}
|
363
|
+
|
364
|
+
- (void)resume {
|
365
|
+
if (![self isPaused]) {
|
366
|
+
return;
|
367
|
+
}
|
368
|
+
|
369
|
+
[self.lock lock];
|
370
|
+
self.state = AFOperationReadyState;
|
371
|
+
|
372
|
+
[self start];
|
373
|
+
[self.lock unlock];
|
374
|
+
}
|
375
|
+
|
376
|
+
#pragma mark - NSOperation
|
377
|
+
|
378
|
+
- (BOOL)isReady {
|
379
|
+
return self.state == AFOperationReadyState && [super isReady];
|
380
|
+
}
|
381
|
+
|
382
|
+
- (BOOL)isExecuting {
|
383
|
+
return self.state == AFOperationExecutingState;
|
384
|
+
}
|
385
|
+
|
386
|
+
- (BOOL)isFinished {
|
387
|
+
return self.state == AFOperationFinishedState;
|
388
|
+
}
|
389
|
+
|
390
|
+
- (BOOL)isConcurrent {
|
391
|
+
return YES;
|
392
|
+
}
|
393
|
+
|
394
|
+
- (void)start {
|
395
|
+
[self.lock lock];
|
396
|
+
if ([self isReady]) {
|
397
|
+
self.state = AFOperationExecutingState;
|
398
|
+
|
399
|
+
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
|
400
|
+
}
|
401
|
+
[self.lock unlock];
|
402
|
+
}
|
403
|
+
|
404
|
+
- (void)operationDidStart {
|
405
|
+
[self.lock lock];
|
406
|
+
if ([self isCancelled]) {
|
407
|
+
[self finish];
|
408
|
+
} else {
|
409
|
+
self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
|
410
|
+
|
411
|
+
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
|
412
|
+
for (NSString *runLoopMode in self.runLoopModes) {
|
413
|
+
[self.connection scheduleInRunLoop:runLoop forMode:runLoopMode];
|
414
|
+
[self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode];
|
415
|
+
}
|
416
|
+
|
417
|
+
[self.connection start];
|
418
|
+
}
|
419
|
+
[self.lock unlock];
|
420
|
+
}
|
421
|
+
|
422
|
+
- (void)finish {
|
423
|
+
self.state = AFOperationFinishedState;
|
424
|
+
}
|
425
|
+
|
426
|
+
- (void)cancel {
|
427
|
+
[self.lock lock];
|
428
|
+
if (![self isFinished] && ![self isCancelled]) {
|
429
|
+
[self willChangeValueForKey:@"isCancelled"];
|
430
|
+
_cancelled = YES;
|
431
|
+
[super cancel];
|
432
|
+
[self didChangeValueForKey:@"isCancelled"];
|
433
|
+
|
434
|
+
// Cancel the connection on the thread it runs on to prevent race conditions
|
435
|
+
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
|
436
|
+
}
|
437
|
+
[self.lock unlock];
|
438
|
+
}
|
439
|
+
|
440
|
+
- (void)cancelConnection {
|
441
|
+
if (self.connection) {
|
442
|
+
[self.connection cancel];
|
443
|
+
|
444
|
+
// Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate
|
445
|
+
NSDictionary *userInfo = nil;
|
446
|
+
if ([self.request URL]) {
|
447
|
+
userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey];
|
448
|
+
}
|
449
|
+
[self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]];
|
450
|
+
}
|
451
|
+
}
|
452
|
+
|
453
|
+
#pragma mark - NSURLConnectionDelegate
|
454
|
+
|
455
|
+
- (BOOL)connection:(NSURLConnection *)connection
|
456
|
+
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
|
457
|
+
{
|
458
|
+
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
|
459
|
+
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
|
460
|
+
return YES;
|
461
|
+
}
|
462
|
+
#endif
|
463
|
+
|
464
|
+
if (self.authenticationAgainstProtectionSpace) {
|
465
|
+
return self.authenticationAgainstProtectionSpace(connection, protectionSpace);
|
466
|
+
} else if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
|
467
|
+
return NO;
|
468
|
+
} else {
|
469
|
+
return YES;
|
470
|
+
}
|
471
|
+
}
|
472
|
+
|
473
|
+
- (void)connection:(NSURLConnection *)connection
|
474
|
+
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
|
475
|
+
{
|
476
|
+
#ifdef _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
|
477
|
+
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
|
478
|
+
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
|
479
|
+
return;
|
480
|
+
}
|
481
|
+
#endif
|
482
|
+
|
483
|
+
if (self.authenticationChallenge) {
|
484
|
+
self.authenticationChallenge(connection, challenge);
|
485
|
+
} else {
|
486
|
+
if ([challenge previousFailureCount] == 0) {
|
487
|
+
NSURLCredential *credential = nil;
|
488
|
+
|
489
|
+
NSString *username = (__bridge_transfer NSString *)CFURLCopyUserName((__bridge CFURLRef)[self.request URL]);
|
490
|
+
NSString *password = (__bridge_transfer NSString *)CFURLCopyPassword((__bridge CFURLRef)[self.request URL]);
|
491
|
+
|
492
|
+
if (username && password) {
|
493
|
+
credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
|
494
|
+
} else if (username) {
|
495
|
+
credential = [[[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[challenge protectionSpace]] objectForKey:username];
|
496
|
+
} else {
|
497
|
+
credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];
|
498
|
+
}
|
499
|
+
|
500
|
+
if (credential) {
|
501
|
+
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
|
502
|
+
} else {
|
503
|
+
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
|
504
|
+
}
|
505
|
+
} else {
|
506
|
+
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
|
507
|
+
}
|
508
|
+
}
|
509
|
+
}
|
510
|
+
|
511
|
+
- (NSURLRequest *)connection:(NSURLConnection *)connection
|
512
|
+
willSendRequest:(NSURLRequest *)request
|
513
|
+
redirectResponse:(NSURLResponse *)redirectResponse
|
514
|
+
{
|
515
|
+
if (self.redirectResponse) {
|
516
|
+
return self.redirectResponse(connection, request, redirectResponse);
|
517
|
+
} else {
|
518
|
+
return request;
|
519
|
+
}
|
520
|
+
}
|
521
|
+
|
522
|
+
- (void)connection:(NSURLConnection *)__unused connection
|
523
|
+
didSendBodyData:(NSInteger)bytesWritten
|
524
|
+
totalBytesWritten:(NSInteger)totalBytesWritten
|
525
|
+
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
|
526
|
+
{
|
527
|
+
if (self.uploadProgress) {
|
528
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
529
|
+
self.uploadProgress(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
|
530
|
+
});
|
531
|
+
}
|
532
|
+
}
|
533
|
+
|
534
|
+
- (void)connection:(NSURLConnection *)__unused connection
|
535
|
+
didReceiveResponse:(NSURLResponse *)response
|
536
|
+
{
|
537
|
+
self.response = response;
|
538
|
+
|
539
|
+
[self.outputStream open];
|
540
|
+
}
|
541
|
+
|
542
|
+
- (void)connection:(NSURLConnection *)__unused connection
|
543
|
+
didReceiveData:(NSData *)data
|
544
|
+
{
|
545
|
+
self.totalBytesRead += [data length];
|
546
|
+
|
547
|
+
if ([self.outputStream hasSpaceAvailable]) {
|
548
|
+
const uint8_t *dataBuffer = (uint8_t *) [data bytes];
|
549
|
+
[self.outputStream write:&dataBuffer[0] maxLength:[data length]];
|
550
|
+
}
|
551
|
+
|
552
|
+
if (self.downloadProgress) {
|
553
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
554
|
+
self.downloadProgress([data length], self.totalBytesRead, self.response.expectedContentLength);
|
555
|
+
});
|
556
|
+
}
|
557
|
+
}
|
558
|
+
|
559
|
+
- (void)connectionDidFinishLoading:(NSURLConnection *)__unused connection {
|
560
|
+
self.responseData = [self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
|
561
|
+
|
562
|
+
[self.outputStream close];
|
563
|
+
|
564
|
+
[self finish];
|
565
|
+
|
566
|
+
self.connection = nil;
|
567
|
+
}
|
568
|
+
|
569
|
+
- (void)connection:(NSURLConnection *)__unused connection
|
570
|
+
didFailWithError:(NSError *)error
|
571
|
+
{
|
572
|
+
self.error = error;
|
573
|
+
|
574
|
+
[self.outputStream close];
|
575
|
+
|
576
|
+
[self finish];
|
577
|
+
|
578
|
+
self.connection = nil;
|
579
|
+
}
|
580
|
+
|
581
|
+
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
|
582
|
+
willCacheResponse:(NSCachedURLResponse *)cachedResponse
|
583
|
+
{
|
584
|
+
if (self.cacheResponse) {
|
585
|
+
return self.cacheResponse(connection, cachedResponse);
|
586
|
+
} else {
|
587
|
+
if ([self isCancelled]) {
|
588
|
+
return nil;
|
589
|
+
}
|
590
|
+
|
591
|
+
return cachedResponse;
|
592
|
+
}
|
593
|
+
}
|
594
|
+
|
595
|
+
#pragma mark - NSCoding
|
596
|
+
|
597
|
+
- (id)initWithCoder:(NSCoder *)aDecoder {
|
598
|
+
NSURLRequest *request = [aDecoder decodeObjectForKey:@"request"];
|
599
|
+
|
600
|
+
self = [self initWithRequest:request];
|
601
|
+
if (!self) {
|
602
|
+
return nil;
|
603
|
+
}
|
604
|
+
|
605
|
+
self.state = [aDecoder decodeIntegerForKey:@"state"];
|
606
|
+
self.cancelled = [aDecoder decodeBoolForKey:@"isCancelled"];
|
607
|
+
self.response = [aDecoder decodeObjectForKey:@"response"];
|
608
|
+
self.error = [aDecoder decodeObjectForKey:@"error"];
|
609
|
+
self.responseData = [aDecoder decodeObjectForKey:@"responseData"];
|
610
|
+
self.totalBytesRead = [[aDecoder decodeObjectForKey:@"totalBytesRead"] longLongValue];
|
611
|
+
|
612
|
+
return self;
|
613
|
+
}
|
614
|
+
|
615
|
+
- (void)encodeWithCoder:(NSCoder *)aCoder {
|
616
|
+
[self pause];
|
617
|
+
|
618
|
+
[aCoder encodeObject:self.request forKey:@"request"];
|
619
|
+
|
620
|
+
switch (self.state) {
|
621
|
+
case AFOperationExecutingState:
|
622
|
+
case AFOperationPausedState:
|
623
|
+
[aCoder encodeInteger:AFOperationReadyState forKey:@"state"];
|
624
|
+
break;
|
625
|
+
default:
|
626
|
+
[aCoder encodeInteger:self.state forKey:@"state"];
|
627
|
+
break;
|
628
|
+
}
|
629
|
+
|
630
|
+
[aCoder encodeBool:[self isCancelled] forKey:@"isCancelled"];
|
631
|
+
[aCoder encodeObject:self.response forKey:@"response"];
|
632
|
+
[aCoder encodeObject:self.error forKey:@"error"];
|
633
|
+
[aCoder encodeObject:self.responseData forKey:@"responseData"];
|
634
|
+
[aCoder encodeObject:[NSNumber numberWithLongLong:self.totalBytesRead] forKey:@"totalBytesRead"];
|
635
|
+
}
|
636
|
+
|
637
|
+
#pragma mark - NSCopying
|
638
|
+
|
639
|
+
- (id)copyWithZone:(NSZone *)zone {
|
640
|
+
AFURLConnectionOperation *operation = [[[self class] allocWithZone:zone] initWithRequest:self.request];
|
641
|
+
|
642
|
+
operation.uploadProgress = self.uploadProgress;
|
643
|
+
operation.downloadProgress = self.downloadProgress;
|
644
|
+
operation.authenticationAgainstProtectionSpace = self.authenticationAgainstProtectionSpace;
|
645
|
+
operation.authenticationChallenge = self.authenticationChallenge;
|
646
|
+
operation.cacheResponse = self.cacheResponse;
|
647
|
+
operation.redirectResponse = self.redirectResponse;
|
648
|
+
|
649
|
+
return operation;
|
650
|
+
}
|
651
|
+
|
652
|
+
@end
|