Skip to content
CryoCryo home
Stdlibnet::http2

server

import std::net::http2::server; · source

Types

Http2Server

type struct Http2Server {
    listener: TcpListener;

    static bind(addr: SocketAddr) -> Result<Http2Server, IoError>;
    local_addr(&this) -> Result<SocketAddr, IoError>;
    async serve_one(mut &this, handler: (Request) -> Response) -> Result<(), IoError>;
    async run(mut &this, handler: (Request) -> Response) -> Result<(), IoError>;
    async accept_one(mut &this) -> Result<TcpStream, IoError>;
}

A single-connection-at-a-time h2c server. bind(addr) opens the listener; serve_one(handler) accepts one connection and serves it until the peer closes or sends GOAWAY; run(handler) does that in a loop. The handler is the same (Request) -> Response function pointer the HTTP/1.1 server takes, so a service can offer both protocols from one set of handlers.

serve is the building block: it takes ownership of an already-connected BufStream<S>, performs the server handshake, and dispatches each request on the connection to handler. Over a TlsStream whose ALPN negotiated h2, that is how you serve HTTPS/2 — accept with a TlsAcceptor built with_alpn_h2, check negotiated_alpn(), and route to serve or to the HTTP/1.1 serve_connection accordingly.

Http2Server closes its listener through an inherent drop.

Properties

PropertyType
listenerTcpListener

Constructors

static bind(addr: SocketAddr) -> Result<Http2Server, IoError>;

Bind + listen on addr for h2c connections. The listener is put in non-blocking mode here: TcpAccept parks on readiness, and a blocking listener would stall an executor's whole worker thread inside accept.

Methods

local_addr(&this) -> Result<SocketAddr, IoError>;

The local address the listener is bound to. After bind(.., 0) this reports the kernel-assigned ephemeral port.

async serve_one(mut &this, handler: (Request) -> Response) -> Result<(), IoError>;

Accept one connection and serve it to completion, then return.

async run(mut &this, handler: (Request) -> Response) -> Result<(), IoError>;

Accept and serve connections forever, one at a time. A failure on a single connection is swallowed so one client can't stop the server; only an accept failure ends the loop.

async accept_one(mut &this) -> Result<TcpStream, IoError>;

Take the listener out of this, accept one connection on it, and put it back. The swap is what lets the accept own the listener for the duration without this server holding a second address for it across the suspension.

Free Functions

async function serve<S>(conn: BufStream<S>, handler: (Request) -> Response) -> Result<(), IoError> where S: AsyncTransport;

Serve HTTP/2 over conn, dispatching each request to handler until the peer closes or sends GOAWAY. Takes ownership of the connection.

Http2Server::serve_one and run are built on it.