Chromium Code Reviews| Index: src/common/mac/HTTPMultipartUpload.m |
| diff --git a/src/common/mac/HTTPMultipartUpload.m b/src/common/mac/HTTPMultipartUpload.m |
| index 2ed1b63226519f09de512521af03a241b83f2e3e..f213d3ed706ce45efd5ee3c4b20c0b43a237c30e 100644 |
| --- a/src/common/mac/HTTPMultipartUpload.m |
| +++ b/src/common/mac/HTTPMultipartUpload.m |
| @@ -30,6 +30,55 @@ |
| #import "HTTPMultipartUpload.h" |
| #import "GTMDefines.h" |
| +// As -[NSString stringByAddingPercentEscapesUsingEncoding:] has been |
| +// deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implement it |
|
Mark Mentovai
2016/01/07 17:19:15
re-implements
sdefresne
2016/01/07 17:45:48
Done.
|
| +// using -[NSString stringByAddingPercentEncodingWithAllowedCharacters:] when |
| +// using those SDKs. |
| +static NSString *EscapeStringByAddingPercent(NSString *key) { |
|
Mark Mentovai
2016/01/07 17:19:15
Naming like PercentEncodeNSString()?
sdefresne
2016/01/07 17:45:49
Done.
|
| +#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \ |
|
Mark Mentovai
2016/01/07 17:19:15
#include <Availability.h> for this.
sdefresne
2016/01/07 17:45:48
Done.
|
| + __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || \ |
| + (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
|
Mark Mentovai
2016/01/07 17:19:16
#include <AvailabilityMacros.h> for this.
sdefresne
2016/01/07 17:45:48
Done.
|
| + MAC_OS_X_VERSION_MIN_REQUIRED >= 1011) |
|
Mark Mentovai
2016/01/07 17:19:16
MAC_OS_X_VERSION_10_11 instead of 1011. Because 10
sdefresne
2016/01/07 17:45:48
Done.
|
| + return [key stringByAddingPercentEncodingWithAllowedCharacters: |
|
Mark Mentovai
2016/01/07 17:19:15
Does this %-encode a literal % too?
sdefresne
2016/01/07 17:45:49
Yes according to my little testing in a swift play
|
| + [NSCharacterSet alphanumericCharacterSet]]; |
| +#else |
| + return [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
| +#endif |
| +} |
| + |
| +// As -[NSURLConnection sendSynchronousRequest:returningResponse:error:] has |
| +// been deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implement |
|
Mark Mentovai
2016/01/07 17:19:15
re-implements
sdefresne
2016/01/07 17:45:48
Done.
|
| +// it using -[NSURLSession dataTaskWithRequest:completionHandler:] when using |
| +// those SDKs. |
| +static NSData *SendSynchronousRequest(NSURLRequest *req, |
|
Mark Mentovai
2016/01/07 17:19:15
Something like SendSynchronousNSURLRequest()?
Giv
sdefresne
2016/01/07 17:45:49
Done.
|
| + NSURLResponse **response, |
| + NSError **error) { |
| +#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \ |
|
Mark Mentovai
2016/01/07 17:19:16
Same here.
sdefresne
2016/01/07 17:45:48
Done.
|
| + __IPHONE_OS_VERSION_MIN_REQUIRED >= 90000) || \ |
| + (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
| + MAC_OS_X_VERSION_MIN_REQUIRED >= 1011) |
| + __block NSData* result = nil; |
| + dispatch_semaphore_t wait_semaphone = dispatch_semaphore_create(0); |
| + [[[NSURLSession sharedSession] |
| + dataTaskWithRequest:req |
| + completionHandler:^(NSData *data, NSURLResponse *resp, NSError *err) { |
| + if (error) |
| + *error = err; |
| + if (response) |
| + *response = resp; |
| + if (err == nil) |
| + result = data; |
| + dispatch_semaphore_signal(wait_semaphone); |
| + }] resume]; |
| + dispatch_semaphore_wait(wait_semaphone, DISPATCH_TIME_FOREVER); |
|
Mark Mentovai
2016/01/07 17:19:15
Looks like it’s leaked, there’s no dispatch_releas
sdefresne
2016/01/07 17:45:48
Thanks, fixed.
|
| + return result; |
| +#else |
| + return [NSURLConnection sendSynchronousRequest:req |
| + returningResponse:response |
| + error:error]; |
| +#endif |
| +} |
| + |
| @interface HTTPMultipartUpload(PrivateMethods) |
| - (NSString *)multipartBoundary; |
| // Each of the following methods will append the starting multipart boundary, |
| @@ -52,8 +101,7 @@ |
| //============================================================================= |
| - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { |
| - NSString *escaped = |
| - [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
| + NSString *escaped = EscapeStringByAddingPercent(key); |
| NSString *fmt = |
| @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; |
| NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; |
| @@ -64,8 +112,7 @@ |
| //============================================================================= |
| - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { |
| NSMutableData *data = [NSMutableData data]; |
| - NSString *escaped = |
| - [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
| + NSString *escaped = EscapeStringByAddingPercent(name); |
| NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " |
| "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n"; |
| NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; |
| @@ -196,9 +243,7 @@ |
| [[req HTTPBody] writeToURL:[req URL] options:0 error:error]; |
| } else { |
| NSURLResponse *response = nil; |
| - data = [NSURLConnection sendSynchronousRequest:req |
| - returningResponse:&response |
| - error:error]; |
| + data = SendSynchronousRequest(req, &response, error); |
| response_ = (NSHTTPURLResponse *)[response retain]; |
| } |
| [req release]; |