Debian Codegraph @

crates/wit-component/tests/interfaces/wasi-http/types.wit

raw
1 /// This interface defines all of the types and methods for implementing
2 /// HTTP Requests and Responses, both incoming and outgoing, as well as
3 /// their headers, trailers, and bodies.
4 interface types {
5 use wasi:clocks/[email protected].{duration};
6 use wasi:io/[email protected].{input-stream, output-stream};
7 use wasi:io/[email protected].{error as io-error};
8 use wasi:io/[email protected].{pollable};
9
10 /// This type corresponds to HTTP standard Methods.
11 variant method {
12 get,
13 head,
14 post,
15 put,
16 delete,
17 connect,
18 options,
19 trace,
20 patch,
21 other(string)
22 }
23
24 /// This type corresponds to HTTP standard Related Schemes.
25 variant scheme {
26 HTTP,
27 HTTPS,
28 other(string)
29 }
30
31 /// These cases are inspired by the IANA HTTP Proxy Error Types:
32 /// https://www.iana.org/assignments/http-proxy-status/http-proxy-status.xhtml#table-http-proxy-error-types
33 variant error-code {
34 DNS-timeout,
35 DNS-error(DNS-error-payload),
36 destination-not-found,
37 destination-unavailable,
38 destination-IP-prohibited,
39 destination-IP-unroutable,
40 connection-refused,
41 connection-terminated,
42 connection-timeout,
43 connection-read-timeout,
44 connection-write-timeout,
45 connection-limit-reached,
46 TLS-protocol-error,
47 TLS-certificate-error,
48 TLS-alert-received(TLS-alert-received-payload),
49 HTTP-request-denied,
50 HTTP-request-length-required,
51 HTTP-request-body-size(option<u64>),
52 HTTP-request-method-invalid,
53 HTTP-request-URI-invalid,
54 HTTP-request-URI-too-long,
55 HTTP-request-header-section-size(option<u32>),
56 HTTP-request-header-size(option<field-size-payload>),
57 HTTP-request-trailer-section-size(option<u32>),
58 HTTP-request-trailer-size(field-size-payload),
59 HTTP-response-incomplete,
60 HTTP-response-header-section-size(option<u32>),
61 HTTP-response-header-size(field-size-payload),
62 HTTP-response-body-size(option<u64>),
63 HTTP-response-trailer-section-size(option<u32>),
64 HTTP-response-trailer-size(field-size-payload),
65 HTTP-response-transfer-coding(option<string>),
66 HTTP-response-content-coding(option<string>),
67 HTTP-response-timeout,
68 HTTP-upgrade-failed,
69 HTTP-protocol-error,
70 loop-detected,
71 configuration-error,
72 /// This is a catch-all error for anything that doesn't fit cleanly into a
73 /// more specific case. It also includes an optional string for an
74 /// unstructured description of the error. Users should not depend on the
75 /// string for diagnosing errors, as it's not required to be consistent
76 /// between implementations.
77 internal-error(option<string>)
78 }
79
80 /// Defines the case payload type for `DNS-error` above:
81 record DNS-error-payload {
82 rcode: option<string>,
83 info-code: option<u16>
84 }
85
86 /// Defines the case payload type for `TLS-alert-received` above:
87 record TLS-alert-received-payload {
88 alert-id: option<u8>,
89 alert-message: option<string>
90 }
91
92 /// Defines the case payload type for `HTTP-response-{header,trailer}-size` above:
93 record field-size-payload {
94 field-name: option<string>,
95 field-size: option<u32>
96 }
97
98 /// Attempts to extract a http-related `error` from the wasi:io `error`
99 /// provided.
100 ///
101 /// Stream operations which return
102 /// `wasi:io/stream/stream-error::last-operation-failed` have a payload of
103 /// type `wasi:io/error/error` with more information about the operation
104 /// that failed. This payload can be passed through to this function to see
105 /// if there's http-related information about the error to return.
106 ///
107 /// Note that this function is fallible because not all io-errors are
108 /// http-related errors.
109 http-error-code: func(err: borrow<io-error>) -> option<error-code>;
110
111 /// This type enumerates the different kinds of errors that may occur when
112 /// setting or appending to a `fields` resource.
113 variant header-error {
114 /// This error indicates that a `field-key` or `field-value` was
115 /// syntactically invalid when used with an operation that sets headers in a
116 /// `fields`.
117 invalid-syntax,
118
119 /// This error indicates that a forbidden `field-key` was used when trying
120 /// to set a header in a `fields`.
121 forbidden,
122
123 /// This error indicates that the operation on the `fields` was not
124 /// permitted because the fields are immutable.
125 immutable,
126 }
127
128 /// Field keys are always strings.
129 type field-key = string;
130
131 /// Field values should always be ASCII strings. However, in
132 /// reality, HTTP implementations often have to interpret malformed values,
133 /// so they are provided as a list of bytes.
134 type field-value = list<u8>;
135
136 /// This following block defines the `fields` resource which corresponds to
137 /// HTTP standard Fields. Fields are a common representation used for both
138 /// Headers and Trailers.
139 ///
140 /// A `fields` may be mutable or immutable. A `fields` created using the
141 /// constructor, `from-list`, or `clone` will be mutable, but a `fields`
142 /// resource given by other means (including, but not limited to,
143 /// `incoming-request.headers`, `outgoing-request.headers`) might be be
144 /// immutable. In an immutable fields, the `set`, `append`, and `delete`
145 /// operations will fail with `header-error.immutable`.
146 resource fields {
147
148 /// Construct an empty HTTP Fields.
149 ///
150 /// The resulting `fields` is mutable.
151 constructor();
152
153 /// Construct an HTTP Fields.
154 ///
155 /// The resulting `fields` is mutable.
156 ///
157 /// The list represents each key-value pair in the Fields. Keys
158 /// which have multiple values are represented by multiple entries in this
159 /// list with the same key.
160 ///
161 /// The tuple is a pair of the field key, represented as a string, and
162 /// Value, represented as a list of bytes. In a valid Fields, all keys
163 /// and values are valid UTF-8 strings. However, values are not always
164 /// well-formed, so they are represented as a raw list of bytes.
165 ///
166 /// An error result will be returned if any header or value was
167 /// syntactically invalid, or if a header was forbidden.
168 from-list: static func(
169 entries: list<tuple<field-key,field-value>>
170 ) -> result<fields, header-error>;
171
172 /// Get all of the values corresponding to a key. If the key is not present
173 /// in this `fields`, an empty list is returned. However, if the key is
174 /// present but empty, this is represented by a list with one or more
175 /// empty field-values present.
176 get: func(name: field-key) -> list<field-value>;
177
178 /// Returns `true` when the key is present in this `fields`. If the key is
179 /// syntactically invalid, `false` is returned.
180 has: func(name: field-key) -> bool;
181
182 /// Set all of the values for a key. Clears any existing values for that
183 /// key, if they have been set.
184 ///
185 /// Fails with `header-error.immutable` if the `fields` are immutable.
186 set: func(name: field-key, value: list<field-value>) -> result<_, header-error>;
187
188 /// Delete all values for a key. Does nothing if no values for the key
189 /// exist.
190 ///
191 /// Fails with `header-error.immutable` if the `fields` are immutable.
192 delete: func(name: field-key) -> result<_, header-error>;
193
194 /// Append a value for a key. Does not change or delete any existing
195 /// values for that key.
196 ///
197 /// Fails with `header-error.immutable` if the `fields` are immutable.
198 append: func(name: field-key, value: field-value) -> result<_, header-error>;
199
200 /// Retrieve the full set of keys and values in the Fields. Like the
201 /// constructor, the list represents each key-value pair.
202 ///
203 /// The outer list represents each key-value pair in the Fields. Keys
204 /// which have multiple values are represented by multiple entries in this
205 /// list with the same key.
206 entries: func() -> list<tuple<field-key,field-value>>;
207
208 /// Make a deep copy of the Fields. Equivelant in behavior to calling the
209 /// `fields` constructor on the return value of `entries`. The resulting
210 /// `fields` is mutable.
211 clone: func() -> fields;
212 }
213
214 /// Headers is an alias for Fields.
215 type headers = fields;
216
217 /// Trailers is an alias for Fields.
218 type trailers = fields;
219
220 /// Represents an incoming HTTP Request.
221 resource incoming-request {
222
223 /// Returns the method of the incoming request.
224 method: func() -> method;
225
226 /// Returns the path with query parameters from the request, as a string.
227 path-with-query: func() -> option<string>;
228
229 /// Returns the protocol scheme from the request.
230 scheme: func() -> option<scheme>;
231
232 /// Returns the authority from the request, if it was present.
233 authority: func() -> option<string>;
234
235 /// Get the `headers` associated with the request.
236 ///
237 /// The returned `headers` resource is immutable: `set`, `append`, and
238 /// `delete` operations will fail with `header-error.immutable`.
239 ///
240 /// The `headers` returned are a child resource: it must be dropped before
241 /// the parent `incoming-request` is dropped. Dropping this
242 /// `incoming-request` before all children are dropped will trap.
243 headers: func() -> headers;
244
245 /// Gives the `incoming-body` associated with this request. Will only
246 /// return success at most once, and subsequent calls will return error.
247 consume: func() -> result<incoming-body>;
248 }
249
250 /// Represents an outgoing HTTP Request.
251 resource outgoing-request {
252
253 /// Construct a new `outgoing-request` with a default `method` of `GET`, and
254 /// `none` values for `path-with-query`, `scheme`, and `authority`.
255 ///
256 /// * `headers` is the HTTP Headers for the Request.
257 ///
258 /// It is possible to construct, or manipulate with the accessor functions
259 /// below, an `outgoing-request` with an invalid combination of `scheme`
260 /// and `authority`, or `headers` which are not permitted to be sent.
261 /// It is the obligation of the `outgoing-handler.handle` implementation
262 /// to reject invalid constructions of `outgoing-request`.
263 constructor(
264 headers: headers
265 );
266
267 /// Returns the resource corresponding to the outgoing Body for this
268 /// Request.
269 ///
270 /// Returns success on the first call: the `outgoing-body` resource for
271 /// this `outgoing-request` can be retrieved at most once. Subsequent
272 /// calls will return error.
273 body: func() -> result<outgoing-body>;
274
275 /// Get the Method for the Request.
276 method: func() -> method;
277 /// Set the Method for the Request. Fails if the string present in a
278 /// `method.other` argument is not a syntactically valid method.
279 set-method: func(method: method) -> result;
280
281 /// Get the combination of the HTTP Path and Query for the Request.
282 /// When `none`, this represents an empty Path and empty Query.
283 path-with-query: func() -> option<string>;
284 /// Set the combination of the HTTP Path and Query for the Request.
285 /// When `none`, this represents an empty Path and empty Query. Fails is the
286 /// string given is not a syntactically valid path and query uri component.
287 set-path-with-query: func(path-with-query: option<string>) -> result;
288
289 /// Get the HTTP Related Scheme for the Request. When `none`, the
290 /// implementation may choose an appropriate default scheme.
291 scheme: func() -> option<scheme>;
292 /// Set the HTTP Related Scheme for the Request. When `none`, the
293 /// implementation may choose an appropriate default scheme. Fails if the
294 /// string given is not a syntactically valid uri scheme.
295 set-scheme: func(scheme: option<scheme>) -> result;
296
297 /// Get the HTTP Authority for the Request. A value of `none` may be used
298 /// with Related Schemes which do not require an Authority. The HTTP and
299 /// HTTPS schemes always require an authority.
300 authority: func() -> option<string>;
301 /// Set the HTTP Authority for the Request. A value of `none` may be used
302 /// with Related Schemes which do not require an Authority. The HTTP and
303 /// HTTPS schemes always require an authority. Fails if the string given is
304 /// not a syntactically valid uri authority.
305 set-authority: func(authority: option<string>) -> result;
306
307 /// Get the headers associated with the Request.
308 ///
309 /// The returned `headers` resource is immutable: `set`, `append`, and
310 /// `delete` operations will fail with `header-error.immutable`.
311 ///
312 /// This headers resource is a child: it must be dropped before the parent
313 /// `outgoing-request` is dropped, or its ownership is transfered to
314 /// another component by e.g. `outgoing-handler.handle`.
315 headers: func() -> headers;
316 }
317
318 /// Parameters for making an HTTP Request. Each of these parameters is
319 /// currently an optional timeout applicable to the transport layer of the
320 /// HTTP protocol.
321 ///
322 /// These timeouts are separate from any the user may use to bound a
323 /// blocking call to `wasi:io/poll.poll`.
324 resource request-options {
325 /// Construct a default `request-options` value.
326 constructor();
327
328 /// The timeout for the initial connect to the HTTP Server.
329 connect-timeout: func() -> option<duration>;
330
331 /// Set the timeout for the initial connect to the HTTP Server. An error
332 /// return value indicates that this timeout is not supported.
333 set-connect-timeout: func(duration: option<duration>) -> result;
334
335 /// The timeout for receiving the first byte of the Response body.
336 first-byte-timeout: func() -> option<duration>;
337
338 /// Set the timeout for receiving the first byte of the Response body. An
339 /// error return value indicates that this timeout is not supported.
340 set-first-byte-timeout: func(duration: option<duration>) -> result;
341
342 /// The timeout for receiving subsequent chunks of bytes in the Response
343 /// body stream.
344 between-bytes-timeout: func() -> option<duration>;
345
346 /// Set the timeout for receiving subsequent chunks of bytes in the Response
347 /// body stream. An error return value indicates that this timeout is not
348 /// supported.
349 set-between-bytes-timeout: func(duration: option<duration>) -> result;
350 }
351
352 /// Represents the ability to send an HTTP Response.
353 ///
354 /// This resource is used by the `wasi:http/incoming-handler` interface to
355 /// allow a Response to be sent corresponding to the Request provided as the
356 /// other argument to `incoming-handler.handle`.
357 resource response-outparam {
358
359 /// Set the value of the `response-outparam` to either send a response,
360 /// or indicate an error.
361 ///
362 /// This method consumes the `response-outparam` to ensure that it is
363 /// called at most once. If it is never called, the implementation
364 /// will respond with an error.
365 ///
366 /// The user may provide an `error` to `response` to allow the
367 /// implementation determine how to respond with an HTTP error response.
368 set: static func(
369 param: response-outparam,
370 response: result<outgoing-response, error-code>,
371 );
372 }
373
374 /// This type corresponds to the HTTP standard Status Code.
375 type status-code = u16;
376
377 /// Represents an incoming HTTP Response.
378 resource incoming-response {
379
380 /// Returns the status code from the incoming response.
381 status: func() -> status-code;
382
383 /// Returns the headers from the incoming response.
384 ///
385 /// The returned `headers` resource is immutable: `set`, `append`, and
386 /// `delete` operations will fail with `header-error.immutable`.
387 ///
388 /// This headers resource is a child: it must be dropped before the parent
389 /// `incoming-response` is dropped.
390 headers: func() -> headers;
391
392 /// Returns the incoming body. May be called at most once. Returns error
393 /// if called additional times.
394 consume: func() -> result<incoming-body>;
395 }
396
397 /// Represents an incoming HTTP Request or Response's Body.
398 ///
399 /// A body has both its contents - a stream of bytes - and a (possibly
400 /// empty) set of trailers, indicating that the full contents of the
401 /// body have been received. This resource represents the contents as
402 /// an `input-stream` and the delivery of trailers as a `future-trailers`,
403 /// and ensures that the user of this interface may only be consuming either
404 /// the body contents or waiting on trailers at any given time.
405 resource incoming-body {
406
407 /// Returns the contents of the body, as a stream of bytes.
408 ///
409 /// Returns success on first call: the stream representing the contents
410 /// can be retrieved at most once. Subsequent calls will return error.
411 ///
412 /// The returned `input-stream` resource is a child: it must be dropped
413 /// before the parent `incoming-body` is dropped, or consumed by
414 /// `incoming-body.finish`.
415 ///
416 /// This invariant ensures that the implementation can determine whether
417 /// the user is consuming the contents of the body, waiting on the
418 /// `future-trailers` to be ready, or neither. This allows for network
419 /// backpressure is to be applied when the user is consuming the body,
420 /// and for that backpressure to not inhibit delivery of the trailers if
421 /// the user does not read the entire body.
422 %stream: func() -> result<input-stream>;
423
424 /// Takes ownership of `incoming-body`, and returns a `future-trailers`.
425 /// This function will trap if the `input-stream` child is still alive.
426 finish: static func(this: incoming-body) -> future-trailers;
427 }
428
429 /// Represents a future which may eventaully return trailers, or an error.
430 ///
431 /// In the case that the incoming HTTP Request or Response did not have any
432 /// trailers, this future will resolve to the empty set of trailers once the
433 /// complete Request or Response body has been received.
434 resource future-trailers {
435
436 /// Returns a pollable which becomes ready when either the trailers have
437 /// been received, or an error has occured. When this pollable is ready,
438 /// the `get` method will return `some`.
439 subscribe: func() -> pollable;
440
441 /// Returns the contents of the trailers, or an error which occured,
442 /// once the future is ready.
443 ///
444 /// The outer `option` represents future readiness. Users can wait on this
445 /// `option` to become `some` using the `subscribe` method.
446 ///
447 /// The outer `result` is used to retrieve the trailers or error at most
448 /// once. It will be success on the first call in which the outer option
449 /// is `some`, and error on subsequent calls.
450 ///
451 /// The inner `result` represents that either the HTTP Request or Response
452 /// body, as well as any trailers, were received successfully, or that an
453 /// error occured receiving them. The optional `trailers` indicates whether
454 /// or not trailers were present in the body.
455 ///
456 /// When some `trailers` are returned by this method, the `trailers`
457 /// resource is immutable, and a child. Use of the `set`, `append`, or
458 /// `delete` methods will return an error, and the resource must be
459 /// dropped before the parent `future-trailers` is dropped.
460 get: func() -> option<result<result<option<trailers>, error-code>>>;
461 }
462
463 /// Represents an outgoing HTTP Response.
464 resource outgoing-response {
465
466 /// Construct an `outgoing-response`, with a default `status-code` of `200`.
467 /// If a different `status-code` is needed, it must be set via the
468 /// `set-status-code` method.
469 ///
470 /// * `headers` is the HTTP Headers for the Response.
471 constructor(headers: headers);
472
473 /// Get the HTTP Status Code for the Response.
474 status-code: func() -> status-code;
475
476 /// Set the HTTP Status Code for the Response. Fails if the status-code
477 /// given is not a valid http status code.
478 set-status-code: func(status-code: status-code) -> result;
479
480 /// Get the headers associated with the Request.
481 ///
482 /// The returned `headers` resource is immutable: `set`, `append`, and
483 /// `delete` operations will fail with `header-error.immutable`.
484 ///
485 /// This headers resource is a child: it must be dropped before the parent
486 /// `outgoing-request` is dropped, or its ownership is transfered to
487 /// another component by e.g. `outgoing-handler.handle`.
488 headers: func() -> headers;
489
490 /// Returns the resource corresponding to the outgoing Body for this Response.
491 ///
492 /// Returns success on the first call: the `outgoing-body` resource for
493 /// this `outgoing-response` can be retrieved at most once. Subsequent
494 /// calls will return error.
495 body: func() -> result<outgoing-body>;
496 }
497
498 /// Represents an outgoing HTTP Request or Response's Body.
499 ///
500 /// A body has both its contents - a stream of bytes - and a (possibly
501 /// empty) set of trailers, inducating the full contents of the body
502 /// have been sent. This resource represents the contents as an
503 /// `output-stream` child resource, and the completion of the body (with
504 /// optional trailers) with a static function that consumes the
505 /// `outgoing-body` resource, and ensures that the user of this interface
506 /// may not write to the body contents after the body has been finished.
507 ///
508 /// If the user code drops this resource, as opposed to calling the static
509 /// method `finish`, the implementation should treat the body as incomplete,
510 /// and that an error has occured. The implementation should propogate this
511 /// error to the HTTP protocol by whatever means it has available,
512 /// including: corrupting the body on the wire, aborting the associated
513 /// Request, or sending a late status code for the Response.
514 resource outgoing-body {
515
516 /// Returns a stream for writing the body contents.
517 ///
518 /// The returned `output-stream` is a child resource: it must be dropped
519 /// before the parent `outgoing-body` resource is dropped (or finished),
520 /// otherwise the `outgoing-body` drop or `finish` will trap.
521 ///
522 /// Returns success on the first call: the `output-stream` resource for
523 /// this `outgoing-body` may be retrieved at most once. Subsequent calls
524 /// will return error.
525 write: func() -> result<output-stream>;
526
527 /// Finalize an outgoing body, optionally providing trailers. This must be
528 /// called to signal that the response is complete. If the `outgoing-body`
529 /// is dropped without calling `outgoing-body.finalize`, the implementation
530 /// should treat the body as corrupted.
531 ///
532 /// Fails if the body's `outgoing-request` or `outgoing-response` was
533 /// constructed with a Content-Length header, and the contents written
534 /// to the body (via `write`) does not match the value given in the
535 /// Content-Length.
536 finish: static func(
537 this: outgoing-body,
538 trailers: option<trailers>
539 ) -> result<_, error-code>;
540 }
541
542 /// Represents a future which may eventaully return an incoming HTTP
543 /// Response, or an error.
544 ///
545 /// This resource is returned by the `wasi:http/outgoing-handler` interface to
546 /// provide the HTTP Response corresponding to the sent Request.
547 resource future-incoming-response {
548 /// Returns a pollable which becomes ready when either the Response has
549 /// been received, or an error has occured. When this pollable is ready,
550 /// the `get` method will return `some`.
551 subscribe: func() -> pollable;
552
553 /// Returns the incoming HTTP Response, or an error, once one is ready.
554 ///
555 /// The outer `option` represents future readiness. Users can wait on this
556 /// `option` to become `some` using the `subscribe` method.
557 ///
558 /// The outer `result` is used to retrieve the response or error at most
559 /// once. It will be success on the first call in which the outer option
560 /// is `some`, and error on subsequent calls.
561 ///
562 /// The inner `result` represents that either the incoming HTTP Response
563 /// status and headers have recieved successfully, or that an error
564 /// occured. Errors may also occur while consuming the response body,
565 /// but those will be reported by the `incoming-body` and its
566 /// `output-stream` child.
567 get: func() -> option<result<result<incoming-response, error-code>>>;
568
569 }
570 }