From 4dab05c3124324a4be4cbfc13f33275ab1291c08 Mon Sep 17 00:00:00 2001 From: Finn Voorhees Date: Wed, 17 Apr 2024 17:05:21 +0100 Subject: [PATCH] Add configuration to pass prefetched html --- .../FaviconFinder+Configuration.swift | 7 ++++- .../Finders/HTMLFaviconFinder.swift | 30 +++++++++++-------- .../WebApplicationManifestFaviconFinder.swift | 30 +++++++++++-------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Sources/FaviconFinder/FaviconFinder+Configuration.swift b/Sources/FaviconFinder/FaviconFinder+Configuration.swift index 60cbd62..d2c0872 100644 --- a/Sources/FaviconFinder/FaviconFinder+Configuration.swift +++ b/Sources/FaviconFinder/FaviconFinder+Configuration.swift @@ -6,6 +6,7 @@ // import Foundation +import SwiftSoup public extension FaviconFinder { @@ -22,16 +23,20 @@ public extension FaviconFinder { /// Indicates if we should check for a meta-refresh-redirect tag in the HTML header public let checkForMetaRefreshRedirect: Bool + public let prefetchedHTML: Document? + // MARK: - Lifecycle public init( preferredSource: FaviconSourceType = .html, preferences: [FaviconSourceType : String] = [:], - checkForMetaRefreshRedirect: Bool = false + checkForMetaRefreshRedirect: Bool = false, + prefetchedHTML: Document? = nil ) { self.preferredSource = preferredSource self.preferences = preferences self.checkForMetaRefreshRedirect = checkForMetaRefreshRedirect + self.prefetchedHTML = prefetchedHTML } } diff --git a/Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift b/Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift index 5fb8479..a679628 100755 --- a/Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift +++ b/Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift @@ -38,21 +38,27 @@ class HTMLFaviconFinder: FaviconFinderProtocol { } func find() async throws -> [FaviconURL] { - // Download the web page at our URL - let response = try await FaviconURLSession.dataTask( - with: self.url, - checkForMetaRefreshRedirect: self.configuration.checkForMetaRefreshRedirect - ) + let html: Document + + if let prefetchedHTML = configuration.prefetchedHTML { + html = prefetchedHTML + } else { + // Download the web page at our URL + let response = try await FaviconURLSession.dataTask( + with: self.url, + checkForMetaRefreshRedirect: self.configuration.checkForMetaRefreshRedirect + ) - let data = response.data + let data = response.data - // Make sure we can parse the response into a string - guard let htmlStr = String(data: data, encoding: response.textEncoding) else { - throw FaviconError.failedToParseHTML - } + // Make sure we can parse the response into a string + guard let htmlStr = String(data: data, encoding: response.textEncoding) else { + throw FaviconError.failedToParseHTML + } - // Turn our HTML string as an XML document we can check out - let html = try SwiftSoup.parse(htmlStr) + // Turn our HTML string as an XML document we can check out + html = try SwiftSoup.parse(htmlStr) + } // Get just the head of our HTML document guard let head = html.head() else { diff --git a/Sources/FaviconFinder/Finders/WebApplicationManifestFaviconFinder.swift b/Sources/FaviconFinder/Finders/WebApplicationManifestFaviconFinder.swift index 1300668..7468348 100755 --- a/Sources/FaviconFinder/Finders/WebApplicationManifestFaviconFinder.swift +++ b/Sources/FaviconFinder/Finders/WebApplicationManifestFaviconFinder.swift @@ -35,21 +35,27 @@ class WebApplicationManifestFaviconFinder: FaviconFinderProtocol { } func find() async throws -> [FaviconURL] { - // Download the web page at our URL - let response = try await FaviconURLSession.dataTask( - with: self.url, - checkForMetaRefreshRedirect: self.configuration.checkForMetaRefreshRedirect - ) + let html: Document + + if let prefetchedHTML = configuration.prefetchedHTML { + html = prefetchedHTML + } else { + // Download the web page at our URL + let response = try await FaviconURLSession.dataTask( + with: self.url, + checkForMetaRefreshRedirect: self.configuration.checkForMetaRefreshRedirect + ) - let data = response.data + let data = response.data - // Make sure we can parse the response into a string - guard let htmlStr = String(data: data, encoding: response.textEncoding) else { - throw FaviconError.failedToParseHTML - } + // Make sure we can parse the response into a string + guard let htmlStr = String(data: data, encoding: response.textEncoding) else { + throw FaviconError.failedToParseHTML + } - // Turn our HTML string as an XML document we can check out - let html = try SwiftSoup.parse(htmlStr) + // Turn our HTML string as an XML document we can check out + html = try SwiftSoup.parse(htmlStr) + } // Get just the head of our HTML document guard let head = html.head() else {