From 8b6ff5f1de125d8ed99244dc82950bccd9d08faa Mon Sep 17 00:00:00 2001 From: Carlos McEvilly Date: Wed, 27 May 2015 22:12:32 -0700 Subject: [PATCH 1/2] Adding redirectURL property to store the destination of any redirect. --- Source/Request/CMRequest.h | 3 +++ Source/Request/CMRequest.m | 7 +++++++ 2 files changed, 10 insertions(+) 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) { From 858b803789377130099567f1ad67867941a7b9dd Mon Sep 17 00:00:00 2001 From: Carlos McEvilly Date: Thu, 28 May 2015 13:20:45 -0700 Subject: [PATCH 2/2] support pre-formatted string payloads of form key1=value1&key2=value2 --- Source/Coders/CMFormCoder.m | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) 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; }