Skip to content

HTTP/2 Main Points

This blog is the reading notes for the book HTTP/2 in Action. It requires the knowledge of HTTP/1.1 as I will only show the drawbacks of HTTP/1.1 the HTTP/2 intends to solve.

The book itself is nice, and cover all the main features with details. The only problem is the epub book has lost several diagrams and the contents of frames occupied a few pages while reading in my Kindle:(

Drawback of HTTP/1.1

If the old one is perfect, no efforts should be taken to conceive a newer protocol. But everything has its flaw because nobody could foresee problems introduced by the future thrives. HTTP/1.1 has some problems, of course, and the main issue is the performance.

The fundamental performance problem is latency caused by single request/response model. (TODO: check whether it's true) Pipeline is introduced but head-of-line(HOL) blocking occurs.

Further improvements such as using multiple connections or larger HTTP requests are involved, however, they solved the old problem but introduce new troubles.

The problem of multiple connections is its inefficiency, from handshake latency and bandwidth issue. TCP and potential TLS handshakes require a few RTTs, and no priority causes the TCP timeouts and retransmissions on other connections. It's a workaround under HTTP/1.1 rather a good solution. Fewer but larger requests thwarts the user-side cache and duplication in server files.

The other issues, like the large text-based protocol packets, security issue(not integrating HTTPS) and lacking states(addressing less successfully by addition of cookie) exist in HTTP/1.1 as well.

In short, the drawbacks motivate to conceive a new protocol to solve them, for a faster and more robust network in the real world.

SPDY: Predecessor of HTTP/2

Roman wasn't built in one day. Without a successful experiment to demonstrate it improves the deficiencies of current solution, there is hardly a mature standard proposal. Before HTTP/2, SPDY proposed by Google has been verified, due to its unique position of being in control of both a major browser (Chrome) and some of the most popular websites.

Some ideas of the SPDY are derives into HTTP/2, like binary protocol, multiplexing, request prioritization and header compression.

New Features of HTTP/2

HTTP/2 proposed several unique features than HTTP/1.1, and all of them aim to improve the efficiency of HTTP protocol.

  • binary protocol
  • multiplexing
  • flow control on application level
  • header compression
  • server push
  • HTTPS-based(in practice)

HTTP/2 Connection Establish

Similar with HTTP/2, TCP connection is required. However, in practice, HTTP/2 is supported based on HTTPS only. There are several ways to establish HTTP/2 connection:

  • HTTPS negotiate
  • HTTP upgrade header
  • Use prior knowledge

The client wants to use HTTP/2 tries to send an ALPN in ClientHello to establish an HTTP/2 connection. Another way is to use HTTP upgrade header and it fits for the unencrypted messages only. If the client request carries the Upgrade header, the server should treat it as an HTTP/2 request.

However, Upgrading header might cause the inconsistency between headers and body format, which fails to decode the request from server side for the proxy case.

HTTP/2 Preface Message

The client sends the preface message as the first message to confirm the server understands the HTTP/2. It's a nonsense message and encoded in octets, and the plaintext is listed below:

# message in octets
0x505249202a20485454502f322e300d0a0d0a534d0d0a0d0a

# message in plaintext
PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n

If the server fails to recognize the preface message, it responses an error. Otherwise, it will return a SETTINGS frame to client that client should send an ACK.

HTTP/2 Connection, Stream and Frame

Connection is not a new concept, and it refers to the TCP/TLS connection. Stream and Frame are the new concepts introduced to support multiplexing, as the multiplexing requires multiple requests share the same connection.

img.png

The stream performs at the request level in HTTP/2, while the frame is the transportation in the application layer. Because we need to use stream to distinguish different requests while using frame to pack contents of different requests.

HTTP/2 Frames

Each of frame is made of a fixed-size header, followed by a payload:

+-----------------------------------------------------------+
|                    Frame Header Format                    |
+========+=======+=======+==============+===================+
| Length | Type  | Flags | Reserved Bit | Stream Identifier |
+--------+-------+-------+--------------+-------------------+
| 24bits | 8bits | 8bits | 1bit         | 31bits            |
+--------+-------+-------+--------------+-------------------+

The length refers to the length of the frame without the headers, and currently there are 14 types, such as DATA, HEADER, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE and so on. The stream identifier will never be re-used in the same connection.