HTTP Working Group | J. Reschke |
Internet-Draft | greenbytes |
Intended status: Standards Track | July 6, 2024 |
Expires: January 7, 2025 |
This document establishes a convention for use of JSON-encoded field values in HTTP header fields.¶
Discussion of this draft takes place on the HTTPBIS working group mailing list (ietf-http-wg@w3.org), which is archived at <https://lists.w3.org/Archives/Public/ietf-http-wg/>.¶
Working Group information can be found at <http://httpwg.github.io/>; source code and issues list for this draft can be found at <https://github.com/httpwg/http-extensions>.¶
The changes in this draft are summarized in Appendix C.¶
This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.¶
Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet-Drafts is at https://datatracker.ietf.org/drafts/current/.¶
Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as “work in progress”.¶
This Internet-Draft will expire on January 7, 2025.¶
Copyright (c) 2024 IETF Trust and the persons identified as the document authors. All rights reserved.¶
This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Revised BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Revised BSD License.¶
Defining syntax for new HTTP header fields ([RFC7230], Section 3.2) is non-trivial. Among the commonly encountered problems are: ¶
(See Section 8.3.1 of [RFC7231] for a summary of considerations for new header fields.)¶
This specification addresses the issues listed above by defining both a generic JSON-based ([RFC7159]) data model and a concrete wire format that can be used in definitions of new header fields, where the goals were:¶
In HTTP, header fields with the same field name can occur multiple times within a single message (Section 3.2.2 of [RFC7230]). When this happens, recipients are allowed to combine the field values using commas as delimiter. This rule matches nicely JSON's array format (Section 5 of [RFC7159]). Thus, the basic data model used here is the JSON array.¶
Header field definitions that need only a single value can restrict themselves to arrays of length 1, and are encouraged to define error handling in case more values are received (such as "first wins", "last wins", or "abort with fatal error message").¶
JSON arrays are mapped to field values by creating a sequence of serialized member elements, separated by commas and optionally whitespace. This is equivalent to using the full JSON array format, while leaving out the "begin-array" ('[') and "end-array" (']') delimiters.¶
The ABNF character names and classes below are used (copied from [RFC5234], Appendix B.1):
CR = %x0D ; carriage return HTAB = %x09 ; horizontal tab LF = %x0A ; line feed SP = %x20 ; space VCHAR = %x21-7E ; visible (printing) characters
Characters in JSON strings that are not allowed or discouraged in HTTP header field values — that is, not in the "VCHAR" definition — need to be represented using JSON's "backslash" escaping mechanism ([RFC7159], Section 7).¶
The control characters CR, LF, and HTAB do not appear inside JSON strings, but can be used outside (line breaks, indentation etc.). These characters need to be either stripped or replaced by space characters (ABNF "SP").¶
To map a JSON array to an HTTP header field value, process each array element separately by: ¶
To map a set of HTTP header field instances to a JSON array: ¶
The result of the parsing operation is either an error (in which case the header field values needs to be considered invalid), or a JSON array.¶
[rfc.comment.1: Explain what a definition of a new header field needs to do precisely to use this format, mention must-ignore extensibility] ¶
This JSON-based syntax will only apply to newly introduced header fields, thus backwards compatibility is not a problem. That being said, it is conceivable that there is existing code that might trip over double quotes not being used for HTTP's quoted-string syntax (Section 3.2.6 of [RFC7230]).¶
The "I-JSON Message Format" specification ([RFC7493]) addresses known JSON interoperability pain points. This specification borrows from the requirements made over there:¶
This specification requires that field values use only US-ASCII characters, and thus by definition use a subset of UTF-8 (Section 2.1 of [RFC7493]).¶
Be aware of the issues around number precision, as discussed in Section 2.2 of [RFC7493].¶
As described in Section 4 of [RFC7159], JSON parser implementations differ in the handling of duplicate object names. Therefore, senders MUST NOT use duplicate object names, and recipients SHOULD either treat field values with duplicate names as invalid (consistent with [RFC7493], Section 2.3) or use the lexically last value (consistent with [ECMA-262], Section 24.3.1.1).¶
Furthermore, ordering of object members is not significant and can not be relied upon.¶
In HTTP/1.1, header field values are represented by octet sequences, usually used to transmit ASCII characters, with restrictions on the use of certain control characters, and no associated default character encoding, nor a way to describe it ([RFC7230], Section 3.2). HTTP/2 does not change this.¶
This specification maps all characters which can cause problems to JSON escape sequences, thereby solving the HTTP header field internationalization problem.¶
Future specifications of HTTP might change to allow non-ASCII characters natively. In that case, header fields using the syntax defined by this specification would have a simple migration path (by just stopping to require escaping of non-ASCII characters).¶
Using JSON-shaped field values is believed to not introduce any new threads beyond those described in Section 12 of [RFC7159], namely the risk of recipients using the wrong tools to parse them.¶
Other than that, any syntax that makes extensions easy can be used to smuggle information through field values; however, this concern is shared with other widely used formats, such as those using parameters in the form of name/value pairs.¶
This section shows how some of the existing HTTP header fields would look like if they would use the format defined by this specification.¶
"Content-Length" is defined in Section 3.3.2 of [RFC7230], with the field value's ABNF being:¶
Content-Length = 1*DIGIT
Content-Length is restricted to a single field instance, as it doesn't use the list production (as per Section 3.2.2 of [RFC7230]). However, in practice multiple instances do occur, and the definition of the header field does indeed discuss how to handle these cases.¶
If Content-Length was defined using the JSON format discussed here, the ABNF would be something like:¶
...and the prose definition would: ¶
Content-Disposition field values, defined in [RFC6266], consist of a "disposition type" (a string), plus multiple parameters, of which at least one ("filename") sometime needs to carry non-ASCII characters.¶
Attachment; filename=example.html
has a disposition type of "Attachment", with filename parameter value "example.html". A JSON representation of this information might be:¶
{ "Attachment": { "filename" : "example.html" } }
which would translate to a header field value of:¶
{ "Attachment": { "filename" : "example.html" } }
The third example in Section 5 of [RFC6266] uses a filename parameter containing non-US-ASCII characters:¶
attachment; filename*=UTF-8''%e2%82%ac%20rates
Note that in this case, the "filename*" parameter uses the encoding defined in [RFC5987], representing a filename starting with the Unicode character U+20AC (EURO SIGN), followed by " rates". If the definition of Content-Disposition would have used the format proposed here, the workaround involving the "parameter*" syntax would not have been needed at all.¶
The JSON representation of this value could then be:¶
{ "attachment": { "filename" : "\u20AC rates" } }
The WWW-Authenticate header field value is defined in Section 4.1 of [RFC7235] as a list of "challenges":¶
WWW-Authenticate = 1#challenge
...where a challenge consists of a scheme with optional parameters:¶
challenge = auth-scheme [ 1*SP ( token68 / #auth-param ) ]
An example for a complex header field value given in the definition of the header field is:¶
Newauth realm="apps", type=1, title="Login to \"apps\"", Basic realm="simple"
(line break added for readability)
A possible JSON representation of this field value would be the array below:¶
[ { "Newauth" : { "realm": "apps", "type" : 1, "title" : "Login to \"apps\"" } }, { "Basic" : { "realm": "simple" } } ]
...which would translate to a header field value of:¶
{ "Newauth" : { "realm": "apps", "type" : 1, "title": "Login to \"apps\"" }}, { "Basic" : { "realm": "simple"}}
The Accept-Encoding header field value is defined in Section 5.3.4 of [RFC7231] as a list of codings, each of which allowing a weight parameter 'q':¶
Accept-Encoding = #( codings [ weight ] ) codings = content-coding / "identity" / "*" weight = OWS ";" OWS "q=" qvalue qvalue = ( "0" [ "." 0*3DIGIT ] ) / ( "1" [ "." 0*3("0") ] )
An example for a complex header field value given in the definition of the header field is:¶
gzip;q=1.0, identity; q=0.5, *;q=0
Due to the defaulting rules for the quality value ([RFC7231], Section 5.3.1), this could also be written as:¶
gzip, identity; q=0.5, *; q=0
A JSON representation could be:¶
[ { "gzip" : { } }, { "identity" : { "q": 0.5 } }, { "*" : { "q": 0 } } ]
...which would translate to a header field value of:¶
{"gzip": {}}, {"identity": {"q": 0.5}}, {"*": {"q": 0}}
In this example, the part about "gzip" appears unnecessarily verbose, as the value is just an empty object. A simpler notation would collapse members like these to string literals:¶
"gzip", {"identity": {"q": 0.5}}, {"*": {"q": 0}}
If this is desirable, the header field definition could allow both string literals and objects, and define that a mere string literal would be mapped to a member whose name is given by the string literal, and the value is an empty object.¶
For what it's worth, one of the most common cases for 'Accept-Encoding' would become:
"gzip", "deflate"
which would be only a small overhead over the original format.
This approach uses a default of "JSON array", using implicit array markers. An alternative would be a default of "JSON object". This would simplify the syntax for non-list-typed header fields, but all the benefits of having the same data model for both types of header fields would be gone. A hybrid approach might make sense, as long as it doesn't require any heuristics on the recipient's side.¶
[rfc.comment.2: Use of generic libs vs compactness of field values..] ¶
[rfc.comment.3: Mention potential "Key" header field extension ([KEY]).] ¶
Editorial fixes + working on the TODOs.¶
Mention slightly increased risk of smuggling information in header field values.¶
Mention Kazuho Oku's proposal for abbreviated forms.¶
Added a bit of text about the motivation for a concrete JSON subset (ack Cory Benfield).¶
Expand I18N section.¶
Mention relation to KEY header field.¶
Change to HTTP Working Group draft.¶
Added example for "Accept-Encoding" (inspired by Kazuho's feedback), showing a potential way to optimize the format when default values apply.¶
Add interop discussion, building on I-JSON and ECMA-262 (see <https://github.com/httpwg/http-extensions/issues/225>).¶
Move non-essential parts into appendix.¶
Updated XHR reference.¶
Thanks go to the Hypertext Transfer Protocol Working Group participants. ¶