From c60c1a6bd405beff97d5bd1bafa60fdeca36602a Mon Sep 17 00:00:00 2001 From: cybai Date: Thu, 18 May 2023 18:47:19 +0900 Subject: [PATCH 1/4] Introduce `get the MIME type` for request and response Previously, the spec didn't clarify how to get the MIME type for both request and response. Thus, this patch will introduce the "get the MIME type" algorithm to clarify how to get it; then, other places which requires MIME type can reuse this algorithm. Also, with moving to use the algorithm, it will fix a potential null-pointer dereference issue in the `formData()` method because a request or a response's MIME type could potentially be null. --- fetch.bs | 146 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 64 deletions(-) diff --git a/fetch.bs b/fetch.bs index 3081acbd..9ba50718 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7138,10 +7138,6 @@ HTML, will likely not be exposed here. Rather, an HTML parser API might accept a due course. -

Objects including the {{Body}} interface mixin need to define an associated -MIME type algorithm which takes no arguments and -returns failure or a MIME type. -

Objects including the {{Body}} interface mixin have an associated body (null or a body). @@ -7177,6 +7173,26 @@ returns failure or a MIME type.


+
+

To get the MIME type, given a {{Request}} +or {{Response}} object requestOrResponse: + +

    +
  1. Let headers be null. + +

  2. If requestOrResponse is a {{Request}} object, then set headers to + requestOrResponse's request's header list. + +

  3. Otherwise, set headers to requestOrResponse's + response's header list. + +

  4. Let mimeTypeInHeaders be the result of + extracting a MIME type from headers. + +

  5. Return mimeTypeInHeaders. +

+
+

The body getter steps are to return null if this's body is null; otherwise this's body's @@ -7233,69 +7249,79 @@ of running consume body with this and the following step

The blob() method steps are to return the result of running consume body with this and the following step given a byte sequence bytes: return a {{Blob}} whose contents are bytes -and whose {{Blob/type}} attribute is this's MIME type. +and whose {{Blob/type}} attribute is to get the MIME type with this. +

The formData() method steps are to return the result of running consume body with this and the following step given a -byte sequence bytes: switch on this's MIME type's -essence and run the corresponding steps: +byte sequence bytes: -

-
"multipart/form-data", -
-
    -
  1. -

    Parse bytes, using the value of the `boundary` parameter from - this's MIME type, per the rules set forth in - Returning Values from Forms: multipart/form-data. [[!RFC7578]]

    - -

    Each part whose `Content-Disposition` header contains a `filename` - parameter must be parsed into an entry whose value is a {{File}} object - whose contents are the contents of the part. The {{File/name}} attribute of the {{File}} object - must have the value of the `filename` parameter of the part. The {{Blob/type}} - attribute of the {{File}} object must have the value of the `Content-Type` header - of the part if the part has such header, and `text/plain` (the default defined by - [[!RFC7578]] section 4.4) otherwise.

    - -

    Each part whose `Content-Disposition` header does not contain a - `filename` parameter must be parsed into an entry whose - value is the UTF-8 decoded without BOM content of the part. - This is done regardless of the presence or the value of a - `Content-Type` header and regardless of the presence or the value of a - `charset` parameter.

    - -

    A part whose `Content-Disposition` header contains a - `name` parameter whose value is `_charset_` is parsed like any other - part. It does not change the encoding.

    -
  2. - -
  3. If that fails for some reason, then throw a {{TypeError}}. - -

  4. Return a new {{FormData}} object, appending each entry, resulting - from the parsing operation, to its entry list. -

