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 // As -[NSString stringByAddingPercentEscapesUsingEncoding:] has been |
| 34 // deprecated with iOS 9.0 / OS X 10.11 SDKs, this function re-implements it |
| 35 // using -[NSString stringByAddingPercentEncodingWithAllowedCharacters:] when |
| 36 // using those SDKs. |
| 37 static NSString *PercentEncodeNSString(NSString *key) { |
| 38 #if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && defined(__IPHONE_9_0) && \ |
| 39 __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0) || \ |
| 40 (defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \ |
| 41 defined(MAC_OS_X_VERSION_10_11) && \ |
| 42 MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_11) |
| 43 return [key stringByAddingPercentEncodingWithAllowedCharacters: |
| 44 [NSCharacterSet URLQueryAllowedCharacterSet]]; |
| 45 #else |
| 46 return [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
| 47 #endif |
| 48 } |
| 49 |
33 @interface HTTPMultipartUpload(PrivateMethods) | 50 @interface HTTPMultipartUpload(PrivateMethods) |
34 - (NSString *)multipartBoundary; | 51 - (NSString *)multipartBoundary; |
35 // Each of the following methods will append the starting multipart boundary, | 52 // Each of the following methods will append the starting multipart boundary, |
36 // but not the ending one. | 53 // but not the ending one. |
37 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; | 54 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; |
38 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; | 55 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; |
39 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; | 56 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; |
40 @end | 57 @end |
41 | 58 |
42 @implementation HTTPMultipartUpload | 59 @implementation HTTPMultipartUpload |
43 //============================================================================= | 60 //============================================================================= |
44 #pragma mark - | 61 #pragma mark - |
45 #pragma mark || Private || | 62 #pragma mark || Private || |
46 //============================================================================= | 63 //============================================================================= |
47 - (NSString *)multipartBoundary { | 64 - (NSString *)multipartBoundary { |
48 // The boundary has 27 '-' characters followed by 16 hex digits | 65 // The boundary has 27 '-' characters followed by 16 hex digits |
49 return [NSString stringWithFormat:@"---------------------------%08X%08X", | 66 return [NSString stringWithFormat:@"---------------------------%08X%08X", |
50 rand(), rand()]; | 67 rand(), rand()]; |
51 } | 68 } |
52 | 69 |
53 //============================================================================= | 70 //============================================================================= |
54 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { | 71 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { |
55 NSString *escaped = | 72 NSString *escaped = PercentEncodeNSString(key); |
56 [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
57 NSString *fmt = | 73 NSString *fmt = |
58 @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; | 74 @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; |
59 NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; | 75 NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; |
60 | 76 |
61 return [form dataUsingEncoding:NSUTF8StringEncoding]; | 77 return [form dataUsingEncoding:NSUTF8StringEncoding]; |
62 } | 78 } |
63 | 79 |
64 //============================================================================= | 80 //============================================================================= |
65 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { | 81 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { |
66 NSMutableData *data = [NSMutableData data]; | 82 NSMutableData *data = [NSMutableData data]; |
67 NSString *escaped = | 83 NSString *escaped = PercentEncodeNSString(name); |
68 [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
69 NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " | 84 NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " |
70 "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n
"; | 85 "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n
"; |
71 NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; | 86 NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; |
72 | 87 |
73 [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]]; | 88 [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]]; |
74 [data appendData:contents]; | 89 [data appendData:contents]; |
75 | 90 |
76 return data; | 91 return data; |
77 } | 92 } |
78 | 93 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 | 220 |
206 return data; | 221 return data; |
207 } | 222 } |
208 | 223 |
209 //============================================================================= | 224 //============================================================================= |
210 - (NSHTTPURLResponse *)response { | 225 - (NSHTTPURLResponse *)response { |
211 return response_; | 226 return response_; |
212 } | 227 } |
213 | 228 |
214 @end | 229 @end |
OLD | NEW |