-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCNetworkOperation.m
339 lines (284 loc) · 11.4 KB
/
DCNetworkOperation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// DCNetworkOperation.m
//
// Created by David Cairns on 11/21/10.
// Copyright 2010 David Cairns. All rights reserved.
//
#import "DCNetworkOperation.h"
#import "DCNetworkOperation+Helpers.h"
#import "DCOperation+ARCSupport.h"
NSString *DCNetworkOperationErrorDomain = @"DCNetworkOperationErrorDomain";
@interface NSString (DCNetworkEncoding)
@end
@implementation NSString (DCNetworkEncoding)
- (NSString *)DC_stringByEscapingForURLArgument {
// Encode all the reserved characters, per RFC 3986 (<http://www.ietf.org/rfc/rfc3986.txt>)
CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)self, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);
return DC_AUTORELEASE((__bridge_transfer NSString *)escaped);
}
@end
@interface DCNetworkOperation ()
@property(nonatomic, strong)NSURLConnection *urlConnection;
@property(nonatomic, assign)long long expectedContentLength;
@property(nonatomic, strong)NSMutableData *mutableResponseData;
@property(nonatomic, strong)NSMutableDictionary *parametersToValues;
@property(nonatomic, strong)NSMutableSet *parametersRepresentingFiles;
@property(nonatomic, strong)NSData *rawRequestData;
- (NSData *)_requestBodyData;
@end
@implementation DCNetworkOperation
@synthesize urlString = _urlString;
@synthesize HTTPMethod = _HTTPMethod;
@synthesize headerFields = _headerFields;
@synthesize expectedContentLength = _expectedContentLength;
@synthesize mutableResponseData = _mutableResponseData;
@synthesize urlConnection = _urlConnection;
@synthesize parametersToValues = _parametersToValues;
@synthesize parametersRepresentingFiles = _parametersRepresentingFiles;
@synthesize rawRequestData = _rawRequestData;
@synthesize transferUpdateBlock = _transferUpdateBlock;
@synthesize authenticationUserName = _authenticationUserName;
@synthesize authenticationPassword = _authenticationPassword;
@synthesize responseProcessingBlock = _responseProcessingBlock;
- (id)init {
if((self = [super init])) {
self.HTTPMethod = @"GET";
self.timeoutInterval = 30.0;
self.needsRunLoop = YES;
self.parametersToValues = [NSMutableDictionary dictionary];
self.parametersRepresentingFiles = [NSMutableSet set];
}
return self;
}
- (void)dealloc {
DC_RELEASE(_urlString);
DC_RELEASE(_HTTPMethod);
DC_RELEASE(_headerFields);
DC_RELEASE(_urlConnection);
DC_RELEASE(_mutableResponseData);
DC_RELEASE(_parametersToValues);
DC_RELEASE(_parametersRepresentingFiles);
DC_RELEASE(_rawRequestData);
DC_RELEASE(_transferUpdateBlock);
DC_RELEASE(_authenticationUserName);
DC_RELEASE(_authenticationPassword);
DC_RELEASE(_responseProcessingBlock);
#if !defined(DC_USE_ARC)
[super dealloc];
#endif
}
#pragma mark -
- (NSString *)requestValueForParameter:(NSString *)parameter {
return [self.parametersToValues objectForKey:parameter];
}
- (void)setRequestValue:(NSString *)value forParameter:(NSString *)parameter {
// Force the request data to be rebuilt.
self.rawRequestData = nil;
// Make sure that this parameter is not marked a file.
[self.parametersRepresentingFiles removeObject:parameter];
if(value) {
// Set the new value.
[self.parametersToValues setValue:value forKey:parameter];
}
else {
[self.parametersToValues removeObjectForKey:parameter];
}
}
- (void)setRequestFilename:(NSString *)filename forParameter:(NSString *)parameter {
// Clear out the raw request data, if it's been set.
self.rawRequestData = nil;
// Set the new value.
[self.parametersToValues setObject:filename forKey:parameter];
// Make sure this parameter is marked as a file.
[self.parametersRepresentingFiles addObject:parameter];
}
- (void)setRequestData:(NSData *)requestData {
[self.parametersToValues removeAllObjects];
[self.parametersRepresentingFiles removeAllObjects];
self.rawRequestData = requestData;
}
#pragma mark -
- (NSURLRequest *)_urlRequest {
// Determine the real URL string to use.
NSMutableString *urlStringToUse = [NSMutableString stringWithString:self.urlString];
// If this is a GET request and we have parameters, we need to embed them in the URL.
if([self.HTTPMethod isEqualToString:@"GET"]) {
__block BOOL isFirstArgument = YES;
[self.parametersToValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if(isFirstArgument) {
// Make sure the argument section starts with a '?'.
[urlStringToUse appendString:@"?"];
isFirstArgument = NO;
}
else {
[urlStringToUse appendString:@"&"];
}
// If the object is a string, escape its contents.
id objToUse = [obj isKindOfClass:[NSString class]] ? [obj DC_stringByEscapingForURLArgument] : obj;
[urlStringToUse appendFormat:@"%@=%@", key, objToUse];
}];
}
// Set up our URL request.
NSURL *url = [NSURL URLWithString:urlStringToUse];
NSMutableURLRequest *request = DC_AUTORELEASE([[NSMutableURLRequest alloc] init]);
[request setURL:url];
[request setHTTPMethod:self.HTTPMethod];
// Add the HTTP header fields.
NSDictionary *headerFields = [self headerFields];
if(headerFields) {
// Override each of these fields.
for(NSString *key in [headerFields allKeys]) {
[request addValue:[headerFields objectForKey:key] forHTTPHeaderField:key];
}
}
// Add the request body.
NSData *requestBody = [self _requestBodyData];
if(requestBody) {
[request setHTTPBody:requestBody];
}
// NSLog(@"Request URL: %@, HTTP method:%@, header fields:%@, body:%@", [request URL], [request HTTPMethod], [request allHTTPHeaderFields], requestBody ? self.parametersToValues : nil);
return request;
}
- (BOOL)operationShouldStart {
// Check the preflight test to see if our request is valid.
NSURLRequest *request = [self _urlRequest];
if(![NSURLConnection canHandleRequest:request]) {
self.error = [NSError errorWithDomain:DCNetworkOperationErrorDomain code:DCNetworkOperationErrorCodeBadRequest userInfo:nil];
return NO;
}
// Set up the mutable data buffer into which we'll store our result.
self.mutableResponseData = [NSMutableData data];
// Set up our NSURLConnection.
self.urlConnection = DC_AUTORELEASE([[NSURLConnection alloc] initWithRequest:request delegate:self]);
// Schedule the NSURLConnection to run on the main run loop.
#if TARGET_OS_IPHONE
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
#endif
[self.urlConnection start];
return YES;
}
- (void)operationDidFinish {
[super operationDidFinish];
#if TARGET_OS_IPHONE
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
#endif
// Kill our URL connection.
[self.urlConnection cancel];
self.urlConnection = nil;
// Call our response-processing block.
if(self.responseProcessingBlock) {
self.responseProcessingBlock(self);
}
}
- (NSData *)responseData {
return self.mutableResponseData;
}
#pragma mark -
- (BOOL)_useFormStyleRequestBodyData {
return [self.parametersRepresentingFiles count] > 0;
}
- (NSData *)_formStyleRequestBodyData {
NSMutableData *requestBodyData = [NSMutableData data];
for(NSString *parameter in [self.parametersToValues allKeys]) {
NSString *value = [self.parametersToValues objectForKey:parameter];
if([self.parametersRepresentingFiles containsObject:parameter]) {
// This parameter represents a file, use our special path.
[requestBodyData appendData:DCDataWithFileAndParamName(value, parameter)];
}
else {
// This is just a regular parameter but we're in form-style mode.
[requestBodyData appendData:DCDataForValueAndParamName(value, parameter)];
}
}
return requestBodyData;
}
- (NSData *)_argumentStyleRequestBodyData {
NSMutableString *string = [NSMutableString string];
for(NSString *parameter in [self.parametersToValues allKeys]) {
NSString *value = [self.parametersToValues objectForKey:parameter];
[string appendFormat:@"%@=%@;", parameter, value];
}
return [string dataUsingEncoding:NSUTF8StringEncoding];
}
- (NSData *)_requestBodyData {
// GET requests shouldn't have a POST body.
if([self.HTTPMethod isEqualToString:@"GET"]) {
return nil;
}
// If we have raw request data, just use that.
if(self.rawRequestData) {
return self.rawRequestData;
}
// Otherwise, build the request body data buffer based on our parameter dictionary.
return [self _useFormStyleRequestBodyData] ? [self _formStyleRequestBodyData] : [self _argumentStyleRequestBodyData];
}
#pragma mark -
- (float)progress {
if(-1 == self.expectedContentLength || 0 == self.expectedContentLength) {
return DCProgressIndeterminate;
}
return self.responseData.length / self.expectedContentLength;
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// Store the expected content size, so we can refer to it later.
self.expectedContentLength = response.expectedContentLength;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// We've received some data; append it to our data buffer.
[self.mutableResponseData appendData:data];
if(self.transferUpdateBlock) {
self.transferUpdateBlock();
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Our url connection failed!
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObject:error forKey:NSUnderlyingErrorKey];
self.error = [NSError errorWithDomain:DCNetworkOperationErrorDomain code:DCNetworkOperationErrorCodeConnectionFailed userInfo:errorUserInfo];
// Clean up.
[self finish];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Clean up.
[self finish];
}
#pragma mark - NSURLConnectionDelegate Authentication
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if(challenge.previousFailureCount) {
NSLog(@"Authentication challenge was rejected!");
[challenge.sender cancelAuthenticationChallenge:challenge];
[self.urlConnection cancel];
return;
}
// If this is a trust challenge, create a credential for that.
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// First, evaluate the trust.
SecTrustResultType evaluationResult;
OSStatus err = SecTrustEvaluate(challenge.protectionSpace.serverTrust, &evaluationResult);
if(err) {
NSLog(@"Failed to evaluate the trust, with error: %ld", err);
return;
}
// If the trust is invalid, cancel the authentication challenge.
if(kSecTrustResultInvalid == evaluationResult) {
NSLog(@"The server trust challenge was invalid!");
[challenge.sender cancelAuthenticationChallenge:challenge];
}
// Create a credential to respond to the trust.
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
else if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) {
if(!self.authenticationUserName || !self.authenticationPassword) {
NSLog(@"Missing username or password for basic HTTP authentication challenge!");
return;
}
// Create and respond with a user/password credential.
NSURLCredential *credential = [NSURLCredential credentialWithUser:self.authenticationUserName password:self.authenticationPassword persistence:NSURLCredentialPersistenceForSession];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
else {
NSLog(@"Unsupported authentication challenge type: %@", challenge.protectionSpace.authenticationMethod);
}
}
@end