Skip to main content

serve_connection

Function serve_connection 

Source
pub async fn serve_connection<I, S, B>(io: I, service: S, http2_enabled: bool)
where I: Read + Write + Unpin + Send + 'static, S: Service<Request<Incoming>, Response = Response<B>> + Send + 'static, S::Future: Send + 'static, S::Error: Into<Box<dyn Error + Send + Sync>>, B: Body + Send + 'static, B::Data: Send, B::Error: Into<Box<dyn Error + Send + Sync>>,
Expand description

Serves a single (already-handshaked, TokioIo-wrapped) connection.

When http2_enabled, the hyper-util auto builder sniffs the first bytes and dispatches to HTTP/1.1 or HTTP/2 — covering ALPN-negotiated h2 over TLS, h2c prior-knowledge over plaintext, and HTTP/1.1, in one call. Otherwise it serves HTTP/1.1 only. Connection-level errors are logged, not propagated, so one bad connection never affects the accept loop.

Both paths enable connection upgrades (with_upgrades / serve_connection_with_upgrades) so a handler that returns 101 Switching Protocols (HTTP/1.1) or 200 (HTTP/2 extended CONNECT) can hand the raw stream to a WebSocket relay (see crate::server::websocket). The h2 arm also enables the RFC 8441 extended CONNECT protocol (SETTINGS_ENABLE_CONNECT_PROTOCOL) so clients can open WebSockets over HTTP/2; h1/h2 auto-detection is preserved.

The listeners drive connections via build_connection + a graceful-shutdown watcher; this await-and-log form is kept for tests and simple embedding.