[][src]Module hyper::server::conn

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)
                    .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

AddrIncoming

A stream of connections from binding to an address.

AddrStream

A transport returned yieled by AddrIncoming.

Connecting

A future building a new Service to a Connection.

Connection

A future binding a connection with a Service.

Http

A lower-level configuration of the HTTP protocol.

Parts

Deconstructed parts of a Connection.