Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a more robust query string -> NSDictionary class that deals with …
…escaping properly (thanks @protocool).
- Loading branch information
1 parent
0cfb96b
commit 14daa62
Showing
6 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
@interface NSDictionary (PonyExtensions) | |||
+ (NSDictionary *)dictionaryWithFormEncodedString:(NSString *)encodedString | |||
- (NSString *)stringWithFormEncodedComponents; | |||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,49 @@ | |||
@implementation NSDictionary (PonyExtensions) | |||
|
|||
+ (NSDictionary *)dictionaryWithFormEncodedString:(NSString *)encodedString | |||
{ | |||
NSMutableDictionary* result = [NSMutableDictionary dictionary]; | |||
NSArray* pairs = [encodedString componentsSeparatedByString:@"&"]; | |||
|
|||
for (NSString* kvp in pairs) | |||
{ | |||
if ([kvp length] == 0) | |||
continue; | |||
|
|||
NSRange pos = [kvp rangeOfString:@"="]; | |||
NSString *key; | |||
NSString *val; | |||
|
|||
if (pos.location == NSNotFound) | |||
{ | |||
key = [kvp stringByUnescapingFromURLQuery]; | |||
val = @""; | |||
} | |||
else | |||
{ | |||
key = [[kvp substringToIndex:pos.location] stringByUnescapingFromURLQuery]; | |||
val = [[kvp substringFromIndex:pos.location + pos.length] stringByUnescapingFromURLQuery]; | |||
} | |||
|
|||
if (!key || !val) | |||
continue; // I'm sure this will bite my arse one day | |||
|
|||
[result setObject:val forKey:key]; | |||
} | |||
return result; | |||
} | |||
|
|||
- (NSString *)stringWithFormEncodedComponents | |||
{ | |||
NSMutableArray* arguments = [NSMutableArray arrayWithCapacity:[self count]]; | |||
for (NSString* key in self) | |||
{ | |||
[arguments addObject:[NSString stringWithFormat:@"%@=%@", | |||
[key stringByEscapingForURLQuery], | |||
[[[self objectForKey:key] description] stringByEscapingForURLQuery]]]; | |||
} | |||
|
|||
return [arguments componentsJoinedByString:@"&"]; | |||
} | |||
|
|||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
@interface NSString (PonyExtensions) | |||
- (NSString*)stringByEscapingForURLQuery; | |||
- (NSString*)stringByUnescapingFromURLQuery; | |||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
#import "NSString+PonyExtensions.h" | |||
|
|||
@implementation NSString (PonyExtensions) | |||
|
|||
- (NSString*)stringByEscapingForURLQuery | |||
{ | |||
NSString *result = self; | |||
|
|||
CFStringRef originalAsCFString = (CFStringRef) self; | |||
CFStringRef leaveAlone = CFSTR(" "); | |||
CFStringRef toEscape = CFSTR("\n\r?[]()$,!'*;:@&=#%+/"); | |||
|
|||
CFStringRef escapedStr; | |||
escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, originalAsCFString, leaveAlone, toEscape, kCFStringEncodingUTF8); | |||
|
|||
if (escapedStr) { | |||
NSMutableString *mutable = [NSMutableString stringWithString:(NSString *)escapedStr]; | |||
CFRelease(escapedStr); | |||
|
|||
[mutable replaceOccurrencesOfString:@" " withString:@"+" options:0 range:NSMakeRange(0, [mutable length])]; | |||
result = mutable; | |||
} | |||
return result; | |||
} | |||
|
|||
- (NSString*)stringByUnescapingFromURLQuery | |||
{ | |||
return [[self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@" "]; | |||
} | |||
|
|||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters