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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
-
-
+
+
+
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
+
+
+
+
+
-
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
+
+
-
+
+
-
-
+
+
-
+
-
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
-
+
+
-
-
+
+
+
-
+
-
-
+
-
-
-
+
+
+
-
+
-
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
+
+
-
+
-
-
-
+
+
+
-
+
-
-
+
+
-
+
-
-
+
+
+
-
-
-
+
+
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
-
-
-
-
-
+
-
-
-
+
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#import "OFMutableURL.h"
#import "OFMutableURI.h"
#import "OFURI+Private.h"
#import "OFArray.h"
#import "OFDictionary.h"
#ifdef OF_HAVE_FILES
# import "OFFileManager.h"
#endif
#import "OFNumber.h"
#import "OFPair.h"
#import "OFString.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
@implementation OFMutableURL
@dynamic scheme, URLEncodedScheme, host, URLEncodedHost, port, user;
@dynamic URLEncodedUser, password, URLEncodedPassword, path, URLEncodedPath;
@dynamic pathComponents, query, URLEncodedQuery, queryDictionary, fragment;
@dynamic URLEncodedFragment;
@implementation OFMutableURI
@dynamic scheme, host, percentEncodedHost, port, user, percentEncodedUser;
@dynamic password, percentEncodedPassword, path, percentEncodedPath;
@dynamic pathComponents, query, percentEncodedQuery, queryItems, fragment;
@dynamic percentEncodedFragment;
+ (instancetype)URL
+ (instancetype)URIWithScheme: (OFString *)scheme
{
return [[[self alloc] init] autorelease];
return [[[self alloc] initWithScheme: scheme] autorelease];
}
- (instancetype)initWithScheme: (OFString *)scheme
{
self = [super of_init];
@try {
self.scheme = scheme;
_percentEncodedPath = @"";
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)setScheme: (OFString *)scheme
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedScheme;
OFString *old = _scheme;
if (scheme.length < 1 || !OFASCIIIsAlpha(*scheme.UTF8String))
@throw [OFInvalidFormatException exception];
_URLEncodedScheme = [[scheme stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLSchemeAllowedCharacterSet]] copy];
OFURIVerifyIsEscaped(scheme,
[OFCharacterSet URISchemeAllowedCharacterSet], false);
_scheme = [scheme.lowercaseString copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedScheme: (OFString *)URLEncodedScheme
{
OFString *old;
if (URLEncodedScheme != nil)
OFURLVerifyIsEscaped(URLEncodedScheme,
[OFCharacterSet URLSchemeAllowedCharacterSet]);
old = _URLEncodedScheme;
_URLEncodedScheme = [URLEncodedScheme copy];
[old release];
}
- (void)setHost: (OFString *)host
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedHost;
OFString *old = _percentEncodedHost;
if (OFURLIsIPv6Host(host))
_URLEncodedHost = [[OFString alloc]
if (OFURIIsIPv6Host(host))
_percentEncodedHost = [[OFString alloc]
initWithFormat: @"[%@]", host];
else
_URLEncodedHost = [[host
stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLHostAllowedCharacterSet]] copy];
_percentEncodedHost = [[host
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIHostAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedHost: (OFString *)URLEncodedHost
- (void)setPercentEncodedHost: (OFString *)percentEncodedHost
{
OFString *old;
if ([URLEncodedHost hasPrefix: @"["] &&
[URLEncodedHost hasSuffix: @"]"]) {
if (!OFURLIsIPv6Host([URLEncodedHost substringWithRange:
OFRangeMake(1, URLEncodedHost.length - 2)]))
if ([percentEncodedHost hasPrefix: @"["] &&
[percentEncodedHost hasSuffix: @"]"]) {
if (!OFURIIsIPv6Host([percentEncodedHost substringWithRange:
OFMakeRange(1, percentEncodedHost.length - 2)]))
@throw [OFInvalidFormatException exception];
} else if (URLEncodedHost != nil)
OFURLVerifyIsEscaped(URLEncodedHost,
[OFCharacterSet URLHostAllowedCharacterSet]);
} else if (percentEncodedHost != nil)
OFURIVerifyIsEscaped(percentEncodedHost,
[OFCharacterSet URIHostAllowedCharacterSet], true);
old = _URLEncodedHost;
_URLEncodedHost = [URLEncodedHost copy];
old = _percentEncodedHost;
_percentEncodedHost = [percentEncodedHost copy];
[old release];
}
- (void)setPort: (OFNumber *)port
{
OFNumber *old = _port;
if (port.longLongValue < 0 || port.longLongValue > 65535)
@throw [OFInvalidArgumentException exception];
_port = [port copy];
[old release];
}
- (void)setUser: (OFString *)user
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedUser;
OFString *old = _percentEncodedUser;
_percentEncodedUser = [[user
_URLEncodedUser = [[user stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLUserAllowedCharacterSet]] copy];
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIUserAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedUser: (OFString *)URLEncodedUser
- (void)setPercentEncodedUser: (OFString *)percentEncodedUser
{
OFString *old;
if (URLEncodedUser != nil)
OFURLVerifyIsEscaped(URLEncodedUser,
[OFCharacterSet URLUserAllowedCharacterSet]);
if (percentEncodedUser != nil)
OFURIVerifyIsEscaped(percentEncodedUser,
[OFCharacterSet URIUserAllowedCharacterSet], true);
old = _URLEncodedUser;
_URLEncodedUser = [URLEncodedUser copy];
old = _percentEncodedUser;
_percentEncodedUser = [percentEncodedUser copy];
[old release];
}
- (void)setPassword: (OFString *)password
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedPassword;
OFString *old = _percentEncodedPassword;
_URLEncodedPassword = [[password
stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLPasswordAllowedCharacterSet]] copy];
_percentEncodedPassword = [[password
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIPasswordAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedPassword: (OFString *)URLEncodedPassword
- (void)setPercentEncodedPassword: (OFString *)percentEncodedPassword
{
OFString *old;
if (URLEncodedPassword != nil)
OFURLVerifyIsEscaped(URLEncodedPassword,
[OFCharacterSet URLPasswordAllowedCharacterSet]);
if (percentEncodedPassword != nil)
OFURIVerifyIsEscaped(percentEncodedPassword,
[OFCharacterSet URIPasswordAllowedCharacterSet], true);
old = _URLEncodedPassword;
_URLEncodedPassword = [URLEncodedPassword copy];
old = _percentEncodedPassword;
_percentEncodedPassword = [percentEncodedPassword copy];
[old release];
}
- (void)setPath: (OFString *)path
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedPath;
OFString *old = _percentEncodedPath;
_percentEncodedPath = [[path
_URLEncodedPath = [[path stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLPathAllowedCharacterSet]] copy];
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIPathAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedPath: (OFString *)URLEncodedPath
- (void)setPercentEncodedPath: (OFString *)percentEncodedPath
{
OFString *old;
if (URLEncodedPath != nil)
OFURLVerifyIsEscaped(URLEncodedPath,
[OFCharacterSet URLPathAllowedCharacterSet]);
OFURIVerifyIsEscaped(percentEncodedPath,
[OFCharacterSet URIPathAllowedCharacterSet], true);
old = _URLEncodedPath;
_URLEncodedPath = [URLEncodedPath copy];
old = _percentEncodedPath;
_percentEncodedPath = [percentEncodedPath copy];
[old release];
}
- (void)setPathComponents: (OFArray *)components
{
void *pool = objc_autoreleasePoolPush();
if (components == nil) {
self.path = nil;
return;
}
if (components.count == 0)
@throw [OFInvalidFormatException exception];
if ([components.firstObject length] != 0)
@throw [OFInvalidFormatException exception];
if ([components.firstObject isEqual: @"/"]) {
OFMutableArray *mutComponents =
[[components mutableCopy] autorelease];
[mutComponents replaceObjectAtIndex: 0 withObject: @""];
components = mutComponents;
}
self.path = [components componentsJoinedByString: @"/"];
objc_autoreleasePoolPop(pool);
}
- (void)setQuery: (OFString *)query
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedQuery;
OFString *old = _percentEncodedQuery;
_percentEncodedQuery = [[query
_URLEncodedQuery = [[query stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLQueryAllowedCharacterSet]] copy];
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIQueryAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedQuery: (OFString *)URLEncodedQuery
- (void)setPercentEncodedQuery: (OFString *)percentEncodedQuery
{
OFString *old;
if (URLEncodedQuery != nil)
OFURLVerifyIsEscaped(URLEncodedQuery,
[OFCharacterSet URLQueryAllowedCharacterSet]);
if (percentEncodedQuery != nil)
OFURIVerifyIsEscaped(percentEncodedQuery,
[OFCharacterSet URIQueryAllowedCharacterSet], true);
old = _URLEncodedQuery;
_URLEncodedQuery = [URLEncodedQuery copy];
old = _percentEncodedQuery;
_percentEncodedQuery = [percentEncodedQuery copy];
[old release];
}
- (void)setQueryDictionary:
(OFDictionary OF_GENERIC(OFString *, OFString *) *)dictionary
- (void)setQueryItems:
(OFArray OF_GENERIC(OFPair OF_GENERIC(OFString *, OFString *) *) *)
queryItems
{
void *pool;
OFMutableString *URLEncodedQuery;
OFMutableString *percentEncodedQuery;
OFEnumerator OF_GENERIC(OFString *) *keyEnumerator, *objectEnumerator;
OFCharacterSet *characterSet;
OFString *key, *object, *old;
OFString *old;
if (dictionary == nil) {
[_URLEncodedQuery release];
_URLEncodedQuery = nil;
if (queryItems == nil) {
[_percentEncodedQuery release];
_percentEncodedQuery = nil;
return;
}
pool = objc_autoreleasePoolPush();
URLEncodedQuery = [OFMutableString string];
percentEncodedQuery = [OFMutableString string];
keyEnumerator = [dictionary keyEnumerator];
objectEnumerator = [dictionary objectEnumerator];
characterSet = [OFCharacterSet URLQueryKeyValueAllowedCharacterSet];
characterSet = [OFCharacterSet URIQueryKeyValueAllowedCharacterSet];
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil) {
key = [key
stringByURLEncodingWithAllowedCharacters: characterSet];
object = [object
stringByURLEncodingWithAllowedCharacters: characterSet];
for (OFPair OF_GENERIC(OFString *, OFString *) *item in queryItems) {
OFString *key = [item.firstObject
stringByAddingPercentEncodingWithAllowedCharacters:
characterSet];
OFString *value = [item.secondObject
stringByAddingPercentEncodingWithAllowedCharacters:
characterSet];
if (URLEncodedQuery.length > 0)
[URLEncodedQuery appendString: @"&"];
if (percentEncodedQuery.length > 0)
[percentEncodedQuery appendString: @"&"];
[URLEncodedQuery appendFormat: @"%@=%@", key, object];
[percentEncodedQuery appendFormat: @"%@=%@", key, value];
}
old = _URLEncodedQuery;
_URLEncodedQuery = [URLEncodedQuery copy];
old = _percentEncodedQuery;
_percentEncodedQuery = [percentEncodedQuery copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setFragment: (OFString *)fragment
{
void *pool = objc_autoreleasePoolPush();
OFString *old = _URLEncodedFragment;
OFString *old = _percentEncodedFragment;
_URLEncodedFragment = [[fragment
stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLFragmentAllowedCharacterSet]] copy];
_percentEncodedFragment = [[fragment
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIFragmentAllowedCharacterSet]] copy];
[old release];
objc_autoreleasePoolPop(pool);
}
- (void)setURLEncodedFragment: (OFString *)URLEncodedFragment
- (void)setPercentEncodedFragment: (OFString *)percentEncodedFragment
{
OFString *old;
if (URLEncodedFragment != nil)
OFURLVerifyIsEscaped(URLEncodedFragment,
[OFCharacterSet URLFragmentAllowedCharacterSet]);
if (percentEncodedFragment != nil)
OFURIVerifyIsEscaped(percentEncodedFragment,
[OFCharacterSet URIFragmentAllowedCharacterSet], true);
old = _URLEncodedFragment;
_URLEncodedFragment = [URLEncodedFragment copy];
old = _percentEncodedFragment;
_percentEncodedFragment = [percentEncodedFragment copy];
[old release];
}
- (id)copy
{
OFMutableURL *copy = [self mutableCopy];
OFMutableURI *copy = [self mutableCopy];
[copy makeImmutable];
return copy;
}
- (void)appendPathComponent: (OFString *)component
{
[self appendPathComponent: component isDirectory: false];
#ifdef OF_HAVE_FILES
if ([_URLEncodedScheme isEqual: @"file"] &&
![_URLEncodedPath hasSuffix: @"/"] &&
[[OFFileManager defaultManager] directoryExistsAtURL: self]) {
if ([_scheme isEqual: @"file"] &&
![_percentEncodedPath hasSuffix: @"/"] &&
[[OFFileManager defaultManager] directoryExistsAtURI: self]) {
void *pool = objc_autoreleasePoolPush();
OFString *path = [_URLEncodedPath
OFString *path = [_percentEncodedPath
stringByAppendingString: @"/"];
[_URLEncodedPath release];
_URLEncodedPath = [path retain];
[_percentEncodedPath release];
_percentEncodedPath = [path retain];
objc_autoreleasePoolPop(pool);
}
#endif
}
- (void)appendPathComponent: (OFString *)component
isDirectory: (bool)isDirectory
{
void *pool;
OFString *path;
if ([component isEqual: @"/"] && [_URLEncodedPath hasSuffix: @"/"])
if ([component isEqual: @"/"] && [_percentEncodedPath hasSuffix: @"/"])
return;
pool = objc_autoreleasePoolPush();
component = [component stringByURLEncodingWithAllowedCharacters:
[OFCharacterSet URLPathAllowedCharacterSet]];
component = [component
stringByAddingPercentEncodingWithAllowedCharacters:
[OFCharacterSet URIPathAllowedCharacterSet]];
#if defined(OF_WINDOWS) || defined(OF_MSDOS)
if ([_URLEncodedPath hasSuffix: @"/"] ||
([_URLEncodedScheme isEqual: @"file"] &&
[_URLEncodedPath hasSuffix: @":"]))
if ([_percentEncodedPath hasSuffix: @"/"] ||
([_scheme isEqual: @"file"] &&
[_percentEncodedPath hasSuffix: @":"]))
#else
if ([_URLEncodedPath hasSuffix: @"/"])
if ([_percentEncodedPath hasSuffix: @"/"])
#endif
path = [_URLEncodedPath stringByAppendingString: component];
path = [_percentEncodedPath stringByAppendingString: component];
else
path = [_URLEncodedPath
path = [_percentEncodedPath
stringByAppendingFormat: @"/%@", component];
if (isDirectory && ![path hasSuffix: @"/"])
path = [path stringByAppendingString: @"/"];
[_URLEncodedPath release];
_URLEncodedPath = [path retain];
[_percentEncodedPath release];
_percentEncodedPath = [path retain];
objc_autoreleasePoolPop(pool);
}
- (void)standardizePath
{
void *pool;
void *pool = objc_autoreleasePoolPush();
OFMutableArray OF_GENERIC(OFString *) *array;
bool done = false, endsWithEmpty;
bool done = false, startsWithEmpty, endsWithEmpty;
OFString *path;
if (_URLEncodedPath == nil)
return;
pool = objc_autoreleasePoolPush();
array = [[[_URLEncodedPath
array = [[[_percentEncodedPath
componentsSeparatedByString: @"/"] mutableCopy] autorelease];
if ([array.firstObject length] != 0)
@throw [OFInvalidFormatException exception];
endsWithEmpty = ([array.lastObject length] == 0);
startsWithEmpty = ([array.firstObject length] == 0);
while (!done) {
size_t length = array.count;
done = true;
for (size_t i = 0; i < length; i++) {
|