Skip to content

Use new final element support from AsyncStreaming#170

Open
FranzBusch wants to merge 11 commits into
mainfrom
fb-final-element
Open

Use new final element support from AsyncStreaming#170
FranzBusch wants to merge 11 commits into
mainfrom
fb-final-element

Conversation

@FranzBusch
Copy link
Copy Markdown
Member

Motivation

AsyncStreaming added support for final elements which will allow us to remove our concluding reader and writer protocols.

Modifications

This PR moves over to the final elements from AsyncStreaming and introduces a new HTTPResponseSender protocol. With both of them together the code becomes significantly easier and we support fusing the final buffer and the fin flag on the wire. Furthermore, the HTTPResponseSender makes it possible to flush head, body and trailers all in one go.

## Result

No more concluding reader and writer APIs and instead only async streaming and HTTP specific types.

## Motivation

`AsyncStreaming` added support for final elements, which lets us drop our concluding reader and writer protocols and use `AsyncReader` and `CallerAsyncWriter` directly with `FinalElement == HTTPFields?` to carry the HTTP trailers inline.

## Modifications

This PR moves the HTTP body shape over to `AsyncReader` and `CallerAsyncWriter` from `AsyncStreaming` and introduces a new `HTTPResponseSender` protocol. With both together the code becomes significantly easier, we get to fuse the final buffer and the fin flag on the wire, and `HTTPResponseSender` makes it possible to flush head, body and trailers all in one go. All four client and server conformers (URLSession, AsyncHTTPClient, Fetch, NIO) and the example middlewares are updated to the new shape, and the in-process test harness uses upstream `DuplexAsyncChannel` for both directions.

## Result

No more concluding reader and writer APIs and instead only async streaming and HTTP specific types.
Comment thread Package.swift Outdated
Comment on lines +44 to +45
url: "https://github.com/apple/swift-async-algorithms.git",
from: "1.1.4",
url: "https://github.com/FranzBusch/swift-async-algorithms.git",
revision: "e1a6dff375ca9fc079d02276f489663ad9204e01",
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We need to wait for a release with this PR apple/swift-async-algorithms#425

@FranzBusch FranzBusch added the ⚠️ semver/major Breaks existing public API. label Jun 2, 2026
Comment thread Sources/HTTPAPIs/Server/HTTPResponseSender.swift Outdated
Comment thread Sources/HTTPAPIs/Server/HTTPResponseSender.swift Outdated
Comment thread Sources/HTTPClient/DefaultHTTPClient.swift
@FranzBusch FranzBusch requested a review from guoye-zhang June 2, 2026 19:25
///
/// Convenience for HTTP body readers (`FinalElement == HTTPFields?`).
/// The buffer's initial free capacity acts as a hard cap; surplus bytes
/// are read and discarded.
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.

Let's revisit the behavior of collect later. I think it should throw or at least make people aware that the buffer contains truncated data.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. There is a TODO here to move this over to async algos. We should revisit the behavior then

done = true
}
if chunk.count == 0 {
if finalElement == nil {
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.

What does this check do? Why is done true when the chunk is empty and there is no final element?

Also double optional is a bit trippy. Does this check pass just .none or .some(.none) as well?

@guoye-zhang
Copy link
Copy Markdown
Collaborator

Thanks for doing this! Great that it's a net deletion of lines

Comment thread Sources/HTTPAPIs/Server/HTTPResponseSender.swift Outdated
/// Convenience for HTTP body readers (`FinalElement == HTTPFields?`).
/// The buffer's initial free capacity acts as a hard cap; surplus bytes
/// are read and discarded.
// TODO: This should be moved to the AsyncStreaming module
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some reason why it's not there now?

Comment thread Tests/HTTPAPIsTests/ServerCapabilityTests.swift Outdated
Comment on lines +115 to +116
// TODO: This is a temporary fix that informs clients that this server does not support
// keep-alive. This server should be updated to eventually support keep-alive.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This has been fixed upstream. I don't know if you care about it though, or if someone should focus on merging #137

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.

We need to land this prior to fixing #137 since it requires the server repo to use the same updated abstract APIs

Comment thread Sources/HTTPClientConformance/HTTPServerForTesting/TestHTTPServer.swift Outdated
FranzBusch pushed a commit to swift-server/async-http-client that referenced this pull request Jun 6, 2026
This unblocks apple/swift-http-api-proposal#170

---------

Co-authored-by: Fabian Fett <fabianfett@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Mads Odgaard <mads.odgaard@frameo.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Si Beaumont <simonjbeaumont@gmail.com>
Co-authored-by: hamzahrmalik <hamzah_malik@apple.com>
Co-authored-by: PAVANSAI KUMAR ALLADI <ptpavansai3@gmail.com>
Co-authored-by: Pavan sai kumar alladi <p_alladi@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚠️ semver/major Breaks existing public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants