Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 #import "HTTPMultipartUpload.h" | 30 #import "HTTPMultipartUpload.h" |
| 31 #import "GTMDefines.h" | 31 #import "GTMDefines.h" |
| 32 | 32 |
| 33 #include <Availability.h> | |
| 34 #include <AvailabilityMacros.h> | |
| 35 | |
| 36 // As -[NSString stringByAddingPercentEscapesUsingEncoding:] has been | |
| 37 // deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implements it | |
| 38 // using -[NSString stringByAddingPercentEncodingWithAllowedCharacters:] when | |
| 39 // using those SDKs. | |
| 40 static NSString *PercentEncodeNSString(NSString *key) { | |
| 41 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_9_0) && \ | |
| 42 __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0) || \ | |
| 43 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ | |
| 44 defined(MAC_OS_X_VERSION_10_11) && \ | |
| 45 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) | |
| 46 return [key stringByAddingPercentEncodingWithAllowedCharacters: | |
| 47 [NSCharacterSet alphanumericCharacterSet]]; | |
|
Olivier
2016/01/21 09:12:31
This should be URLQueryAllowedCharacterSet
Using
| |
| 48 #else | |
| 49 return [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
| 50 #endif | |
| 51 } | |
| 52 | |
| 53 // As -[NSURLConnection sendSynchronousRequest:returningResponse:error:] has | |
| 54 // been deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implements | |
| 55 // it using -[NSURLSession dataTaskWithRequest:completionHandler:] when using | |
| 56 // those SDKs. | |
| 57 static NSData *SendSynchronousNSURLRequest(NSURLRequest *req, | |
| 58 NSURLResponse **response, | |
| 59 NSError **error) { | |
| 60 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_9_0) && \ | |
| 61 __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0) || \ | |
| 62 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ | |
| 63 defined(MAC_OS_X_VERSION_10_11) && \ | |
| 64 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) | |
| 65 __block NSData* result = nil; | |
| 66 dispatch_semaphore_t wait_semaphone = dispatch_semaphore_create(0); | |
| 67 [[[NSURLSession sharedSession] | |
| 68 dataTaskWithRequest:req | |
| 69 completionHandler:^(NSData *data, NSURLResponse *resp, | |
| 70 NSError *err) { | |
| 71 if (error) | |
| 72 *error = err; | |
| 73 if (response) | |
| 74 *response = resp; | |
| 75 if (err == nil) | |
| 76 result = data; | |
|
Olivier
2016/01/21 09:12:30
This variable is not retained and will be released
| |
| 77 dispatch_semaphore_signal(wait_semaphone); | |
| 78 }] resume]; | |
| 79 dispatch_semaphore_wait(wait_semaphone, DISPATCH_TIME_FOREVER); | |
| 80 dispatch_release(wait_semaphone); | |
| 81 return result; | |
| 82 #else | |
| 83 return [NSURLConnection sendSynchronousRequest:req | |
| 84 returningResponse:response | |
| 85 error:error]; | |
| 86 #endif | |
| 87 } | |
| 88 | |
| 33 @interface HTTPMultipartUpload(PrivateMethods) | 89 @interface HTTPMultipartUpload(PrivateMethods) |
| 34 - (NSString *)multipartBoundary; | 90 - (NSString *)multipartBoundary; |
| 35 // Each of the following methods will append the starting multipart boundary, | 91 // Each of the following methods will append the starting multipart boundary, |
| 36 // but not the ending one. | 92 // but not the ending one. |
| 37 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; | 93 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; |
| 38 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; | 94 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; |
| 39 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; | 95 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; |
| 40 @end | 96 @end |
| 41 | 97 |
| 42 @implementation HTTPMultipartUpload | 98 @implementation HTTPMultipartUpload |
| 43 //============================================================================= | 99 //============================================================================= |
| 44 #pragma mark - | 100 #pragma mark - |
| 45 #pragma mark || Private || | 101 #pragma mark || Private || |
| 46 //============================================================================= | 102 //============================================================================= |
| 47 - (NSString *)multipartBoundary { | 103 - (NSString *)multipartBoundary { |
| 48 // The boundary has 27 '-' characters followed by 16 hex digits | 104 // The boundary has 27 '-' characters followed by 16 hex digits |
| 49 return [NSString stringWithFormat:@"---------------------------%08X%08X", | 105 return [NSString stringWithFormat:@"---------------------------%08X%08X", |
| 50 rand(), rand()]; | 106 rand(), rand()]; |
| 51 } | 107 } |
| 52 | 108 |
| 53 //============================================================================= | 109 //============================================================================= |
| 54 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { | 110 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { |
| 55 NSString *escaped = | 111 NSString *escaped = PercentEncodeNSString(key); |
| 56 [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
| 57 NSString *fmt = | 112 NSString *fmt = |
| 58 @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; | 113 @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; |
| 59 NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; | 114 NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; |
| 60 | 115 |
| 61 return [form dataUsingEncoding:NSUTF8StringEncoding]; | 116 return [form dataUsingEncoding:NSUTF8StringEncoding]; |
| 62 } | 117 } |
| 63 | 118 |
| 64 //============================================================================= | 119 //============================================================================= |
| 65 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { | 120 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { |
| 66 NSMutableData *data = [NSMutableData data]; | 121 NSMutableData *data = [NSMutableData data]; |
| 67 NSString *escaped = | 122 NSString *escaped = PercentEncodeNSString(name); |
| 68 [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
| 69 NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " | 123 NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " |
| 70 "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n "; | 124 "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n "; |
| 71 NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; | 125 NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; |
| 72 | 126 |
| 73 [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]]; | 127 [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]]; |
| 74 [data appendData:contents]; | 128 [data appendData:contents]; |
| 75 | 129 |
| 76 return data; | 130 return data; |
| 77 } | 131 } |
| 78 | 132 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 [req setHTTPMethod:@"POST"]; | 243 [req setHTTPMethod:@"POST"]; |
| 190 | 244 |
| 191 [response_ release]; | 245 [response_ release]; |
| 192 response_ = nil; | 246 response_ = nil; |
| 193 | 247 |
| 194 NSData *data = nil; | 248 NSData *data = nil; |
| 195 if ([[req URL] isFileURL]) { | 249 if ([[req URL] isFileURL]) { |
| 196 [[req HTTPBody] writeToURL:[req URL] options:0 error:error]; | 250 [[req HTTPBody] writeToURL:[req URL] options:0 error:error]; |
| 197 } else { | 251 } else { |
| 198 NSURLResponse *response = nil; | 252 NSURLResponse *response = nil; |
| 199 data = [NSURLConnection sendSynchronousRequest:req | 253 data = SendSynchronousNSURLRequest(req, &response, error); |
| 200 returningResponse:&response | |
| 201 error:error]; | |
| 202 response_ = (NSHTTPURLResponse *)[response retain]; | 254 response_ = (NSHTTPURLResponse *)[response retain]; |
| 203 } | 255 } |
| 204 [req release]; | 256 [req release]; |
| 205 | 257 |
| 206 return data; | 258 return data; |
| 207 } | 259 } |
| 208 | 260 |
| 209 //============================================================================= | 261 //============================================================================= |
| 210 - (NSHTTPURLResponse *)response { | 262 - (NSHTTPURLResponse *)response { |
| 211 return response_; | 263 return response_; |
| 212 } | 264 } |
| 213 | 265 |
| 214 @end | 266 @end |
| OLD | NEW |