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

Commit

Permalink
Using composition of NSMutableData rather than subclassing
Browse files Browse the repository at this point in the history
Renaming private form data proxy object to match @protocol name
  • Loading branch information
mattt committed Sep 23, 2011
1 parent b621ad0 commit 0c0b1bd
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions AFNetworking/AFHTTPClient.m
Expand Up @@ -34,12 +34,14 @@
return [NSString stringWithFormat:@"--%@--", kAFMultipartFormBoundary];
}

@interface AFMutableMultipartFormData : NSMutableData <AFMultipartFormDataProxy> {
@interface AFMultipartFormDataProxy : NSObject <AFMultipartFormDataProxy> {
@private
NSStringEncoding _stringEncoding;
NSRange _finalBoundaryRange;
NSMutableData *_mutableData;
}

@property (readonly) NSData *data;

- (id)initWithStringEncoding:(NSStringEncoding)encoding;

@end
Expand Down Expand Up @@ -199,7 +201,7 @@ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
}

NSMutableURLRequest *request = [self requestWithMethod:method path:path parameters:nil];
__block AFMutableMultipartFormData *formData = [[AFMutableMultipartFormData alloc] initWithStringEncoding:self.stringEncoding];
__block AFMultipartFormDataProxy *formData = [[AFMultipartFormDataProxy alloc] initWithStringEncoding:self.stringEncoding];

id key = nil;
NSEnumerator *enumerator = [parameters keyEnumerator];
Expand All @@ -213,15 +215,15 @@ - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
data = [[value description] dataUsingEncoding:self.stringEncoding];
}

[formData appendPartWithHeaders:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"form-data; name=\"%@\"", [key description]] forKey:@"Content-Disposition"] body:value];
[formData appendPartWithHeaders:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"form-data; name=\"%@\"", [key description]] forKey:@"Content-Disposition"] body:data];
}

if (block) {
block(formData);
}

[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", kAFMultipartFormBoundary] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:formData];
[request setHTTPBody:[formData data]];

[formData autorelease];

Expand Down Expand Up @@ -272,16 +274,16 @@ - (void)deletePath:(NSString *)path parameters:(NSDictionary *)parameters succes
#pragma mark -

// multipart/form-data; see http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2
@interface AFMutableMultipartFormData ()
@interface AFMultipartFormDataProxy ()
@property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding;
@property (readwrite, nonatomic, assign) NSRange finalBoundaryRange;
@property (readwrite, nonatomic, retain) NSMutableData *mutableData;

- (void)appendBlankLine;
@end

@implementation AFMutableMultipartFormData
@implementation AFMultipartFormDataProxy
@synthesize stringEncoding = _stringEncoding;
@synthesize finalBoundaryRange = _finalBoundaryRange;
@synthesize mutableData = _mutableData;

- (id)initWithStringEncoding:(NSStringEncoding)encoding {
self = [super init];
Expand All @@ -290,20 +292,34 @@ - (id)initWithStringEncoding:(NSStringEncoding)encoding {
}

self.stringEncoding = encoding;
self.finalBoundaryRange = NSMakeRange(0, 0);
self.mutableData = [NSMutableData dataWithLength:0];

return self;
}

- (void)dealloc {
[_mutableData release];
[super dealloc];
}

- (NSData *)data {
NSMutableData *finalizedData = [NSMutableData dataWithData:self.mutableData];
[finalizedData appendData:[AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding]];

return finalizedData;
}

#pragma mark - AFMultipartFormDataProxy

- (void)appendPartWithHeaders:(NSDictionary *)headers body:(NSData *)body {
if ([self length] > 0) {
if ([self.mutableData length] > 0) {
[self appendString:AFMultipartFormEncapsulationBoundary()];
[self appendBlankLine];
}

for (NSString *field in [headers allKeys]) {
[self appendString:[NSString stringWithFormat:@"%@: %@", field, [headers valueForKey:field]]];
[self appendBlankLine];
}

[self appendBlankLine];
Expand Down Expand Up @@ -336,20 +352,15 @@ - (void)appendPartWithFile:(NSURL *)fileURL mimeType:(NSString *)mimeType fileNa
}

- (void)appendData:(NSData *)data {
NSMutableData *mutableData = [NSMutableData dataWithData:data];
[self replaceBytesInRange:self.finalBoundaryRange withBytes:[mutableData bytes]];

NSData *finalBoundary = [AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding];
self.finalBoundaryRange = NSMakeRange([self length], [finalBoundary length]);
[super appendData:finalBoundary];
[self.mutableData appendData:data];
}

- (void)appendString:(NSString *)string {
[self appendData:[string dataUsingEncoding:self.stringEncoding]];
}

- (void)appendBlankLine {
[self appendString:@""];
[self appendString:kAFMultipartFormLineDelimiter];
}

@end

0 comments on commit 0c0b1bd

Please sign in to comment.