forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDictionary+HTTP.m
61 lines (54 loc) · 2.13 KB
/
NSDictionary+HTTP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#import "NSDictionary+HTTP.h"
@implementation NSDictionary (HTTPExtensions)
- (NSString *) formatForHTTP
{
return [self formatForHTTPUsingEncoding:NSASCIIStringEncoding];
}
- (NSString *) formatForHTTPUsingEncoding:(NSStringEncoding)inEncoding
{
return [self formatForHTTPUsingEncoding:inEncoding ordering:nil];
}
- (NSString *) formatForHTTPUsingEncoding:(NSStringEncoding)inEncoding ordering:(NSArray *)inOrdering
{
NSMutableString *s = [NSMutableString stringWithCapacity:256];
NSEnumerator *e = (nil == inOrdering) ? [self keyEnumerator] : [inOrdering objectEnumerator];
CFStringEncoding cfStrEnc = CFStringConvertNSStringEncodingToEncoding(inEncoding);
for (id key in e)
{
id keyObject = [self objectForKey: key];
// conform with rfc 1738 3.3, also escape URL-like characters that might be in the parameters
NSString *escapedKey
= (NSString *) CFURLCreateStringByAddingPercentEscapes(
NULL, (CFStringRef) key, NULL, (CFStringRef) @";:@&=/+", cfStrEnc);
if ([keyObject respondsToSelector: @selector(objectEnumerator)])
{
for (id aValue in [keyObject objectEnumerator])
{
NSString *escapedObject
= (NSString *) CFURLCreateStringByAddingPercentEscapes(
NULL, (CFStringRef) [aValue description], NULL, (CFStringRef) @";:@&=/+", cfStrEnc);
[s appendFormat:@"%@=%@&", escapedKey, escapedObject];
[escapedObject release];
escapedObject = 0;
}
}
else
{
NSString *escapedObject
= (NSString *) CFURLCreateStringByAddingPercentEscapes(
NULL, (CFStringRef) [keyObject description], NULL, (CFStringRef) @";:@&=/+", cfStrEnc);
[s appendFormat:@"%@=%@&", escapedKey, escapedObject];
[escapedObject release];
escapedObject = 0;
}
[escapedKey release];
escapedKey = 0;
}
// Delete final & from the string
if (![s isEqualToString:@""])
{
[s deleteCharactersInRange:NSMakeRange([s length]-1, 1)];
}
return s;
}
@end