Module

whiplash

src/whiplash.pooka

Whiplash: 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.

Pooka
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

src/whiplash.pooka:70
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.

Pooka
let roomy = whiplash.Options(8080, 8, 4096, 67108864, 60000)
field

port

src/whiplash.pooka:71
port: Int
INFERREDInt
field

acceptors

src/whiplash.pooka:72
acceptors: Int
INFERREDInt
field

max_connections

src/whiplash.pooka:73
max_connections: Int
INFERREDInt
field

max_body

src/whiplash.pooka:74
max_body: Int
INFERREDInt
field

request_timeout

src/whiplash.pooka:75
request_timeout: Int
INFERREDInt
constructor

new

src/whiplash.pooka:71
INFERREDfn(Int, Int, Int, Int, Int) -> whiplash.Options
method

address

src/whiplash.pooka:79
address() -> String
INFERREDfn() -> String

The interface the server listens on. Loopback unless changed with with_address, so nothing is reachable from other machines by accident.

method

with_address

src/whiplash.pooka:90
with_address(address: String) -> Options
INFERREDfn(String) -> whiplash.Options

Listen 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.

Pooka
let settings = whiplash.options(8080).with_address("0.0.0.0")
method

retry

src/whiplash.pooka:105
retry() -> Bool
INFERREDfn() -> Bool

Whether 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.

method

with_retry

src/whiplash.pooka:117
with_retry() -> Options
INFERREDfn() -> whiplash.Options

Answer 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.

Pooka
let settings = whiplash.options(8443).with_address("::").with_retry()
class

Tls

src/whiplash.pooka:182
class Tls(certificate: String, key: String)

Certificate and private key paths for an OTP TLS listener.

field

certificate

src/whiplash.pooka:182
certificate: String
INFERREDString
field

key

src/whiplash.pooka:182
key: String
INFERREDString
constructor

new

src/whiplash.pooka:182
INFERREDfn(String, String) -> whiplash.Tls
class

Application

src/whiplash.pooka:186
class 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.

field

handle

src/whiplash.pooka:187
handle: fn(http.Request) -> http.Response
INFERREDfn($package/pooka_http/pooka/http.Request) -> $package/pooka_http/pooka/http.Response
field

websocket

src/whiplash.pooka:188
websocket: fn(http.Request) -> Option(upgrade.Route)
INFERREDfn($package/pooka_http/pooka/http.Request) -> Option(whiplash/websocket/route.Route)
constructor

new

src/whiplash.pooka:187
INFERREDfn(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.Application
method

stream

src/whiplash.pooka:190
stream(request: http.Request) -> Option(body.Stream)
INFERREDfn($package/pooka_http/pooka/http.Request) -> Option(whiplash/body.Stream)
method

upload

src/whiplash.pooka:194
upload(request: http.Request) -> Option(
    fn(http.Request, body.Reader) -> http.Response
  )
INFERREDfn($package/pooka_http/pooka/http.Request) -> Option(fn($package/pooka_http/pooka/http.Request, whiplash/body.Reader) -> $package/pooka_http/pooka/http.Response)
method

has_uploads

src/whiplash.pooka:202
has_uploads() -> Bool
INFERREDfn() -> Bool

Whether any upload routes exist; without them, no request is examined before its body has arrived.

method

with_streaming

src/whiplash.pooka:207
with_streaming(routes: fn(http.Request) -> Option(body.Stream)) -> Application
INFERREDfn(fn($package/pooka_http/pooka/http.Request) -> Option(whiplash/body.Stream)) -> whiplash.Application

Routes whose responses are produced a piece at a time.

method

with_uploads

src/whiplash.pooka:219
with_uploads(
    routes: fn(http.Request) -> Option(
      fn(http.Request, body.Reader) -> http.Response
    )
  ) -> Application
INFERREDfn(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.Application

Routes 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

src/whiplash.pooka:268
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.

field

port

src/whiplash.pooka:268
port: Int
INFERREDInt
field

tree

src/whiplash.pooka:268
tree: supervisor.Supervisor
INFERRED$package/pooka_otp/pooka/otp/supervisor.Supervisor
constructor

new

src/whiplash.pooka:268
INFERREDfn(Int, $package/pooka_otp/pooka/otp/supervisor.Supervisor) -> whiplash.Running
function

options

src/whiplash.pooka:308
options(port: Int) -> Options
INFERREDfn(Int) -> whiplash.Options

Options 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.

function

start

src/whiplash.pooka:321
start(settings: Options, handle: fn(http.Request) -> http.Response) -> Result(
  Running,
  String
)
INFERREDfn(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.

Pooka
let assert Ok(site) = whiplash.start(whiplash.options(8080), fn(_) { http.text(200, "Hello") })
whiplash.stop(site)
function

start_application

src/whiplash.pooka:330
start_application(settings: Options, application: Application) -> Result(
  Running,
  String
)

Start an application: a handler plus WebSocket, streaming and upload routes.

function

start_tls

src/whiplash.pooka:345
start_tls(
  settings: Options,
  tls: Tls,
  handle: fn(http.Request) -> http.Response
) -> Result(Running, String)
INFERREDfn(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.

Pooka
let tls = whiplash.Tls("priv/cert.pem", "priv/key.pem")
let assert Ok(_) = whiplash.start_tls(whiplash.options(8443), tls, handle)
function

start_tls_application

src/whiplash.pooka:354
start_tls_application(settings: Options, tls: Tls, application: Application) -> Result(
  Running,
  String
)

start_application over TLS and HTTP/3, as start_tls does for a handler.

function

stop

src/whiplash.pooka:524
stop(server: Running) -> Nil
INFERREDfn(whiplash.Running) -> Nil

Shut down the complete supervision tree, including active connections.