HTTP entry-notes, http entry notes
Http://mp.weixin.qq.com/s/czx0AHaItrJ-c49XDboIUg HTTP is an application layer protocol based on TCP/IP protocol, which specifies the communication format between the client and the server. By default, port 80 is used.
The request format for version 1.0 is as follows:
GET/HTTP/1.0 // Request command. The Protocol version (HTTP/1.0) must be added at the end)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) // describe the client status
Accept: */* // The client declares that the data format is acceptable.
Accept-Encoding: gzip, deflate // specifies which compression methods can be accepted by the client
Connection: keep-alive // The server must not close the TCP Connection, non-standard
The response format is as follows:
HTTP/1.0 200 OK // Protocol version status code status description
Content-Type: text/plain // tell the client data format
Content-Length: 139099
Expires: Thu, 05 Dec 1997 16:00:00 GMT
Last-Modified: Wed, 5 August 1996 15:55:28 GMT
Server: Apache 0.84
Content-Encoding: gzip // Data Compression Method
Connection: keep-alive // tell the client not to close the TCP Connection, non-standard
Common Content-Type values are as follows:
Text/plain
Text/html
Text/css
Image/jpeg
Image/png
Image/svg + xml
Audio/mp4
Video/mp4
Application/javascript
Application/pdf
Application/zip
Application/atom + xml
It is generally called MIME type, and each value includes a level-1 type/level-2 type
Manufacturers can customize types
You can add parameters at the end of the MIME type, as shown below:
Content-Type: text/html; charset = UTF-8 // The sent webpage code is UTF-8
MIME type can be written in the webpage as follows:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
Content-Length: 3456 // tell the browser the Length of this response, which is followed by the next response
Identifies the response of a packet and declares the length of the response.
Main disadvantage: Each TCP connection can send only one request, and the request is closed after the request is sent.
HTTP1.1
A persistent connection is introduced, that is, the TCP connection is disabled by default and can be reused by multiple requests. After a period of inactivity, the connection is automatically closed.
Standard practice: the client sends Connection: close when the last request is sent, explicitly telling the server to close the Connection
Most browsers allow six persistent connections at the same time
The MPs queue mechanism is in the same TCP connection. The client can send multiple requests at the same time.
Transfer-Encoding: chunked // indicates that the response will consist of a certain number of data blocks.
Before each non-empty data block, a hexadecimal value is used to indicate the block length. The last value is 0, indicating that the response data is sent completely.
Example:
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
3
Con
2
Wo
0
Added verb Methods: PUT, PATCH, HEAD, OPTIONS, and DELETE.
The Host field is added to the client request header.
Host: www.example.com // specify the Domain Name of the server
Disadvantage: data communication is conducted in order. If the response is particularly slow and the requests are queued up, this is "the first line is blocked"
Solution 1: Reduce the number of requests
Method 2 enable multiple persistent connections
HTTP/2
It is a binary protocol. Both the header information and the data body are binary. collectively referred to as frames, including header information frames and data frames.
Multiple workers: In a connection, the client and the browser send multiple requests or responses at the same time, instead of one-to-one matching in order. (That is, when the server receives both requests a and B, it first responds to the request and finds that the process is very time-consuming. It will send the processed part to a and then respond to the B request, before sending the remaining part of)
Each request or response packet is called a data stream with a unique ID. The data stream ID sent by the client is an odd number, and the server sends an even number.
The client can specify the priority of the data stream. The higher the priority, the sooner the server responds.
Header compression is introduced. On the one hand, header information is compressed using gzip or compress before being sent, and on the other hand, the client and the server maintain a table at the same time. All fields in the table will generate an index, send only Index Numbers
Allow resources to be actively sent to the client without request: server push)
Scenario: when a client requests a webpage that contains a lot of static resources, the server expects to request the static resources again after the client requests the page, and then actively sends these static resources along with the webpage to the client.