What is the purpose of http2 pseudo-headers :authority & :method?

Viewed 1506

What is the purpose of http2 pseudo-headers :authority & :method ? I feel confused because :authority & :method seems to repeat the Request URL(the host) and Request Method in http 1.1

Compared to :path pseudo-headers, as explained in https://developers.google.com/web/fundamentals/performance/http2#header_compression I can see it can be used for the consecutive requests for other resource. So I suspect :authority & :method maybe an optimization for that purpose too. But I can't figure out how exactly. For example, if :authority, :method and :path are all different from the original Request URL and Request Method, shouldn't browser issue a new request?

1 Answers

The HTTP/2 :method pseudo-header is equivalent to the HTTP/1.1 request method (the first token of the HTTP/1.1 request line).

The HTTP/2 :authority pseudo-header is a stricter, mandatory, information about the host authority (i.e. host name and host port).
In HTTP/1.1, the host authority is derived from multiple sources, and may even be absent, causing a number of confusing behaviors that depend on server implementations. For example, the authority could be present in the HTTP/1.1 request target, when it is in absolute form.
If the HTTP/1.1 request target is in origin form, typically the authority is defined by the Host header. However, a HTTP/1.1 request could be of this form:

GET / HTTP/1.1\r\n
Host: \r\n
\r\n

where the Host field is empty and hence there is no authority. Requests of this type are uncommon, but technically valid and different server implementations may behave differently.
Furthermore, the authority may be overridden by the Forwarded header and its obsolete predecessors X-Forwarded-* headers, but again with fuzzy rules.

The purpose of the HTTP/2 :authority pseudo-header is to clear out the confusion about the authority so that there is a single source and no more multiple sources such as the HTTP/1.1 absolute form or Host header, etc.

Regarding the optimizations, it is true that HTTP/2 may optimize the send of the :authority information, but that is orthogonal to the purpose of the :authority pseudo-header.
The optimization works (very roughly) in this way: when a user agent (such as a browser) makes a request for a page, it is likely that it will make subsequent requests to the same authority for other, secondary, resources that are necessary to render the page (for example, CSS resources, JavaScript resources, images, etc.).
The HTTP/2 protocol indexes the :authority string, let's say at index 17 of the HPACK context, so the browser sends to the server the first request with the information :authority => (17, "veryverylongdomainname.com").
For the second request, the browser and the server now share this common information, so the browser can just send to the server :authority => 17, and the server will look up the authority from index 17, saving the send of the authority string bytes over the network. This HPACK mechanism is valid for most headers, not only for pseudo-headers. For further information please see the HPACK specification.

Browser must make a request for every resource. If they make requests for the same authority, the HPACK optimization may kick in and reduce the number of bytes over the network.
If browsers make requests for different authorities (imagine a web spider), then the authority is likely to be different in every request and the HPACK optimization will likely not kick in because the authority is always different.
In the worst case (authorities always different), HTTP/2 is as good as HTTP/1.1; but in the common case (web pages with many resources with the same authority), HTTP/2 is better than HTTP/1.1, as less bytes are sent over the network.

Related