wasi_http (wasm v0.16.0)

View Source

A slice of wasi:http@0.2: the outbound (outgoing-handler) path and the wasi:http/types resources it needs.

http/1 returns the import map a wasi:cli/command (or proxy) component links against. The guest builds an outgoing-request (method, path, scheme, authority and a fields header set), hands it to outgoing-handler.handle, and the host performs the request with the h1 client and hands back a future-incoming-response that is already resolved (our client is synchronous), from which the guest reads the status, headers and body.

The wire transport is the host's choice and invisible to the guest; this binding uses HTTP/1.1 (h1). Requests are gated by a wasi_net grant, so a component reaches only a granted authority: the network grant the sockets slice uses, or an http grant of its own, which lets the host resolve the names a guest asks for without giving the guest sockets or name lookups. An http_authorize hook can refuse a request by its host name before any lookup, and by its address once the grant has admitted one. perform/4 decides, for 0.2 and 0.3 alike.

Summary

Functions

Append bytes to an outgoing-body's buffer (its write stream). A write that would carry the body past its declared content-length, or past http_max_body_bytes, fails with an io-error the guest recovers through http-error-code as HTTP-request-body-size.

The options of http/1 that the options of wasi_preview2:command/1 give: network as grant, http, http_authorize as authorize, http_transport as transport, http_tls as tls and http_max_body_bytes as max_body_bytes. The 0.2 and the 0.3 host read them through this one map.

The error-code variant, the same at 0.2 and 0.3.

The wasi:http/types + outgoing-handler import map. Opts carries what an outbound request is checked against, as policy/1 reads it: the grant (a wasi_net grant, the network one), or an http grant of its own, and an authorize hook.

Create the incoming-request resource the host hands a reactor's handle.

What an outbound request is checked against, from the options of http/1.

Read the response a reactor set on its outparam, after handle returns.

Types

policy()

-type policy() ::
          #{grant := wasi_net:grant(),
            names := boolean(),
            authorize := fun((map()) -> allow | deny) | undefined}.

Functions

append_body(B, Bytes)

-spec append_body(term(), binary()) -> ok | {error, {http_body_size, non_neg_integer()}}.

Append bytes to an outgoing-body's buffer (its write stream). A write that would carry the body past its declared content-length, or past http_max_body_bytes, fails with an io-error the guest recovers through http-error-code as HTTP-request-body-size.

command_options(Opts)

-spec command_options(map()) -> map().

The options of http/1 that the options of wasi_preview2:command/1 give: network as grant, http, http_authorize as authorize, http_transport as transport, http_tls as tls and http_max_body_bytes as max_body_bytes. The 0.2 and the 0.3 host read them through this one map.

error_code()

-spec error_code() -> wasm_canon:desc().

The error-code variant, the same at 0.2 and 0.3.

field_error(Name, Value)

http(Opts)

-spec http(#{grant => term(),
             http => term(),
             authorize => fun((map()) -> allow | deny),
             transport => module(),
             tls => [ssl:tls_client_option()],
             max_body_bytes => pos_integer()}) ->
              #{{binary(), binary()} => fun()}.

The wasi:http/types + outgoing-handler import map. Opts carries what an outbound request is checked against, as policy/1 reads it: the grant (a wasi_net grant, the network one), or an http grant of its own, and an authorize hook.

incoming_request(Request)

-spec incoming_request(map()) -> non_neg_integer().

Create the incoming-request resource the host hands a reactor's handle.

perform/4

policy(Opts)

-spec policy(map()) -> policy().

What an outbound request is checked against, from the options of http/1.

  • http: a grant for wasi:http alone, #{connect => Rules} with the rules of a wasi_net grant. Under it the host resolves a host name the guest asks for, and checks each address the name gives. When it is given, grant is not used for wasi:http.
  • grant: the network grant, which wasi:sockets uses too. Under it a name is resolved only with resolve => allow.
  • authorize: fun(Request) -> allow | deny, called with #{scheme, host, port} before a name is resolved, so a name it denies is never looked up, and again with address added once an address has passed the grant. Anything but allow, and a raise, is a deny.

A grant it cannot read raises {bad_net_grant, Term}, as wasi_net:grant/1 does, and an authorize that is not a fun of one argument raises {bad_http_authorize, Term}, here, where the import map is built.

read_outparam(Param)

-spec read_outparam(non_neg_integer()) ->
                       {ok, 0..65535, [{binary(), binary()}], binary()} | {error, term()}.

Read the response a reactor set on its outparam, after handle returns.

request_error/1