Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes for parameter encoding #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Classes/NSMutableURLRequest+Parameters.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
10 changes: 4 additions & 6 deletions Classes/OAMutableURLRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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];
Expand Down