From fa321222f0d48e2da910d3a624a1ba8a0feef085 Mon Sep 17 00:00:00 2001 From: Jeremy Schonfeld Date: Thu, 8 Aug 2024 09:17:25 -0700 Subject: [PATCH] [6.0] Implement Data reading hook for remote content --- Package.swift | 4 ++-- .../URLSession/NetworkingSpecific.swift | 10 ++++++++++ Tests/Foundation/TestURL.swift | 12 ++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 4ead01c3ee..6d6603880e 100644 --- a/Package.swift +++ b/Package.swift @@ -105,10 +105,10 @@ var dependencies: [Package.Dependency] { [ .package( url: "https://github.com/apple/swift-foundation-icu", - from: "0.0.9"), + branch: "release/6.0"), .package( url: "https://github.com/apple/swift-foundation", - revision: "d59046871c6b69a13595f18d334afa1553e0ba50") + branch: "release/6.0") ] } } diff --git a/Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift b/Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift index c8ae632a45..c07d751526 100644 --- a/Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift +++ b/Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift @@ -35,6 +35,16 @@ internal func NSRequiresConcreteImplementation(_ fn: String = #function, file: S fatalError("\(fn) must be overridden", file: file, line: line) } +extension Data { + @_dynamicReplacement(for: init(_contentsOfRemote:options:)) + private init(_contentsOfRemote_foundationNetworking url: URL, options: Data.ReadingOptions = []) throws { + let (nsData, _) = try _NSNonfileURLContentLoader().contentsOf(url: url) + self = withExtendedLifetime(nsData) { + return Data(bytes: nsData.bytes, count: nsData.length) + } + } +} + @usableFromInline class _NSNonfileURLContentLoader: _NSNonfileURLContentLoading, @unchecked Sendable { @usableFromInline diff --git a/Tests/Foundation/TestURL.swift b/Tests/Foundation/TestURL.swift index 9aff650183..c10d4963cd 100644 --- a/Tests/Foundation/TestURL.swift +++ b/Tests/Foundation/TestURL.swift @@ -775,6 +775,18 @@ class TestURL : XCTestCase { throw error } } + + func test_dataFromNonFileURL() { + do { + // Tests the up-call to FoundationNetworking to perform the network request + try Data(contentsOf: URL(string: "https://swift.org")!) + } catch { + if let cocoaCode = (error as? CocoaError)?.code { + // Just ensure that we supported this feature, even if the request failed + XCTAssertNotEqual(cocoaCode, .featureUnsupported) + } + } + } // MARK: -