packages

http::server

On this page

The server: an accept loop, a task per connection, HTTP/1.1 with keep-alive and pipelining, HTTP/2 over TLS (ALPN) or by prior knowledge (h2c), graceful shutdown.

Structs

ServerConfigSource

pub struct ServerConfig

Server settings. Every field has a default.

  • pub limits: Limits = Limits {}§

    Source
  • pub header_timeout: Duration | none = Duration::secs(30)§

    Source

    Time to receive a request head once its first byte arrived.

  • pub idle_timeout: Duration | none = Duration::secs(60)§

    Source

    Time a keep-alive connection may wait for its next request.

  • pub io_timeout: Duration | none = Duration::secs(60)§

    Source

    Longest wait for one read of a request body / one write.

  • pub max_body: u64 | none = none§

    Source

    Larger request bodies fail with TooLarge (the connection then closes).

  • pub http2: bool = cfg!(feature = "http2")§

    Source

    Speak HTTP/2: offered as "h2" over TLS, by prior knowledge on plaintext connections.

  • pub h2: h2::Config = h2::Config {}§

    Source

    HTTP/2 settings.

ServerSource

pub struct Server

A bound server. Copies share it.

  • pub fn bind(addr: str, handler: Handler, cfg: ServerConfig | none = none) throws (HttpError) -> Server§

    Source
  • pub fn bind_tls(addr: str, handler: Handler, tls_cfg: tls::ServerConfig, cfg: ServerConfig | none = none) throws (HttpError) -> Server§

    Source
  • pub fn local_addr(self) throws (HttpError) -> SocketAddr§

    Source
  • pub async fn run(self) throws (HttpError)§

    Source

    Accept connections until shutdown. Runs once per server.

  • pub async fn shutdown(self, grace: Duration | none = Duration::secs(30))§

    Source

    Stop accepting, close idle connections, tell HTTP/2 connections to finish (GOAWAY), and wait for in-flight requests; after grace the remaining connections are cut.

Type aliases

HandlerSource

pub type Handler = fn(Request) -> AsyncFuture<Response> + Send + Sync

The request handler: one call per request, each in its own task.

Functions

serveSource

pub async fn serve(addr: str, handler: Handler) throws (HttpError)

Serve handler on addr ("127.0.0.1:8080") until the process ends.

serve_tlsSource

pub async fn serve_tls(addr: str, tls_cfg: tls::ServerConfig, handler: Handler) throws (HttpError)