⚡ Sangsakala Proxy 🏠 Home
Skip to main content

Server

Struct Server 

Source
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

Source

pub fn bind(addr: impl ToSocketAddrs) -> Result<Server>

Resolve and remember the listen address(es). Does not bind yet.

Source

pub fn handler<H: Handler + 'static>(self, handler: H) -> Server

Set the request handler.

Source

pub fn handler_arc(self, handler: Arc<dyn Handler>) -> Server

Set the request handler from an existing Arc.

Source

pub fn serve_dir(self, root: impl Into<PathBuf>) -> Server

Serve static files from root (convenience for a StaticFiles handler).

Source

pub fn workers(self, workers: usize) -> Server

Set the number of worker threads for the thread-pool runtime.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn server_name(self, name: Option<String>) -> Server

Set the Server response header value (None to omit it).

Source

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.

Source

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.

Source

pub fn compression(self, options: Options) -> Server

Configure response compression.

Source

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.

Source

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.

Source

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.

Source

pub fn acme(self, manager: AcmeManager) -> Server

Enable automatic certificates via ACME, routed per-connection by SNI. Takes precedence over a static tls acceptor. Currently served by the thread-pool runtime (run).

Source

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.

Source

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.

Source

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

Source

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.

Source

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.

Source

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

Source

pub fn run_mio(self) -> Result<()>

Run on a single-thread mio readiness event loop. Blocks the calling thread.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.