Traditional Culture Encyclopedia - Traditional festivals - HTTP message

HTTP message

The HTTP protocol defines the format of the message in detail in the specification document, stipulating the components, parsing rules, and processing strategies, so it can implement more flexible and rich functions on top of the TCP/IP layer, such as connection control, cache management,

Data encoding, content negotiation, and more.

Take a TCP message as an example. It appends a 20-byte header data before the actual data to be transmitted, which stores additional information necessary for the TCP protocol, such as the sender's port number, the receiver's port number, and the packet sequence number.

Flags, etc.

With this additional TCP header, the data packet can be transmitted correctly. When it reaches the destination, remove the header and get the real data.

The HTTP protocol is similar to TCP/UDP. It also needs to append some header data before the actual transmitted data. However, unlike TCP/UDP, it is a "plain text" protocol, so the header data is all in ASCII code.

Text. The structure of the request message and response message of the HTTP protocol is basically the same, and consists of three parts: The first two parts of the starting line and the header field are often collectively referred to as the "request header" or "response header".

The message body is also called "entity", but it corresponds to "header" and is often called "body" directly.

The HTTP protocol stipulates that the message must have a header, but it may not have a body, and there must be a "blank line" after the header, which is "CRLF", "0D0A" in the sixteenth mechanism.

After understanding the basic structure of the HTTP message, let's take a look at the starting line in the request message, which is the request line. It briefly describes how the client wants to operate the server-side resources.

The request line consists of three parts: These three parts are usually separated by spaces, and finally end with a CRLF line break.

After reading the request line, let's look at the starting line in the response message. Here it is not called the "response line", but the "status line", which means the status of the server's response.

Compared with the request line, the status line is simpler and is also composed of three parts: The request line or status line plus the set of header fields constitute the complete request header or response header field in the HTTP message.

In the form of key-value, the key and value are separated by ":", and finally a CRLF line break is used to indicate the end of the field.

For example, in the line "Host: 127.0.0.1", the key is "Host" and the value is "127.0.0.1".

The HTTP header field is very flexible. Not only can you use the existing headers such as Host and Connection in the standard, but you can also add custom headers at will. This brings unlimited expansion possibilities to the HTTP protocol.