Structured Headers for HTTPFastlymnot@mnot.nethttps://www.mnot.net/The Varnish Cache Projectphk@varnish-cache.org
Applications and Real-Time
HTTPInternet-DraftThis document describes a set of data types and algorithms associated with them that are intended to make it easier and safer to define and handle HTTP header fields. It is intended for use by new specifications of HTTP header fields as well as revisions of existing header field specifications when doing so does not cause interoperability issues.RFC EDITOR: please remove this section before publicationDiscussion of this draft takes place on the HTTP 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 https://httpwg.github.io/; source code and issues list for this draft can be found at https://github.com/httpwg/http-extensions/labels/header-structure.Tests for implementations are collected at https://github.com/httpwg/structured-header-tests.Implementations are tracked at https://github.com/httpwg/wiki/wiki/Structured-Headers.Specifying the syntax of new HTTP header fields is an onerous task; even with the guidance in , there are many decisions – and pitfalls – for a prospective HTTP header field author.Once a header field is defined, bespoke parsers and serialisers often need to be written, because each header has slightly different handling of what looks like common syntax.This document introduces a set of common data structures for use in HTTP header field values to address these problems. In particular, it defines a generic, abstract model for header field values, along with a concrete serialisation for expressing that model in HTTP/1 header fields.HTTP headers that are defined as “Structured Headers” use the types defined in this specification to define their syntax and basic handling rules, thereby simplifying both their definition by specification writers and handling by implementations.Additionally, future versions of HTTP can define alternative serialisations of the abstract model of these structures, allowing headers that use it to be transmitted more efficiently without being redefined.Note that it is not a goal of this document to redefine the syntax of existing HTTP headers; the mechanisms described herein are only intended to be used with headers that explicitly opt into them.To specify a header field that is a Structured Header, see . defines a number of abstract data types that can be used in Structured Headers.Those abstract types can be serialised into and parsed from textual headers – such as those used in HTTP/1 – using the algorithms described in .The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “NOT RECOMMENDED”, “MAY”, and “OPTIONAL” in this document are to be interpreted as described in BCP 14 when, and only when, they appear in all capitals, as shown here.This document uses the Augmented Backus-Naur Form (ABNF) notation of , including the DIGIT, ALPHA and DQUOTE rules from that document. It also includes the OWS rule from .This document uses algorithms to specify parsing and serialisation behaviours, and ABNF to illustrate expected syntax.For parsing, implementations MUST follow the algorithms, but MAY vary in implementation so as the behaviours are indistinguishable from specified behaviour. If there is disagreement between the parsing algorithms and ABNF, the specified algorithms take precedence.For serialisation, the ABNF illustrates the range of acceptable wire representations with as much fidelity as possible, and the algorithms define the recommended way to produce them. Implementations MAY vary from the specified behaviour so long as the output still matches the ABNF.To define a HTTP header as a structured header, its specification needs to:Reference this specification. Recipients and generators of the header need to know that the requirements of this document are in effect.Specify the header field’s allowed syntax for values, in terms of the types described in , along with their associated semantics. Syntax definitions are encouraged to use the ABNF rules beginning with “sh-“ defined in this specification.Specify any additional constraints upon the syntax of the structured sued, as well as the consequences when those constraints are violated. When Structured Headers parsing fails, the header is discarded (see ); in most situations, header-specific constraints should do likewise.Note that a header field definition cannot relax the requirements of a structure or its processing; they can only add additional constraints, because doing so would preclude handling by generic software.For example:This specification defines minimums for the length or number of various structures supported by Structured Headers implementations. It does not specify maximum sizes in most cases, but header authors should be aware that HTTP implementations do impose various limits on the size of individual header fields, the total number of fields, and/or the size of the entire header block.This section defines the abstract value types that can be composed into Structured Headers. The ABNF provided represents the on-wire format in HTTP/1.Dictionaries are unordered maps of key-value pairs, where the keys are identifiers () and the values are items (). There can be one or more members, and keys are required to be unique.The ABNF for dictionaries is:In HTTP/1, keys and values are separated by “=” (without whitespace), and key/value pairs are separated by a comma with optional whitespace. For example:Typically, a header field specification will define the semantics of individual keys, as well as whether their presence is required or optional. Recipients MUST ignore keys that are undefined or unknown, unless the header field’s specification specifically disallows them.Parsers MUST support dictionaries containing at least 1024 key/value pairs.Lists are arrays of items () with one or more members.The ABNF for lists is:In HTTP/1, each member is separated by a comma and optional whitespace. For example, a header field whose value is defined as a list of strings could look like:Header specifications can constrain the types of individual values if necessary.Parsers MUST support lists containing at least 1024 members.Parameterised Lists are arrays of a parameterised identifiers.A parameterised identifier is an identifier () with an optional set of parameters, each parameter having a identifier and an optional value that is an item (). Ordering between parameters is not significant, and duplicate parameters MUST cause parsing to fail.The ABNF for parameterised lists is:In HTTP/1, each param-id is separated by a comma and optional whitespace (as in Lists), and the parameters are separated by semicolons. For example:Parsers MUST support parameterised lists containing at least 1024 members, and support members with at least 256 parameters.An item is can be a integer (), float (), string (), or binary content ().The ABNF for items is:Integers have a range of −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 inclusive (i.e., a 64-bit signed integer).The ABNF for integers is:For example:Floats are integers with a fractional part, that can be stored as IEEE 754 double precision numbers (binary64) ().The ABNF for floats is:For example, a header whose value is defined as a float could look like:Strings are zero or more printable ASCII characters (i.e., the range 0x20 to 0x7E). Note that this excludes tabs, newlines, carriage returns, etc.The ABNF for strings is:In HTTP/1 headers, strings are delimited with double quotes, using a backslash (“\”) to escape double quotes and backslashes. For example:Note that strings only use DQUOTE as a delimiter; single quotes do not delimit strings. Furthermore, only DQUOTE and “\” can be escaped; other sequences MUST cause parsing to fail.Unicode is not directly supported in this document, because it causes a number of interoperability issues, and – with few exceptions – header values do not require it.When it is necessary for a field value to convey non-ASCII string content, binary content () SHOULD be specified, along with a character encoding (preferably, UTF-8).Parsers MUST support strings with at least 1024 characters.Identifiers are short textual identifiers; their abstract model is identical to their expression in the textual HTTP serialisation. Parsers MUST support identifiers with at least 64 characters.The ABNF for identifiers is:Note that identifiers can only contain lowercase letters.Arbitrary binary content can be conveyed in Structured Headers.The ABNF for binary content is:In HTTP/1 headers, binary content is delimited with asterisks and encoded using base64 (). For example:Parsers MUST support binary content with at least 16384 octets after decoding.This section defines how to serialise and parse Structured Headers in HTTP/1 textual header fields, and protocols compatible with them (e.g., in HTTP/2 before HPACK is applied).Given a structured defined in this specification:If the structure is a dictionary, return the result of Serialising a Dictionary {#ser-dictionary}.If the structure is a list, return the result of Serialising a List {#ser-list}.If the structure is a parameterised list, return the result of Serialising a Parameterised List {#ser-param-list}.If the structure is an item, return the result of Serialising an Item {#ser-item}.Otherwise, fail serialisation.Given a dictionary as input:Let output be an empty string.For each member mem of input: Let name be the result of applying Serialising an Identifier to mem’s member-name.Append name to output.Append “=” to output.Let value be the result of applying Serialising an Item to mem’s member-value.Append value to output.Return output.Given a list as input:Let output be an empty string.For each member mem of input: Let value be the result of applying Serialising an Item to mem.Append value to output.If more members remain in input: Append a COMMA to output.Append a single WS to output.Return output.Given a parameterised list as input:Let output be an empty string.For each member mem of input: Let id be the result of applying Serialising an Identifier to mem’s identifier.Append id to output.For each parameter in mem’s parameters: Let name be the result of applying Serialising an Identifier to parameter’s param-name.Append name to output.If parameter has a param-value: Let value be the result of applying Serialising an Item to parameter’s param-value.Append “=” to output.Append value to output.Return output.Given an item as input:If input is a type other than an integer, float, string or binary content, fail serialisation.Let output be an empty string.If input is an integer, let value be the result of applying Serialising an Integer to input.If input is a float, let value be the result of applying Serialising a Float to input.If input is a string, let value be the result of applying Serialising a String to input.If input is binary content, let value be the result of applying Serialising Binary Content to input.Return output.Given an integer as input:If input is not an integer in the range of −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 inclusive, fail serialisation.Let output be an empty string.If input is less than (but not equal to) 0, append “-“ to output.Append input’s numeric value represented in base 10 using only decimal digits to output.Return output.Given a float as input:If input is not a IEEE 754 double precision number, fail serialisation.Let output be an empty string.If input is less than (but not equal to) 0, append “-“ to output.Append input’s integer component represented in base 10 using only decimal digits to output; if it is zero, append “0”.Append “.” to output.Append input’s decimal component represented in base 10 using only decimal digits to output; if it is zero, append “0”.Return output.Given a string as input:If input is not a sequence of characters, or contains characters outside the range allowed by the ABNF defined in , fail serialisation.Let output be an empty string.Append DQUOTE to output.For each character char in input: If char is “" or DQUOTE: Append “" to output.Append char to output, using ASCII encoding .Append DQUOTE to output.Return output.Given an identifier as input:If input is not a sequence of characters, or contains characters not allowed in , fail serialisation.Let output be an empty string.Append input to output, using ASCII encoding .Return output.Given binary content as input:If input is not a sequence of bytes, fail serialisation.Let output be an empty string.Append “*” to output.Append the result of base64-encoding input as per , taking account of the requirements below.Append “*” to output.Return output.The encoded data is required to be padded with “=”, as per . Likewise, encoded data is required to have pad bits set to zero, as per .When a receiving implementation parses textual HTTP header fields (e.g., in HTTP/1 or HTTP/2) that are known to be Structured Headers, it is important that care be taken, as there are a number of edge cases that can cause interoperability or even security problems. This section specifies the algorithm for doing so.Given an ASCII string input_string that represents the chosen header’s field-value, and header_type, one of “dictionary”, “list”, “param-list”, or “item”, return the parsed header value.Discard any leading OWS from input_string.If header_type is “dictionary”, let output be the result of Parsing a Dictionary from Text ().If header_type is “list”, let output be the result of Parsing a List from Text ().If header_type is “param-list”, let output be the result of Parsing a Parameterised List from Text ().Otherwise, let output be the result of Parsing an Item from Text ().Discard any leading OWS from input_string.If input_string is not empty, fail parsing.Otherwise, return output.When generating input_string, parsers MUST combine all instances of the target header field into one comma-separated field-value, as per ; this assures that the header is processed correctly.For Lists, Parameterised Lists and Dictionaries, this has the effect of correctly concatenating all instances of the header field.Strings can but SHOULD NOT be split across multiple header instances, because comma(s) inserted upon combination will become part of the string output by the parser.Integers, Floats and Binary Content cannot be split across multiple headers because the inserted commas will cause parsing to fail.If parsing fails – including when calling another algorithm – the entire header field’s value MUST be discarded. This is intentionally strict, to improve interoperability and safety, and specifications referencing this document cannot loosen this requirement.Note that this has the effect of discarding any header field with non-ASCII characters in input_string.Given an ASCII string input_string, return a mapping of (identifier, item). input_string is modified to remove the parsed value.Let dictionary be an empty, unordered mapping.While input_string is not empty: Let this_key be the result of running Parse Identifier from Text () with input_string.If dictionary already contains this_key, fail parsing.Consume a “=” from input_string; if none is present, fail parsing.Let this_value be the result of running Parse Item from Text () with input_string.Add key this_key with value this_value to dictionary.Discard any leading OWS from input_string.If input_string is empty, return dictionary.Consume a COMMA from input_string; if no comma is present, fail parsing.Discard any leading OWS from input_string.If input_string is empty, fail parsing.No structured data has been found; fail parsing.Given an ASCII string input_string, return a list of items. input_string is modified to remove the parsed value.Let items be an empty array.While input_string is not empty: Let item be the result of running Parse Item from Text () with input_string.Append item to items.Discard any leading OWS from input_string.If input_string is empty, return items.Consume a COMMA from input_string; if no comma is present, fail parsing.Discard any leading OWS from input_string.If input_string is empty, fail parsing.No structured data has been found; fail parsing.Given an ASCII string input_string, return a list of parameterised identifiers. input_string is modified to remove the parsed value.Let items be an empty array.While input_string is not empty: Let item be the result of running Parse Parameterised Identifier from Text () with input_string.Append item to items.Discard any leading OWS from input_string.If input_string is empty, return items.Consume a COMMA from input_string; if no comma is present, fail parsing.Discard any leading OWS from input_string.If input_string is empty, fail parsing.No structured data has been found; fail parsing.Given an ASCII string input_string, return a identifier with an mapping of parameters. input_string is modified to remove the parsed value.Let primary_identifier be the result of Parsing a Identifier from Text () from input_string.Let parameters be an empty, unordered mapping.In a loop: Discard any leading OWS from input_string.If the first character of input_string is not “;”, exit the loop.Consume a “;” character from the beginning of input_string.Discard any leading OWS from input_string.let param_name be the result of Parsing a Identifier from Text () from input_string.If param_name is already present in parameters, fail parsing.Let param_value be a null value.If the first character of input_string is “=”: Consume the “=” character at the beginning of input_string.Let param_value be the result of Parsing an Item from Text () from input_string.Insert (param_name, param_value) into parameters.Return the tuple (primary_identifier, parameters).Given an ASCII string input_string, return an item. input_string is modified to remove the parsed value.Discard any leading OWS from input_string.If the first character of input_string is a “-“ or a DIGIT, process input_string as a number () and return the result.If the first character of input_string is a DQUOTE, process input_string as a string () and return the result.If the first character of input_string is “*”, process input_string as binary content () and return the result.Otherwise, fail parsing.NOTE: This algorithm parses both Integers and Floats , and returns the corresponding structure.Let type be “integer”.Let sign be 1.Let input_number be an empty string.If the first character of input_string is “-“, remove it from input_string and set sign to -1.If input_string is empty, fail parsing.If the first character of input_string is not a DIGIT, fail parsing.While input_string is not empty: Let char be the result of removing the first character of input_string.If char is a DIGIT, append it to input_number.Else, if type is “integer” and char is “.”, append char to input_number and set type to “float”.Otherwise, fail parsing.If type is “integer” and input_number contains more than 19 characters, fail parsing.If type is “float” and input_number contains more than 16 characters, fail parsing.If type is “integer”, parse input_number as an integer and let output_number be the result.Otherwise: If the final character of input_number is “.”, fail parsing.Parse input_number as a float and let output_number be the result.Return the product of output_number and sign.Parsers that encounter an integer outside the range defined in MUST fail parsing. Therefore, the value “9223372036854775808” would be invalid. Likewise, values that do not conform to the ABNF above are invalid, and MUST fail parsing.Parsers that encounter a float that does not conform to the ABNF in MUST fail parsing.Given an ASCII string input_string, return an unquoted string. input_string is modified to remove the parsed value.Let output_string be an empty string.If the first character of input_string is not DQUOTE, fail parsing.Discard the first character of input_string.While input_string is not empty: Let char be the result of removing the first character of input_string.If char is a backslash (“\”): If input_string is now empty, fail parsing.Else: Let next_char be the result of removing the first character of input_string.If next_char is not DQUOTE or “\”, fail parsing.Append next_char to output_string.Else, if char is DQUOTE, return output_string.Else, append char to output_string.Otherwise, fail parsing.Given an ASCII string input_string, return a identifier. input_string is modified to remove the parsed value.If the first character of input_string is not lcalpha, fail parsing.Let output_string be an empty string.While input_string is not empty: Let char be the result of removing the first character of input_string.If char is not one of lcalpha, DIGIT, “_”, “-“, “*” or “/”: Prepend char to input_string.Return output_string.Append char to output_string.Return output_string.Given an ASCII string input_string, return binary content. input_string is modified to remove the parsed value.If the first character of input_string is not “*”, fail parsing.Discard the first character of input_string.Let b64_content be the result of removing content of input_string up to but not including the first instance of the character “*”. If there is not a “*” character before the end of input_string, fail parsing.Consume the “*” character at the beginning of input_string.Let binary_content be the result of Base 64 Decoding b64_content, synthesising padding if necessary (note the requirements about recipient behaviour below).Return binary_content.As per , it is RECOMMENDED that parsers reject encoded data that is not properly padded, although this might not be possible in some base64 implementations.As per , it is RECOMMENDED that parsers fail on encoded data that has non-zero pad bits, although this might not be possible in some base64 implementations.This specification does not relax the requirements in and 3.3; therefore, parsers MUST fail on characters outside the base64 alphabet, and on line feeds in encoded data.This draft has no actions for IANA.The size of most types defined by Structured Headers is not limited; as a result, extremely large header fields could be an attack vector (e.g., for resource consumption). Most HTTP implementations limit the sizes of size of individual header fields as well as the overall header block size to mitigate such attacks.It is possible for parties with the ability to inject new HTTP header fields to change the meaning of a Structured Headers. In some circumstances, this will cause parsing to fail, but it is not possible to reliably fail in all such circumstances.Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and RoutingKey words for use in RFCs to Indicate Requirement LevelsAmbiguity of Uppercase vs Lowercase in RFC 2119 Key WordsAugmented BNF for Syntax Specifications: ABNFASCII format for network interchangeThe Base16, Base32, and Base64 Data EncodingsIEEE Standard for Floating-Point ArithmeticIEEESee also http://grouper.ieee.org/groups/754/.Hypertext Transfer Protocol (HTTP/1.1): Semantics and ContentHypertext Transfer Protocol Version 2 (HTTP/2)HPACK: Header Compression for HTTP/2Reorganise specification to separate parsing out.Allow referencing specs to use ABNF.Define serialisation algorithms.Refine relationship between ABNF, parsing and serialisation algorithms.Remove identifiers from item.Remove most limits on sizes.Refine number parsing.Strengthen language around failure handling.Split Numbers into Integers and Floats.Define number parsing.Tighten up binary parsing and give it an explicit end delimiter.Clarify that mappings are unordered.Allow zero-length strings.Improve string parsing algorithm.Improve limits in algorithms.Require parsers to combine header fields before processing.Throw an error on trailing garbage.Replaced with draft-nottingham-structured-headers.Added signed 64bit integer type.Drop UTF8, and settle on BCP137 ::EmbeddedUnicodeChar for h1-unicode-string.Change h1_blob delimiter to “:” since “’” is valid t_char