From 1561f0392b2355dc7dc8e938c8bd199222c9f0f5 Mon Sep 17 00:00:00 2001 From: RayMorgan Date: Tue, 29 Nov 2011 14:16:48 -0800 Subject: [PATCH] Fix issue with multipart form boundaries. The first boundary should not have a preceeding newline. This causes some multipart parsers to miss the inital boundary. An example of a parser facing this issue is node-formidable by felixge. --- AFNetworking/AFHTTPClient.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index a812d33..dbb66b5 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -381,6 +381,10 @@ static NSString * AFPropertyListStringFromParameters(NSDictionary *parameters) { #pragma mark - +static inline NSString * AFMultipartFormInitialBoundary() { + return [NSString stringWithFormat:@"--%@%@", kAFMultipartFormBoundary, kAFMultipartFormLineDelimiter]; +} + static inline NSString * AFMultipartFormEncapsulationBoundary() { return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormLineDelimiter, kAFMultipartFormBoundary, kAFMultipartFormLineDelimiter]; } @@ -424,7 +428,11 @@ static inline NSString * AFMultipartFormFinalBoundary() { #pragma mark - AFMultipartFormData - (void)appendPartWithHeaders:(NSDictionary *)headers body:(NSData *)body { - [self appendString:AFMultipartFormEncapsulationBoundary()]; + if ([self.mutableData length] == 0) { + [self appendString:AFMultipartFormInitialBoundary()]; + } else { + [self appendString:AFMultipartFormEncapsulationBoundary()]; + } for (NSString *field in [headers allKeys]) { [self appendString:[NSString stringWithFormat:@"%@: %@%@", field, [headers valueForKey:field], kAFMultipartFormLineDelimiter]];