afmotion 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +1 -1
- data/Gemfile.lock +21 -28
- data/lib/afmotion/patch/NSURLRequest_params.rb +4 -2
- data/lib/afmotion/version.rb +1 -1
- metadata +2 -53
- data/vendor/Pods/AFNetworking/AFNetworking.podspec +0 -28
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.h +0 -574
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPClient.m +0 -1187
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.h +0 -133
- data/vendor/Pods/AFNetworking/AFNetworking/AFHTTPRequestOperation.m +0 -341
- data/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.h +0 -108
- data/vendor/Pods/AFNetworking/AFNetworking/AFImageRequestOperation.m +0 -237
- data/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.h +0 -71
- data/vendor/Pods/AFNetworking/AFNetworking/AFJSONRequestOperation.m +0 -136
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.h +0 -75
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworkActivityIndicatorManager.m +0 -131
- data/vendor/Pods/AFNetworking/AFNetworking/AFNetworking.h +0 -44
- data/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.h +0 -68
- data/vendor/Pods/AFNetworking/AFNetworking/AFPropertyListRequestOperation.m +0 -145
- data/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.h +0 -295
- data/vendor/Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.m +0 -652
- data/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.h +0 -89
- data/vendor/Pods/AFNetworking/AFNetworking/AFXMLRequestOperation.m +0 -169
- data/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.h +0 -78
- data/vendor/Pods/AFNetworking/AFNetworking/UIImageView+AFNetworking.m +0 -180
- data/vendor/Pods/AFNetworking/LICENSE +0 -19
- data/vendor/Pods/AFNetworking/README.md +0 -179
- data/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPClient.h +0 -574
- data/vendor/Pods/BuildHeaders/AFNetworking/AFHTTPRequestOperation.h +0 -133
- data/vendor/Pods/BuildHeaders/AFNetworking/AFImageRequestOperation.h +0 -108
- data/vendor/Pods/BuildHeaders/AFNetworking/AFJSONRequestOperation.h +0 -71
- data/vendor/Pods/BuildHeaders/AFNetworking/AFNetworkActivityIndicatorManager.h +0 -75
- data/vendor/Pods/BuildHeaders/AFNetworking/AFNetworking.h +0 -44
- data/vendor/Pods/BuildHeaders/AFNetworking/AFPropertyListRequestOperation.h +0 -68
- data/vendor/Pods/BuildHeaders/AFNetworking/AFURLConnectionOperation.h +0 -295
- data/vendor/Pods/BuildHeaders/AFNetworking/AFXMLRequestOperation.h +0 -89
- data/vendor/Pods/BuildHeaders/AFNetworking/UIImageView+AFNetworking.h +0 -78
- data/vendor/Pods/Headers/AFNetworking/AFHTTPClient.h +0 -574
- data/vendor/Pods/Headers/AFNetworking/AFHTTPRequestOperation.h +0 -133
- data/vendor/Pods/Headers/AFNetworking/AFImageRequestOperation.h +0 -108
- data/vendor/Pods/Headers/AFNetworking/AFJSONRequestOperation.h +0 -71
- data/vendor/Pods/Headers/AFNetworking/AFNetworkActivityIndicatorManager.h +0 -75
- data/vendor/Pods/Headers/AFNetworking/AFNetworking.h +0 -44
- data/vendor/Pods/Headers/AFNetworking/AFPropertyListRequestOperation.h +0 -68
- data/vendor/Pods/Headers/AFNetworking/AFURLConnectionOperation.h +0 -295
- data/vendor/Pods/Headers/AFNetworking/AFXMLRequestOperation.h +0 -89
- data/vendor/Pods/Headers/AFNetworking/UIImageView+AFNetworking.h +0 -78
- data/vendor/Pods/Pods-Acknowledgements.markdown +0 -26
- data/vendor/Pods/Pods-Acknowledgements.plist +0 -56
- data/vendor/Pods/Pods-prefix.pch +0 -14
- data/vendor/Pods/Pods-resources.sh +0 -19
- data/vendor/Pods/Pods.bridgesupport +0 -617
- data/vendor/Pods/Pods.xcconfig +0 -7
- data/vendor/Pods/PodsDummy_Pods.m +0 -4
@@ -1,295 +0,0 @@
|
|
1
|
-
// AFURLConnectionOperation.h
|
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 <Availability.h>
|
26
|
-
|
27
|
-
/**
|
28
|
-
`AFURLConnectionOperation` is a subclass of `NSOperation` that implements `NSURLConnection` delegate methods.
|
29
|
-
|
30
|
-
## Subclassing Notes
|
31
|
-
|
32
|
-
This is the base class of all network request operations. You may wish to create your own subclass in order to implement additional `NSURLConnection` delegate methods (see "`NSURLConnection` Delegate Methods" below), or to provide additional properties and/or class constructors.
|
33
|
-
|
34
|
-
If you are creating a subclass that communicates over the HTTP or HTTPS protocols, you may want to consider subclassing `AFHTTPRequestOperation` instead, as it supports specifying acceptable content types or status codes.
|
35
|
-
|
36
|
-
## NSURLConnection Delegate Methods
|
37
|
-
|
38
|
-
`AFURLConnectionOperation` implements the following `NSURLConnection` delegate methods:
|
39
|
-
|
40
|
-
- `connection:didReceiveResponse:`
|
41
|
-
- `connection:didReceiveData:`
|
42
|
-
- `connectionDidFinishLoading:`
|
43
|
-
- `connection:didFailWithError:`
|
44
|
-
- `connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:`
|
45
|
-
- `connection:willCacheResponse:`
|
46
|
-
- `connection:canAuthenticateAgainstProtectionSpace:`
|
47
|
-
- `connection:didReceiveAuthenticationChallenge:`
|
48
|
-
|
49
|
-
If any of these methods are overridden in a subclass, they _must_ call the `super` implementation first.
|
50
|
-
|
51
|
-
## Class Constructors
|
52
|
-
|
53
|
-
Class constructors, or methods that return an unowned instance, are the preferred way for subclasses to encapsulate any particular logic for handling the setup or parsing of response data. For instance, `AFJSONRequestOperation` provides `JSONRequestOperationWithRequest:success:failure:`, which takes block arguments, whose parameter on for a successful request is the JSON object initialized from the `response data`.
|
54
|
-
|
55
|
-
## Callbacks and Completion Blocks
|
56
|
-
|
57
|
-
The built-in `completionBlock` provided by `NSOperation` allows for custom behavior to be executed after the request finishes. It is a common pattern for class constructors in subclasses to take callback block parameters, and execute them conditionally in the body of its `completionBlock`. Make sure to handle cancelled operations appropriately when setting a `completionBlock` (i.e. returning early before parsing response data). See the implementation of any of the `AFHTTPRequestOperation` subclasses for an example of this.
|
58
|
-
|
59
|
-
Subclasses are strongly discouraged from overriding `setCompletionBlock:`, as `AFURLConnectionOperation`'s implementation includes a workaround to mitigate retain cycles, and what Apple rather ominously refers to as ["The Deallocation Problem"](http://developer.apple.com/library/ios/#technotes/tn2109/).
|
60
|
-
|
61
|
-
## NSCoding & NSCopying Conformance
|
62
|
-
|
63
|
-
`AFURLConnectionOperation` conforms to the `NSCoding` and `NSCopying` protocols, allowing operations to be archived to disk, and copied in memory, respectively. However, because of the intrinsic limitations of capturing the exact state of an operation at a particular moment, there are some important caveats to keep in mind:
|
64
|
-
|
65
|
-
### NSCoding Caveats
|
66
|
-
|
67
|
-
- Encoded operations do not include any block or stream properties. Be sure to set `completionBlock`, `outputStream`, and any callback blocks as necessary when using `-initWithCoder:` or `NSKeyedUnarchiver`.
|
68
|
-
- Operations are paused on `encodeWithCoder:`. If the operation was encoded while paused or still executing, its archived state will return `YES` for `isReady`. Otherwise, the state of an operation when encoding will remain unchanged.
|
69
|
-
|
70
|
-
### NSCopying Caveats
|
71
|
-
|
72
|
-
- `-copy` and `-copyWithZone:` return a new operation with the `NSURLRequest` of the original. So rather than an exact copy of the operation at that particular instant, the copying mechanism returns a completely new instance, which can be useful for retrying operations.
|
73
|
-
- A copy of an operation will not include the `outputStream` of the original.
|
74
|
-
- Operation copies do not include `completionBlock`. `completionBlock` often strongly captures a reference to `self`, which would otherwise have the unintuitive side-effect of pointing to the _original_ operation when copied.
|
75
|
-
*/
|
76
|
-
@interface AFURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSCoding, NSCopying>
|
77
|
-
|
78
|
-
///-------------------------------
|
79
|
-
/// @name Accessing Run Loop Modes
|
80
|
-
///-------------------------------
|
81
|
-
|
82
|
-
/**
|
83
|
-
The run loop modes in which the operation will run on the network thread. By default, this is a single-member set containing `NSRunLoopCommonModes`.
|
84
|
-
*/
|
85
|
-
@property (nonatomic, strong) NSSet *runLoopModes;
|
86
|
-
|
87
|
-
///-----------------------------------------
|
88
|
-
/// @name Getting URL Connection Information
|
89
|
-
///-----------------------------------------
|
90
|
-
|
91
|
-
/**
|
92
|
-
The request used by the operation's connection.
|
93
|
-
*/
|
94
|
-
@property (readonly, nonatomic, strong) NSURLRequest *request;
|
95
|
-
|
96
|
-
/**
|
97
|
-
The last response received by the operation's connection.
|
98
|
-
*/
|
99
|
-
@property (readonly, nonatomic, strong) NSURLResponse *response;
|
100
|
-
|
101
|
-
/**
|
102
|
-
The error, if any, that occurred in the lifecycle of the request.
|
103
|
-
*/
|
104
|
-
@property (readonly, nonatomic, strong) NSError *error;
|
105
|
-
|
106
|
-
///----------------------------
|
107
|
-
/// @name Getting Response Data
|
108
|
-
///----------------------------
|
109
|
-
|
110
|
-
/**
|
111
|
-
The data received during the request.
|
112
|
-
*/
|
113
|
-
@property (readonly, nonatomic, strong) NSData *responseData;
|
114
|
-
|
115
|
-
/**
|
116
|
-
The string representation of the response data.
|
117
|
-
|
118
|
-
@discussion This method uses the string encoding of the response, or if UTF-8 if not specified, to construct a string from the response data.
|
119
|
-
*/
|
120
|
-
@property (readonly, nonatomic, copy) NSString *responseString;
|
121
|
-
|
122
|
-
///------------------------
|
123
|
-
/// @name Accessing Streams
|
124
|
-
///------------------------
|
125
|
-
|
126
|
-
/**
|
127
|
-
The input stream used to read data to be sent during the request.
|
128
|
-
|
129
|
-
@discussion This property acts as a proxy to the `HTTPBodyStream` property of `request`.
|
130
|
-
*/
|
131
|
-
@property (nonatomic, strong) NSInputStream *inputStream;
|
132
|
-
|
133
|
-
/**
|
134
|
-
The output stream that is used to write data received until the request is finished.
|
135
|
-
|
136
|
-
@discussion By default, data is accumulated into a buffer that is stored into `responseData` upon completion of the request. When `outputStream` is set, the data will not be accumulated into an internal buffer, and as a result, the `responseData` property of the completed request will be `nil`. The output stream will be scheduled in the network thread runloop upon being set.
|
137
|
-
*/
|
138
|
-
@property (nonatomic, strong) NSOutputStream *outputStream;
|
139
|
-
|
140
|
-
///------------------------------------------------------
|
141
|
-
/// @name Initializing an AFURLConnectionOperation Object
|
142
|
-
///------------------------------------------------------
|
143
|
-
|
144
|
-
/**
|
145
|
-
Initializes and returns a newly allocated operation object with a url connection configured with the specified url request.
|
146
|
-
|
147
|
-
@param urlRequest The request object to be used by the operation connection.
|
148
|
-
|
149
|
-
@discussion This is the designated initializer.
|
150
|
-
*/
|
151
|
-
- (id)initWithRequest:(NSURLRequest *)urlRequest;
|
152
|
-
|
153
|
-
///----------------------------------
|
154
|
-
/// @name Pausing / Resuming Requests
|
155
|
-
///----------------------------------
|
156
|
-
|
157
|
-
/**
|
158
|
-
Pauses the execution of the request operation.
|
159
|
-
|
160
|
-
@discussion A paused operation returns `NO` for `-isReady`, `-isExecuting`, and `-isFinished`. As such, it will remain in an `NSOperationQueue` until it is either cancelled or resumed. Pausing a finished, cancelled, or paused operation has no effect.
|
161
|
-
*/
|
162
|
-
- (void)pause;
|
163
|
-
|
164
|
-
/**
|
165
|
-
Whether the request operation is currently paused.
|
166
|
-
|
167
|
-
@return `YES` if the operation is currently paused, otherwise `NO`.
|
168
|
-
*/
|
169
|
-
- (BOOL)isPaused;
|
170
|
-
|
171
|
-
/**
|
172
|
-
Resumes the execution of the paused request operation.
|
173
|
-
|
174
|
-
@discussion Pause/Resume behavior varies depending on the underlying implementation for the operation class. In its base implementation, resuming a paused requests restarts the original request. However, since HTTP defines a specification for how to request a specific content range, `AFHTTPRequestOperation` will resume downloading the request from where it left off, instead of restarting the original request.
|
175
|
-
*/
|
176
|
-
- (void)resume;
|
177
|
-
|
178
|
-
///----------------------------------------------
|
179
|
-
/// @name Configuring Backgrounding Task Behavior
|
180
|
-
///----------------------------------------------
|
181
|
-
|
182
|
-
/**
|
183
|
-
Specifies that the operation should continue execution after the app has entered the background, and the expiration handler for that background task.
|
184
|
-
|
185
|
-
@param handler A handler to be called shortly before the application’s remaining background time reaches 0. The handler is wrapped in a block that cancels the operation, and cleans up and marks the end of execution, unlike the `handler` parameter in `UIApplication -beginBackgroundTaskWithExpirationHandler:`, which expects this to be done in the handler itself. The handler is called synchronously on the main thread, thus blocking the application’s suspension momentarily while the application is notified.
|
186
|
-
*/
|
187
|
-
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
188
|
-
- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler;
|
189
|
-
#endif
|
190
|
-
|
191
|
-
///---------------------------------
|
192
|
-
/// @name Setting Progress Callbacks
|
193
|
-
///---------------------------------
|
194
|
-
|
195
|
-
/**
|
196
|
-
Sets a callback to be called when an undetermined number of bytes have been uploaded to the server.
|
197
|
-
|
198
|
-
@param block A block object to be called when an undetermined number of bytes have been uploaded to the server. This block has no return value and takes three arguments: the number of bytes written since the last time the upload progress block was called, the total bytes written, and the total bytes expected to be written during the request, as initially determined by the length of the HTTP body. This block may be called multiple times, and will execute on the main thread.
|
199
|
-
*/
|
200
|
-
- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block;
|
201
|
-
|
202
|
-
/**
|
203
|
-
Sets a callback to be called when an undetermined number of bytes have been downloaded from the server.
|
204
|
-
|
205
|
-
@param block A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread.
|
206
|
-
*/
|
207
|
-
- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;
|
208
|
-
|
209
|
-
///-------------------------------------------------
|
210
|
-
/// @name Setting NSURLConnection Delegate Callbacks
|
211
|
-
///-------------------------------------------------
|
212
|
-
|
213
|
-
/**
|
214
|
-
Sets a block to be executed to determine whether the connection should be able to respond to a protection space's form of authentication, as handled by the `NSURLConnectionDelegate` method `connection:canAuthenticateAgainstProtectionSpace:`.
|
215
|
-
|
216
|
-
@param block A block object to be executed to determine whether the connection should be able to respond to a protection space's form of authentication. The block has a `BOOL` return type and takes two arguments: the URL connection object, and the protection space to authenticate against.
|
217
|
-
|
218
|
-
@discussion If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is defined, `connection:canAuthenticateAgainstProtectionSpace:` will accept invalid SSL certificates, returning `YES` if the protection space authentication method is `NSURLAuthenticationMethodServerTrust`.
|
219
|
-
*/
|
220
|
-
- (void)setAuthenticationAgainstProtectionSpaceBlock:(BOOL (^)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace))block;
|
221
|
-
|
222
|
-
/**
|
223
|
-
Sets a block to be executed when the connection must authenticate a challenge in order to download its request, as handled by the `NSURLConnectionDelegate` method `connection:didReceiveAuthenticationChallenge:`.
|
224
|
-
|
225
|
-
@param block A block object to be executed when the connection must authenticate a challenge in order to download its request. The block has no return type and takes two arguments: the URL connection object, and the challenge that must be authenticated.
|
226
|
-
|
227
|
-
@discussion If `_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_` is defined, `connection:didReceiveAuthenticationChallenge:` will attempt to have the challenge sender use credentials with invalid SSL certificates.
|
228
|
-
*/
|
229
|
-
- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block;
|
230
|
-
|
231
|
-
/**
|
232
|
-
Sets a block to be executed when the server redirects the request from one URL to another URL, or when the request URL changed by the `NSURLProtocol` subclass handling the request in order to standardize its format, as handled by the `NSURLConnectionDelegate` method `connection:willSendRequest:redirectResponse:`.
|
233
|
-
|
234
|
-
@param block A block object to be executed when the request URL was changed. The block returns an `NSURLRequest` object, the URL request to redirect, and takes three arguments: the URL connection object, the the proposed redirected request, and the URL response that caused the redirect.
|
235
|
-
*/
|
236
|
-
- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block;
|
237
|
-
|
238
|
-
|
239
|
-
/**
|
240
|
-
Sets a block to be executed to modify the response a connection will cache, if any, as handled by the `NSURLConnectionDelegate` method `connection:willCacheResponse:`.
|
241
|
-
|
242
|
-
@param block A block object to be executed to determine what response a connection will cache, if any. The block returns an `NSCachedURLResponse` object, the cached response to store in memory or `nil` to prevent the response from being cached, and takes two arguments: the URL connection object, and the cached response provided for the request.
|
243
|
-
*/
|
244
|
-
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block;
|
245
|
-
|
246
|
-
@end
|
247
|
-
|
248
|
-
///----------------
|
249
|
-
/// @name Constants
|
250
|
-
///----------------
|
251
|
-
|
252
|
-
/**
|
253
|
-
## User info dictionary keys
|
254
|
-
|
255
|
-
These keys may exist in the user info dictionary, in addition to those defined for NSError.
|
256
|
-
|
257
|
-
- `NSString * const AFNetworkingOperationFailingURLRequestErrorKey`
|
258
|
-
- `NSString * const AFNetworkingOperationFailingURLResponseErrorKey`
|
259
|
-
|
260
|
-
### Constants
|
261
|
-
|
262
|
-
`AFNetworkingOperationFailingURLRequestErrorKey`
|
263
|
-
The corresponding value is an `NSURLRequest` containing the request of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`.
|
264
|
-
|
265
|
-
`AFNetworkingOperationFailingURLResponseErrorKey`
|
266
|
-
The corresponding value is an `NSURLResponse` containing the response of the operation associated with an error. This key is only present in the `AFNetworkingErrorDomain`.
|
267
|
-
|
268
|
-
## Error Domains
|
269
|
-
|
270
|
-
The following error domain is predefined.
|
271
|
-
|
272
|
-
- `NSString * const AFNetworkingErrorDomain`
|
273
|
-
|
274
|
-
### Constants
|
275
|
-
|
276
|
-
`AFNetworkingErrorDomain`
|
277
|
-
AFNetworking errors. Error codes for `AFNetworkingErrorDomain` correspond to codes in `NSURLErrorDomain`.
|
278
|
-
*/
|
279
|
-
extern NSString * const AFNetworkingErrorDomain;
|
280
|
-
extern NSString * const AFNetworkingOperationFailingURLRequestErrorKey;
|
281
|
-
extern NSString * const AFNetworkingOperationFailingURLResponseErrorKey;
|
282
|
-
|
283
|
-
///--------------------
|
284
|
-
/// @name Notifications
|
285
|
-
///--------------------
|
286
|
-
|
287
|
-
/**
|
288
|
-
Posted when an operation begins executing.
|
289
|
-
*/
|
290
|
-
extern NSString * const AFNetworkingOperationDidStartNotification;
|
291
|
-
|
292
|
-
/**
|
293
|
-
Posted when an operation finishes.
|
294
|
-
*/
|
295
|
-
extern NSString * const AFNetworkingOperationDidFinishNotification;
|
@@ -1,652 +0,0 @@
|
|
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
|