Skip to content

Implement hook for data reading from remote URL #5049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ var dependencies: [Package.Dependency] {
[
.package(
url: "https://github.com/apple/swift-foundation-icu",
from: "0.0.9"),
branch: "main"),
.package(
url: "https://github.com/apple/swift-foundation",
revision: "acae3d26b69113cec2db7772b4144ab9558241db")
branch: "main")
]
}
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/FoundationNetworking/URLSession/NetworkingSpecific.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Tests/Foundation/TestURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: -

Expand Down