Stop using Foundation on platforms with FoundationEssentials (#108)

Resolves #107
This commit is contained in:
Guoye Zhang authored and GitHub committed 2025-10-21 08:44:07 -07:00
1 parent 9410f6b9a3
commit 45eb022491
2 files changed
+11 -13

No files matched your search

+8 -10
View File
@@ -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)
}
}
+3 -3
View File
@@ -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)
}