Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
fixed zero-length appendData bug and added appendPartWithStreamingURL…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
lansing committed Jun 13, 2012
1 parent d7852c1 commit d92462e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
13 changes: 13 additions & 0 deletions AFNetworking/AFHTTPClient.h 100644 → 100755
Expand Up @@ -498,6 +498,19 @@ extern NSString * AFQueryStringFromParametersWithEncoding(NSDictionary *paramete
*/
- (BOOL)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name error:(NSError **)error;

/**
Appends the HTTP header `Content-Disposition: file; filename=#{generated filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary, using NSInputStream to read input.
@param fileURL The URL corresponding to the file whose content will be appended to the form.
@param name The name to be associated with the specified data. This parameter must not be `nil`.
@param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`.
@discussion The filename is generated from the last component of the streamingURL parameter. The size of the buffer used to copy from streamingURL to the output stream is defined by the constant kAFStreamToStreamBufferSize.
*/
- (void)appendPartWithStreamingURL:(NSURL *)streamingURL
name:(NSString *)name
mimeType:(NSString *)mimeType;

/**
Appends encoded data to the form data.
Expand Down
48 changes: 42 additions & 6 deletions AFNetworking/AFHTTPClient.m 100644 → 100755
Expand Up @@ -680,6 +680,8 @@ - (void)patchPath:(NSString *)path

static NSString * const kAFMultipartFormCRLF = @"\r\n";

static NSInteger const kAFStreamToStreamBufferSize = 1024*1024; //1 meg default

static inline NSString * AFMultipartFormInitialBoundary() {
return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormCRLF];
}
Expand Down Expand Up @@ -751,10 +753,10 @@ - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {

[self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"];
[self.request setValue:[[self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey] stringValue] forHTTPHeaderField:@"Content-Length"];
[self.request setHTTPBodyStream:[NSInputStream inputStreamWithFileAtPath:self.temporaryFilePath]];

[self.outputStream close];

[self.request setHTTPBodyStream:[NSInputStream inputStreamWithFileAtPath:self.temporaryFilePath]];

return self.request;
}

Expand Down Expand Up @@ -790,16 +792,45 @@ - (void)appendPartWithFormData:(NSData *)data
[self appendPartWithHeaders:mutableHeaders body:data];
}

- (NSMutableDictionary *)fileHeadersWithName:(NSString *)name
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType {
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];
return mutableHeaders;
}

- (void)appendPartWithFileData:(NSData *)data
name:(NSString *)name
fileName:(NSString *)fileName
mimeType:(NSString *)mimeType
{
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];

[self appendPartWithHeaders:mutableHeaders body:data];
[self appendPartWithHeaders:[self fileHeadersWithName:name fileName:fileName mimeType:mimeType] body:data];
}

- (void)appendPartWithStreamingURL:(NSURL *)streamingURL
name:(NSString *)name
mimeType:(NSString *)mimeType
{

NSString * fileName = [[streamingURL pathComponents] objectAtIndex:([[streamingURL pathComponents] count] - 1)];
[self appendPartWithHeaders:[self fileHeadersWithName:name fileName:fileName mimeType:mimeType] body:nil];

NSInputStream * inputStream = [NSInputStream inputStreamWithURL:streamingURL];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
[inputStream open];

void * dataBuffer = malloc(kAFStreamToStreamBufferSize);
NSInteger bytesRead = [inputStream read:dataBuffer maxLength:kAFStreamToStreamBufferSize];
while (bytesRead > 0) {
NSData * tempData = [NSData dataWithBytesNoCopy:dataBuffer length:bytesRead freeWhenDone:NO];
[self appendData:tempData];
bytesRead = [inputStream read:dataBuffer maxLength:kAFStreamToStreamBufferSize];
}
free(dataBuffer);
}

- (BOOL)appendPartWithFileURL:(NSURL *)fileURL
Expand Down Expand Up @@ -837,9 +868,14 @@ - (void)appendString:(NSString *)string {
}

- (void)appendData:(NSData *)data {
if ([data length] == 0) {
return;
}
if ([self.outputStream hasSpaceAvailable]) {
const uint8_t *dataBuffer = (uint8_t *) [data bytes];
[self.outputStream write:&dataBuffer[0] maxLength:[data length]];
} else {
NSLog(@"Failed to append to outputStream!");
}
}

Expand Down

0 comments on commit d92462e

Please sign in to comment.