forked from karelia/KSFileUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KSURLNormalization.m
544 lines (478 loc) · 18.6 KB
/
KSURLNormalization.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Created by Warren Dodge
// Copyright © 2012 Karelia Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Normalization of URLs based on
// http://en.wikipedia.org/wiki/URL_normalization
#import "KSURLNormalization.h"
#ifndef NS_ENUM
#define NS_ENUM(_type, _name) _type _name; enum
#endif
typedef NS_ENUM(NSInteger, ks_URLPart)
{
ks_URLPartScheme = kCFURLComponentScheme,
ks_URLPartPath = kCFURLComponentPath,
ks_URLPartUserAndPassword = kCFURLComponentPassword - 1,
//ks_URLPartPassword = kCFURLComponentPassword,
ks_URLPartHost = kCFURLComponentHost,
ks_URLPartPort = kCFURLComponentPort,
ks_URLPartParameterString = kCFURLComponentParameterString,
ks_URLPartQuery = kCFURLComponentQuery,
ks_URLPartFragment = kCFURLComponentFragment
};
@interface NSURL (KSURLNormalizationPrivate)
- (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart;
#pragma mark Normalizations that preserve semantics.
@property (nonatomic, readonly, copy) NSURL *ks_URLByLowercasingSchemeAndHost;
@property (nonatomic, readonly, copy) NSURL *ks_URLByUppercasingEscapes;
@property (nonatomic, readonly, copy) NSURL *ks_URLByUnescapingUnreservedCharactersInPath;
@property (nonatomic, readonly, copy) NSURL *ks_URLByAddingTrailingSlashToDirectory;
@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDefaultPort;
@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDotSegments;
#pragma mark Normalizations that change semantics.
@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDirectoryIndex;
@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingFragment;
//- (NSURL *)ks_URLByReplacingIPWithHost;
@property (nonatomic, readonly, copy) NSURL *ks_URLByRemovingDuplicateSlashes;
//- (NSURL *)ks_URLByRemovingEmptyQuery
@end
@implementation NSURL (KSURLNormalization)
- (NSURL *)ks_normalizedURL
{
NSURL *norm = self;
@autoreleasepool {
norm = [norm ks_URLByRemovingDuplicateSlashes]; // must be 1st!
norm = [norm ks_URLByRemovingDotSegments];
norm = [norm ks_URLByRemovingDuplicateSlashes];
norm = [norm ks_URLByLowercasingSchemeAndHost];
norm = [norm ks_URLByUppercasingEscapes];
norm = [norm ks_URLByUnescapingUnreservedCharactersInPath];
norm = [norm ks_URLByAddingTrailingSlashToDirectory];
norm = [norm ks_URLByRemovingDefaultPort];
if (!norm.fileURL)
{
norm = [norm ks_URLByRemovingDirectoryIndex];
}
norm = [norm ks_URLByRemovingFragment];
}
return norm;
}
- (NSRange)ks_replacementRangeOfURLPart:(ks_URLPart)anURLPart
{
// Determine correct range for replacing the specified URL part INCLUDING DELIMITERS.
// Note that if the URL part is not found, the range length is 0, but the range location is NOT NSNotFound, but rather the location that the indicated URL part could be inserted.
// NOTE: Duplicate "/" must be removed from URL before finding the path's (and later URL element's) range is guaranteed correct!
NSString *scheme = self.scheme.lowercaseString;
NSString *templateSchemePart = @"://";
NSString *realSchemePart = @"";
NSString *user = self.user;
NSString *password = self.password;
NSString *passwordDelimiter = @":";
NSString *userDelimiter = @"@";
NSString *host = self.host.lowercaseString;
NSString *port = nil;
if (self.port.intValue > 0)
{ // lurking bug: if port has leading 0
port = [NSString stringWithFormat:@"%d", self.port.intValue];
}
NSString *portDelimiter = @":";
NSString *path = self.path; // updated later
NSString *parameterString = self.parameterString;
NSString *parameterDelimiter = @";";
NSString *query = self.query;
NSString *queryDelimiter = @"?";
NSString *fragment = self.fragment;
NSString *fragmentDelimiter = @"#";
NSRange rPart = (NSRange){0,0};
if (anURLPart >= ks_URLPartScheme)
{
rPart.location += 0;
rPart.length = scheme.length;
}
if (anURLPart > ks_URLPartScheme)
{
rPart.location += scheme.length;
NSRange testForSchemePart = NSMakeRange(rPart.location, templateSchemePart.length);
NSRange searchForSchemePart = [self.absoluteString rangeOfString:templateSchemePart];
if (NSEqualRanges(testForSchemePart, searchForSchemePart))
{
realSchemePart = templateSchemePart;
rPart.length = realSchemePart.length;
}
else
{
realSchemePart = @"";
rPart.length = realSchemePart.length;
}
}
if (anURLPart >= ks_URLPartUserAndPassword || anURLPart == ks_URLPartPath)
{
rPart.location += realSchemePart.length;
rPart.length = 0;
if (user && user.length)
{
rPart.length += (user.length + userDelimiter.length);
}
if (password && password.length)
{
rPart.length += (password.length + passwordDelimiter.length);
}
}
if (anURLPart >= ks_URLPartHost || anURLPart == ks_URLPartPath)
{
if (user && user.length)
{
rPart.location += (user.length + userDelimiter.length);
}
if (password && password.length)
{
rPart.location += (password.length + passwordDelimiter.length);
}
rPart.length = host.length;
}
if (anURLPart >= ks_URLPartPort || anURLPart == ks_URLPartPath)
{
rPart.location += host.length;
rPart.length = 0;
if (port && port.length)
{
rPart.length = port.length + portDelimiter.length;
}
}
if (anURLPart >= ks_URLPartParameterString || anURLPart == ks_URLPartPath)
{
if (port && port.length)
{
rPart.location += (port.length + portDelimiter.length);
}
// NOTE: Duplicate "/" must be removed from URL before finding the path's (and later URL element's) range is guaranteed correct!
// There are 2 problems with NSURL's path method: first it drops trailing "/" characters, second it percent-unescapes the path.
// We have to work around those issues here.
// Get the length of un-escaped path by comparing the length of complete URL to URL with path stripped.
NSInteger urlLength = self.absoluteString.length;
NSURL *rootPathURL = [self copy];
NSArray *pathComponents = rootPathURL.pathComponents;
NSInteger cnt = pathComponents.count;
for (NSInteger i = cnt; i > 1; i--)
{
rootPathURL = rootPathURL.URLByDeletingLastPathComponent;
}
// At this point, the path of rootPathURL is "/" or "".
NSInteger rootPathURLLength = rootPathURL.absoluteString.length;
rPart.length = urlLength - rootPathURLLength + rootPathURL.path.length;
// Replace path with corrected version.
path = [self.absoluteString substringWithRange:rPart];
}
if (anURLPart >= ks_URLPartParameterString)
{
rPart.location += path.length;
rPart.length = 0;
if (parameterString && parameterString.length)
{
rPart.length = parameterString.length + parameterDelimiter.length;
}
}
if (anURLPart >= ks_URLPartQuery)
{
if (parameterString && parameterString.length)
{
rPart.location += (parameterString.length + parameterDelimiter.length);
}
rPart.length = 0;
if (query && query.length)
{
rPart.length = query.length + queryDelimiter.length;
}
}
if (anURLPart >= ks_URLPartFragment)
{
if (query && query.length)
{
rPart.location += (query.length + queryDelimiter.length);
}
rPart.length = 0;
if (fragment && fragment.length)
{
rPart.length = fragment.length + fragmentDelimiter.length;
}
else
{
NSString *abs = self.absoluteString;
if (rPart.location < abs.length)
{
NSString *maybeFragmentDelimiter = [abs substringWithRange:NSMakeRange(rPart.location, 1)];
if ([maybeFragmentDelimiter isEqualToString:fragmentDelimiter])
{
rPart.length = 1;
}
}
}
}
return rPart;
}
#pragma mark Normalizations that preserve semantics.
// Convert scheme and host to lower case.
- (NSURL *)ks_URLByLowercasingSchemeAndHost
{
// If both scheme and host are already lowercase, nothing need be done
NSString *scheme = self.scheme;
NSString *host = self.host;
NSCharacterSet *uppercase = [NSCharacterSet uppercaseLetterCharacterSet];
if ([scheme rangeOfCharacterFromSet:uppercase].location == NSNotFound &&
[host rangeOfCharacterFromSet:uppercase].location == NSNotFound)
{
return self;
}
NSRange rScheme = [self ks_replacementRangeOfURLPart:ks_URLPartScheme];
NSRange rHost = [self ks_replacementRangeOfURLPart:ks_URLPartHost];
NSString *abs = self.absoluteString;
if (scheme.length)
{
abs = [abs stringByReplacingCharactersInRange:rScheme withString:scheme.lowercaseString];
}
if (host.length)
{
abs = [abs stringByReplacingCharactersInRange:rHost withString:host.lowercaseString];
}
NSURL *correctedURL = [NSURL URLWithString:abs];
return correctedURL;
}
// Capitalize letters in escape sequences.
- (NSURL *)ks_URLByUppercasingEscapes
{
// http://en.wikipedia.org/wiki/Percent_encoding
NSString *anURLStr = self.absoluteString;
NSRange rSearch = NSMakeRange(0, anURLStr.length);
while (rSearch.length > 0)
{
NSRange rFound = [anURLStr rangeOfString:@"%" options:0 range:rSearch];
if (rFound.location != NSNotFound)
{
rFound.length += 2;
if (rFound.location + rFound.length < anURLStr.length)
{
NSString *escapeUpper = [anURLStr substringWithRange:rFound].uppercaseString;
anURLStr = [anURLStr stringByReplacingCharactersInRange:rFound withString:escapeUpper];
}
rSearch.location = rFound.location + rFound.length;
rSearch.length = anURLStr.length - rSearch.location;
}
else
{
// done
break;
}
}
NSURL *correctedURL = [NSURL URLWithString:anURLStr];
return correctedURL;
}
// Decode percent-encoded octets of unreserved characters.
- (NSURL *)ks_URLByUnescapingUnreservedCharactersInPath
{
NSString *abs = self.absoluteString;
NSRange pathRange = [self ks_replacementRangeOfURLPart:ks_URLPartPath];
NSString *rawPath = [abs substringWithRange:pathRange];
// Transform only very limited characters: "!"
// All reserved characters per RFC 3986: @" !*'();:@&=+$,/?#[]"
// Since only 1 character, do this manually to avoid possible issues with unicode chars being unencoded.
NSString *unescapedPath = [rawPath stringByReplacingOccurrencesOfString:@"%21" withString:@"!"];
if ([unescapedPath isEqualToString:rawPath]) return self;
NSString *unescapedURLString = [abs stringByReplacingCharactersInRange:pathRange withString:unescapedPath];
NSURL *unescapedURL = [NSURL URLWithString:unescapedURLString];
return unescapedURL;
}
// Add trailing "/".
- (NSURL *)ks_URLByAddingTrailingSlashToDirectory
{
NSString *pathExt = self.pathExtension;
if (pathExt && pathExt.length > 0)
{ // No need for trailing slash.
return self;
}
NSRange rPath = [self ks_replacementRangeOfURLPart:ks_URLPartPath];
if (rPath.length == 0 && self.host.length == 0)
{ // No need for trailing slash.
return self;
}
NSString *abs = self.absoluteString;
if (abs.length == 0)
{
return self;
}
NSString *path = [abs substringWithRange:rPath];
if ([path rangeOfString:@"/" options:NSBackwardsSearch].location == (path.length - 1))
{ // last char of path is "/" already
return self;
}
path = [path stringByAppendingString:@"/"];
abs = [abs stringByReplacingCharactersInRange:rPath withString:path];
NSURL *correctedURL = [NSURL URLWithString:abs];
return correctedURL;
}
// Remove default port for http, https.
- (NSURL *)ks_URLByRemovingDefaultPort
{
NSString *scheme = self.scheme.lowercaseString;
NSInteger portVal = self.port.integerValue;
BOOL removePort = NO;
if ([scheme isEqualToString:@"http"] && portVal == 80)
{
removePort = YES;
}
else if ([scheme isEqualToString:@"https"] && portVal == 443)
{
removePort = YES;
}
if (!removePort)
{
return self;
}
NSRange rPort = [self ks_replacementRangeOfURLPart:ks_URLPartPort];
NSString *abs = self.absoluteString;
abs = [abs stringByReplacingCharactersInRange:rPort withString:@""];
NSURL *correctedURL = [NSURL URLWithString:abs];
return correctedURL;
}
// Remove dot-segments.
- (NSURL *)ks_URLByRemovingDotSegments
{
NSURL *standardized = self.standardizedURL;
if (standardized)
{
return standardized;
}
else
{
return self;
}
}
#pragma mark Normalizations that change semantics.
// Remove common directory index filenames.
- (NSURL *)ks_URLByRemovingDirectoryIndex
{
// Check whether a "directory index" page specified in URL.
NSString *lastPathComponent = self.lastPathComponent;
static NSArray *defaultsArray;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
defaultsArray = [[NSArray alloc] initWithObjects:
@"index.html",
@"index.htm",
@"index.php",
@"index.asp",
@"index.aspx",
@"index.cfm",
@"default.htm",
@"default.asp",
@"default.aspx",
nil];
});
for (NSString *defPage in defaultsArray)
{
if ([defPage caseInsensitiveCompare:lastPathComponent] == NSOrderedSame)
{
return self.URLByDeletingLastPathComponent;
}
}
return self;
}
// Remove the fragment.
- (NSURL *)ks_URLByRemovingFragment
{
NSRange rFragment = [self ks_replacementRangeOfURLPart:ks_URLPartFragment];
if (rFragment.length == 0)
{
return self;
}
NSString *abs = self.absoluteString;
abs = [abs stringByReplacingCharactersInRange:rFragment withString:@""];
NSURL *correctedURL = [NSURL URLWithString:abs];
return correctedURL;
}
// Replace IP with host.
//- (NSURL *)ks_URLByReplacingIPWithHost;
// Remove duplicate slashes.
- (NSURL *)ks_URLByRemovingDuplicateSlashes
{
// Replace all duplicate slashes ("//") except if preceeded by ":"
// NOTE: This method does not depend on the -ks_replacementRangeOfURLPart: method. This is required because all the URL ranges for the path and later URL components depend on this method being called on the URL first.
NSMutableString *abs = [self.absoluteString mutableCopy];
NSRange schemeSlashesRange = [abs rangeOfString:@"://"];
NSRange replaceRange;
if (NSNotFound == schemeSlashesRange.location)
{
replaceRange = NSMakeRange(0, abs.length);
}
else
{
NSInteger start = schemeSlashesRange.location + schemeSlashesRange.length;
replaceRange = NSMakeRange(start, abs.length - start);
}
while ([abs replaceOccurrencesOfString:@"//" withString:@"/" options:0 range:replaceRange])
{
NSInteger start = schemeSlashesRange.location + schemeSlashesRange.length;
replaceRange = NSMakeRange(start, abs.length - start);
}
NSURL *cleanedURL = [NSURL URLWithString:abs];
return cleanedURL;
// // CFURLCopyStrictPath leaves out the first slash, so the resulting path is likely relative, making it safe to call -stringByStandardizingPath on
// CFStringRef path = CFURLCopyStrictPath((CFURLRef)self, NULL);
// if (!path) return self;
//
// // Deal with any leading slash such as from http://example.com//foo/
// NSString *standardized = (NSString *)path;
// while ([standardized isAbsolutePath]) standardized = [standardized substringFromIndex:1];
//
// standardized = [standardized stringByStandardizingPath];
//
// // Pop back on the directory indicator
// if (CFURLHasDirectoryPath((CFURLRef)self)) standardized = [standardized stringByAppendingString:@"/"];
//
// BOOL changed = ![standardized isEqualToString:(NSString *)path];
// CFRelease(path);
//
// if (!changed) return self;
//
// // Prepend the "/" that CFURLCopyStrictPath removed.
// standardized = [NSString stringWithFormat:@"/%@", standardized];
//
// NSString *abs = [self absoluteString];
// // The following line is our problem: ks_replacementRangeOfURLPart: for URL parts >= path are not valid until duplicate "/" characters have been removed.
// NSRange rPath = [self ks_replacementRangeOfURLPart:ks_URLPartPath];
//
// abs = [abs stringByReplacingCharactersInRange:rPath withString:standardized];
// NSURL *correctedURL = [NSURL URLWithString:abs];
// return correctedURL;
}
// Remove empty query string.
//- (NSURL *)ks_URLByRemovingEmptyQuery
//{
// NSRange rQuery = [self ks_replacementRangeOfURLPart:ks_URLPartQuery];
// if (rQuery.length != 1)
// { // Not an empty query.
// return self;
// }
//
// NSString *abs = [self absoluteString];
// abs = [abs stringByReplacingCharactersInRange:rQuery withString:@""];
// NSURL *correctedURL = [NSURL URLWithString:abs];
// return correctedURL;
//}
@end