Skip to content

Commit

Permalink
Add configuration to pass prefetched html
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor committed Apr 17, 2024
1 parent c87a6d2 commit 4dab05c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
7 changes: 6 additions & 1 deletion Sources/FaviconFinder/FaviconFinder+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import SwiftSoup

public extension FaviconFinder {

Expand All @@ -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
}
}

Expand Down
30 changes: 18 additions & 12 deletions Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4dab05c

Please sign in to comment.