pub struct Server { /* private fields */ }Expand description
A configured HTTP(S) server, ready to run.
Build it with Server::bind, attach a Handler (or
serve_dir), optionally enable TLS, then call one of
the run* methods for the runtime you compiled in.
Implementations§
Source§impl Server
impl Server
Sourcepub fn bind(addr: impl ToSocketAddrs) -> Result<Server>
pub fn bind(addr: impl ToSocketAddrs) -> Result<Server>
Resolve and remember the listen address(es). Does not bind yet.
Sourcepub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server
pub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server
Set the request handler from an existing Arc.
Sourcepub fn serve_dir(self, root: impl Into<PathBuf>) -> Server
pub fn serve_dir(self, root: impl Into<PathBuf>) -> Server
Serve static files from root (convenience for a StaticFiles handler).
Sourcepub fn workers(self, workers: usize) -> Server
pub fn workers(self, workers: usize) -> Server
Set the number of worker threads for the thread-pool runtime.
Sourcepub fn max_conns_per_ip(self, max: u32) -> Server
pub fn max_conns_per_ip(self, max: u32) -> Server
Cap the number of concurrent connections from a single client IP (0 = unlimited, the default). Applies to the TCP runtimes and the HTTP redirect listener; a single source that exceeds the cap has further connections dropped so it cannot exhaust the global connection budget.
Sourcepub fn limits(self, limits: Limits) -> Server
pub fn limits(self, limits: Limits) -> Server
Set the request size Limits (request line +
header block, and buffered request body) applied on every protocol and
runtime. Defaults to 64 KiB of headers and a 16 MiB body.
Sourcepub fn max_body_bytes(self, max: usize) -> Server
pub fn max_body_bytes(self, max: usize) -> Server
Set the maximum buffered request body size, in bytes (default 16 MiB).
A larger body is refused with 413 Content Too Large. Shorthand for
adjusting limits.
Sourcepub fn lenient_h1(self, on: bool) -> Server
pub fn lenient_h1(self, on: bool) -> Server
Parse HTTP/1 requests leniently (off by default): accept bare-LF line
endings and colon-less header lines, and expose each request’s head as
received via Request::raw_head. Meant for
emulating servers with looser parsers, such as PHP’s built-in server.
Don’t enable it behind a proxy — see
H1Conn::set_lenient.
Sourcepub fn server_name(self, name: Option<String>) -> Server
pub fn server_name(self, name: Option<String>) -> Server
Set the Server response header value (None to omit it).
Sourcepub fn tls(self, acceptor: TlsAcceptor) -> Server
pub fn tls(self, acceptor: TlsAcceptor) -> Server
Enable TLS with the given acceptor (turns the server into HTTPS). The
acceptor is fixed — its certificate cannot be reloaded at runtime; use
tls_reloadable for that.
Sourcepub fn tls_reloadable(self, acceptor: ReloadableAcceptor) -> Server
pub fn tls_reloadable(self, acceptor: ReloadableAcceptor) -> Server
Enable TLS with a ReloadableAcceptor, whose certificate files can be
re-read at runtime (e.g. on SIGHUP) via the ReloadHandle returned by
reload_handle.
Sourcepub fn compression(self, options: Options) -> Server
pub fn compression(self, options: Options) -> Server
Configure response compression.
Sourcepub fn hsts(self, value: Option<String>) -> Server
pub fn hsts(self, value: Option<String>) -> Server
Set the Strict-Transport-Security header value sent on secure
responses (e.g. "max-age=31536000"), or None to omit it. Never sent
over plain HTTP.
Sourcepub fn allow_http(self, allow: bool) -> Server
pub fn allow_http(self, allow: bool) -> Server
Serve content over plain HTTP instead of redirecting to HTTPS. Off by default — this server upgrades HTTP requests to HTTPS.
Sourcepub fn http_redirect(self, addr: impl ToSocketAddrs) -> Result<Server>
pub fn http_redirect(self, addr: impl ToSocketAddrs) -> Result<Server>
Also bind a plain-HTTP listener (e.g. port 80) that redirects to HTTPS
and serves ACME HTTP-01 challenges. Runs on its own thread under
run.
Sourcepub fn acme(self, manager: AcmeManager) -> Server
pub fn acme(self, manager: AcmeManager) -> Server
Sourcepub fn alt_svc(self, value: Option<String>) -> Server
pub fn alt_svc(self, value: Option<String>) -> Server
Set the Alt-Svc header value (e.g. advertising HTTP/3) sent on
responses, or None to omit it.
Sourcepub fn notify_bound(self, tx: Sender<()>) -> Server
pub fn notify_bound(self, tx: Sender<()>) -> Server
Register a notifier fired exactly once, after every listener this server
owns has been bound, just before it starts serving. This lets an external
coordinator wait until all privileged binds are done before dropping
privileges. The TCP runtime (run) signals once both the
main listener and any HTTP redirect listener are bound; the HTTP/3
runtime (run_h3) signals once its UDP socket is bound.
Sourcepub fn graceful(self, shutdown: Shutdown) -> Server
pub fn graceful(self, shutdown: Shutdown) -> Server
Install a Shutdown handle. When triggered, run* stops accepting,
drains in-flight connections (up to a grace period), and returns Ok(()).
Sourcepub fn reload_handle(&self) -> ReloadHandle
pub fn reload_handle(&self) -> ReloadHandle
Obtain a ReloadHandle for this server’s reloadable certificate
sources (its ACME manager and/or static ReloadableAcceptor). Call it
after configuring TLS/ACME; the handle can move into a signal watcher and
reload certificates without a restart. Works regardless of the cert kind,
so the caller need not know whether ACME or a static file backs the server.
Sourcepub fn run(self) -> Result<()>
pub fn run(self) -> Result<()>
Run on the blocking thread-pool runtime. Blocks the calling thread. If an HTTP redirect listener is configured, it runs on its own thread.
Sourcepub async fn run_tokio(self) -> Result<()>
pub async fn run_tokio(self) -> Result<()>
Run on a tokio runtime. Requires being called from within a tokio
runtime context (e.g. under #[tokio::main]).
Sourcepub fn run_mio(self) -> Result<()>
pub fn run_mio(self) -> Result<()>
Run on a single-thread mio readiness event loop. Blocks the calling thread.
Sourcepub fn run_h3(self) -> Result<()>
pub fn run_h3(self) -> Result<()>
Run an HTTP/3 server on a QUIC/UDP event loop, listening on the same address(es) as the TCP server (but over UDP). HTTP/3 is always encrypted.
Under ACME, certificates are selected per-connection by peeking the SNI
from the client’s QUIC Initial datagrams (a ClientHello with a
post-quantum key share spans two, so the first is held until the second
arrives); the QUIC loop serves already-issued certs (the TCP path does
the issuing). With a
static tls acceptor, that one certificate is used.
Blocks the calling thread.