From 46c9b9bdd54fccf5eba1fc10d68ffd575f8518b9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 7 Jan 2022 15:15:21 +0000 Subject: [PATCH] Fix #2869 - never return nil as the base domain (#2940) (#2950) * Fix #2869 - never return nil as the base domain * Return host if failed to extract the domain Co-authored-by: Stefan Arentz (cherry picked from commit e2dc3a39738080b04debd337c2170438c52953c0) Co-authored-by: Tse Kit Yam --- Blockzilla/URLExtensions.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Blockzilla/URLExtensions.swift b/Blockzilla/URLExtensions.swift index a2c6d7ccd0..254514f076 100644 --- a/Blockzilla/URLExtensions.swift +++ b/Blockzilla/URLExtensions.swift @@ -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 } /** @@ -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 }