HTTP 1.X/2.0

HTTP

HTTP 1.X

  1. HTTP is built on the TCP protocol, and the bottleneck and optimization of the HTTP protocol are based on the characteristics of the TCP protocol itself.

  2. There will be a delay of 1.5RTT in the three-way handshake when TCP establishes a connection. In order to avoid the delay of the handshake waiting for each request, the application layer will choose a long http connection with different strategies.

HTTP 1.0 connections cannot be reused and have head of line blocking problems.

http1.0协议头里可以设置Connection:Keep-Alive。在header里设置Keep-Alive可以在一定时间内复用连接,具体复用时间的长短可以由服务器控制,一般在15s左右。到http1.1之后Connection的默认值就是Keep-Alive,如果要关闭连接复用需要显式的设置Connection:Close。
<br />
head of line blocking会因为一个request没有到达服务器或者一个response因为网络没有及时返回而影响后续所有请求。

connection multiplexing problem

tcp long link
http long-polling

The client will send a polling request to the server in the initial state, and the server will not return the business data immediately, but will return when new business data is generated. So the connection will be maintained, and once it ends, a new polling request will be initiated, and so on.

http streaming

Unlike long-polling, the server does not end the initial streaming request, but continues to return the latest business data through this channel, but this channel is one-way.

web socket

Similar to the traditional tcp socket connection, it is also based on the tcp protocol and provides a bidirectional data channel.

解决head of line blocking

http pipelining

Let each request not wait for the response of other requests to return before sending, but send the request to the server almost at the same time.

SPDY

There are many problems in http 1.X, and the SPDY scheme proposed after trying various optimization methods.

SPDY target

  • To reduce the delay, the client's single connection and single request, and the server's FIFO response queue are all delays.
  • The original design of http is that the client initiates a request, and then the server responds. The server cannot actively push the content to the client.
  • Compress the http header, the header of http1.x is getting more and more bloated, cookies and user agent can easily increase the size of the header to 1kb or more. Moreover, due to the stateless nature of http, the header must be carried repeatedly for each request, which is a waste of traffic.

SPDY basic functions

  • Multiplexing. Multiplexing solves the problem of http 1.x hold of line blocking by sharing a tcp connection with multiple request streams, reducing latency and improving bandwidth utilization.
  • request priority. Multiplexing brings a new problem, which may cause some critical requests to be blocked on the basis of connection sharing.
  • header compression. The headers of http1.X are often redundant. Choosing an appropriate compression algorithm can reduce the size and number of packets.

SPDY Advanced Features

  • server push. http1.x can only be requested by the client, and then the server passively sends the response. After enabling server push, the server X-Associated-Content headerinforms the client that new content will be pushed.
  • server hints. Different from server push, server hint does not actively push content, but only tells that new content is generated, and the download of content still requires the client to actively initiate a request. The server hint is passed X-Subresources headerto notify.

HTTP 2.0

  • The basic pattern of the client sending a request to the server will not change.
  • The old scheme will not change, and http://和https://the services and applications used will not be changed.
  • Clients and servers using http1.x can seamlessly transfer to http2.0 through proxy
  • Proxy servers that do not recognize http2.0 can downgrade requests to http1.x

Major changes in HTTP 2.0

new binary format

http 1.x is a clear text protocol, the format is composed strat lineof , header, body. Protocol parsing is required to identify these three parts. The parsing of http1.x is based on text, and the parsing of the text format has natural defects. The binary format is more convenient and robust than the text format. <br /> The format definition of http 2.0 is closer to tcp. It consists Lengthof , Type, Flags, Stream ID, Payload5 parts.

  • lengthDefines the start to the end of the entire frame
  • typeDefine the type of frame
  • flagsSome important parameters are defined with bits
  • stream idfor flow control
  • payloadis the body of the request

connection sharing

stream idThe function is the connection sharing mechanism. A request corresponds to a stream and is assigned an id. In this way, there can be multiple streams on a connection, and the frames of each stream are randomly mixed together. The receiver assigns the frames to different requests according to the stream id. in. Priorities and dependencies can be set for each stream.

header compression

http2.0 uses encoder to reduce the size of headers to be transmitted. Both parties in the communication cache a header fields table to avoid repeated header transmission and reduce the transmission size.

Compression Algorithm Selection

SPDY/2 uses the gzip compression algorithm, BREACHand CRIMEtwo attack methods appeared later. Even SPDY of SSL can crack the content. HPACKThe compression algorithm used by http2.0.

reset connection behavior

For http 1.x, the peer end is notified to close the connection by setting the reset flag in the tcp segment. http2.0 introduced RST_STREAMthe type of frame, which can cancel the stream of a request without disconnecting the connection.

flow control

http2.0 Through a similar receive windowapproach, the receiver of the data flow windowindicates how much data it can receive by informing the other party of its size. Only frames of type Data have the flow control function.

service push

http2.0 pushes the content required by the client in advance by means of push, also called cache push. If the client exits, it needs to be canceled server push, which can be done by sending RST_STREAMa frame of type.

Nagle Algorithm/TCP Delayed Ack

Nagle Algorithm/TCP Delayed Ackis a set of opposing algorithms. http2.0 can be disabled by TCP_NODELAYdisabling Nagle or TCP_QUICKACKdisabling ACK. Officially recommended settingsTCP_NODELAY

more secure SSL

HTTP2.0 uses the extension ALPN of tls to upgrade the protocol. In addition to the encryption, there is another change. HTTP2.0 further strengthens the security of tls

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324922330&siteId=291194637