diff --git a/Classes/NSMutableURLRequest+Parameters.m b/Classes/NSMutableURLRequest+Parameters.m index 5eb005f..a422c19 100755 --- a/Classes/NSMutableURLRequest+Parameters.m +++ b/Classes/NSMutableURLRequest+Parameters.m @@ -37,24 +37,41 @@ - (NSArray *)parameters { NSString *encodedParameters = nil; if (![self isMultipart]) { - if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) { + // Allow PUT and DELETE methods as well, assuming they have query params. + if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"] || [[self HTTPMethod] isEqualToString:@"PUT"] || [[self URL] query]) { encodedParameters = [[self URL] query]; } else { encodedParameters = [[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding]; } } + else if ([[self URL] query]) { + encodedParameters = [[self URL] query]; + } + // Do not mess with application/json in the body. + if ([[self valueForHTTPHeaderField:@"Content-Type"] hasPrefix:@"application/json"] && !encodedParameters) { + return nil; + } + + // Nothing to encode, return nothing. if (encodedParameters == nil || [encodedParameters isEqualToString:@""]) { return nil; } -// NSLog(@"raw parameters %@", encodedParameters); + NSArray *encodedParameterPairs = [encodedParameters componentsSeparatedByString:@"&"]; NSMutableArray *requestParameters = [NSMutableArray arrayWithCapacity:[encodedParameterPairs count]]; for (NSString *encodedPair in encodedParameterPairs) { - NSArray *encodedPairElements = [encodedPair componentsSeparatedByString:@"="]; - OARequestParameter *parameter = [[OARequestParameter alloc] initWithName:[[encodedPairElements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] - value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + NSRange equalsRange = [encodedPair rangeOfString:@"="]; + if (equalsRange.location == NSNotFound) { + continue; + } + // Everything to the left of the first equal is the key, everything + // after is the value. + NSString *key = [encodedPair substringToIndex:equalsRange.location]; + NSString *value = [encodedPair substringFromIndex:equalsRange.location + equalsRange.length]; + OARequestParameter *parameter = [[OARequestParameter alloc] initWithName:[key stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] + value:[value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [requestParameters addObject:parameter]; } diff --git a/Classes/OAMutableURLRequest.m b/Classes/OAMutableURLRequest.m index bc7ee8d..eb9af4d 100644 --- a/Classes/OAMutableURLRequest.m +++ b/Classes/OAMutableURLRequest.m @@ -132,7 +132,7 @@ - (void)_generateNonce { //NSMakeCollectable(theUUID); if (nonce) { } - nonce = (__bridge NSString *)string; + nonce = CFBridgingRelease(string); } NSInteger normalize(id obj1, id obj2, void *context) @@ -180,11 +180,9 @@ - (NSString *)_signatureBaseString { [parameterPairs addObject:[[OARequestParameter requestParameter:k value:[tokenParameters objectForKey:k]] URLEncodedNameValuePair]]; } - if (![[self valueForHTTPHeaderField:@"Content-Type"] hasPrefix:@"multipart/form-data"]) { - for (OARequestParameter *param in parameters) { - [parameterPairs addObject:[param URLEncodedNameValuePair]]; - } - } + for (OARequestParameter *param in parameters) { + [parameterPairs addObject:[param URLEncodedNameValuePair]]; + } // Oauth Spec, Section 3.4.1.3.2 "Parameters Normalization NSArray *sortedPairs = [parameterPairs sortedArrayUsingFunction:normalize context:NULL];