aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwaylonis <waylonis@4c0a9323-5329-0410-9bdc-e9ce6186880e>2007-01-04 04:43:41 +0000
committerwaylonis <waylonis@4c0a9323-5329-0410-9bdc-e9ce6186880e>2007-01-04 04:43:41 +0000
commit0ec76c7fad331595270db25c29f997657289f725 (patch)
tree0c229a722eabfcd07ab12d083e198b5efd9623ce
parentAllows the caller of CrashReportSender::SendCrashReport() to determine that (diff)
downloadbreakpad-0ec76c7fad331595270db25c29f997657289f725.tar.xz
Added method to allow adding a file's contents directly. Fixes issue #105.
git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@100 4c0a9323-5329-0410-9bdc-e9ce6186880e
-rw-r--r--src/common/mac/HTTPMultipartUpload.h1
-rw-r--r--src/common/mac/HTTPMultipartUpload.m42
2 files changed, 31 insertions, 12 deletions
diff --git a/src/common/mac/HTTPMultipartUpload.h b/src/common/mac/HTTPMultipartUpload.h
index ce9956f0..95f259b9 100644
--- a/src/common/mac/HTTPMultipartUpload.h
+++ b/src/common/mac/HTTPMultipartUpload.h
@@ -51,6 +51,7 @@
- (NSDictionary *)parameters;
- (void)addFileAtPath:(NSString *)path name:(NSString *)name;
+- (void)addFileContents:(NSData *)data name:(NSString *)name;
- (NSDictionary *)files;
// Set the data and return the response
diff --git a/src/common/mac/HTTPMultipartUpload.m b/src/common/mac/HTTPMultipartUpload.m
index 623bde6f..7cd77677 100644
--- a/src/common/mac/HTTPMultipartUpload.m
+++ b/src/common/mac/HTTPMultipartUpload.m
@@ -32,6 +32,7 @@
@interface HTTPMultipartUpload(PrivateMethods)
- (NSString *)multipartBoundary;
- (NSData *)formDataForKey:(NSString *)key value:(NSString *)value;
+- (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name;
- (NSData *)formDataForFile:(NSString *)file name:(NSString *)name;
@end
@@ -48,7 +49,7 @@
//=============================================================================
- (NSData *)formDataForKey:(NSString *)key value:(NSString *)value {
- NSString *escaped =
+ NSString *escaped =
[key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *fmt =
@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n";
@@ -58,24 +59,30 @@
}
//=============================================================================
-- (NSData *)formDataForFile:(NSString *)file name:(NSString *)name {
+- (NSData *)formDataForFileContents:(NSData *)contents name:(NSString *)name {
NSMutableData *data = [NSMutableData data];
- NSString *escaped =
+ NSString *escaped =
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *fmt = @"--%@\r\nContent-Disposition: form-data; name=\"%@\"; "
- "filename=\"%@\"\r\nContent-Type: application/octet-stream\r\n\r\n";
- NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped,
- [file lastPathComponent]];
+ "filename=\"minidump.dmp\"\r\nContent-Type: application/octet-stream\r\n\r\n";
+ NSString *pre = [NSString stringWithFormat:fmt, boundary_, escaped];
NSString *post = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary_];
[data appendData:[pre dataUsingEncoding:NSUTF8StringEncoding]];
- [data appendData:[NSData dataWithContentsOfFile:file]];
+ [data appendData:contents];
[data appendData:[post dataUsingEncoding:NSUTF8StringEncoding]];
return data;
}
//=============================================================================
+- (NSData *)formDataForFile:(NSString *)file name:(NSString *)name {
+ NSData *contents = [NSData dataWithContentsOfFile:file];
+
+ return [self formDataForFileContents:contents name:name];
+}
+
+//=============================================================================
#pragma mark -
#pragma mark || Public ||
//=============================================================================
@@ -83,6 +90,7 @@
if ((self = [super init])) {
url_ = [url copy];
boundary_ = [[self multipartBoundary] retain];
+ files_ = [[NSMutableDictionary alloc] init];
}
return self;
@@ -119,13 +127,15 @@
//=============================================================================
- (void)addFileAtPath:(NSString *)path name:(NSString *)name {
- if (!files_)
- files_ = [[NSMutableDictionary alloc] init];
-
[files_ setObject:path forKey:name];
}
//=============================================================================
+- (void)addFileContents:(NSData *)data name:(NSString *)name {
+ [files_ setObject:data forKey:name];
+}
+
+//=============================================================================
- (NSDictionary *)files {
return files_;
}
@@ -155,9 +165,17 @@
count = [fileNames count];
for (i = 0; i < count; ++i) {
NSString *name = [fileNames objectAtIndex:i];
- NSString *file = [files_ objectForKey:name];
+ id fileOrData = [files_ objectForKey:name];
+ NSData *fileData;
+
+ // The object can be either the path to a file (NSString) or the contents
+ // of the file (NSData).
+ if ([fileOrData isKindOfClass:[NSData class]])
+ fileData = [self formDataForFileContents:fileOrData name:name];
+ else
+ fileData = [self formDataForFile:fileOrData name:name];
- [postBody appendData:[self formDataForFile:file name:name]];
+ [postBody appendData:fileData];
}
[req setHTTPBody:postBody];