Normalize homePageURL in FaviconDownloader — this avoids doing duplicate work for http://foo.com/ and http://foo.com — they’re both treated as http://foo.com/

This commit is contained in:
Brent Simmons 2017-11-25 16:11:24 -08:00
parent 61c429d45c
commit ba638ecda1

View File

@ -61,7 +61,7 @@ final class FaviconDownloader {
func favicon(withHomePageURL homePageURL: String) -> NSImage? {
guard let seekingFavicon = seekingFavicon(with: homePageURL) else {
guard let seekingFavicon = seekingFavicon(with: normalizedHomePageURL(homePageURL)) else {
return nil
}
return favicon(withSeekingFavicon: seekingFavicon)
@ -92,6 +92,26 @@ final class FaviconDownloader {
private extension FaviconDownloader {
private static let localeForLowercasing = Locale(identifier: "en_US")
func normalizedHomePageURL(_ url: String) -> String {
// Many times the homePageURL is missing a trailing /.
// We add one when needed.
guard !url.hasSuffix("/") else {
return url
}
let lowercasedURL = url.lowercased(with: FaviconDownloader.localeForLowercasing)
guard lowercasedURL.hasPrefix("http://") || lowercasedURL.hasPrefix("https://") else {
return url
}
guard url.components(separatedBy: "/").count < 4 else {
return url
}
return url + "/"
}
@discardableResult
func favicon(withSeekingFavicon seekingFavicon: SeekingFavicon) -> NSImage? {