+
    +
  1. Let mimeType be the result of get the MIME type + with this. -

    The above is a rough approximation of what is needed for - `multipart/form-data`, a more detailed parsing specification is to be - written. Volunteers welcome. +

  2. +

    If mimeType is non-null, then switch on mimeType's + essence and run the corresponding steps: -

    "application/x-www-form-urlencoded", -
    -
      -
    1. Let entries be the result of parsing - bytes. +

      +
      "multipart/form-data", +
      +
        +
      1. +

        Parse bytes, using the value of the `boundary` parameter from + mimeType, per the rules set forth in + Returning Values from Forms: multipart/form-data. [[!RFC7578]]

        + +

        Each part whose `Content-Disposition` header contains a + `filename` parameter must be parsed into an entry + whose value is a {{File}} object whose contents are the contents of the part. The + {{File/name}} attribute of the {{File}} object must have the value of the + `filename` parameter of the part. The {{Blob/type}} attribute of the {{File}} + object must have the value of the `Content-Type` header of the part if the part + has such header, and `text/plain` (the default defined by [[!RFC7578]] section + 4.4) otherwise. +

        + +

        Each part whose `Content-Disposition` header does not contain a + `filename` parameter must be parsed into an entry whose + value is the UTF-8 decoded without BOM content of the + part. This is done regardless of the presence or the value of a + `Content-Type` header and regardless of the presence or the value of a + `charset` parameter.

        + +

        A part whose `Content-Disposition` header contains a + `name` parameter whose value is `_charset_` is parsed like any + other part. It does not change the encoding.

        +
      2. + +
      3. If that fails for some reason, then throw a {{TypeError}}. + +

      4. Return a new {{FormData}} object, appending each entry, + resulting from the parsing operation, to its entry list. +

      -
    2. If entries is failure, then throw a {{TypeError}}. +

      The above is a rough approximation of what is needed for + `multipart/form-data`, a more detailed parsing specification is to be + written. Volunteers welcome. -

    3. Return a new {{FormData}} object whose entry list is - entries. -

    +
    "application/x-www-form-urlencoded", +
    +
      +
    1. Let entries be the result of parsing + bytes. -

      Otherwise -

      Throw a {{TypeError}}. -

