1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/mac/HTTPMultipartUpload.m Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,209 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +#import "HTTPMultipartUpload.h" 1.34 +#import "GTMDefines.h" 1.35 + 1.36 +@interface HTTPMultipartUpload(PrivateMethods) 1.37 +- (NSString *)multipartBoundary; 1.38 +// Each of the following methods will append the starting multipart boundary, 1.39 +// but not the ending one. 1.40 +- (NSData *)formDataForKey:(NSString *)key value:(NSString *)value; 1.41 +- (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name; 1.42 +- (NSData *)formDataForFile:(NSString *)file name:(NSString *)name; 1.43 +@end 1.44 + 1.45 +@implementation HTTPMultipartUpload 1.46 +//============================================================================= 1.47 +#pragma mark - 1.48 +#pragma mark || Private || 1.49 +//============================================================================= 1.50 +- (NSString *)multipartBoundary { 1.51 + // The boundary has 27 '-' characters followed by 16 hex digits 1.52 + return [NSString stringWithFormat:@"---------------------------%08X%08X", 1.53 + rand(), rand()]; 1.54 +} 1.55 + 1.56 +//============================================================================= 1.57 +- (NSData *)formDataForKey:(NSString *)key value:(NSString *)value { 1.58 + NSString *escaped = 1.59 + [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 1.60 + NSString *fmt = 1.61 + @"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n"; 1.62 + NSString *form = [NSString stringWithFormat:fmt, boundary_, escaped, value]; 1.63 + 1.64 + return [form dataUsingEncoding:NSUTF8StringEncoding]; 1.65 +} 1.66 + 1.67 +//============================================================================= 1.68 +- (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name { 1.69 + NSMutableData *data = [NSMutableData data]; 1.70 + NSString *escaped = 1.71 + [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 1.72 + NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; " 1.73 + "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n"; 1.74 + NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped]; 1.75 + 1.76 + [data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]]; 1.77 + [data appendData:contents]; 1.78 + 1.79 + return data; 1.80 +} 1.81 + 1.82 +//============================================================================= 1.83 +- (NSData *)formDataForFile:(NSString *)file name:(NSString *)name { 1.84 + NSData *contents = [NSData dataWithContentsOfFile:file]; 1.85 + 1.86 + return [self formDataForFileContents:contents name:name]; 1.87 +} 1.88 + 1.89 +//============================================================================= 1.90 +#pragma mark - 1.91 +#pragma mark || Public || 1.92 +//============================================================================= 1.93 +- (id)initWithURL:(NSURL *)url { 1.94 + if ((self = [super init])) { 1.95 + url_ = [url copy]; 1.96 + boundary_ = [[self multipartBoundary] retain]; 1.97 + files_ = [[NSMutableDictionary alloc] init]; 1.98 + } 1.99 + 1.100 + return self; 1.101 +} 1.102 + 1.103 +//============================================================================= 1.104 +- (void)dealloc { 1.105 + [url_ release]; 1.106 + [parameters_ release]; 1.107 + [files_ release]; 1.108 + [boundary_ release]; 1.109 + [response_ release]; 1.110 + 1.111 + [super dealloc]; 1.112 +} 1.113 + 1.114 +//============================================================================= 1.115 +- (NSURL *)URL { 1.116 + return url_; 1.117 +} 1.118 + 1.119 +//============================================================================= 1.120 +- (void)setParameters:(NSDictionary *)parameters { 1.121 + if (parameters != parameters_) { 1.122 + [parameters_ release]; 1.123 + parameters_ = [parameters copy]; 1.124 + } 1.125 +} 1.126 + 1.127 +//============================================================================= 1.128 +- (NSDictionary *)parameters { 1.129 + return parameters_; 1.130 +} 1.131 + 1.132 +//============================================================================= 1.133 +- (void)addFileAtPath:(NSString *)path name:(NSString *)name { 1.134 + [files_ setObject:path forKey:name]; 1.135 +} 1.136 + 1.137 +//============================================================================= 1.138 +- (void)addFileContents:(NSData *)data name:(NSString *)name { 1.139 + [files_ setObject:data forKey:name]; 1.140 +} 1.141 + 1.142 +//============================================================================= 1.143 +- (NSDictionary *)files { 1.144 + return files_; 1.145 +} 1.146 + 1.147 +//============================================================================= 1.148 +- (NSData *)send:(NSError **)error { 1.149 + NSMutableURLRequest *req = 1.150 + [[NSMutableURLRequest alloc] 1.151 + initWithURL:url_ cachePolicy:NSURLRequestUseProtocolCachePolicy 1.152 + timeoutInterval:10.0 ]; 1.153 + 1.154 + NSMutableData *postBody = [NSMutableData data]; 1.155 + 1.156 + [req setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", 1.157 + boundary_] forHTTPHeaderField:@"Content-type"]; 1.158 + 1.159 + // Add any parameters to the message 1.160 + NSArray *parameterKeys = [parameters_ allKeys]; 1.161 + NSString *key; 1.162 + 1.163 + NSInteger count = [parameterKeys count]; 1.164 + for (NSInteger i = 0; i < count; ++i) { 1.165 + key = [parameterKeys objectAtIndex:i]; 1.166 + [postBody appendData:[self formDataForKey:key 1.167 + value:[parameters_ objectForKey:key]]]; 1.168 + } 1.169 + 1.170 + // Add any files to the message 1.171 + NSArray *fileNames = [files_ allKeys]; 1.172 + count = [fileNames count]; 1.173 + for (NSInteger i = 0; i < count; ++i) { 1.174 + NSString *name = [fileNames objectAtIndex:i]; 1.175 + id fileOrData = [files_ objectForKey:name]; 1.176 + NSData *fileData; 1.177 + 1.178 + // The object can be either the path to a file (NSString) or the contents 1.179 + // of the file (NSData). 1.180 + if ([fileOrData isKindOfClass:[NSData class]]) 1.181 + fileData = [self formDataForFileContents:fileOrData name:name]; 1.182 + else 1.183 + fileData = [self formDataForFile:fileOrData name:name]; 1.184 + 1.185 + [postBody appendData:fileData]; 1.186 + } 1.187 + 1.188 + NSString *epilogue = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary_]; 1.189 + [postBody appendData:[epilogue dataUsingEncoding:NSUTF8StringEncoding]]; 1.190 + 1.191 + [req setHTTPBody:postBody]; 1.192 + [req setHTTPMethod:@"POST"]; 1.193 + 1.194 + [response_ release]; 1.195 + response_ = nil; 1.196 + 1.197 + NSData *data = [NSURLConnection sendSynchronousRequest:req 1.198 + returningResponse:&response_ 1.199 + error:error]; 1.200 + 1.201 + [response_ retain]; 1.202 + [req release]; 1.203 + 1.204 + return data; 1.205 +} 1.206 + 1.207 +//============================================================================= 1.208 +- (NSHTTPURLResponse *)response { 1.209 + return response_; 1.210 +} 1.211 + 1.212 +@end