Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Fix #2869 - never return nil as the base domain (#2940) (#2950)
Browse files Browse the repository at this point in the history
* Fix #2869 - never return nil as the base domain

* Return host if failed to extract the domain

Co-authored-by: Stefan Arentz <[email protected]>
(cherry picked from commit e2dc3a3)

Co-authored-by: Tse Kit Yam <[email protected]>
  • Loading branch information
mergify[bot] and tsekityam authored Jan 7, 2022
1 parent da04820 commit 46c9b9b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Blockzilla/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ extension URL {
:returns: The base domain string for the given host name.
*/
public var baseDomain: String? {
guard !isIPv6, let host = host else { return nil }
guard !isIPv4 && !isIPv6, let host = host else { return host }

// If this is just a hostname and not a FQDN, use the entire hostname.
if !host.contains(".") {
return host
}

return publicSuffixFromHost(host, withAdditionalParts: 1)
return publicSuffixFromHost(host, withAdditionalParts: 1) ?? host
}

/**
Expand Down Expand Up @@ -285,6 +285,12 @@ extension URL {
return host.lowercased() == "localhost" || host == "127.0.0.1"
}

public var isIPv4: Bool {
let ipv4Pattern = #"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"#

return host?.range(of: ipv4Pattern, options: .regularExpression) != nil
}

public var isIPv6: Bool {
return host?.contains(":") ?? false
}
Expand Down

0 comments on commit 46c9b9b

Please sign in to comment.