OLD | NEW |
1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 29 matching lines...) Expand all Loading... |
40 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ | 40 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
41 defined(MAC_OS_X_VERSION_10_11) && \ | 41 defined(MAC_OS_X_VERSION_10_11) && \ |
42 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) | 42 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) |
43 return [key stringByAddingPercentEncodingWithAllowedCharacters: | 43 return [key stringByAddingPercentEncodingWithAllowedCharacters: |
44 [NSCharacterSet URLQueryAllowedCharacterSet]]; | 44 [NSCharacterSet URLQueryAllowedCharacterSet]]; |
45 #else | 45 #else |
46 return [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | 46 return [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
47 #endif | 47 #endif |
48 } | 48 } |
49 | 49 |
| 50 // As -[NSURLConnection sendSynchronousRequest:returningResponse:error:] has |
| 51 // been deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implements |
| 52 // it using -[NSURLSession dataTaskWithRequest:completionHandler:] when using |
| 53 // those SDKs. |
| 54 static NSData *SendSynchronousNSURLRequest(NSURLRequest *req, |
| 55 NSURLResponse **out_response, |
| 56 NSError **out_error) { |
| 57 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_9_0) && \ |
| 58 __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0) || \ |
| 59 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
| 60 defined(MAC_OS_X_VERSION_10_11) && \ |
| 61 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) |
| 62 __block NSData* result = nil; |
| 63 __block NSError* error = nil; |
| 64 __block NSURLResponse* response = nil; |
| 65 dispatch_semaphore_t wait_semaphone = dispatch_semaphore_create(0); |
| 66 [[[NSURLSession sharedSession] |
| 67 dataTaskWithRequest:req |
| 68 completionHandler:^(NSData *data, |
| 69 NSURLResponse *resp, |
| 70 NSError *err) { |
| 71 if (out_error) |
| 72 error = [err retain]; |
| 73 if (out_response) |
| 74 response = [resp retain]; |
| 75 if (err == nil) |
| 76 result = [data retain]; |
| 77 dispatch_semaphore_signal(wait_semaphone); |
| 78 }] resume]; |
| 79 dispatch_semaphore_wait(wait_semaphone, DISPATCH_TIME_FOREVER); |
| 80 dispatch_release(wait_semaphone); |
| 81 if (out_error) |
| 82 *out_error = [error autorelease]; |
| 83 if (out_response) |
| 84 *out_response = [response autorelease]; |
| 85 return [result autorelease]; |
| 86 #else |
| 87 return [NSURLConnection sendSynchronousRequest:req |
| 88 returningResponse:out_response |
| 89 error:out_error]; |
| 90 #endif |
| 91 } |
50 @interface HTTPMultipartUpload(PrivateMethods) | 92 @interface HTTPMultipartUpload(PrivateMethods) |
51 - (NSString *)multipartBoundary; | 93 - (NSString *)multipartBoundary; |
52 // Each of the following methods will append the starting multipart boundary, | 94 // Each of the following methods will append the starting multipart boundary, |
53 // but not the ending one. | 95 // but not the ending one. |
54 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; | 96 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; |
55 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; | 97 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; |
56 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; | 98 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; |
57 @end | 99 @end |
58 | 100 |
59 @implementation HTTPMultipartUpload | 101 @implementation HTTPMultipartUpload |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 [req setHTTPMethod:@"POST"]; | 246 [req setHTTPMethod:@"POST"]; |
205 | 247 |
206 [response_ release]; | 248 [response_ release]; |
207 response_ = nil; | 249 response_ = nil; |
208 | 250 |
209 NSData *data = nil; | 251 NSData *data = nil; |
210 if ([[req URL] isFileURL]) { | 252 if ([[req URL] isFileURL]) { |
211 [[req HTTPBody] writeToURL:[req URL] options:0 error:error]; | 253 [[req HTTPBody] writeToURL:[req URL] options:0 error:error]; |
212 } else { | 254 } else { |
213 NSURLResponse *response = nil; | 255 NSURLResponse *response = nil; |
214 data = [NSURLConnection sendSynchronousRequest:req | 256 data = SendSynchronousNSURLRequest(req, &response, error); |
215 returningResponse:&response | |
216 error:error]; | |
217 response_ = (NSHTTPURLResponse *)[response retain]; | 257 response_ = (NSHTTPURLResponse *)[response retain]; |
218 } | 258 } |
219 [req release]; | 259 [req release]; |
220 | 260 |
221 return data; | 261 return data; |
222 } | 262 } |
223 | 263 |
224 //============================================================================= | 264 //============================================================================= |
225 - (NSHTTPURLResponse *)response { | 265 - (NSHTTPURLResponse *)response { |
226 return response_; | 266 return response_; |
227 } | 267 } |
228 | 268 |
229 @end | 269 @end |
OLD | NEW |