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

Commit

Permalink
Fix #1876: Revert domainUrl, add new method to strip www from url. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub authored and kylehickinson committed Nov 2, 2019
1 parent 05b5b67 commit 9866737
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3034,7 +3034,7 @@ extension BrowserViewController {
guard searchQuery != "",
let iconURL = URL(string: favicon.url),
let url = URL(string: searchQuery.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!),
let shortName = url.domainURL().host else {
let shortName = url.domainURL.host else {
let alert = ThirdPartySearchAlerts.failedToAddThirdPartySearch()
self.present(alert, animated: true, completion: nil)
return
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/FaviconHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class FaviconHandler {
guard let image = image else {
// If we failed to download a page-level icon, try getting the domain-level icon
// instead before ultimately failing.
let siteIconURL = currentURL.domainURL().appendingPathComponent("favicon.ico")
let siteIconURL = currentURL.domainURL.appendingPathComponent("favicon.ico")
imageOperation = webImageCache.load(from: siteIconURL, options: [.lowPriority], progress: onProgress, completion: onCompletedSiteFavicon)

return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class TabLocationView: UIView {

fileprivate func updateTextWithURL() {
(urlTextField as? DisplayTextField)?.hostString = url?.host ?? ""
urlTextField.text = url?.domainURL(stripWWWSubdomainOnly: true).schemelessAbsoluteString.trim("/")
urlTextField.text = url?.withoutWWW.schemelessAbsoluteString.trim("/")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Utils/FaviconFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ open class FaviconFetcher: NSObject, XMLParserDelegate {
// Returns a single Favicon UIImage for a given URL
class func fetchFavImageForURL(forURL url: URL, profile: Profile) -> Deferred<Maybe<UIImage>> {
let deferred = Deferred<Maybe<UIImage>>()
FaviconFetcher.getForURL(url.domainURL()).uponQueue(.main) { result in
FaviconFetcher.getForURL(url.domainURL).uponQueue(.main) { result in
guard let favicons = result.successValue, let favicon = favicons.first, let faviconURL = favicon.url.asURL else {
return deferred.fill(Maybe(failure: FaviconError()))
}
Expand Down
2 changes: 1 addition & 1 deletion Client/WebAuthN/U2FExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class U2FExtensions: NSObject {
guard let url = self.tab?.url else {
return nil
}
return url.domainURL().absoluteString
return url.domainURL.absoluteString
}

private func setCurrentTabId() {
Expand Down
4 changes: 2 additions & 2 deletions Data/models/Domain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ extension Domain {
class func getOrCreateInternal(_ url: URL,
context: NSManagedObjectContext = DataController.viewContext,
save: Bool = true) -> Domain {
let domainString = url.domainURL().absoluteString
let domainString = url.domainURL.absoluteString
if let domain = Domain.first(where: NSPredicate(format: "url == %@", domainString), context: context) {
return domain
}
Expand Down Expand Up @@ -158,7 +158,7 @@ extension Domain {
}

class func getForUrl(_ url: URL) -> Domain? {
let domainString = url.domainURL().absoluteString
let domainString = url.domainURL.absoluteString
return Domain.first(where: NSPredicate(format: "url == %@", domainString))
}

Expand Down
2 changes: 1 addition & 1 deletion DataTests/DomainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DomainTests: CoreDataTestCase {

XCTAssertEqual(domain.bookmarks?.count, 0)
XCTAssertEqual(domain.historyItems?.count, 0)
XCTAssertEqual(domain.url, url.domainURL().absoluteString)
XCTAssertEqual(domain.url, url.domainURL.absoluteString)
}

/// Tests non-HTTPSE shields
Expand Down
29 changes: 19 additions & 10 deletions Shared/Extensions/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,28 @@ extension URL {
}

/**
* Returns just the domain, but with the same scheme, and a trailing '/'.
* Returns just the domain, but with the same scheme.
*
* E.g., https://m.foo.com/bar/baz?noo=abc#123 => https://foo.com/
* E.g., https://m.foo.com/bar/baz?noo=abc#123 => https://foo.com
*
* Any failure? Return this URL.
*
* @param stripWWWSubdomainOnly only strips the www host if true. Else m and mobile are also stripped
* E.g., https://mobile.foo.com/bar/baz?noo=abc#123 => https://mobile.foo.com/
*/
public func domainURL(stripWWWSubdomainOnly: Bool = false) -> URL {
// Use URLComponents instead of URL since the former correctly preserves
// brackets for IPv6 hosts, whereas the latter escapes them.
if let normalized = self.normalizedHost(stripWWWSubdomainOnly),
public var domainURL: URL {
if let normalized = self.normalizedHost() {
// Use URLComponents instead of URL 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
return components.url ?? self
}

return self
}

public var withoutWWW: URL {
if let normalized = self.normalizedHost(stripWWWSubdomainOnly: true),
var components = URLComponents(url: self, resolvingAgainstBaseURL: false) {
components.scheme = self.scheme
components.port = self.port
Expand All @@ -261,7 +270,7 @@ extension URL {
return self
}

public func normalizedHost(_ stripWWWSubdomainOnly: Bool = false) -> String? {
public func normalizedHost(stripWWWSubdomainOnly: Bool = false) -> 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 {
Expand Down
14 changes: 7 additions & 7 deletions SharedTests/NSURLExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,22 @@ class NSURLExtensionsTests: XCTestCase {
badurls.forEach { XCTAssertFalse(URL(string:$0)!.isWebPage(), $0) }
}

func testdomainURL() {
func testdomainWithoutWWW() {
let urls = [
("https://www.example.com/index.html", "https://example.com/index.html"),
("https://mail.example.com/index.html", "https://mail.example.com/index.html"),
("https://mail.example.co.uk/index.html", "https://mail.example.co.uk/index.html"),
]
urls.forEach { XCTAssertEqual(URL(string:$0.0)!.domainURL().absoluteString, $0.1) }
urls.forEach { XCTAssertEqual(URL(string:$0.0)!.withoutWWW.absoluteString, $0.1) }
}

func testdomainURLStrippingOnlyWWW() {
func testdomainUrl() {
let urls = [
("https://www.example.com/index.html", "https://example.com/index.html"),
("https://m.example.com/index.html", "https://m.example.com/index.html"),
("https://mobile.example.co.uk/index.html", "https://mobile.example.co.uk/index.html"),
("https://www.example.com/index.html", "https://example.com"),
("https://mail.example.com/index.html", "https://mail.example.com"),
("https://mail.example.co.uk/index.html", "https://mail.example.co.uk"),
]
urls.forEach { XCTAssertEqual(URL(string:$0.0)!.domainURL(stripWWWSubdomainOnly: true).absoluteString, $0.1) }
urls.forEach { XCTAssertEqual(URL(string:$0.0)!.domainURL.absoluteString, $0.1) }
}

func testdisplayURL() {
Expand Down
2 changes: 1 addition & 1 deletion Storage/Site.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ open class Site: Identifiable, Hashable {
var guid: String?

open var tileURL: URL {
return URL(string: url)?.domainURL() ?? URL(string: "about:blank")!
return URL(string: url)?.domainURL ?? URL(string: "about:blank")!
}

public let url: String
Expand Down

0 comments on commit 9866737

Please sign in to comment.