1. The difference between HTTP1 and HTTP1.1
1.1 Persistent Connection
HTTP1.1 supports long connections and requestsassembly lineProcessing, in oneTCPMultiple HTTP requests and responses can be transmitted on the connection, reducing the consumption and delay of establishing and closing connections. Keep-alive is enabled by default in HTTP1.1, which makes up for the disadvantage of creating a connection every time HTTP1.0 requests. HTTP 1.0 requires the keep-alive parameter to inform the server that it wants to establish a long connection.
1.2 Save bandwidth
There are some waste of bandwidth in HTTP 1.0. For example, the client only needs part of a certain object, but the server sends the entire object over and does not support the breakpoint continuous transmission function. HTTP1.1 supports sending onlyheaderInformation (without any body information). If the server believes that the client has permission to request the server, it returns 100. The client only starts sending the request body to the server after receiving 100. If it returns 401, the client can save bandwidth without sending the request body.
1.3 HOST domain
In HTTP 1.0, each server is believed to be bound to a unique IP address, so the URL in the request message does not pass the host name (hostname), and HTTP 1.0 does not have a host domain. along withVirtual HostWith the development of technology, multiple virtual hosts (Multi-homed Web Servers) can exist on a physical server, and they share an IP address. Both the request and response messages of HTTP1.1 support the host domain, and if there is no host domain in the request message, an error will be reported (400 Bad Request).
1.4 Cache processing
In HTTP 1.0, if-Modified-Since and Expires in the header are mainly used as the standard for cache judgment. HTTP 1.1 introduces more cache control strategies such as Entity tag, If-Unmodified-Since, If-Match, If-None-Match and other more optional cache headers to control cache policies.
1.5 Management of error notifications
24 new error status response codes have been added in HTTP 1.1. For example, 409 (Conflict) means that the requested resource conflicts with the current status of the resource; 410 (Gone) means that a resource on the server is permanently deleted.
The persistent connection and pipeline mechanism of HTTP/1.1 allows multiple TCP connections to be reused. In a TCP connection, multiple requests can be sent at the same time, but all data communications are completed in order. The server will only process the next response after processing one response. For example, the client needs two resources A and B. The pipeline mechanism allows the browser to issue A request and B request at the same time, but the server still responds to A request first and then responds to B request after completion. In this way, if the previous response is particularly slow, there will be many requests waiting in line later. This is called "Head-of-line blocking"
2. The difference between HTTP1.1 and HTTP2
2.1 Binary Framing
First of all, HTTP/2 believes that plaintext transmission is too troublesome for machines and is inconvenient for computer parsing, because there will be ambiguity characters for text, such as whether carriage return or line break is content or separator, and the state machine is needed to identify internally, which is relatively inefficient. So HTTP/2 simply replaces all messages into binary format and transmits them all.01
Strings facilitate machine analysis.
turn out to beHeaders + Body
The message format is now split into binary frames, usingHeaders framesStore header fields,Data framesStore requested volume data. After frame-up, the server no longer sees complete HTTP request packets, but a bunch of out-of-order binary frames. These binary frames do not have a sequence relationship, so they will not wait in line, and there will be no problem of head-of-team blocking in HTTP.
2.2 Multiplexing
Both parties in the communication can send binary frames to the other party. This bidirectional transmission sequence of binary frames is also called a stream. HTTP/2 uses streams to communicate multiple data frames on a TCP connection, which is the concept of multiplexing.
You may have a question: Since it is the out-of-order first, how to deal with these out-of-order data frames in the end?
The first thing to declare is that the so-called out-of-order means that Streams with different IDs are out-of-order, but frames of the same Stream ID must be transmitted sequentially. After the binary frame arrives, the other party will assemble the binary frame with the same Stream ID into a complete request message and response message.
2.3 Header data compression
In the era of HTTP/1.1 and before,Request bodyThere will generally be a responsive compression coding process, throughContent-Encoding
The header field is specified, but have you ever thought about the compression of the header field itself? When the request field is very complex, especially for GET requests, the request packets are almost all request headers, and there is still a lot of room for optimization at this time. HTTP/2 also uses corresponding compression for header fieldsalgorithm——HPACK, compress the request header.
The HPACK algorithm is specially used for HTTP/2, and its main highlights are two:
-
First, create a hash table between the server and the client, and store the used fields in this table. Therefore, when transmitting, you only need to put the values that have appeared before.index(For example, 0, 1, 2,…) is just passed to the other party, and the other party just needs to get the index lookup table. ThisPass the indexThe method can be said to allow the request header field to be greatly reduced and reused.
HTTP/2 abolishes the concept of starting line and converts the request method, URI, and status code in the starting line into header fields. However, these fields have a ":" prefix to distinguish them from other request headers.
-
Secondly, for integers and stringsHuffman codeThe principle of Huffman encoding is to first create an index table for all the characters that appear, and then make the index corresponding to the characters with many occurrences as short as possible. This is also the same when transmitting.Index sequence, can achieve very high compression rates.
2.4 Server push
Server push is a mechanism to send data before the client requests. Web pages use many resources: HTML, style sheets, scripts, pictures, and more. In HTTP 1.1, each of these resources must be requested explicitly. This is a very slow process. The browser starts by getting HTML and then incrementally obtains more resources as it parses and evaluates the page. Because the server has to wait for the browser to make every request, the network is often idle and underused.
To improve latency, HTTP 2.0 has introducedserverpush, which allows the server to push resources to the browser, preventing the client from creating a connection again before the browser makes a clear request to the server to obtain. In this way, the client can load these resources directly from the local area without having to go through the network