/thirdparty/breakpad/common/mac/HTTPMultipartUpload.m

http://github.com/tomahawk-player/tomahawk · Objective C · 209 lines · 122 code · 36 blank · 51 comment · 8 complexity · 18f2667064a12c8ea2ece940174d8e51 MD5 · raw file

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