Skip to content

Expose the negotiated HTTP version to request handlers - #125

Open
dioKaratzas wants to merge 2 commits into
swift-server:mainfrom
dioKaratzas:expose-negotiated-http-version
Open

dioKaratzas wants to merge 2 commits into
swift-server:mainfrom
dioKaratzas:expose-negotiated-http-version

Conversation

@dioKaratzas

Copy link
Copy Markdown
Contributor

Resolves #113.

Adds HTTPServerCapability.NegotiatedHTTPVersion, following the existing ConnectionInfo / PeerCertificate pattern, and conforms NIOHTTPServer.RequestContext to it by forwarding to the connection context. ConnectionContext stays internal to the handler surface. Purely additive.

Test plan: asserted the negotiated version from the request handler in the existing HTTP/1.1 and HTTP/2 end-to-end tests.

@gjcairo gjcairo added the 🆕 semver/minor Adds new public API. label Sep 7, 2026
@gjcairo

gjcairo commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Thanks @dioKaratzas !

@gjcairo
gjcairo enabled auto-merge (squash) September 7, 2026 22:22
/// Servers whose request context conforms to this capability surface the
/// application-level HTTP version negotiated for the connection carrying
/// the request.
public protocol NegotiatedHTTPVersion: RequestContext {

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.

I think Negotiated is potentially wrong here. If the server runs over bare TCP without TLS then nothing is negotiated necessarily. What about just HTTPVersion?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, renamed to HTTPServerCapability.HTTPVersion and reworded the docs so they no longer imply negotiation (041371f). Also rebased on latest main.

dioKaratzas and others added 2 commits October 14, 2026 19:26
Motivation:

`NIOHTTPServer.ConnectionContext` carries the application protocol
negotiated for a connection as a `public let httpVersion`, but request
handlers only ever receive a `RequestContext`, whose `connectionContext`
is internal. `RequestContext` already surfaces the peer / local
addresses and the validated peer certificate chain, but there was no
capability for the negotiated version, so a handler had no way to tell
which protocol it was serving.

Modifications:

- Added `HTTPServerCapability.NegotiatedHTTPVersion`, a request-context
  capability exposing `httpVersion`, following the shape of the existing
  `ConnectionInfo` and `PeerCertificate` capabilities.
- Conformed `NIOHTTPServer.RequestContext` to it, forwarding to the
  connection context, and updated the capability lists in the
  `RequestContext` and `ConnectionContext` documentation.
- Added a test asserting the version reported by the request context for
  every supported protocol: plaintext HTTP/1.1, HTTP/1.1 over TLS,
  HTTP/2 and HTTP/3.

Result:

Request handlers can read the negotiated HTTP version, and generic
library code can constrain on the capability without depending on
`NIOHTTPServer` directly. `ConnectionContext` stays internal to the
handler surface, and the change is purely additive.
Motivation:

The capability was named `NegotiatedHTTPVersion`, but nothing is
necessarily negotiated: a server running over bare TCP without TLS
serves plaintext HTTP/1.1 without any ALPN exchange.

Modifications:

- Renamed `HTTPServerCapability.NegotiatedHTTPVersion` to
  `HTTPServerCapability.HTTPVersion`.
- Reworded the capability, conformance and test descriptions to refer
  to the HTTP version in use on the connection rather than the
  negotiated one.

Result:

The capability name and documentation describe the HTTP version
accurately for both TLS and plaintext connections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
auto-merge was automatically disabled October 14, 2026 16:27

Head branch was pushed to by a user without write access

@dioKaratzas
dioKaratzas force-pushed the expose-negotiated-http-version branch from add9ac9 to 041371f Compare October 14, 2026 16:27
@dioKaratzas
dioKaratzas requested a review from aryan-25 as a code owner October 14, 2026 16:27
@dioKaratzas

Copy link
Copy Markdown
Contributor Author

@FranzBusch @gjcairo rebased on latest main and addressed the naming feedback (NegotiatedHTTPVersion → HTTPVersion). Could you take another look when you get a chance? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose the negotiated HTTP version to request handlers

3 participants