pub struct Http1<Io> { /* private fields */ }h1 only.Expand description
An HTTP/1.x connection handler.
Http1 wraps an async I/O stream (Io) and provides a complete
HTTP/1.0 and HTTP/1.1 server implementation, including:
- Request head parsing (via
httparse) - Streaming request bodies (content-length and chunked transfer-encoding)
- Chunked response encoding and trailer support
100 Continueand103 Early Hintsinterim responses- HTTP connection upgrades (e.g. WebSocket)
- Optional zero-copy response sending on Linux (see
Http1::zerocopy) - Keep-alive connection reuse
- Graceful shutdown via a
CancellationToken
§Construction
let http1 = Http1::new(tcp_stream, Http1Options::default());§Serving requests
Use the HttpProtocol trait methods (handle /
handle_with_error_fn) to drive the
connection to completion:
http1.handle(|req| async move {
Ok::<_, Infallible>(Response::new(Full::new(Bytes::from("Hello!"))))
}).await?;Implementations§
Source§impl<Io> Http1<Io>
impl<Io> Http1<Io>
Sourcepub fn zerocopy(self) -> Http1Zerocopy<Io>
Available on Linux and crate feature h1-zerocopy only.
pub fn zerocopy(self) -> Http1Zerocopy<Io>
h1-zerocopy only.Converts this Http1 into an Http1Zerocopy that uses emulated
sendfile (Linux only) to send response bodies without copying data
through user space.
The response body must have a ZerocopyResponse extension installed
(via install_zerocopy) containing the file descriptor to send from.
Responses without that extension are sent normally.
Only available on Linux (target_os = "linux"), and only when Io
implements vibeio::io::AsInnerRawHandle.
Source§impl<Io> Http1<Io>
impl<Io> Http1<Io>
Sourcepub fn new(io: Io, options: Http1Options) -> Self
pub fn new(io: Io, options: Http1Options) -> Self
Creates a new Http1 connection handler wrapping the given I/O stream.
The options value controls limits, timeouts, and optional features;
see Http1Options for details.
§Example
let http1 = Http1::new(tcp_stream, Http1Options::default());Sourcepub fn graceful_shutdown_token(self, token: CancellationToken) -> Self
pub fn graceful_shutdown_token(self, token: CancellationToken) -> Self
Attaches a CancellationToken for graceful shutdown.
After the current in-flight request has been fully handled and its response written, the connection loop checks whether the token has been cancelled. If it has, the loop exits cleanly instead of waiting for the next request.
This allows the server to drain active connections without abruptly closing them mid-response.