/
KSURLQueryParameters.m
206 lines (159 loc) · 6.23 KB
/
KSURLQueryParameters.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// KSURLQueryParameters.m
// KSFileUtilities
//
// Created by Mike on 14/10/2013.
// Copyright (c) 2013 Karelia Software. All rights reserved.
//
#import "KSURLQueryParameters.h"
@implementation KSURLQueryParameters
#pragma mark Creating a KSURLQueryParameters object
- init;
{
if (self = [super init])
{
_string = [[NSMutableString alloc] init];
}
return self;
}
- initWithPercentEncodedString:(NSString *)query;
{
NSParameterAssert(query);
if (self = [self init])
{
[_string setString:query];
}
return self;
}
+ (instancetype)queryParametersWithPercentEncodedString:(NSString *)query;
{
if (!query) return nil;
return [[[self alloc] initWithPercentEncodedString:query] autorelease];
}
+ (instancetype)queryParametersOfURL:(NSURL *)url;
{
if (!url) return nil;
CFRange queryRange = CFURLGetByteRangeForComponent((CFURLRef)url, kCFURLComponentQuery, NULL);
if (queryRange.location == kCFNotFound) return nil;
CFIndex length = CFURLGetBytes((CFURLRef)url, NULL, 0);
NSMutableData *data = [[NSMutableData alloc] initWithLength:length];
void *bytes = [data mutableBytes];
CFURLGetBytes((CFURLRef)url, bytes, length);
NSString *string = [[NSString alloc] initWithBytes:(bytes + queryRange.location)
length:queryRange.length
encoding:NSASCIIStringEncoding];
KSURLQueryParameters *result = [self queryParametersWithPercentEncodedString:string];
[data release];
[string release];
return result;
}
- initWithDictionary:(NSDictionary *)dictionary
{
NSParameterAssert(dictionary);
self = [self init];
[self addParametersFromDictionary:dictionary];
return self;
}
+ (instancetype)queryParametersWithDictionary:(NSDictionary *)dictionary;
{
return [[[self alloc] initWithDictionary:dictionary] autorelease];
}
#pragma mark Retrieving Query and Parameters
- (NSString *)percentEncodedQuery;
{
return [[_string copy] autorelease];
}
- (NSDictionary *)dictionaryRepresentation;
{
__block NSMutableDictionary *result = [NSMutableDictionary dictionary];
[self enumerateParametersUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
// Bail if doesn't fit dictionary paradigm
if (!value || [result objectForKey:key])
{
*stop = YES;
result = nil;
return;
}
[result setObject:value forKey:key];
}];
return result;
}
- (void)enumerateParametersUsingBlock:(void (^)(NSString *, NSString *, BOOL *))block;
{
BOOL stop = NO;
NSString *query = self.percentEncodedQuery; // we'll do our own decoding after separating components
NSRange searchRange = NSMakeRange(0, query.length);
while (!stop)
{
NSRange keySeparatorRange = [query rangeOfString:@"=" options:NSLiteralSearch range:searchRange];
if (keySeparatorRange.location == NSNotFound) keySeparatorRange = NSMakeRange(NSMaxRange(searchRange), 0);
NSRange keyRange = NSMakeRange(searchRange.location, keySeparatorRange.location - searchRange.location);
NSString *key = [query substringWithRange:keyRange];
NSString *value = nil;
if (keySeparatorRange.length) // there might be no value, so report as nil
{
searchRange = NSMakeRange(NSMaxRange(keySeparatorRange), query.length - NSMaxRange(keySeparatorRange));
NSRange valueSeparatorRange = [query rangeOfString:@"&" options:NSLiteralSearch range:searchRange];
if (valueSeparatorRange.location == NSNotFound)
{
valueSeparatorRange.location = NSMaxRange(searchRange);
stop = YES;
}
NSRange valueRange = NSMakeRange(searchRange.location, valueSeparatorRange.location - searchRange.location);
value = [query substringWithRange:valueRange];
searchRange = NSMakeRange(NSMaxRange(valueSeparatorRange), query.length - NSMaxRange(valueSeparatorRange));
}
else
{
stop = YES;
}
block([key stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
&stop);
}
}
#pragma mark Adding and Removing Parameters
- (void)addParameter:(NSString *)key value:(NSString *)value;
{
NSParameterAssert(key);
if (_string.length) [_string appendString:@"&"];
CFStringRef escapedKey = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)key, NULL, CFSTR("+=&#"), kCFStringEncodingUTF8);
// Escape + for safety as some backends interpret it as a space
// = indicates the start of value, so must be escaped
// & indicates the start of next parameter, so must be escaped
// # indicates the start of fragment, so must be escaped
[_string appendString:(NSString *)escapedKey];
CFRelease(escapedKey);
if (value)
{
[_string appendString:@"="];
CFStringRef escapedValue = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)value, NULL, CFSTR("+&#"), kCFStringEncodingUTF8);
// Escape + for safety as some backends interpret it as a space
// = is allowed in values, as there's no further value to indicate
// & indicates the start of next parameter, so must be escaped
// # indicates the start of fragment, so must be escaped
[_string appendString:(NSString *)escapedValue];
CFRelease(escapedValue);
}
}
- (void)addParametersFromDictionary:(NSDictionary *)dictionary;
{
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[self addParameter:key value:obj];
}];
}
- (void)removeAllParameters;
{
[_string deleteCharactersInRange:NSMakeRange(0, _string.length)];
}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone;
{
return [[KSURLQueryParameters alloc] initWithPercentEncodedString:self.percentEncodedQuery];
}
#pragma mark Debugging
- (NSString *)description;
{
return [[super description] stringByAppendingFormat:@" ?%@", self.percentEncodedQuery];
}
@end