From c227182443a9eada96763cf1c784ab49eb86a72c Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Mon, 20 Oct 2025 15:39:22 -0700 Subject: [PATCH 1/2] Stop using Foundation on platforms with FoundationEssentials --- Sources/HTTPTypes/HTTPRequest+URL.swift | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Sources/HTTPTypes/HTTPRequest+URL.swift b/Sources/HTTPTypes/HTTPRequest+URL.swift index 75b655e..502763a 100644 --- a/Sources/HTTPTypes/HTTPRequest+URL.swift +++ b/Sources/HTTPTypes/HTTPRequest+URL.swift @@ -14,14 +14,12 @@ #if canImport(FoundationEssentials) import FoundationEssentials -#else +#else // canImport(FoundationEssentials) import Foundation -#endif - #if canImport(CoreFoundation) -import Foundation import CoreFoundation #endif // canImport(CoreFoundation) +#endif // canImport(FoundationEssentials) extension HTTPRequest { /// The URL of the request synthesized from the scheme, authority, and path pseudo header @@ -92,7 +90,7 @@ extension URL { buffer.append(contentsOf: authority) buffer.append(contentsOf: path) - #if canImport(CoreFoundation) + #if !canImport(FoundationEssentials) && canImport(CoreFoundation) if let url = buffer.withUnsafeBytes({ buffer in CFURLCreateAbsoluteURLWithBytes( kCFAllocatorDefault, @@ -107,14 +105,14 @@ extension URL { } else { return nil } - #else // canImport(CoreFoundation) + #else // !canImport(FoundationEssentials) && canImport(CoreFoundation) // This initializer does not preserve WHATWG URLs self.init(string: String(decoding: buffer, as: UTF8.self)) - #endif // canImport(CoreFoundation) + #endif // !canImport(FoundationEssentials) && canImport(CoreFoundation) } fileprivate var httpRequestComponents: (scheme: [UInt8], authority: [UInt8]?, path: [UInt8]) { - #if canImport(CoreFoundation) + #if !canImport(FoundationEssentials) && canImport(CoreFoundation) // CFURL parser based on byte ranges does not unnecessarily percent-encode WHATWG URL let url = unsafeBitCast(self.absoluteURL as NSURL, to: CFURL.self) let length = CFURLGetBytes(url, nil, 0) @@ -167,7 +165,7 @@ extension URL { } return (scheme, authority, path) } - #else // canImport(CoreFoundation) + #else // !canImport(FoundationEssentials) && canImport(CoreFoundation) guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true), let urlString = components.string else { @@ -213,6 +211,6 @@ extension URL { } } return (scheme, authority, path) - #endif // canImport(CoreFoundation) + #endif // !canImport(FoundationEssentials) && canImport(CoreFoundation) } } From 1f98b21cc7c52c439a6a92c59ef8655ea03255b8 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Mon, 20 Oct 2025 15:43:06 -0700 Subject: [PATCH 2/2] Fix test --- Tests/HTTPTypesTests/HTTPTypesURLTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/HTTPTypesTests/HTTPTypesURLTests.swift b/Tests/HTTPTypesTests/HTTPTypesURLTests.swift index 69f0603..1088ae4 100644 --- a/Tests/HTTPTypesTests/HTTPTypesURLTests.swift +++ b/Tests/HTTPTypesTests/HTTPTypesURLTests.swift @@ -50,11 +50,11 @@ final class HTTPTypesURLTests: XCTestCase { let request5 = HTTPRequest(url: URL(string: "data:,Hello%2C%20World%21")!) XCTAssertEqual(request5.scheme, "data") XCTAssertNil(request5.authority) - #if canImport(CoreFoundation) + #if !canImport(FoundationEssentials) XCTAssertEqual(request5.path, "/") - #else // canImport(CoreFoundation) + #else // !canImport(FoundationEssentials) XCTAssertEqual(request5.path, ",Hello%2C%20World%21") - #endif // canImport(CoreFoundation) + #endif // !canImport(FoundationEssentials) XCTAssertNil(request5.url) }