Introducing Swift HTTP Structured Headers

I am delighted to introduce a new open source project for the Swift Server ecosystem, Swift HTTP Structured Headers. Swift HTTP Structured Headers is a brand-new package that enables the Swift HTTP ecosystem to take advantage of new work being done by the IETF HTTP Working Group.

What are HTTP Structured Headers?

HTTP Structured Header fields are a draft specification being worked on by the IETF. They provide a set of data types and algorithms for handling HTTP header field values in a consistent way, allowing a single parser and serializer to handle a wide range of header field values. Working with HTTP header fields in the absence of Structured Header Fields often requires separate parsers and serialisers for each individual field, leading to a proliferation of parsers and serializers. Structured Header Fields address this head-on, defining a common base language for defining header fields going forward.

So, what is the Swift HTTP Structured Headers package?

The Swift HTTP Structured Headers package provides a parser and serializer that match draft 19 of the working group's draft specification. They are entirely complete, able to handle all valid HTTP structured header fields. This package also provides Encoder and Decoder objects for working with Codable in Swift. This allows users to take advantage of the expressiveness of Swift for defining their header fields. The lower-level serializer and parser remain available for use-cases that require additional flexibility or have particular performance characteristics.

This package provides two top-level modules: StructuredHeaders and CodableStructuredHeaders .

The base module, StructuredHeaders , provides a low-level implementation of a serializer and parser. Both of these have been written to avoid using Foundation, making them suitable for a range of use-cases where Foundation is not available. They rely entirely on the Swift standard library and are implemented as generically as possible. One of the limitations due to the absence of Foundation is that this interface is not capable of performing Base64 encoding or decoding: users are free to bring whatever encoder and decoder they choose to use.

This API is low-level, exposing the raw parse tree as the format for the serializer and parser. This allows high-performance and high-flexibility parsing and serialization, at the cost of being verbose and complex. Users are required to understand the structured header format and to operate the slightly awkward types, but maximal fidelity is retained and the performance overhead is low.

The upper-level module, CodableStructuredHeaders, brings along the Encoder and Decoder and also adds a dependency on Foundation. This Foundation dependency is necessary to correctly handle the base64 formatting, as well as to provide a good natural container for binary data: Data . This interface is substantially friendlier and easier to work with, using Swift's Codable support to provide a great user experience.

In most cases users should prefer to use CodableStructuredHeaders unless they know they need the performance advantages or flexibility of StructuredHeaders. The experience will be much better.

Working with Structured Header Fields

swift-http-structured-headers has a simply, easy-to-use high-level API for working with structured header fields. To begin with, let's consider the HTTP Client Hints draft specification. This defines the following new header field:

The Accept-CH response header field indicates server support for the hints indicated in its value. Servers wishing to receive user agent information through Client Hints SHOULD add Accept-CH response header to their responses as early as possible.

Accept-CH is a Structured Header. Its value MUST be an sf-list whose members are tokens. Its ABNF is:

Accept-CH = sf-list

For example:

Accept-CH: Sec-CH-Example, Sec-CH-Example-2

swift-http-structured-headers can parse and serialize this field very simply:

let field = Array("Sec-CH-Example, Sec-CH-Example-2".utf8)

struct AcceptCH: StructuredHeaderField {
    static let structuredFieldType: StructuredHeaderFieldType = .list

    var items: [String]
}

// Decoding
let decoder = StructuredFieldDecoder()
let parsed = try decoder.decode(AcceptCH.self, from: field)

// Encoding
let encoder = StructuredFieldEncoder()
let serialized = try encoder.encode(AcceptCH(items: ["Sec-CH-Example", "Sec-CH-Example-2"]))

However, structured header fields can be substantially more complex. Structured header fields can make use of 4 containers and 6 base item types. The containers are:

  1. Dictionaries. These are top-level elements and associate token keys with values. The values may be items, or may be inner lists, and each value may also have parameters associated with them. CodableStructuredHeaders can model dictionaries as either Swift objects (where the property names are dictionary keys) or as dictionaries directly.
  2. Lists. These are top-level elements, providing a sequence of items or inner lists. Each item or inner list may have parameters associated with them. CodableStructuredHeaders models these as Swift objects with one key, items, that must be a collection of entries.
  3. Inner Lists. These are lists that may be sub-entries of a dictionary or a list. The list entries are items, which may have parameters associated with them: additionally, an inner list may have parameters associated with itself as well. CodableStructuredHeaders models these as either Swift arrays or, if it's important to extract parameters, as a two-field Swift struct where one field is called items and contains an array, and other field is called parameters and contains a dictionary.
  4. Parameters. Parameters associate token keys with bare items. These are used to store metadata about objects within a field. CodableStructuredHeaders models these as either Swift objects (where the property names are the parameter keys) or as Swift dictionaries.

