Summary of HTTP protocol knowledge points

Time:2021-12-31

HTTP protocol

HTTP is a protocol that can obtain network resources such as HTML。 It is the basis of data exchange on the web. It is a client server protocol. In other words, requests are usually initiated by recipients such as browsers.

HTTP was designed in the early 1990s and is an extensible protocol. It is an application layer protocol, which is sent through TCP or TLS encrypted TCP connection. Theoretically, any reliable transmission protocol can be used. Because of its good scalability, today, it is not only used to transmit hypertext documents, but also used to transmit pictures, videos or send information such as HTML forms to the server. HTTP can also get only part of the web document content and update the web page according to the web page requirements.

Forms: submitting data

Interaction: http protocol

Client large front end request ——- >

< ——- response server backend

There are two types of HTTP messages:Request message and response message。 As the name suggests, the request message is the signal that the client sends the request to the server, and the response message is the signal that the server sends back to the client after response processing.

2.1.Request message

The HTTP request message consists of four parts:Request line, request header, blank line and request bodyThe blank line is also one of the components, which is used to separate and is essential.

2.1.1.Request line

The first line is the request line, which is composed of three fields: request method, URI and HTTP protocol version. They are separated by spaces. Finally, the content is divided by carriage return and line feed, Indicates that the next content (starting from the next line) is not the content of the request line. For example, get / index.php http / 1.1, get is the request method, / index.php is the URL, and HTTP / 1.1 indicates that the HTTP version used is 1.1.

This line is easy to understand. Only there are many types of request methods, such as get, post, head, put, delete, options, trace and connect. Get and post are the most commonly used. They are described in detail here.

GET

The most common request method of get is usually used when the client wants to read a document from the server, such as clicking a link on the web page or browsing the web page by entering the web address in the address bar of the browser. The get method is used. It should be noted that when using the get method to request, the request parameters and corresponding values follow the URL through the question mark (“?”) And and connectors (“&”) and “=” Number connection. The get method requires the server to put the resource located by the URL in the data part of the response message and send it back to the client. For example, / index php? Id = 100 & slec = yes, so the data passed through get is directly represented in the address. Formally, because the parameters and values to be transmitted already exist in the URL, the HTTP message requested by the get method has no request body.

Obviously, the get method exposes the parameters and parameter values in the URL. The advantages are obvious, but the disadvantages are not conducive to the confidentiality and security of the data. In addition, browsers have restrictions on the character length of the address. Although their respective standards are different, they generally do not exceed 1024 characters. Therefore, if the amount of data to be transmitted is relatively large, the get method should not be used.

POST

Post is also a very common request method, and perfectly makes up for the deficiency of get method. Because the post method encapsulates the data to be transmitted in the request body of the message and has no limit on the size of the transmitted data, it can transmit a large amount of data. Because the data is encapsulated in the request body and is not directly exposed in the URL, it is more private and secure.

-Other request methods

Other request methods are rarely used. The head method is occasionally used. After receiving the head request, the server only returns the response header without sending the response content, which improves the transmission efficiency. Therefore, when we only need to view the status of a page, using head is very efficient.

2.1.2.Request header

The request header consists of key / value pairs, each line is a pair, and the key and value are separated by colon “: (English). The request header tells the server that there is all information about the client request. Typical request headers are:

User agent: generate the requested user agent information (browser information): Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_12_3) applewebkit / 537.36, etc;

Accept: content types recognized by the client. Columns: text / HTML, application / XHTML + XML, application / XML;

Accept language: natural language acceptable to the client – zh CN, Zh; q=0.8,en; q=0.6,id; q=0.4;

Accept encoding: the encoding compression format acceptable to the client – gzip, deflate, sdch, br

Host: the requested host name, which allows multiple domain names to share one IP address, that is, the virtual host

Connection: there are two connection modes: close and keep alive.

Close: tell the web server or proxy server to disconnect after completing the response of this request

Keep alive: tell the web server or proxy server. After completing the response of this request, keep connected to wait for subsequent requests

Cookie: stored in the client extension field, send a cookie belonging to the domain to the server of the same domain name – PSTM = 1490844191; BIDUPSID=2145FF54639208435F60E1E165379255;

2.1.3.Blank line

The user performs content segmentation, which means that the request header ends here, and the content of the next line is no longer the request header.

2.1.4.Request body

The request body contains the request data. As mentioned above, when the get method is used, there is no request body.

2.2.response message

After the client sends a request to the server, the server will normally return an HTTP response message, which is the response message, after receiving and processing the request sent by the client.

The HTTP response also consists of four parts:Status line, message header, blank line, and response body。 Formally, except for the status line, the other three parts are similar to the request message.

2.2.1.Status line

Format: http version status code reason phase CRLF. Where HTTP version represents the version of the Server HTTP protocol; Status code indicates the response status code sent back by the server; Reason Phrase represents the text description of the status code. The status code consists of three digits. The first digit defines the category of response, and there are five possible values.

1XX: instruction information — indicates that the request has been received and continues processing.

2XX: successful — indicates that the request has been successfully received, understood and accepted.

3xx: redirection — further operations are required to complete the request.

4xx: client error — the request has syntax error or the request cannot be implemented.

5xx: server side error — the server failed to implement the legal request.

Descriptions of common status codes and status descriptions of various types are as follows:

  • 200 OK: the client request succeeded
  • 301 permanent movement. The requested resource has been permanently moved to the new URI, the returned information will include the new URI, and the browser will automatically direct to the new URI. Any new request in the future should be replaced by a new URI
  • 400 bad request: the client request has syntax error and cannot be understood by the server
  • 401 unauthorized: the request is unauthorized. This status code must be used with the WWW authenticate header field
  • 403 Forbidden: the server received the request but refused to provide the service
  • 404 not found: the requested resource does not exist. For example, the wrong URL was entered
  • 500 Internal Server Error: unexpected error occurred in the server
  • 501 the server does not support the requested function and cannot complete the request
  • 503 server unavailable: the server cannot process the client’s request at present. It may return to normal after a period of time. For example, http / 1.1 200 OK (CRLF)
2.2. 2. Response head

Similar to the request header of the request message, the response header is also composed of key value pairs, one pair for each line, and the keys and values are separated by English colon:. The response header field allows the server to pass additional information that cannot be placed on the status line. These fields mainly describe the information of the server and further information of the request URI. Typical response headers are:

  • Server: contains the software information of the original server processing the request;
  • Date: server date;
  • Content type: the returned resource type (MIME);
  • Connection: connection mode;
  • Close: the connection has been closed;
  • Keep alive: the connection has been maintained and is waiting for subsequent requests for this connection;
  • Cache control: cache control;
  • Expires: set the expiration time;
  • Set Cookie: set cookie information.
2.2.3.Blank line

As a content segmentation, it means that the following is no longer the content of the response header.

2.2.4.Responder

This is the response information returned by the server to the browser.