Available on crate feature
server
only.Expand description
Lower-level Server connection API.
The types in this module are to provide a lower-level API based around a single connection. Accepting a connection and binding it with a service are not handled at this level. This module provides the building blocks to customize those things externally.
If you don’t have need to manage connections yourself, consider using the higher-level Server API.
§Example
A simple example that uses the Http
struct to talk HTTP over a Tokio TCP stream
use http::{Request, Response, StatusCode};
use hyper::{server::conn::Http, service::service_fn, Body};
use std::{net::SocketAddr, convert::Infallible};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let addr: SocketAddr = ([127, 0, 0, 1], 8080).into();
let mut tcp_listener = TcpListener::bind(addr).await?;
loop {
let (tcp_stream, _) = tcp_listener.accept().await?;
tokio::task::spawn(async move {
if let Err(http_err) = Http::new()
.http1_only(true)
.http1_keep_alive(true)
.serve_connection(tcp_stream, service_fn(hello))
.await {
eprintln!("Error while serving HTTP connection: {}", http_err);
}
});
}
}
async fn hello(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello World!")))
}
Structs§
- A stream of connections from binding to an address.
- A transport returned yieled by
AddrIncoming
. - Connecting
http1
orhttp2
A future building a newService
to aConnection
. - Connection
http1
orhttp2
A future binding a connection with a Service. - Http
http1
orhttp2
A lower-level configuration of the HTTP protocol. - Parts
http1
orhttp2
Deconstructed parts of aConnection
.