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 @@ -31,6 +31,16 @@ extension HTTPServerCapability {
var localAddress: NIOHTTPServer.SocketAddress? { get }
}

/// A request-context capability exposing the application-level HTTP version.
///
/// Servers whose request context conforms to this capability surface the
/// HTTP version in use on the connection carrying the request, whether it
/// was negotiated via ALPN or fixed by a plaintext listener.
public protocol HTTPVersion: RequestContext {
/// The application-level HTTP version in use on the connection.
var httpVersion: NIOHTTPServer.HTTPVersion { get }
}

/// A request-context capability exposing the validated peer certificate chain.
///
/// Servers whose request context conforms to this capability surface the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extension NIOHTTPServer {
///
/// User code accesses this state via the corresponding ``RequestContext``
/// capabilities (``HTTPServerCapability/ConnectionInfo``,
/// ``HTTPServerCapability/HTTPVersion``,
/// ``HTTPServerCapability/PeerCertificate``) when handling individual
/// requests, and directly when implementing an
/// ``NIOHTTPServerConnectionHandler``.
Expand Down
10 changes: 10 additions & 0 deletions Sources/NIOHTTPServer/NIOHTTPServer+RequestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extension NIOHTTPServer {
///
/// Conforms to:
/// - ``HTTPServerCapability/ConnectionInfo`` — peer / local addresses.
/// - ``HTTPServerCapability/HTTPVersion`` — HTTP version of the connection.
/// - ``HTTPServerCapability/PeerCertificate`` — mTLS-validated peer chain.
///
/// Generic library code can constrain on these capabilities to access
Expand Down Expand Up @@ -60,6 +61,15 @@ extension NIOHTTPServer.RequestContext: HTTPServerCapability.ConnectionInfo {
}
}

@available(anyAppleOS 27.0, *)
extension NIOHTTPServer.RequestContext: HTTPServerCapability.HTTPVersion {
/// The application-level HTTP version in use on the connection carrying
/// this request.
public var httpVersion: NIOHTTPServer.HTTPVersion {
self.connectionContext.httpVersion
}
}

@available(anyAppleOS 27.0, *)
extension NIOHTTPServer.RequestContext: HTTPServerCapability.PeerCertificate {
/// The peer's mTLS-validated certificate chain, when available.
Expand Down
37 changes: 37 additions & 0 deletions Tests/NIOHTTPServerTests/NIOHTTPServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ struct NIOHTTPServerTests {
}
}

@available(anyAppleOS 27.0, *)
#if HTTP3
@Test(
"Request context reports the HTTP version",
arguments: [NIOHTTPServer.HTTPVersion.plaintextHTTP1_1, .http1_1, .http2, .http3]
)
#else
@Test(
"Request context reports the HTTP version",
arguments: [NIOHTTPServer.HTTPVersion.plaintextHTTP1_1, .http1_1, .http2]
)
#endif
func testRequestContextHTTPVersion(httpVersion: NIOHTTPServer.HTTPVersion) async throws {
let (server, clientConfiguration) = try TestHelpers.makeServerAndClientConfiguration(
for: httpVersion,
clientLogger: self.clientLogger,
serverLogger: self.serverLogger
)

try await TestHelpers.withClientServerRequestChannel(
clientConfiguration: clientConfiguration,
server: server,
serverHandler: HTTPServerClosureRequestHandler { request, requestContext, reader, responseWriter in
#expect(requestContext.httpVersion == httpVersion)
try await responseWriter.sendAndFinish(.init(status: .ok))
}
) { _, inbound, outbound in
try await outbound.write(.testHead(method: .get, for: httpVersion))
try await outbound.write(.end(nil))

var iterator = inbound.makeAsyncIterator()
while let part = try await iterator.next() {
if case .end = part { break }
}
}
}

@available(anyAppleOS 27.0, *)
@Test(
"mTLS request-response with custom verification callback returning peer certificates",
Expand Down