diff --git a/Source/Coders/CMFormCoder.m b/Source/Coders/CMFormCoder.m index 3ddd510..c90623b 100644 --- a/Source/Coders/CMFormCoder.m +++ b/Source/Coders/CMFormCoder.m @@ -51,23 +51,31 @@ - (NSData *)encodeObject:(id)object { NSStringEncoding encoding = NSUTF8StringEncoding; NSMutableArray *pairs = [NSMutableArray new]; - - if (NO == [object isKindOfClass:[NSDictionary class]]) { - return nil; - } - - for (NSString *key in [object allKeys]) { - id value = [object valueForKey:key]; - if (NO == [value isKindOfClass:[NSString class]]) { - if ([value respondsToSelector:@selector(description)]) { - value = [value description]; + + NSData *payloadData = nil; + + if (YES == [object isKindOfClass:[NSDictionary class]]) { + for (NSString *key in [object allKeys]) { + id value = [object valueForKey:key]; + if (NO == [value isKindOfClass:[NSString class]]) { + if ([value respondsToSelector:@selector(description)]) { + value = [value description]; + } } + value = [self urlencodeString:value withEncoding:encoding]; + [pairs addObject:[NSString stringWithFormat:@"%@=%@", key, value]]; } - value = [self urlencodeString:value withEncoding:encoding]; - [pairs addObject:[NSString stringWithFormat:@"%@=%@", key, value]]; + NSString *payloadString = [pairs componentsJoinedByString:@"&"]; + payloadData = [payloadString dataUsingEncoding:encoding]; + } + else if (YES == [object isKindOfClass:[NSString class]]) { + // Preformatted payload string of form @"key1=value1&key2=value2" + NSAssert([object containsString:@"="], @"A form payload of type NSString should contain key=value pairs"); + NSString *payloadString = (NSString *)object; + payloadData = [payloadString dataUsingEncoding:encoding]; } - NSString *payloadString = [pairs componentsJoinedByString:@"&"]; - return [payloadString dataUsingEncoding:encoding]; + + return payloadData; } diff --git a/Source/Request/CMRequest.h b/Source/Request/CMRequest.h index 6b59bb3..7ded894 100644 --- a/Source/Request/CMRequest.h +++ b/Source/Request/CMRequest.h @@ -131,6 +131,9 @@ /** When set, the request will include the appropriate 'Range' and 'if-Range' along with 'ETag' and/or 'Last-Modified' headers to request a range of the resource. */ @property (nonatomic) CMContentRange range; +// The URL if any to which the request was redirected; nil otherwise. +@property (nonatomic, strong) NSString *redirectURL; + // ========================================================================== // diff --git a/Source/Request/CMRequest.m b/Source/Request/CMRequest.m index 0948b01..715ca93 100644 --- a/Source/Request/CMRequest.m +++ b/Source/Request/CMRequest.m @@ -676,6 +676,13 @@ - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticatio - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { + NSHTTPURLResponse *httpURLResponse = (NSHTTPURLResponse *)response; + if (httpURLResponse) { + if (httpURLResponse.statusCode >= kHTTPStatusMultipleChoices && httpURLResponse.statusCode < kHTTPStatusBadRequest) { + // it's a redirect + self.redirectURL = [[httpURLResponse allHeaderFields] valueForKey:@"Location"]; + } + } if (response && self.authProviders.count > 0) { NSMutableURLRequest *authorizedRequest = [request mutableCopy]; for (id authProvider in self.authProviders) {