From f7d066e730f741373328bcbd55e8e644220a4e90 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Tue, 25 Aug 2020 11:04:39 +0200 Subject: [PATCH] Allow a request body to be a byte sequence/string This simplifies things for several callers. Fixes #1073. --- fetch.bs | 76 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/fetch.bs b/fetch.bs index a317c5166..42d9c222e 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1090,11 +1090,12 @@ outlawing forbidden methods and forbidden header names.

A request has an associated -body (null or a -body). Unless stated otherwise it is null. +body (null, a byte sequence, a +scalar value string, or a body). Unless stated otherwise it is null. -

This can be updated during redirects to null as described in -HTTP fetch. +

A byte sequence and a scalar value string will +be safely extracted into a body early on in fetch. As +part of HTTP fetch it is possible for this field to be set to null due to certain redirects.


@@ -3435,6 +3436,17 @@ the request.

Run these steps, but abort when the ongoing fetch is terminated:

    +
  1. +

    If request's body is a byte sequence or a + scalar value string, then: + +

      +
    1. Let body and ignoreType be the result of + safely extracting request's body. + +

    2. Set request's body to body. +

    +
  2. If request's window is "client", set request's window to request's @@ -5007,10 +5019,9 @@ Range Requests. [[HTTP-RANGE]] However, this is not widely supported by b

  3. If bytes is failure, then terminate the ongoing fetch. -

  4. Enqueue a Uint8Array object wrapping an - ArrayBuffer containing bytes to stream. If that threw an - exception, terminate the ongoing fetch, and - error stream with that exception. +

  5. Enqueue view wrapped bytes to + stream. If that threw an exception, terminate the + ongoing fetch, and error stream with that exception.

  6. If stream doesn't need more data and request's synchronous flag is unset, ask the user agent to @@ -5659,7 +5670,7 @@ method steps are: sort and combine with this's header list. -

    Body mixin

    +

    BodyInit unions

     typedef (Blob or BufferSource or FormData or URLSearchParams or USVString) XMLHttpRequestBodyInit;
    @@ -5686,7 +5697,8 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

    To extract a body and a `Content-Type` value from -object, with an optional keepalive flag, run these steps: +object, with an optional boolean keepalive (default false), run +these steps:

    1. Let stream be the result of @@ -5700,7 +5712,7 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

    2. Let source be null.

    3. -

      Switch on object's type: +

      Switch on object:

      {{Blob}} @@ -5712,12 +5724,19 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

      Set source to object. +

      byte sequence +
      +

      Enqueue a view wrapped object to + stream and close stream. If that threw an exception, + error stream with that exception. + +

      Set source to object. +

      BufferSource
      -

      Enqueue a Uint8Array object - wrapping an ArrayBuffer containing a copy of the bytes held by object - to stream and close - stream. If that threw an exception, +

      Enqueue a view wrapped + copy of the bytes held by object to + stream and close stream. If that threw an exception, error stream with that exception.

      Set source to object. @@ -5749,7 +5768,7 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

      Set source to object. -

      USVString +
      scalar value string

      Set action to an action that runs UTF-8 encode on object. @@ -5759,7 +5778,7 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

      {{ReadableStream}}
      -

      If the keepalive flag is set, then throw a {{TypeError}}. +

      If keepalive is true, then throw a {{TypeError}}.

      If object is disturbed or locked, then throw a {{TypeError}}. @@ -5771,15 +5790,12 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

      If action is non-null, run action in parallel:

        -
      1. Whenever one or more bytes are available, let bytes be the bytes and - enqueue a Uint8Array object - wrapping an ArrayBuffer containing bytes to stream. If - creating the ArrayBuffer threw an exception, - error stream with that exception - and cancel running action. - -

      2. When running action is done, - close stream. +

      3. Whenever one or more bytes are available, let bytes be those bytes and + enqueue view wrapped bytes to stream. If + that threw an exception, error stream with that exception and + cancel running action. + +

      4. When running action is done, close stream.

    4. Let body be a body whose stream is @@ -5788,6 +5804,12 @@ typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit;

    5. Return body and Content-Type.

    +

    To view wrap a byte sequence bytes, +return a {{Uint8Array}} object wrapping an {{ArrayBuffer}} object containing bytes. +This operation can throw due to allocation failing. + + +

    Body mixin

     interface mixin Body {
    @@ -6458,7 +6480,7 @@ constructor steps are:
     
        
  7. If init["{{RequestInit/keepalive}}"] exists and is true, then set body and Content-Type to the result of extracting - init["{{RequestInit/body}}"], with the keepalive flag set. + init["{{RequestInit/body}}"], with keepalive set to true.

  8. Otherwise, set body and Content-Type to the result of extracting init["{{RequestInit/body}}"].