Module
whiplash
src/whiplash.pookaWhiplash: an HTTP server that speaks every version a browser does.
One port answers HTTP/1.1 and HTTP/2 (h2c, by prior knowledge). With a certificate it answers HTTP/1.1 and HTTP/2 over TLS, chosen by ALPN, and HTTP/3 over QUIC on the same port number. WebSockets ride on all three.
import pooka/http
import pooka/otp
import whiplash
handle(request: http.Request) -> http.Response {
match request.segments() {
[] -> http.text(200, "Hello, Joe!")
["birds", name] -> http.text(200, "Hello, " + name)
_ -> http.text(404, "Nobody here")
}
}
main() {
let assert Ok(_) = whiplash.start(whiplash.options(8080), handle)
otp.sleep_forever()
}
The protocols live on the Pooka side of the socket, in sans-IO modules
(whiplash/http1, whiplash/http2, whiplash/http3 and their
friends). OTP supervises the listener, the acceptors and a temporary
process per connection, so a crash takes down one conversation, never
the server. HTTP/2 and HTTP/3 handlers run in bounded linked workers:
one slow stream cannot hold up its neighbours.
API reference
class Options(
port: Int,
acceptors: Int,
max_connections: Int,
max_body: Int,
request_timeout: Int
)
How a server listens and what it will put up with: the port (0 picks a free one), how many acceptors wait for connections, how many connections are admitted at once, the largest request body in bytes, and the milliseconds a request may take or a connection may sit idle. The deadline covers the whole request; it is not extended byte by byte.
options(port) fills in sensible limits; build Options yourself when
you need others.
let roomy = whiplash.Options(8080, 8, 4096, 67108864, 60000)
port: Int
Intacceptors: Int
Intmax_connections: Int
Intmax_body: Int
Intrequest_timeout: Int
Int
fn(Int, Int, Int, Int, Int) -> whiplash.Optionsaddress() -> String
fn() -> StringThe interface the server listens on. Loopback unless changed with
with_address, so nothing is reachable from other machines by accident.
with_address(address: String) -> Options
fn(String) -> whiplash.OptionsListen on another interface: an IPv4 or IPv6 address, "0.0.0.0" for
every IPv4 interface or "::" for every interface. TLS and HTTP/3 use
the same address.
let settings = whiplash.options(8080).with_address("0.0.0.0")
retry() -> Bool
fn() -> BoolWhether HTTP/3 clients must prove their address with a Retry before the
server does any handshake work for them (RFC 9000 section 8.1.2). Off
unless changed with with_retry.
with_retry() -> Options
fn() -> whiplash.OptionsAnswer every new HTTP/3 client with a Retry first. It costs each new connection one round trip, and spares the server the work of a handshake for a datagram with a forged source address: worth it for a server exposed to floods.
let settings = whiplash.options(8443).with_address("::").with_retry()
class Tls(certificate: String, key: String)
Certificate and private key paths for an OTP TLS listener.
certificate: String
Stringkey: String
String
fn(String, String) -> whiplash.Tlsclass Application(
handle: fn(http.Request) -> http.Response,
websocket: fn(http.Request) -> Option(upgrade.Route)
)
Application routes stay typed: ordinary requests return HTTP responses; WebSocket upgrades select an optional conversation handler.
handle: fn(http.Request) -> http.Response
fn($package/pooka_http/pooka/http.Request) -> $package/pooka_http/pooka/http.Responsewebsocket: fn(http.Request) -> Option(upgrade.Route)
fn($package/pooka_http/pooka/http.Request) -> Option(whiplash/websocket/route.Route)
fn(fn($package/pooka_http/pooka/http.Request) -> $package/pooka_http/pooka/http.Response, fn($package/pooka_http/pooka/http.Request) -> Option(whiplash/websocket/route.Route)) -> whiplash.Applicationstream(request: http.Request) -> Option(body.Stream)
fn($package/pooka_http/pooka/http.Request) -> Option(whiplash/body.Stream)upload(request: http.Request) -> Option(
fn(http.Request, body.Reader) -> http.Response
)
fn($package/pooka_http/pooka/http.Request) -> Option(fn($package/pooka_http/pooka/http.Request, whiplash/body.Reader) -> $package/pooka_http/pooka/http.Response)has_uploads() -> Bool
fn() -> BoolWhether any upload routes exist; without them, no request is examined before its body has arrived.
with_streaming(routes: fn(http.Request) -> Option(body.Stream)) -> Application
fn(fn($package/pooka_http/pooka/http.Request) -> Option(whiplash/body.Stream)) -> whiplash.ApplicationRoutes whose responses are produced a piece at a time.
with_uploads(
routes: fn(http.Request) -> Option(
fn(http.Request, body.Reader) -> http.Response
)
) -> Application
fn(fn($package/pooka_http/pooka/http.Request) -> Option(fn($package/pooka_http/pooka/http.Request, whiplash/body.Reader) -> $package/pooka_http/pooka/http.Response)) -> whiplash.ApplicationRoutes that read their request body as it arrives. The handler gets the request with an empty body, and a Reader for the body itself.
class Running(port: Int, tree: supervisor.Supervisor)
A server that is up: the port it listens on (the real one, when the
options asked for port 0) and the supervision tree holding it. Hand it to
stop when you are done.
port: Int
Inttree: supervisor.Supervisor
$package/pooka_otp/pooka/otp/supervisor.Supervisor
fn(Int, $package/pooka_otp/pooka/otp/supervisor.Supervisor) -> whiplash.Runningoptions(port: Int) -> Options
fn(Int) -> whiplash.OptionsOptions with everyday limits: 8 acceptors, 1024 connections at once,
request bodies up to 1 MiB, and 30 seconds for each request or idle wait.
The server listens on loopback; see Options.with_address.
start(settings: Options, handle: fn(http.Request) -> http.Response) -> Result(
Running,
String
)
fn(whiplash.Options, fn($package/pooka_http/pooka/http.Request) -> $package/pooka_http/pooka/http.Response) -> Result(whiplash.Running, String)Start serving HTTP/1.1 and HTTP/2 (h2c) with one handler for every
request. The handler returns a whole response; Whiplash writes the
framing fields (content-length, transfer-encoding, connection),
applies the rules for HEAD, 204 and 304, and adds a date.
let assert Ok(site) = whiplash.start(whiplash.options(8080), fn(_) { http.text(200, "Hello") })
whiplash.stop(site)
start_application(settings: Options, application: Application) -> Result(
Running,
String
)
fn(whiplash.Options, whiplash.Application) -> Result(whiplash.Running, String)Start an application: a handler plus WebSocket, streaming and upload routes.
start_tls(
settings: Options,
tls: Tls,
handle: fn(http.Request) -> http.Response
) -> Result(Running, String)
fn(whiplash.Options, whiplash.Tls, fn($package/pooka_http/pooka/http.Request) -> $package/pooka_http/pooka/http.Response) -> Result(whiplash.Running, String)Serve HTTP/1.1 and HTTP/2 over TLS 1.2 or 1.3, chosen by ALPN, and
HTTP/3 over QUIC on the same port number. TCP responses advertise HTTP/3
with an alt-svc field, unless the handler sets its own.
let tls = whiplash.Tls("priv/cert.pem", "priv/key.pem")
let assert Ok(_) = whiplash.start_tls(whiplash.options(8443), tls, handle)
start_tls_application(settings: Options, tls: Tls, application: Application) -> Result(
Running,
String
)
fn(whiplash.Options, whiplash.Tls, whiplash.Application) -> Result(whiplash.Running, String)start_application over TLS and HTTP/3, as start_tls does for a handler.
stop(server: Running) -> Nil
fn(whiplash.Running) -> NilShut down the complete supervision tree, including active connections.