+
  • If entries is failure, then throw a {{TypeError}}. + +

  • Return a new {{FormData}} object whose entry list is + entries. + + + +

  • Throw a {{TypeError}}. +

  • @@ -7387,10 +7413,6 @@ omitted from RequestMode as it cannot be used nor obser

    A {{Request}} object has an associated signal (null or an {{AbortSignal}} object), initially null. -

    A {{Request}} object's MIME type is to return the result of -extracting a MIME type from its request's -header list. -

    A {{Request}} object's body is its request's body. @@ -8078,10 +8100,6 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire

    A {{Response}} object also has an associated headers (null or a {{Headers}} object), initially null. -

    A {{Response}} object's MIME type is to return the result of -extracting a MIME type from its response's -header list. -

    A {{Response}} object's body is its response's body. From 36d5526892feb8d79ef44ca40768609735fe9dcf Mon Sep 17 00:00:00 2001 From: cybai Date: Thu, 1 Jun 2023 23:06:05 +0900 Subject: [PATCH 2/4] Add myself (cybai) to ack --- fetch.bs | 1 + 1 file changed, 1 insertion(+) diff --git a/fetch.bs b/fetch.bs index 9ba50718..61e89a8e 100644 --- a/fetch.bs +++ b/fetch.bs @@ -8996,6 +8996,7 @@ Brad Porter, Bryan Smith, Caitlin Potter, Cameron McCormack, +白丞祐 (Cheng-You Bai), Chirag S Kumar, Chris Needham, Chris Rebert, From ded133ef6d27e6962f33428f35b42ec833b4cff7 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 2 Jun 2023 11:23:05 +0200 Subject: [PATCH 3/4] Nits: restore ID, fix indentation, remove closing tags --- fetch.bs | 98 +++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/fetch.bs b/fetch.bs index 61e89a8e..8fc038b7 100644 --- a/fetch.bs +++ b/fetch.bs @@ -7174,8 +7174,8 @@ due course.


    -

    To get the MIME type, given a {{Request}} -or {{Response}} object requestOrResponse: +

    To get the MIME type, given a {{Request}} or +{{Response}} object requestOrResponse:

    1. Let headers be null. @@ -7259,65 +7259,61 @@ running consume body with this and the following step giv byte sequence bytes:

        -
      1. Let mimeType be the result of get the MIME type - with this. +

      2. Let mimeType be the result of get the MIME type with this.

      3. If mimeType is non-null, then switch on mimeType's essence and run the corresponding steps:

        -
        "multipart/form-data", -
        -
          -
        1. -

          Parse bytes, using the value of the `boundary` parameter from - mimeType, per the rules set forth in - Returning Values from Forms: multipart/form-data. [[!RFC7578]]

          - -

          Each part whose `Content-Disposition` header contains a - `filename` parameter must be parsed into an entry - whose value is a {{File}} object whose contents are the contents of the part. The - {{File/name}} attribute of the {{File}} object must have the value of the - `filename` parameter of the part. The {{Blob/type}} attribute of the {{File}} - object must have the value of the `Content-Type` header of the part if the part - has such header, and `text/plain` (the default defined by [[!RFC7578]] section - 4.4) otherwise. -

          - -

          Each part whose `Content-Disposition` header does not contain a - `filename` parameter must be parsed into an entry whose - value is the UTF-8 decoded without BOM content of the - part. This is done regardless of the presence or the value of a - `Content-Type` header and regardless of the presence or the value of a - `charset` parameter.

          - -

          A part whose `Content-Disposition` header contains a - `name` parameter whose value is `_charset_` is parsed like any - other part. It does not change the encoding.

          -
        2. - -
        3. If that fails for some reason, then throw a {{TypeError}}. - -

        4. Return a new {{FormData}} object, appending each entry, - resulting from the parsing operation, to its entry list. -

        +
        "multipart/form-data" +
        +
          +
        1. +

          Parse bytes, using the value of the `boundary` parameter from + mimeType, per the rules set forth in + Returning Values from Forms: multipart/form-data. [[!RFC7578]] + +

          Each part whose `Content-Disposition` header contains a + `filename` parameter must be parsed into an entry whose + value is a {{File}} object whose contents are the contents of the part. The {{File/name}} + attribute of the {{File}} object must have the value of the `filename` parameter + of the part. The {{Blob/type}} attribute of the {{File}} object must have the value of the + `Content-Type` header of the part if the part has such header, and + `text/plain` (the default defined by [[!RFC7578]] section 4.4) otherwise. + +

          Each part whose `Content-Disposition` header does not contain a + `filename` parameter must be parsed into an entry whose + value is the UTF-8 decoded without BOM content of the + part. This is done regardless of the presence or the value of a + `Content-Type` header and regardless of the presence or the value of a + `charset` parameter. + +

          A part whose `Content-Disposition` header contains a + `name` parameter whose value is `_charset_` is parsed like any other + part. It does not change the encoding. + +

        2. If that fails for some reason, then throw a {{TypeError}}. + +

        3. Return a new {{FormData}} object, appending each entry, + resulting from the parsing operation, to its entry list. +

        -

        The above is a rough approximation of what is needed for - `multipart/form-data`, a more detailed parsing specification is to be - written. Volunteers welcome. +

        The above is a rough approximation of what is needed for + `multipart/form-data`, a more detailed parsing specification is to be written. + Volunteers welcome. -

        "application/x-www-form-urlencoded", -
        -
          -
        1. Let entries be the result of parsing - bytes. +

          "application/x-www-form-urlencoded" +
          +
            +
          1. Let entries be the result of parsing + bytes. -

          2. If entries is failure, then throw a {{TypeError}}. +

          3. If entries is failure, then throw a {{TypeError}}. -

          4. Return a new {{FormData}} object whose entry list is - entries. -

          +
        2. Return a new {{FormData}} object whose entry list is + entries. +

      4. Throw a {{TypeError}}. From 23a1f62d12212d5976c493073c72ca0902452011 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 2 Jun 2023 11:40:51 +0200 Subject: [PATCH 4/4] fix a few other issues --- fetch.bs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/fetch.bs b/fetch.bs index 8fc038b7..a5cec60f 100644 --- a/fetch.bs +++ b/fetch.bs @@ -3747,7 +3747,8 @@ defined here as the model defined in HTTP is not compatible with web content. [[

        To extract a MIME type -from a header list headers, run these steps: +from a header list headers, run these steps. They return failure or a +MIME type.

        1. Let charset be null. @@ -4773,7 +4774,7 @@ steps: extracting a MIME type from response's header list. -

        2. If mimeType is non-null, then set bodyInfo's +

        3. If mimeType is not failure, then set bodyInfo's content type to the result of minimizing a supported MIME type given mimeType.

        @@ -7186,10 +7187,12 @@ due course.
      5. Otherwise, set headers to requestOrResponse's response's header list. -

      6. Let mimeTypeInHeaders be the result of - extracting a MIME type from headers. +

      7. Let mimeType be the result of extracting a MIME type + from headers. + +

      8. If mimeType is failure, then return null. -

      9. Return mimeTypeInHeaders. +

      10. Return mimeType.

    @@ -7249,7 +7252,8 @@ of running consume body with this and the following step

    The blob() method steps are to return the result of running consume body with this and the following step given a byte sequence bytes: return a {{Blob}} whose contents are bytes -and whose {{Blob/type}} attribute is to get the MIME type with this. +and whose {{Blob/type}} attribute is the result of get the MIME type with +this.