The base types are:

  1. Booleans. CodableStructuredHeaders models these as Swift's Bool type.
  2. Integers. CodableStructuredHeaders models these as any fixed-width integer type.
  3. Decimals. CodableStructuredHeaders models these as any floating-point type, or as Foundation's Decimal .
  4. Tokens. CodableStructuredHeaders models these as Swift's String type, where the range of characters is restricted.
  5. Strings. CodableStructuredHeaders models these as Swift's String type.
  6. Binary data. CodableStructuredHeaders models this as Foundation's Data type.

For any Structured Header Field Item, the item may either be represented directly by the appropriate type, or by a Swift struct with two properties: item and parameters . This latter mode is how parameters on a given item may be captured.

The top-level structured header field must identify what kind of header field it corresponds to: .item , .list , or .dictionary . This is inherent in the type of the field and will be specified in the relevant field specification.

Lower Levels

In some cases the Codable interface will not be either performant enough or flexible enough for the intended use-case. In cases like this, users can use the types in the StructuredHeaders module instead.

There are two core types: StructuredFieldParser and StructuredFieldSerializer . Rather than work with high-level Swift objects, these two objects either produce or accept a Swift representation of the data tree for a given structured header field.

This exposes the maximum amount of information about the header field. It allows users to handle situations where Codable cannot necessarily provide the relevant information, such in cases where dictionary ordering is semantic, or where it's necessary to control whether fields are tokens or strings more closely.

The cost is that the APIs are substantially more verbose. Consider the above header field, Accept-CH . To parse or serialize this in StructuredHeaders would look like this:

let field = Array("Sec-CH-Example, Sec-CH-Example-2".utf8)
var parser = StructuredFieldParser(field)
let parsed = parser.parseListField()

print(parsed)
// [
// .item(Item(bareItem: .token("Sec-CH-Example"), parameters: [])),
// .item(Item(bareItem: .token("Sec-CH-Example-2"), parameters: [])),
// ]

var serializer = StructuredFieldSerializer()
let serialized = serializer.writeListHeader(parsed)

Notice the substantially more verbose types involved in this operation. These types are highly generic, giving the opportunity for parsing and serializing that greatly reduces the runtime overhead. They also make it easier to distinguish between tokens and strings, and to observe the order of objects in dictionaries or parameters, which can be lost at the Codable level.

In general, users should consider this API only when they are confident they need either the flexibility or the performance. This may be valuable for header fields that do not evolve often, or that are highly dynamic.

Command Line Tool

As an example of how to use the package, we also provide an example module that builds a command line tool, sh-parser . This tool is intended to allow you to dissect an example structured header field. You can get some help by using --help . An example of what using this tool looks like is here:

$ echo "Sec-CH-Example, Sec-CH-Example-2" | sh-parser --list
- list (2 entries):
    - [0]:
        - item: token Sec-CH-Example
        - parameters (0 entries):
    - [1]:
        - item: token Sec-CH-Example-2
        - parameters (0 entries):

Additional Resources

Additional documentation and examples can be found on GitHub.

Project Status

This project is in a pre-release state for two reasons. Firstly, we have a few API evolutions we’d still like to perform to make it easier to do things like merge header field lines together. Secondly, the HTTP structured header field specification is not final yet! While it’s late in the day and unlikely to evolve much at this stage, it’s entirely possible that breaking changes may come along and require us to break the API of the package itself.

Sometime shortly after the specification is finalised we will look to finalise the API as well.

Getting Involved

If you are interested in structured headers, please get involved! Swift HTTP Structured Headers is a fully open-source project, developed on GitHub. Contributions from the open source community are welcome at all times. We encourage discussion on the Swift forums. For bug reports, feature requests, and pull requests, please use the GitHub repository.

We’re very excited to see the Swift community enthusiastically adopting this new HTTP technology, and we look forward to seeing what you do with it.

44 Likes

That's a very exciting spec and it's awesome to have a high quality impl in Swift, awesome stuff Cory! :partying_face:

2 Likes

This is great work, well done Cory!

1 Like

This is awesome to see Swift building early implementations of important stuff!

2 Likes

Great to see it! I haven't had time to review the code, but in regards to module naming: if the APIs in CodableStructuredHeaders are intended to be the primary surface for the library, shouldn't that module get the nicer name? Perhaps the current StructuredHeaders could be LowLevelStructuredHeaders or something less verbose but still indicative of its intended use. That way using the obvious name, StructuredHeaders, would give users the functionality they expect.

3 Likes

Yes, it's definitely worth considering here. In many cases we suspect users won't be importing these types at all (most of them will be managed by frameworks), but certainly the naming warrants consideration.

Do you mind filing a GitHub issue?

1 Like

Sure, posted.

1 Like

Never heard about the spec but now that I did this implementation will the best place to start getting familiar with it. Great work!

Terms of Service

Privacy Policy

Cookie Policy