Skip to content
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
13 changes: 12 additions & 1 deletion Sources/URLSessionHTTPClient/URLSessionRequestStreamBridge.swift
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
Copy Markdown
Collaborator 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd like to understand why the distinction matters.

What is the difference in corrective action that a user will take if:

  • the trailer is missing cause the server didn't provide it
  • the client under-read and didn't process trailers as a result

My point is, either way, the outcome is that they didn't receive trailers and there isn't much corrective action because the request is over either way.

I could be wrong though, so an example would help me understand why.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Suppose that a checksum is sometimes sent in the trailer for data integrity. With the current API behavior, collect silently truncates and gives nil trailer, so all checks are skipped. It’s really easy to introduce data truncation issues with the current set of APIs.

}
}

Expand Down
24 changes: 20 additions & 4 deletions Sources/URLSessionHTTPClient/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] {
Comment thread
xbhatnag marked this conversation as resolved.
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
3 changes: 1 addition & 2 deletions Tests/HTTPClientTests/DarwinHTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ struct DarwinHTTPClientTests {
// TODO: Writing just an empty span causes an indefinite stall. The terminating chunk (size 0) is not written out on the wire.
.testEmptyChunkedBody,

// TODO: Trailers are not supported by URLSession
.testTrailerRead,
// TODO: Trailer writes are not supported by URLSession
.testTrailerWrite,
]) {
return DefaultHTTPClient.shared
Expand Down
Loading