Skip to content

Commit

Permalink
add accessor methods to outgoing-request and outgoing-response
Browse files Browse the repository at this point in the history
Taken from wasmtime b663ccf9e1eef4b0a1e5637cd6f86d8d42783403

This change has two purposes:

1. adding the accessors enables treating the resource's representation of all state as canonical, instead of requiring a component to retain a copy of all state contained in the resource.

1. Adding mutators for all state supports use cases where it's required to change a request's/response's state after writing to its body. This is in particular the case when implementing preexisting systems or specifications, such as the WHATWG fetch spec used in JavaScript environments.

Co-authored-by: Till Schneidereit <till@tillschneidereit.net>
  • Loading branch information
Pat Hickey and tschneidereit committed Oct 26, 2023
1 parent 896dac2 commit c7412a4
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions wit/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,29 @@ interface types {
resource outgoing-request {

/// Construct a new `outgoing-request`.
///
/// * `method` represents the HTTP Method for the Request.
/// * `path-with-query` is the combination of the HTTP Path and Query for
/// the Request. When `none`, this represents an empty Path and empty
/// Query.
/// * `scheme` is the HTTP Related Scheme for the Request. When `none`,
/// the implementation may choose an appropriate default scheme.
/// * `authority` is the HTTP Authority for the Request. A value of `none`
/// may be used with Related Schemes which do not require an Authority.
/// The HTTP and HTTPS schemes always require an authority.
/// * `headers` is the HTTP Headers for the Request.
///
/// It is possible to construct, or manipulate with the accessor functions
/// below, an `outgoing-request` with an invalid combination of `scheme`
/// and `authority`, or `headers` which are not permitted to be sent.
/// It is the obligation of the `outgoing-handler.handle` implementation
/// to reject invalid constructions of `outgoing-request`.
constructor(
method: method,
path-with-query: option<string>,
scheme: option<scheme>,
authority: option<string>,
headers: borrow<headers>
headers: headers
);

/// Returns the resource corresponding to the outgoing Body for this
Expand All @@ -142,6 +159,41 @@ interface types {
/// this `outgoing-response` can be retrieved at most once. Subsequent
/// calls will return error.
body: func() -> result<outgoing-body>;

/// Get the Method for the Request.
method: func() -> method;
/// Set the Method for the Request.
set-method: func(method: method);

/// Get the combination of the HTTP Path and Query for the Request.
/// When `none`, this represents an empty Path and empty Query.
path-with-query: func() -> option<string>;
/// Set 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>);

/// Get the HTTP Related Scheme for the Request. When `none`, the
/// implementation may choose an appropriate default scheme.
scheme: func() -> option<scheme>;
/// Set the HTTP Related Scheme for the Request. When `none`, the
/// implementation may choose an appropriate default scheme.
set-scheme: func(scheme: option<scheme>);

/// Get the HTTP Authority for the Request. A value of `none` may be used
/// with Related Schemes which do not require an Authority. The HTTP and
/// HTTPS schemes always require an authority.
authority: func() -> option<string>;
/// Set the HTTP Authority for the Request. A value of `none` may be used
/// with Related Schemes which do not require an Authority. The HTTP and
/// HTTPS schemes always require an authority.
set-authority: func(authority: option<string>);

/// Get the headers associated with the Request.
///
/// This headers resource is a child: it must be dropped before the parent
/// `outgoing-request` is dropped, or its ownership is transfered to
/// another component by e.g. `outgoing-handler.handle`.
headers: func() -> headers;
}

/// Parameters for making an HTTP Request. Each of these parameters is an
Expand Down Expand Up @@ -180,10 +232,17 @@ interface types {
/// This method consumes the `response-outparam` to ensure that it is
/// called at most once. If it is never called, the implementation
/// will respond with an error.
///
/// The user may provide an `error` to `response` to allow the
/// implementation determine how to respond with an HTTP error response.
///
/// This method may return an error when the `outgoing-response` contains
/// a `status-code` or `headers` which the implementation does not
/// support.
set: static func(
param: response-outparam,
response: result<outgoing-response, error>,
);
) -> result<_, error>;
}

/// This type corresponds to the HTTP standard Status Code.
Expand Down Expand Up @@ -263,7 +322,22 @@ interface types {
resource outgoing-response {

/// Construct an `outgoing-response`.
constructor(status-code: status-code, headers: borrow<headers>);
///
/// * `status-code` is the HTTP Status Code for the Response.
/// * `headers` is the HTTP Headers for the Response.
constructor(status-code: status-code, headers: headers);

/// Get the HTTP Status Code for the Response.
status-code: func() -> status-code;
/// Set the HTTP Status Code for the Response.
set-status-code: func(status-code: status-code);

/// Get the headers associated with the Request.
///
/// This headers resource is a child: it must be dropped before the parent
/// `outgoing-request` is dropped, or its ownership is transfered to
/// another component by e.g. `outgoing-handler.handle`.
headers: func() -> headers;

/// Returns the resource corresponding to the outgoing Body for this Response.
///
Expand Down

0 comments on commit c7412a4

Please sign in to comment.