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 parsing 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.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 for it often need to be written, because each header has slightly different handling of what looks like common syntax.This document introduces structured HTTP header field values (hereafter, Structured Headers) to address these problems. Structured Headers define a generic, abstract model for header field values, along with a concrete serialisation for expressing that model in textual HTTP headers, as used by HTTP/1 and HTTP/2 .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 and parsing.Additionally, future versions of HTTP can define alternative serialisations of the abstract model of Structured Headers, 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 uses Structured Headers, see . defines a number of abstract data types that can be used in Structured Headers. Dictionaries and lists are only usable at the “top” level, while the remaining types can be specified appear at the top level or inside those structures.Those abstract types can be serialised into textual headers – such as those used in HTTP/1 and HTTP/2 – 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 .A HTTP header that uses Structured Headers need to be defined to do so explicitly; recipients and generators need to know that the requirements of this document are in effect. The simplest way to do that is by referencing this document in its definition.The field’s definition will also need to specify the field-value’s allowed syntax, in terms of the types described in , along with their associated semantics.A header field definition cannot relax or otherwise modify the requirements of this specification; doing so would preclude handling by generic software.However, header field authors are encouraged to clearly state additional constraints upon the syntax, as well as the consequences when those constraints are violated. Such additional constraints could include additional structure (e.g., a list of URLs inside a string) that cannot be expressed using the primitives defined here.For example:Note that empty header field values are not allowed by the syntax, and therefore will be considered errors.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, return the parsed header value.Discard any leading OWS from input_string.If the field-value is defined to be a dictionary, let output be the result of Parsing a Dictionary from Textual headers ().If the field-value is defined to be a list, let output be the result of Parsing a List from Text ().If the field-value is defined to be a parameterised label, let output be the result of Parsing a Parameterised Label from Textual headers ().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, throw an error.Otherwise, return output.When generating input_string for a given header field, parsers MUST combine all instances of it into one comma-separated field-value, as per ; this assures that the header is processed correctly.Note that in the case of lists and dictionaries, this has the effect of coalescing all of the values for that field. However, for singular items and parameterised labels, it will result in an error being thrown.Additionally, note that the effect of the parsing algorithms as specified is generally intolerant of syntax errors; if one is encountered, the typical response is to throw an error, thereby discarding the entire header field value. This includes any non-ASCII characters in input_string.This section defines the abstract value types that can be composed into Structured Headers, along with the textual HTTP serialisations of them.Dictionaries are unordered maps of key-value pairs, where the keys are labels () and the values are items (). There can be between 1 and 1024 members, and keys are required to be unique.In the textual HTTP serialisation, keys and values are separated by “=” (without whitespace), and key/value pairs are separated by a comma with optional whitespace. Duplicate keys MUST be considered an error.For example, a header field whose value is defined as a dictionary could look like: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.Given an ASCII string input_string, return a mapping of (label, 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 Label from Text () with input_string. If an error is encountered, throw it.If dictionary already contains this_key, throw an error.Consume a “=” from input_string; if none is present, throw an error.Let this_value be the result of running Parse Item from Text () with input_string. If an error is encountered, throw it.Add key this_key with value this_value to dictionary.If dictionary has more than 1024 members, throw an error.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, throw an error.Discard any leading OWS from input_string.Return dictionary.Lists are arrays of items () or parameterised labels (), with one to 1024 members.In the textual HTTP serialisation, each member is separated by a comma and optional whitespace.For example, a header field whose value is defined as a list of labels could look like:and a header field whose value is defined as a list of parameterised labels could look like: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. If an error is encountered, throw it.Append item to items.If items has more than 1024 members, throw an error.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, throw an error.Discard any leading OWS from input_string.Return items.Parameterised Labels are labels () with up to 256 parameters; each parameter has a label and an optional value that is an item (). Ordering between parameters is not significant, and duplicate parameters MUST be considered an error.The textual HTTP serialisation uses semicolons (“;”) to delimit the parameters from each other, and equals (“=”) to delimit the parameter name from its value.For example,Given an ASCII string input_string, return a label with an mapping of parameters. input_string is modified to remove the parsed value.Let primary_label be the result of Parsing a Label 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 Label from Text () from input_string.If param_name is already present in parameters, throw an error.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.If parameters has more than 255 members, throw an error.Add param_name to parameters with the value param_value.Return the tuple (primary_label, parameters).An item is can be a integer (), float (), string (), label () or binary content ().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, throwing any errors encountered.If the first character of input_string is a DQUOTE, process input_string as a string () and return the result, throwing any errors encountered.If the first character of input_string is “*”, process input_string as binary content () and return the result, throwing any errors encountered.If the first character of input_string is an lcalpha, process input_string as a label () and return the result, throwing any errors encountered.Otherwise, throw an error.Abstractly, 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).Parsers that encounter an integer outside the range defined above MUST throw an error. Therefore, the value “9223372036854775809” would be invalid. Likewise, values that do not conform to the ABNF above are invalid, and MUST throw an error.For example, a header whose value is defined as a integer could look like:NOTE: This algorithm parses both Integers and Floats , and returns the corresponding structure.If the first character of input_string is not “-“ or a DIGIT, throw an error.Let input_number be the result of consuming input_string up to (but not including) the first character that is not in DIGIT, “-“, and “.”.If input_number contains “.”, parse it as a floating point number and let output_number be the result.Otherwise, parse input_number as an integer and let output_number be the result.Return output_number.Abstractly, floats are integers with a fractional part. They have a maximum of fifteen digits available to be used in both of the parts, as reflected in the ABNF below; this allows them to be stored as IEEE 754 double precision numbers (binary64) ().The textual HTTP serialisation of floats allows a maximum of fifteen digits between the integer and fractional part, with at least one required on each side, along with an optional “-“ indicating negative numbers.Values that do not conform to the ABNF above are invalid, and MUST throw an error.For example, a header whose value is defined as a float could look like:See for the parsing algorithm for floats.Abstractly, strings are ASCII strings , excluding control characters (i.e., the range 0x20 to 0x7E). Note that this excludes tabs, newlines and carriage returns. They may be at most 1024 characters long.The textual HTTP serialisation of strings uses a backslash (“") to escape double quotes and backslashes in strings.For example, a header whose value is defined as a string could look like: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 generate an error.Unicode is not directly supported in Structured Headers, 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 (most likely, UTF-8).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, throw an error.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, throw an error.Else: Let next_char be the result of removing the first character of input_string.If next_char is not DQUOTE or “\”, throw an error.Append next_char to output_string.Else, if char is DQUOTE, return output_string.Else, append char to output_string.If output_string contains more than 1024 characters, throw an error.Otherwise, throw an error.Labels are short (up to 256 characters) textual identifiers; their abstract model is identical to their expression in the textual HTTP serialisation.Note that labels can only contain lowercase letters.For example, a header whose value is defined as a label could look like:Given an ASCII string input_string, return a label. input_string is modified to remove the parsed value.If the first character of input_string is not lcalpha, throw an error.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.If output_string contains more than 256 characters, throw an error.Return output_string.Arbitrary binary content up to 16K in size can be conveyed in Structured Headers.The textual HTTP serialisation indicates their presence by a leading “*”, with the data encoded using Base 64 Encoding .Parsers MUST consider encoded data that is padded an error, as “=” might be confused with the use of dictionaries). See .Likewise, parsers MUST consider encoded data that has non-zero pad bits an error. See .This specification does not relax the requirements in and 3.3; therefore, parsers MUST consider characters outside the base64 alphabet and line feeds in encoded data as errors.For example, a header whose value is defined as binary content could look like: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 “*”, throw an error.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, throw an error.Consume the “*” character at the beginning of input_string.If b64_content is has more than 21846 characters, throw an error.Let binary_content be the result of Base 64 Decoding b64_content, synthesising padding if necessary. If an error is encountered, throw it (note the requirements about recipient behaviour in ).Return binary_content.This draft has no actions for IANA.TBDHypertext 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 ArithmeticIEEEHypertext Transfer Protocol (HTTP/1.1): Semantics and ContentHypertext Transfer Protocol Version 2 (HTTP/2)Uniform Resource Identifier (URI): Generic SyntaxSplit 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