-
Notifications
You must be signed in to change notification settings - Fork 3k
/
URLExtensions.swift
562 lines (475 loc) · 21 KB
/
URLExtensions.swift
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
private struct ETLDEntry: CustomStringConvertible {
let entry: String
var isNormal: Bool { return isWild || !isException }
var isWild: Bool = false
var isException: Bool = false
init(entry: String) {
self.entry = entry
self.isWild = entry.hasPrefix("*")
self.isException = entry.hasPrefix("!")
}
fileprivate var description: String {
return "{ Entry: \(entry), isWildcard: \(isWild), isException: \(isException) }"
}
}
private typealias TLDEntryMap = [String: ETLDEntry]
private func loadEntries() -> TLDEntryMap? {
var entries = TLDEntryMap()
for line in ETLD_NAMES_LIST where !line.isEmpty && !line.hasPrefix("//") {
let entry = ETLDEntry(entry: line)
let key: String
if entry.isWild {
// Trim off the '*.' part of the line
key = String(line[line.index(line.startIndex, offsetBy: 2)...])
} else if entry.isException {
// Trim off the '!' part of the line
key = String(line[line.index(line.startIndex, offsetBy: 1)...])
} else {
key = line
}
entries[key] = entry
}
return entries
}
private var etldEntries: TLDEntryMap? = {
return loadEntries()
}()
// MARK: - Local Resource URL Extensions
extension URL {
public func allocatedFileSize() -> Int64 {
// First try to get the total allocated size and in failing that, get the file allocated size
return getResourceLongLongForKey(URLResourceKey.totalFileAllocatedSizeKey.rawValue)
?? getResourceLongLongForKey(URLResourceKey.fileAllocatedSizeKey.rawValue)
?? 0
}
public func getResourceValueForKey(_ key: String) -> Any? {
let resourceKey = URLResourceKey(key)
let keySet = Set<URLResourceKey>([resourceKey])
var val: Any?
do {
let values = try resourceValues(forKeys: keySet)
val = values.allValues[resourceKey]
} catch _ {
return nil
}
return val
}
public func getResourceLongLongForKey(_ key: String) -> Int64? {
return (getResourceValueForKey(key) as? NSNumber)?.int64Value
}
public func getResourceBoolForKey(_ key: String) -> Bool? {
return getResourceValueForKey(key) as? Bool
}
public var isRegularFile: Bool {
return getResourceBoolForKey(URLResourceKey.isRegularFileKey.rawValue) ?? false
}
public func lastComponentIsPrefixedBy(_ prefix: String) -> Bool {
return (pathComponents.last?.hasPrefix(prefix) ?? false)
}
}
// The list of permanent URI schemes has been taken from http://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
private let permanentURISchemes = ["aaa", "aaas", "about", "acap", "acct", "cap", "cid", "coap", "coaps", "crid", "data", "dav", "dict", "dns", "example", "file", "ftp", "geo", "go", "gopher", "h323", "http", "https", "iax", "icap", "im", "imap", "info", "ipp", "ipps", "iris", "iris.beep", "iris.lwz", "iris.xpc", "iris.xpcs", "jabber", "javascript", "ldap", "mailto", "mid", "msrp", "msrps", "mtqp", "mupdate", "news", "nfs", "ni", "nih", "nntp", "opaquelocktoken", "pkcs11", "pop", "pres", "reload", "rtsp", "rtsps", "rtspu", "service", "session", "shttp", "sieve", "sip", "sips", "sms", "snmp", "soap.beep", "soap.beeps", "stun", "stuns", "tag", "tel", "telnet", "tftp", "thismessage", "tip", "tn3270", "turn", "turns", "tv", "urn", "vemmi", "vnc", "ws", "wss", "xcon", "xcon-userid", "xmlrpc.beep", "xmlrpc.beeps", "xmpp", "z39.50r", "z39.50s"]
extension URL {
public func withQueryParams(_ params: [URLQueryItem]) -> URL {
var components = URLComponents(url: self, resolvingAgainstBaseURL: false)!
var items = (components.queryItems ?? [])
for param in params {
items.append(param)
}
components.queryItems = items
return components.url!
}
public func withQueryParam(_ name: String, value: String) -> URL {
var components = URLComponents(url: self, resolvingAgainstBaseURL: false)!
let item = URLQueryItem(name: name, value: value)
components.queryItems = (components.queryItems ?? []) + [item]
return components.url!
}
public func getQuery() -> [String: String] {
var results = [String: String]()
let keyValues = self.query?.components(separatedBy: "&")
if keyValues?.count ?? 0 > 0 {
for pair in keyValues! {
let kv = pair.components(separatedBy: "=")
if kv.count > 1 {
results[kv[0]] = kv[1]
}
}
}
return results
}
public var hostPort: String? {
if let host = self.host {
if let port = (self as NSURL).port?.int32Value {
return "\(host):\(port)"
}
return host
}
return nil
}
public var origin: String? {
guard isWebPage(includeDataURIs: false), let hostPort = self.hostPort, let scheme = scheme else {
return nil
}
return "\(scheme)://\(hostPort)"
}
/**
* Returns a shorter displayable string for a domain
*
* E.g., https://m.foo.com/bar/baz?noo=abc#123 => foo
* https://accounts.foo.com/bar/baz?noo=abc#123 => accounts.foo
**/
public var shortDisplayString: String {
guard let publicSuffix = self.publicSuffix, let baseDomain = self.normalizedHost else {
return self.normalizedHost ?? self.absoluteString
}
return baseDomain.replacingOccurrences(of: ".\(publicSuffix)", with: "")
}
public var normalizedHostAndPath: String? {
return normalizedHost.flatMap { $0 + self.path }
}
public var absoluteDisplayString: String {
var urlString = self.absoluteString
// For http URLs, get rid of the trailing slash if the path is empty or '/'
if (self.scheme == "http" || self.scheme == "https") && (self.path == "/") && urlString.hasSuffix("/") {
urlString = String(urlString[..<urlString.index(urlString.endIndex, offsetBy: -1)])
}
// If it's basic http, strip out the string but leave anything else in
if urlString.hasPrefix("http://") {
return String(urlString[urlString.index(urlString.startIndex, offsetBy: 7)...])
} else {
return urlString
}
}
/// String suitable for displaying outside of the app, for example in notifications, were Data Detectors will
/// linkify the text and make it into a openable-in-Safari link.
public var absoluteDisplayExternalString: String {
return self.absoluteDisplayString.replacingOccurrences(of: ".", with: "\u{2024}")
}
public var displayURL: URL? {
if AppConstants.IsRunningTest || AppConstants.IsRunningPerfTest, path.contains("test-fixture/") {
return self
}
if self.absoluteString.starts(with: "blob:") {
return URL(string: "blob:")
}
if self.isFileURL {
return URL(string: "file://\(self.lastPathComponent)")
}
if self.isReaderModeURL {
return self.decodeReaderModeURL?.havingRemovedAuthorisationComponents()
}
if let internalUrl = InternalURL(self), internalUrl.isErrorPage {
return internalUrl.originalURLFromErrorPage?.displayURL
}
if !InternalURL.isValid(url: self) {
return self.havingRemovedAuthorisationComponents()
}
return nil
}
/**
Returns the base domain from a given hostname. The base domain name is defined as the public domain suffix
with the base private domain attached to the front. For example, for the URL www.bbc.co.uk, the base domain
would be bbc.co.uk. The base domain includes the public suffix (co.uk) + one level down (bbc).
:returns: The base domain string for the given host name.
*/
public var baseDomain: String? {
guard !isIPv6, let host = host else { return nil }
// If this is just a hostname and not a FQDN, use the entire hostname.
if !host.contains(".") {
return host
}
return publicSuffixFromHost(host, withAdditionalParts: 1)
}
/**
* Returns just the domain, but with the same scheme, and a trailing '/'.
*
* E.g., https://m.foo.com/bar/baz?noo=abc#123 => https://foo.com/
*
* Any failure? Return this URL.
*/
public var domainURL: URL {
if let normalized = self.normalizedHost {
// Use NSURLComponents instead of NSURL since the former correctly preserves
// brackets for IPv6 hosts, whereas the latter escapes them.
var components = URLComponents()
components.scheme = self.scheme
components.port = self.port
components.host = normalized
components.path = "/"
return components.url ?? self
}
return self
}
public var normalizedHost: String? {
// Use components.host instead of self.host since the former correctly preserves
// brackets for IPv6 hosts, whereas the latter strips them.
guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false), var host = components.host, host != "" else {
return nil
}
if let range = host.range(of: "^(www|mobile|m)\\.", options: .regularExpression) {
host.replaceSubrange(range, with: "")
}
return host
}
/**
Returns the public portion of the host name determined by the public suffix list found here: https://publicsuffix.org/list/.
For example for the url www.bbc.co.uk, based on the entries in the TLD list, the public suffix would return co.uk.
:returns: The public suffix for within the given hostname.
*/
public var publicSuffix: String? {
return host.flatMap { publicSuffixFromHost($0, withAdditionalParts: 0) }
}
public func isWebPage(includeDataURIs: Bool = true) -> Bool {
let schemes = includeDataURIs ? ["http", "https", "data"] : ["http", "https"]
return scheme.map { schemes.contains($0) } ?? false
}
public var isIPv6: Bool {
return host?.contains(":") ?? false
}
/**
Returns whether the URL's scheme is one of those listed on the official list of URI schemes.
This only accepts permanent schemes: historical and provisional schemes are not accepted.
*/
public var schemeIsValid: Bool {
guard let scheme = scheme else { return false }
return permanentURISchemes.contains(scheme.lowercased())
}
public func havingRemovedAuthorisationComponents() -> URL {
guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
return self
}
urlComponents.user = nil
urlComponents.password = nil
if let url = urlComponents.url {
return url
}
return self
}
public func isEqual(_ url: URL) -> Bool {
if self == url {
return true
}
// Try an additional equality case by chopping off the trailing slash
let urls: [String] = [url.absoluteString, absoluteString].map { item in
if let lastCh = item.last, lastCh == "/" {
return item.dropLast().lowercased()
}
return item.lowercased()
}
return urls[0] == urls[1]
}
}
// Extensions to deal with ReaderMode URLs
extension URL {
public var isReaderModeURL: Bool {
let scheme = self.scheme, host = self.host, path = self.path
return scheme == "http" && host == "localhost" && path == "/reader-mode/page"
}
public var isSyncedReaderModeURL: Bool {
return self.absoluteString.hasPrefix("about:reader?url=")
}
public var decodeReaderModeURL: URL? {
if self.isReaderModeURL || self.isSyncedReaderModeURL {
if let components = URLComponents(url: self, resolvingAgainstBaseURL: false), let queryItems = components.queryItems {
if let queryItem = queryItems.find({ $0.name == "url"}), let value = queryItem.value {
return URL(string: value)
}
}
}
return nil
}
public func encodeReaderModeURL(_ baseReaderModeURL: String) -> URL? {
if let encodedURL = absoluteString.addingPercentEncoding(withAllowedCharacters: .alphanumerics) {
if let aboutReaderURL = URL(string: "\(baseReaderModeURL)?url=\(encodedURL)") {
return aboutReaderURL
}
}
return nil
}
}
// Helpers to deal with ErrorPage URLs
public struct InternalURL {
public static let uuid = UUID().uuidString
public static let scheme = "internal"
public static let baseUrl = "\(scheme)://local"
public enum Path: String {
case errorpage = "errorpage"
case sessionrestore = "sessionrestore"
func matches(_ string: String) -> Bool {
return string.range(of: "/?\(self.rawValue)", options: .regularExpression, range: nil, locale: nil) != nil
}
}
public enum Param: String {
case uuidkey = "uuidkey"
case url = "url"
func matches(_ string: String) -> Bool { return string == self.rawValue }
}
public let url: URL
private let sessionRestoreHistoryItemBaseUrl = "\(InternalURL.baseUrl)/\(InternalURL.Path.sessionrestore.rawValue)?url="
public static func isValid(url: URL) -> Bool {
let isWebServerUrl = url.absoluteString.hasPrefix("http://localhost:\(AppInfo.webserverPort)/")
if isWebServerUrl, url.path.hasPrefix("/test-fixture/") {
// internal test pages need to be treated as external pages
return false
}
// TODO: (reader-mode-custom-scheme) remove isWebServerUrl when updating code.
return isWebServerUrl || InternalURL.scheme == url.scheme
}
public init?(_ url: URL) {
guard InternalURL.isValid(url: url) else {
return nil
}
self.url = url
}
public var isAuthorized: Bool {
return (url.getQuery()[InternalURL.Param.uuidkey.rawValue] ?? "") == InternalURL.uuid
}
public var stripAuthorization: String {
guard var components = URLComponents(string: url.absoluteString), let items = components.queryItems else { return url.absoluteString }
components.queryItems = items.filter { !Param.uuidkey.matches($0.name) }
if let items = components.queryItems, items.count == 0 {
components.queryItems = nil // This cleans up the url to not end with a '?'
}
return components.url?.absoluteString ?? ""
}
public static func authorize(url: URL) -> URL? {
guard var components = URLComponents(string: url.absoluteString) else { return nil }
if components.queryItems == nil {
components.queryItems = []
}
if var item = components.queryItems?.find({ Param.uuidkey.matches($0.name) }) {
item.value = InternalURL.uuid
} else {
components.queryItems?.append(URLQueryItem(name: Param.uuidkey.rawValue, value: InternalURL.uuid))
}
return components.url
}
public var isSessionRestore: Bool {
return url.absoluteString.hasPrefix(sessionRestoreHistoryItemBaseUrl)
}
public var isErrorPage: Bool {
// Error pages can be nested in session restore URLs, and session restore handler will forward them to the error page handler
let path = url.absoluteString.hasPrefix(sessionRestoreHistoryItemBaseUrl) ? extractedUrlParam?.path : url.path
return InternalURL.Path.errorpage.matches(path ?? "")
}
public var originalURLFromErrorPage: URL? {
if !url.absoluteString.hasPrefix(sessionRestoreHistoryItemBaseUrl) {
return isErrorPage ? extractedUrlParam : nil
}
if let urlParam = extractedUrlParam, let nested = InternalURL(urlParam), nested.isErrorPage {
return nested.extractedUrlParam
}
return nil
}
public var extractedUrlParam: URL? {
if let nestedUrl = url.getQuery()[InternalURL.Param.url.rawValue]?.unescape() {
return URL(string: nestedUrl)
}
return nil
}
public var isAboutHomeURL: Bool {
if let urlParam = extractedUrlParam, let internalUrlParam = InternalURL(urlParam) {
return internalUrlParam.aboutComponent?.hasPrefix("home") ?? false
}
return aboutComponent?.hasPrefix("home") ?? false
}
public var isAboutURL: Bool {
return aboutComponent != nil
}
/// Return the path after "about/" in the URI.
public var aboutComponent: String? {
let aboutPath = "/about/"
guard let url = URL(string: stripAuthorization) else {
return nil
}
if url.path.hasPrefix(aboutPath) {
return String(url.path.dropFirst(aboutPath.count))
}
return nil
}
}
//MARK: Private Helpers
private extension URL {
func publicSuffixFromHost( _ host: String, withAdditionalParts additionalPartCount: Int) -> String? {
if host.isEmpty {
return nil
}
// Check edge case where the host is either a single or double '.'.
if host.isEmpty || NSString(string: host).lastPathComponent == "." {
return ""
}
/**
* The following algorithm breaks apart the domain and checks each sub domain against the effective TLD
* entries from the effective_tld_names.dat file. It works like this:
*
* Example Domain: test.bbc.co.uk
* TLD Entry: bbc
*
* 1. Start off by checking the current domain (test.bbc.co.uk)
* 2. Also store the domain after the next dot (bbc.co.uk)
* 3. If we find an entry that matches the current domain (test.bbc.co.uk), perform the following checks:
* i. If the domain is a wildcard AND the previous entry is not nil, then the current domain matches
* since it satisfies the wildcard requirement.
* ii. If the domain is normal (no wildcard) and we don't have anything after the next dot, then
* currentDomain is a valid TLD
* iii. If the entry we matched is an exception case, then the base domain is the part after the next dot
*
* On the next run through the loop, we set the new domain to check as the part after the next dot,
* update the next dot reference to be the string after the new next dot, and check the TLD entries again.
* If we reach the end of the host (nextDot = nil) and we haven't found anything, then we've hit the
* top domain level so we use it by default.
*/
let tokens = host.components(separatedBy: ".")
let tokenCount = tokens.count
var suffix: String?
var previousDomain: String?
var currentDomain: String = host
for offset in 0..<tokenCount {
// Store the offset for use outside of this scope so we can add additional parts if needed
let nextDot: String? = offset + 1 < tokenCount ? tokens[offset + 1..<tokenCount].joined(separator: ".") : nil
if let entry = etldEntries?[currentDomain] {
if entry.isWild && (previousDomain != nil) {
suffix = previousDomain
break
} else if entry.isNormal || (nextDot == nil) {
suffix = currentDomain
break
} else if entry.isException {
suffix = nextDot
break
}
}
previousDomain = currentDomain
if let nextDot = nextDot {
currentDomain = nextDot
} else {
break
}
}
var baseDomain: String?
if additionalPartCount > 0 {
if let suffix = suffix {
// Take out the public suffixed and add in the additional parts we want.
let literalFromEnd: NSString.CompareOptions = [.literal, // Match the string exactly.
.backwards, // Search from the end.
.anchored] // Stick to the end.
let suffixlessHost = host.replacingOccurrences(of: suffix, with: "", options: literalFromEnd, range: nil)
let suffixlessTokens = suffixlessHost.components(separatedBy: ".").filter { $0 != "" }
let maxAdditionalCount = max(0, suffixlessTokens.count - additionalPartCount)
let additionalParts = suffixlessTokens[maxAdditionalCount..<suffixlessTokens.count]
let partsString = additionalParts.joined(separator: ".")
baseDomain = [partsString, suffix].joined(separator: ".")
} else {
return nil
}
} else {
baseDomain = suffix
}
return baseDomain
}
}