diff --git a/docs/Proposals.md b/docs/Proposals.md index 91e7e320..698264e0 100644 --- a/docs/Proposals.md +++ b/docs/Proposals.md @@ -35,6 +35,7 @@ You can learn more about contributing new proposals (and other ways to contribut | Proposal | Champion | Versions | | -------------------------------------------------- | --------------------------------------------------------------------- | -------- | | [Clocks: Timezone][wasi-clocks] | Dan Gohman, Colin Murphy | | +| [HTTP: Filter][wasi-http] | Pat Hickey, Lann Martin, John VanEnk | | | [HTTP: Informational Outbound Response][wasi-http] | Pat Hickey, Lann Martin, John VanEnk | | | [I2C][wasi-i2c] | Friedrich Vandenberghe, Merlijn Sebrechts, Maximilian Seidler | | | [Key-value Store][wasi-kv-store] | Jiaxiao Zhou, Dan Chiarlone | | diff --git a/proposals/http/wit/types.wit b/proposals/http/wit/types.wit index a1f6094c..a013bb3c 100644 --- a/proposals/http/wit/types.wit +++ b/proposals/http/wit/types.wit @@ -470,4 +470,14 @@ interface types { /// acquired from it previously will remain valid. consume-body: static func(this: response, res: future>) -> tuple, future, error-code>>>; } + + /// The action to take after inspecting the incoming request. + @unstable(feature = http-filter) + variant filter-action { + /// Forward the (possibly modified) request to origin. + forward(request), + + /// Short-circuit with a response (skip origin). + respond(response), + } } diff --git a/proposals/http/wit/worlds.wit b/proposals/http/wit/worlds.wit index 3d7da8b7..4b5ae014 100644 --- a/proposals/http/wit/worlds.wit +++ b/proposals/http/wit/worlds.wit @@ -78,6 +78,54 @@ world middleware { import handler; } +/// Memory-efficient middleware intended for resource-constrained environments. +/// +/// Separating the request and response phases can let hosts reclaim a filter's +/// memory while the next stage processes a request, allowing more concurrent +/// requests within the same memory budget. +@unstable(feature = http-filter) +world filter { + /// HTTP services have access to time and randomness. + include wasi:clocks/imports@0.3.2; + include wasi:random/imports@0.3.2; + + /// Services have standard output and error streams which are expected to + /// terminate in a developer-facing console provided by the host. + import wasi:cli/stdout@0.3.2; + import wasi:cli/stderr@0.3.2; + import wasi:cli/stdin@0.3.2; + + /// This is the default `client` to use when user code simply wants to make an + /// HTTP request (e.g., via `fetch()`). + import client; + + export filter-handler; +} + +/// Filtering handler interface for HTTP middleware. +/// +/// Components export this to process requests and responses in a linear +/// (non-blocking, two-phase) fashion. +/// +/// For a given HTTP request, hosts may invoke `handle-request` and +/// `handle-response` on different component instances. Filters must not rely +/// on instance-local state being preserved between the two phases. +@unstable(feature = http-filter) +interface filter-handler { + @unstable(feature = http-filter) + use types.{request, response, error-code, filter-action}; + + /// Called during the request phase. Receives the client request and + /// decides whether to proceed to origin or respond immediately. + @unstable(feature = http-filter) + handle-request: async func(req: request) -> result; + + /// Called during the response phase. Receives the origin response and + /// returns a (possibly modified) response to send to the client. + @unstable(feature = http-filter) + handle-response: async func(resp: response) -> result; +} + /// This interface defines a handler of HTTP Requests. /// /// In a `wasi:http/service` this interface is exported to respond to an