wasi_http_listener (wasm v0.16.0)
View SourceA live HTTP server that serves a wasi:http reactor component.
Each request the server accepts is turned into an incoming request, handed to the
component's handler through wasi_preview3:run_serve/3 (a 0.2
wasi:http/incoming-handler or a 0.3 wasi:http/handler), and answered with the
response the guest made. The wire protocol is pluggable, the same seam the outbound path uses: a
listener speaks HTTP/1.1 (h1) or cleartext HTTP/2 (h2), so one component serves
either.
This is modelled on the barrel_mcp HTTP listener: the wire library owns the
acceptor pool, framing and one process per request; this module is the translator
between that process and the component. The component is instantiated per request
(host resources are per-process, so each request is isolated), which keeps a slow
or trapping guest from touching any other. The listener keeps the component's core
modules loaded for its life, so instantiating per request does not load them again.
{ok, L} = wasi_http_listener:start(#{port => 0, component => Bin}),
Addr = wasi_http_listener:address(L),
wasi_http_listener:stop(L).
Summary
Functions
The host:port the listener is bound to.
Start a listener for one reactor component.
Stop a listener and close its socket.
Types
Functions
The host:port the listener is bound to.
-spec start(#{component := binary(), transport => h1 | h2, ip => inet:ip_address(), port => inet:port_number(), scheme => binary(), serve_opts => map(), request_timeout => pos_integer(), max_body_bytes => pos_integer()}) -> {ok, server()} | {error, term()}.
Start a listener for one reactor component.
Opts carries the component (the reactor's bytes) and, optionally, the
transport (h1, the default, or h2), the ip to listen on ({127, 0, 0, 1} by default, so only this machine can connect; {0, 0, 0, 0} listens on
every interface), the port (0 for an ephemeral one), the
scheme reported to the guest (http by default), and serve_opts passed through
to run_serve/3 (a network grant lets the reactor make its own outbound
requests, and execution limits such as fuel bound each request's guest).
Two limits bound each request:
request_timeout, the most milliseconds from the request's arrival to its answer, body and guest included (60 s by default). A body not received in time is answered 408 and never reaches the guest; a guest still running is stopped and the request answered 504.max_body_bytes, the largest request body (8 MiB by default). A larger one is answered 413 and never reaches the guest.
-spec stop(server()) -> ok.
Stop a listener and close its socket.