Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,19 @@ final class URLSessionRequestStreamBridge: NSObject, StreamDelegate, Sendable {
}
}

func close() {
func close(trailerFields: HTTPFields?) {
self.lockedState.withLock { state in
if let trailerFields {
var trailerNames: Set<HTTPField.Name> = []
for field in trailerFields {
trailerNames.insert(field.name)
}
var trailerDictionary: [String: String] = [:]
for name in trailerNames {
trailerDictionary[name.rawName] = trailerFields[name]
}
state.inputStream.setProperty(trailerDictionary, forKey: .init("_kCFStreamPropertyHTTPTrailer"))
}
state.outputStream.close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension URLSessionTaskDelegateBridge: ConcludingAsyncReader {
func consumeAndConclude<Return, Failure: Error>(
body: (consuming sending URLSessionTaskDelegateBridge) async throws(Failure) -> Return
) async throws(Failure) -> (Return, HTTPFields?) {
try await (body(self), nil)
try await (body(self), self.responseTrailerFields)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the reasons I think early returning before completing the read should throw is that we don't know whether nil means a trailer isn't present or it hasn't been received.

}
}

Expand Down
24 changes: 20 additions & 4 deletions Sources/HTTPClient/URLSession/URLSessionTaskDelegateBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@ final class URLSessionTaskDelegateBridge: NSObject, Sendable, URLSessionDataDele
}
var state: State = .awaitingResponse
var completionContinuation: CheckedContinuation<Void, Never>? = nil
var responseTrailerFields: HTTPFields?
}

private let state: Mutex<TaskState> = .init(.init())

var responseTrailerFields: HTTPFields? {
self.state.withLock { $0.responseTrailerFields }
}

func urlSession(
_ session: URLSession,
dataTask: URLSessionDataTask,
Expand Down Expand Up @@ -153,6 +158,15 @@ final class URLSessionTaskDelegateBridge: NSObject, Sendable, URLSessionDataDele
state.state = .awaitingConsumption(existingData, complete: true, error: error, suspendedTask: nil)
}
state.completionContinuation = nil
if let trailerDictionary = task.value(forKey: "_trailers") as? [String: String] {
var trailerFields = HTTPFields()
for (name, value) in trailerDictionary {
if let name = HTTPField.Name(name) {
trailerFields.append(.init(name: name, value: value))
}
}
state.responseTrailerFields = trailerFields
}
}
return state
}
Expand Down Expand Up @@ -247,15 +261,16 @@ final class URLSessionTaskDelegateBridge: NSObject, Sendable, URLSessionDataDele
let bridge = URLSessionRequestStreamBridge(task: task)
completionHandler(bridge.inputStream)
do {
_ = try await requestBody.produce(into: bridge)
let trailerFields = try await requestBody.produce(into: bridge)
bridge.close(trailerFields: trailerFields)
} catch {
if bridge.writeFailed {
// Ignore error
} else {
self.requestBodyStreamFailed(with: error)
}
bridge.close(trailerFields: nil)
}
bridge.close()
}
}
}
Expand All @@ -277,15 +292,16 @@ final class URLSessionTaskDelegateBridge: NSObject, Sendable, URLSessionDataDele
let bridge = URLSessionRequestStreamBridge(task: task)
completionHandler(bridge.inputStream)
do {
_ = try await requestBody.produce(offset: offset, into: bridge)
let trailerFields = try await requestBody.produce(offset: offset, into: bridge)
bridge.close(trailerFields: trailerFields)
} catch {
if bridge.writeFailed {
// Ignore error
} else {
self.requestBodyStreamFailed(with: error)
}
bridge.close(trailerFields: nil)
}
bridge.close()
}
}
}
Expand Down
Loading