Skip to content

Commit

Permalink
Merge pull request #70 from will-lumley/tech/linux-tests
Browse files Browse the repository at this point in the history
Replaced NetworkingEngine in Linux to `async-http-client`
  • Loading branch information
will-lumley authored Feb 20, 2024
2 parents b1ab8c4 + f3279c5 commit e0d930a
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 242 deletions.
143 changes: 129 additions & 14 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,131 @@
{
"object": {
"pins": [
{
"package": "SwiftSoup",
"repositoryURL": "https://github.com/scinfu/SwiftSoup.git",
"state": {
"branch": null,
"revision": "f707b8680cddb96dc1855632340a572ef37bbb98",
"version": "2.5.3"
}
}
]
},
"version": 1
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "291438696abdd48d2a83b52465c176efbd94512b",
"version" : "1.20.1"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "12358d55a3824bd5fed310b999ea8cf83a9a1a65",
"version" : "1.0.3"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5",
"version" : "1.5.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "635b2589494c97e48c62514bc8b37ced762e0a62",
"version" : "2.63.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "363da63c1966405764f380c627409b2f9d9e710b",
"version" : "1.21.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "0904bf0feb5122b7e5c3f15db7df0eabe623dd87",
"version" : "1.30.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "7c381eb6083542b124a6c18fae742f55001dc2b5",
"version" : "2.26.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "6cbe0ed2b394f21ab0d46b9f0c50c6be964968ce",
"version" : "1.20.1"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
"version" : "1.2.1"
}
},
{
"identity" : "swiftsoup",
"kind" : "remoteSourceControl",
"location" : "https://github.com/scinfu/SwiftSoup.git",
"state" : {
"revision" : "f707b8680cddb96dc1855632340a572ef37bbb98",
"version" : "2.5.3"
}
}
],
"version" : 2
}
39 changes: 31 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
// swift-tools-version:5.5
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

#if os(Linux)
let dependencies: [PackageDescription.Package.Dependency] = [
// URLSession on Linux is notoriously unreliable and freezes, so this is used instead (only for Linux)
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),

// SwiftSoup is used to parse the HTML tree
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.7")
]

let targetDependencies: [Target.Dependency] = [
"SwiftSoup",
.product(name: "AsyncHTTPClient", package: "async-http-client")
]

#else
let dependencies: [PackageDescription.Package.Dependency] = [
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0"),
// SwiftSoup is used to parse the HTML tree
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.7")
]

let targetDependencies: [Target.Dependency] = [
"SwiftSoup",
.product(name: "AsyncHTTPClient", package: "async-http-client")
]
#endif

let package = Package(
name: "FaviconFinder",
platforms: [.iOS(.v15), .macOS(.v12), .watchOS(.v8), .tvOS(.v15)],
Expand All @@ -12,19 +39,15 @@ let package = Package(
name: "FaviconFinder",
targets: ["FaviconFinder"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.7")
],
dependencies: dependencies,
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "FaviconFinder",
dependencies: [
"SwiftSoup"
]
dependencies: targetDependencies
),

.testTarget(name: "FaviconFinderTests", dependencies: ["FaviconFinder"]),
]
)
9 changes: 4 additions & 5 deletions Sources/FaviconFinder/Finders/HTMLFaviconFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ class HTMLFaviconFinder: FaviconFinderProtocol {

func find() async throws -> [FaviconURL] {
// Download the web page at our URL
let urlResponse = try await FaviconURLSession.dataTask(
let response = try await FaviconURLSession.dataTask(
with: self.url,
checkForMetaRefreshRedirect: self.configuration.checkForMetaRefreshRedirect
)

let data = urlResponse.data
let response = urlResponse.rawResponse

let data = response.data

// Make sure we can parse the response into a string
guard let htmlStr = String(data: data, encoding: response.encoding) else {
guard let htmlStr = String(data: data, encoding: response.textEncoding) else {
throw FaviconError.failedToParseHTML
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ class WebApplicationManifestFaviconFinder: FaviconFinderProtocol {
)

let data = response.data
let rawResponse = response.rawResponse

// Make sure we can parse the response into a string
guard let htmlStr = String(data: data, encoding: rawResponse.encoding) else {
guard let htmlStr = String(data: data, encoding: response.textEncoding) else {
throw FaviconError.failedToParseHTML
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//
// URLRequest+StringEncoding.swift
// Pods
// FaviconFinder
//
// Created by William Lumley on 8/7/2022.
//

import Foundation

#if os(Linux)
import CoreFoundation
import FoundationNetworking
#endif
#if !os(Linux)

extension URLResponse {

Expand All @@ -19,78 +16,14 @@ extension URLResponse {
return .utf8
}

#if os(Linux)
return rawName.encoding
#else
let cfName = CFStringConvertIANACharSetNameToEncoding(rawName as CFString)

let constant = CFStringConvertEncodingToNSStringEncoding(cfName)

let encoded = String.Encoding(rawValue: constant)
return encoded
#endif
}

}

private extension String {

/// We need to manually convert our string to String.Encoding
/// on Linux due to the toll-free bridging from Obj-C to CF classes
/// is something that's only available on Apple platforms.
///
/// - Returns: The String.Encoding enum that our string represents. Will
/// return .utf8 is no known equivalent is found.
var encoding: String.Encoding {
switch self {
case "us-ascii":
return .ascii
case "x-nextstep", "nextstep":
return .nextstep
case "euc-jp":
return .japaneseEUC
case "utf-8":
return .utf8
case "iso-8859-1", "latin1":
return .isoLatin1
case "symbol":
return .symbol
case "non-lossy-ascii":
return .nonLossyASCII
case "shift_jis", "cp932":
return .shiftJIS
case "iso-8859-2", "latin2":
return .isoLatin2
case "unicode":
return .unicode
case "windows-1251":
return .windowsCP1251
case "windows-1252":
return .windowsCP1252
case "windows-1253":
return .windowsCP1253
case "windows-1254":
return .windowsCP1254
case "windows-1250":
return .windowsCP1250
case "iso-2022-jp":
return .iso2022JP
case "macroman", "x-mac-roman":
return .macOSRoman
case "utf-16", "unicodefffe":
return .utf16
case "utf-16be":
return .utf16BigEndian
case "utf-16le":
return .utf16LittleEndian
case "utf-32":
return .utf32
case "utf-32be":
return .utf32BigEndian
case "utf-32le":
return .utf32LittleEndian
default:
return .utf8
}
}
}
#endif
Loading

0 comments on commit e0d930a

Please sign in to comment.