This interface defines a handler of HTTP Requests.
In a wasi:http/service this interface is exported to respond to an
incoming HTTP Request with a Response.
In wasi:http/middleware this interface is both exported and imported as
the "downstream" and "upstream" directions of the middleware chain.
Types
use resource request
Represents an HTTP Request.
new: static func( headers: headers, contents: option<stream<u8>>, trailers: future<result<option<trailers>, error-code>>, options: option<request-options>, ) -> tuple<request, future<result<_, error-code>>>Construct a new
requestwith a defaultmethodofGET, andnonevalues forpath-with-query,scheme, andauthority.headersis the HTTP Headers for the Request.contentsis the optional body content stream withnonerepresenting a zero-length content stream. Once it is closed,trailersfuture must resolve to a result. Iftrailersresolves to an error, underlying connection will be closed immediately.optionsis optionalrequest-optionsresource to be used if the request is sent over a network connection.It is possible to construct, or manipulate with the accessor functions below, a
requestwith an invalid combination ofschemeandauthority, orheaderswhich are not permitted to be sent. It is the obligation of thehandler.handleimplementation to reject invalid constructions ofrequest.The returned future resolves to result of transmission of this request.
get-method: func() -> methodGet the Method for the Request.
set-method: func(method: method) -> resultSet the Method for the Request. Fails if the string present in a
method.otherargument is not a syntactically valid method.get-path-with-query: func() -> option<string>Get the combination of the HTTP Path and Query for the Request. When
none, this represents an empty Path and empty Query.set-path-with-query: func(path-with-query: option<string>) -> resultSet the combination of the HTTP Path and Query for the Request. When
none, this represents an empty Path and empty Query. Fails is the string given is not a syntactically valid path and query uri component.get-scheme: func() -> option<scheme>Get the HTTP Related Scheme for the Request. When
none, the implementation may choose an appropriate default scheme.set-scheme: func(scheme: option<scheme>) -> resultSet the HTTP Related Scheme for the Request. When
none, the implementation may choose an appropriate default scheme. Fails if the string given is not a syntactically valid uri scheme.get-options: func() -> option<request-options>Get the
request-optionsto be associated with this requestThe returned
request-optionsresource is immutable:set-*operations will fail if invoked.This
request-optionsresource is a child: it must be dropped before the parentrequestis dropped, or its ownership is transferred to another component by e.g.handler.handle.get-headers: func() -> headersGet the headers associated with the Request.
The returned
headersresource is immutable:set,append, anddeleteoperations will fail withheader-error.immutable.consume-body: static func( this: request, res: future<result<_, error-code>>, ) -> tuple<stream<u8>, future<result<option<trailers>, error-code>>>Get body of the Request.
Stream returned by this method represents the contents of the body. Once the stream is reported as closed, callers should await the returned future to determine whether the body was received successfully. The future will only resolve after the stream is reported as closed.
This function takes a
resfuture as a parameter, which can be used to communicate an error in handling of the request.Note that function will move the
request, but references to headers or request options acquired from it previously will remain valid.
use resource response
Represents an HTTP Response.
new: static func( headers: headers, contents: option<stream<u8>>, trailers: future<result<option<trailers>, error-code>>, ) -> tuple<response, future<result<_, error-code>>>Construct a new
response, with a defaultstatus-codeof200. If a differentstatus-codeis needed, it must be set via theset-status-codemethod.headersis the HTTP Headers for the Response.contentsis the optional body content stream withnonerepresenting a zero-length content stream. Once it is closed,trailersfuture must resolve to a result. Iftrailersresolves to an error, underlying connection will be closed immediately.The returned future resolves to result of transmission of this response.
get-status-code: func() -> status-codeGet the HTTP Status Code for the Response.
set-status-code: func(status-code: status-code) -> resultSet the HTTP Status Code for the Response. Fails if the status-code given is not a valid http status code.
get-headers: func() -> headersGet the headers associated with the Response.
The returned
headersresource is immutable:set,append, anddeleteoperations will fail withheader-error.immutable.consume-body: static func( this: response, res: future<result<_, error-code>>, ) -> tuple<stream<u8>, future<result<option<trailers>, error-code>>>Get body of the Response.
Stream returned by this method represents the contents of the body. Once the stream is reported as closed, callers should await the returned future to determine whether the body was received successfully. The future will only resolve after the stream is reported as closed.
This function takes a
resfuture as a parameter, which can be used to communicate an error in handling of the response.Note that function will move the
response, but references to headers acquired from it previously will remain valid.
use variant error-code
These cases are inspired by the IANA HTTP Proxy Error Types: https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types
DNS-timeoutDNS-error:DNS-error-payloaddestination-not-founddestination-unavailabledestination-IP-prohibiteddestination-IP-unroutableconnection-refusedconnection-terminatedconnection-timeoutconnection-read-timeoutconnection-write-timeoutconnection-limit-reachedTLS-protocol-errorTLS-certificate-errorTLS-alert-received:TLS-alert-received-payloadHTTP-request-deniedHTTP-request-length-requiredHTTP-request-body-size:option<u64>HTTP-request-method-invalidHTTP-request-URI-invalidHTTP-request-URI-too-longHTTP-request-header-section-size:option<u32>HTTP-request-header-size:option<field-size-payload>HTTP-request-trailer-section-size:option<u32>HTTP-request-trailer-size:field-size-payloadHTTP-response-incompleteHTTP-response-header-section-size:option<u32>HTTP-response-header-size:field-size-payloadHTTP-response-body-size:option<u64>HTTP-response-trailer-section-size:option<u32>HTTP-response-trailer-size:field-size-payloadHTTP-response-transfer-coding:option<string>HTTP-response-content-coding:option<string>HTTP-response-timeoutHTTP-upgrade-failedHTTP-protocol-errorloop-detectedconfiguration-errorinternal-error:option<string>This is a catch-all error for anything that doesn't fit cleanly into a more specific case. It also includes an optional string for an unstructured description of the error. Users should not depend on the string for diagnosing errors, as it's not required to be consistent between implementations.
Functions
handle: async func(request: request) -> result<response, error-code>
This function may be called with either an incoming request read from the network or a request synthesized or forwarded by another component.
WIT
/// This interface defines a handler of HTTP Requests.
///
/// In a `wasi:http/service` this interface is exported to respond to an
/// incoming HTTP Request with a Response.
///
/// In `wasi:http/middleware` this interface is both exported and imported as
/// the "downstream" and "upstream" directions of the middleware chain.
@since(version = 0.3.0)
interface handler {
use types.{request, response, error-code};
/// This function may be called with either an incoming request read from the
/// network or a request synthesized or forwarded by another component.
handle: async func(request: request) -> result<response, error-code>;
}