HTTP Development History Before summarizing http2, let's review the development history of http. The following three pictures are from Jerry Qu HTTP/0.9 (1991) HTTP/1.0 (1996) HTTP/1.1 (1999) HTTP Communication Process As we all know, HTTP is an application layer protocol based on TCP, that is, after the TCP connection is established, data is transmitted on the TCP link.
In HTTP/0.9 and HTTP/1.0, after step 3, the server will close the connection, which is TCP's four waves. However, after HTTP/1.1, the client can bring Connection: Keep-Alive in the HTTP request header when sending HTTP requests, which tells the server to keep the connection and not close TCP. When Connection: Close is sent, the server will close the connection. The communication process of HTTP2 is nothing more than this process, but the data transmitted through TCP will be different, and the behavior of the client and server has new rules. The four concepts of Connection, Stream, Message, and Frame are introduced. The relationship between them can be roughly seen from the figure below.
HTTP/2 New Features
These new features are mainly to solve previous problems. Let's compare them with the previous HTTP/1.1 to see what problems are solved. Binary framing layer Binary framing is to encapsulate http data in a specified format, similar to IP and TCP data packets. A simple Ethernet frame structure that carries HTTP2 data is drawn for easy understanding. The structure of http2 can be seen by capturing the packet with wireshark
The types in HTTP2 frames are as follows: If you want to know the detailed data structure of each type, please refer to my other article http2 frame type detailed explanation Through a diagram from Google Developers, we can better understand the location of HTTP2 framing in network data and how it differs from HTTP/1.1. The header in HTTP/1.1 becomes a HEADERS type frame, and the request body/response body becomes a DATA type frame. Through binary framing, the transmitted data is transmitted in binary format, which reduces the amount of data compared to the text format; different types of frames are used to implement flow control, server push and other functions. Multiplexing & One connection per origin We know that before HTTP2, if we want to speed up the loading of web resources, we will use the method of establishing multiple connections at the same time, but the efficiency of establishing TCP connections each time is relatively low, and browsers often limit the maximum number of connections (for example, the maximum number of connections for Chrome is 6). In addition, Pipeline was introduced in HTTP/1.1, which allows multiple requests to be sent continuously in one TCP connection without worrying about whether the previous response has arrived, but the server must respond in the order in which the requests are received. In this way, once the previous request is blocked, the subsequent requests will not be able to respond in time. In HTTP2, the use of new binary frames makes it easy to reuse a single TCP connection. Clients and servers can break HTTP messages into independent frames, send them interleaved, and then reassemble them at the other end. Still a picture from Google Developers: You can see that we can send multiple responses and requests in parallel and use the same TCP connection without any order. Each frame carries information about how to assemble them, and the client will wait until all the resources required for a certain task are ready before executing. Stream prioritization Since single connection multiplexing is possible, the frames of the server and the client are sent interleaved. For the frames sent to the server, in order to determine which ones should be processed first and which ones should be processed later, the priority of the data stream is introduced, and the server allocates resources according to the priority. For example, higher priority ones get more CPU and bandwidth resources. So how is the priority marked? Remember that there is a Type called PRIORITY in the previous frame type. This type of frame is used to tell the server the priority of this stream. In addition, the HEADERS frame also contains priority information. HTTP/2 uses parent dependencies and weights to indicate priorities. Each stream is marked with a parent stream id. If it is not marked, it is assumed to be a virtual root stream. In this way, a dependency tree is constructed according to this dependency relationship. The streams in the upper layers of the tree have higher weights. Streams in the same layer will have a weight to distinguish the resource allocation ratio. The figure above shows some examples of dependency trees, from left to right, a total of four trees.
One thing to note is that the stream priority is not a mandatory constraint. When a high-priority stream is blocked, it does not prevent the server from processing low-priority streams. Header Compression As the current website content is becoming more and more complex, the number of requests for a single page is basically dozens or even hundreds. Each request must carry the client or user's identification, such as: UA, cookie and other header data. As the number of requests increases, the traffic consumed by transmitting the http header is also very considerable, and most of the header data is the same, which is a naked waste. Therefore, header compression technology was created to save traffic.
Static dictionary The static dictionary maps the commonly used headers to shorter byte index numbers, as shown in the following figure, which intercepts the first few mappings. For all definitions, see Static Table Definition For example, when there is a field in the header: method: GET, then we can look up the table and see that it can be identified by the serial number 2, so the data of this field is 0000010 (binary representation of 2) Dynamic dictionary After all, the header data that a static dictionary can represent is limited, and the compression rate is not high. However, for a site, when interacting with a certain user, a lot of requests will occur, but the headers of each request are not much different, and there will be a lot of repeated data, because the user and browser identifiers are unchanged. Then we can generate a dynamic dictionary that can add mappings for a HTTP2 connection, so that the serial number in the dynamic dictionary can be used in subsequent connections. The process of generating a dynamic dictionary is actually to notify the other party to add a mapping. The client can notify the server to add, and vice versa. The specific notification method is to transmit data in the format specified by the protocol. Huffman Coding The characteristic of Huffman coding is that the higher the frequency of occurrence, the shorter the coding length. The HTTP2 protocol generates a canonical Huffman code based on a large number of request header data samples, which are listed in Huffman Code. Flow control The goal of HTTP/2 flow control is to give the receiving end full authority to control the amount of traffic it wants to receive at the moment, subject to the constraints of the initial value of the flow window. algorithm:
Server Push process:
question:
HTTP/2 Simple Practice Okhttp is a well-known http client in the Java ecosystem. It is easy to use, has good performance, and supports http2. Let's use this tool to practice. Since my blog has already configured http2 on nginx, I will use this blog to experiment.
Those who have used Okhttp will find that this is the same as the usual method, there is no difference, yes, there is no difference. Without saying anything else, run it and see, unfortunately you will find that the protocol is still http1.1, not h2, what's the matter? This is because HTTP2 has newly added ALPN (Application Layer Protocol Negotiation), which literally means application layer protocol negotiation, that is, the two parties discuss which protocol to use. Unfortunately, jdk8 was released in 2014, when the HTTP2 protocol was not yet born, but fortunately, ALPN can be supported through a third-party jar package. In addition, jdk9 already supports HTTP2. Although it has not been officially released, we can try JDK 9 Early-Access Builds. JDK7 and JDK8 add third-party alpn support packages by adding jvm parameters. Note that the versions cannot be wrong. JDK7 uses alpn-boot-7.*.jar and JDK8 uses alpn-boot-8.*.jar. Here is the version correspondence alpn-versions
|
>>: What happens from URL input to page display?
Introduction to video course resources for improv...
With the promotion competition so fierce today, s...
With the continuous development of the economy, mo...
If Indians don’t work harder, Mumbai will be surp...
Is the Douyin monetization mission real? Now more...
At this year's E3 exhibition, Microsoft's...
In the Internet age, everything has to be done fa...
Source code introduction Screenshot of excellent ...
As the pace of life quickens, many people are als...
Four years ago during the Spring Festival, the mo...
3,000 years ago, Chinese people began to raise fi...
Source code introduction: add tags, edit, and del...
International star hotel soft decoration design c...
Apple officially released the new generation of M...