toolkit/crashreporter/google-breakpad/src/common/mac/HTTPMultipartUpload.m

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 #import "HTTPMultipartUpload.h"
michael@0 31 #import "GTMDefines.h"
michael@0 32
michael@0 33 @interface HTTPMultipartUpload(PrivateMethods)
michael@0 34 - (NSString *)multipartBoundary;
michael@0 35 // Each of the following methods will append the starting multipart boundary,
michael@0 36 // but not the ending one.
michael@0 37 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value;
michael@0 38 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name;
michael@0 39 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name;
michael@0 40 @end
michael@0 41
michael@0 42 @implementation HTTPMultipartUpload
michael@0 43 //=============================================================================
michael@0 44 #pragma mark -
michael@0 45 #pragma mark || Private ||
michael@0 46 //=============================================================================
michael@0 47 - (NSString *)multipartBoundary {
michael@0 48 // The boundary has 27 '-' characters followed by 16 hex digits
michael@0 49 return [NSString stringWithFormat:@"---------------------------%08X%08X",
michael@0 50 rand(), rand()];
michael@0 51 }
michael@0 52
michael@0 53 //=============================================================================
michael@0 54 - (NSData *)formDataForKey:(NSString *)key value:(NSString *)value {
michael@0 55 NSString *escaped =
michael@0 56 [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
michael@0 57 NSString *fmt =
michael@0 58 @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n";
michael@0 59 NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value];
michael@0 60
michael@0 61 return [form dataUsingEncoding:NSUTF8StringEncoding];
michael@0 62 }
michael@0 63
michael@0 64 //=============================================================================
michael@0 65 - (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name {
michael@0 66 NSMutableData *data = [NSMutableData data];
michael@0 67 NSString *escaped =
michael@0 68 [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
michael@0 69 NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; "
michael@0 70 "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n";
michael@0 71 NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped];
michael@0 72
michael@0 73 [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]];
michael@0 74 [data appendData:contents];
michael@0 75
michael@0 76 return data;
michael@0 77 }
michael@0 78
michael@0 79 //=============================================================================
michael@0 80 - (NSData *)formDataForFile:(NSString *)file name:(NSString *)name {
michael@0 81 NSData *contents = [NSData dataWithContentsOfFile:file];
michael@0 82
michael@0 83 return [self formDataForFileContents:contents name:name];
michael@0 84 }
michael@0 85
michael@0 86 //=============================================================================
michael@0 87 #pragma mark -
michael@0 88 #pragma mark || Public ||
michael@0 89 //=============================================================================
michael@0 90 - (id)initWithURL:(NSURL *)url {
michael@0 91 if ((self = [super init])) {
michael@0 92 url_ = [url copy];
michael@0 93 boundary_ = [[self multipartBoundary] retain];
michael@0 94 files_ = [[NSMutableDictionary alloc] init];
michael@0 95 }
michael@0 96
michael@0 97 return self;
michael@0 98 }
michael@0 99
michael@0 100 //=============================================================================
michael@0 101 - (void)dealloc {
michael@0 102 [url_ release];
michael@0 103 [parameters_ release];
michael@0 104 [files_ release];
michael@0 105 [boundary_ release];
michael@0 106 [response_ release];
michael@0 107
michael@0 108 [super dealloc];
michael@0 109 }
michael@0 110
michael@0 111 //=============================================================================
michael@0 112 - (NSURL *)URL {
michael@0 113 return url_;
michael@0 114 }
michael@0 115
michael@0 116 //=============================================================================
michael@0 117 - (void)setParameters:(NSDictionary *)parameters {
michael@0 118 if (parameters != parameters_) {
michael@0 119 [parameters_ release];
michael@0 120 parameters_ = [parameters copy];
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 //=============================================================================
michael@0 125 - (NSDictionary *)parameters {
michael@0 126 return parameters_;
michael@0 127 }
michael@0 128
michael@0 129 //=============================================================================
michael@0 130 - (void)addFileAtPath:(NSString *)path name:(NSString *)name {
michael@0 131 [files_ setObject:path forKey:name];
michael@0 132 }
michael@0 133
michael@0 134 //=============================================================================
michael@0 135 - (void)addFileContents:(NSData *)data name:(NSString *)name {
michael@0 136 [files_ setObject:data forKey:name];
michael@0 137 }
michael@0 138
michael@0 139 //=============================================================================
michael@0 140 - (NSDictionary *)files {
michael@0 141 return files_;
michael@0 142 }
michael@0 143
michael@0 144 //=============================================================================
michael@0 145 - (NSData *)send:(NSError **)error {
michael@0 146 NSMutableURLRequest *req =
michael@0 147 [[NSMutableURLRequest alloc]
michael@0 148 initWithURL:url_ cachePolicy:NSURLRequestUseProtocolCachePolicy
michael@0 149 timeoutInterval:10.0 ];
michael@0 150
michael@0 151 NSMutableData *postBody = [NSMutableData data];
michael@0 152
michael@0 153 [req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",
michael@0 154 boundary_] forHTTPHeaderField:@"Content-type"];
michael@0 155
michael@0 156 // Add any parameters to the message
michael@0 157 NSArray *parameterKeys = [parameters_ allKeys];
michael@0 158 NSString *key;
michael@0 159
michael@0 160 NSInteger count = [parameterKeys count];
michael@0 161 for (NSInteger i = 0; i < count; ++i) {
michael@0 162 key = [parameterKeys objectAtIndex:i];
michael@0 163 [postBody appendData:[self formDataForKey:key
michael@0 164 value:[parameters_ objectForKey:key]]];
michael@0 165 }
michael@0 166
michael@0 167 // Add any files to the message
michael@0 168 NSArray *fileNames = [files_ allKeys];
michael@0 169 count = [fileNames count];
michael@0 170 for (NSInteger i = 0; i < count; ++i) {
michael@0 171 NSString *name = [fileNames objectAtIndex:i];
michael@0 172 id fileOrData = [files_ objectForKey:name];
michael@0 173 NSData *fileData;
michael@0 174
michael@0 175 // The object can be either the path to a file (NSString) or the contents
michael@0 176 // of the file (NSData).
michael@0 177 if ([fileOrData isKindOfClass:[NSData class]])
michael@0 178 fileData = [self formDataForFileContents:fileOrData name:name];
michael@0 179 else
michael@0 180 fileData = [self formDataForFile:fileOrData name:name];
michael@0 181
michael@0 182 [postBody appendData:fileData];
michael@0 183 }
michael@0 184
michael@0 185 NSString *epilogue = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary_];
michael@0 186 [postBody appendData:[epilogue dataUsingEncoding:NSUTF8StringEncoding]];
michael@0 187
michael@0 188 [req setHTTPBody:postBody];
michael@0 189 [req setHTTPMethod:@"POST"];
michael@0 190
michael@0 191 [response_ release];
michael@0 192 response_ = nil;
michael@0 193
michael@0 194 NSData *data = [NSURLConnection sendSynchronousRequest:req
michael@0 195 returningResponse:&response_
michael@0 196 error:error];
michael@0 197
michael@0 198 [response_ retain];
michael@0 199 [req release];
michael@0 200
michael@0 201 return data;
michael@0 202 }
michael@0 203
michael@0 204 //=============================================================================
michael@0 205 - (NSHTTPURLResponse *)response {
michael@0 206 return response_;
michael@0 207 }
michael@0 208
michael@0 209 @end

mercurial