First, the basic concept
name meaning : The HTTP protocol is an abbreviation for the Hyper Text Transfer Protocol (Hypertext Transfer Protocol).
role : HTTP is a protocol for transmitting data (HTML files, image files, query results, etc.) based on TCP/IP communication protocol, which is used to transfer hypertext to the local browser from the World Wide Web (www:world Wide Web) server.
features :
-
Simple and fast : When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST. Each method specifies a different type of contact between the customer and the server. Because the HTTP protocol is simple, the HTTP server's program size is small, so the communication speed is fast.
-
Flexible : HTTP allows the transfer of any type of data object. The type being transmitted is marked by Content-type.
-
No connection : The meaning of no connection is to limit the processing of only one request per connection. When the server finishes processing the customer's request and receives the customer's answer, the connection is disconnected. In this way, the transmission time can be saved.
- stateless : The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capacity for transactional processing. A lack of state means that if the previous information is required for subsequent processing, it must be re-routed, which may cause the amount of data to be transferred per connection to increase. On the other hand, it responds faster when the server does not need the previous information.
- Support b/S and C/s mode. Second, message format
Third, HTTP messages
The HTTP protocol works on the client-server architecture. The browser sends all requests via URLs to the HTTP server, which is the Web servers, as an HTTP client. The Web server sends a response message to the client, based on the received request.
Request for 3.1 HTTP message
The HTTP request message consists of a request line, a request header (header), a blank line, and a four-part Request data (body).
<method><request-URL><version>
Start line
- An HTTP method, a verb (like GET, PUT, or POST) or a noun (like HEAD or OPTIONS) that describes the action to be performed. For example, get indicates that you want to get a resource, and POST represents pushing data to the server (creating or modifying a resource, or generating a temporary file to return).
- The request target, usually a URL, or an absolute path to the protocol, port, and domain name, is typically characterized by the requested environment. The requested format differs depending on the HTTP method.
- HTTP version, which defines the structure of the remaining message as an indicator of the expected response version.
Header
The HTTP headers from the request follows the same basic structure as the HTTP header: A case-insensitive string, followed by a colon (': ') and a structure that depends on the value of the header. The entire header (including values) consists of one row, which can be quite long.
- General headers, for example Via, applies to the entire message.
- Request headers, such as user-agent,accept-type, through further definition (for example, accept-language), or a given context (such as Referer), or a conditional restriction (for example, If-none) To modify the request.
- Entity headers, such as Content-length, applies to the requested body. Obviously, if there is no body in the request, such a header file will not be sent.
Body
The last part of the request is its body. Not all requests have a body: such as requests for resources, Get,head,delete and OPTIONS, usually they do not need the body. Some requests send data to the server to update the data: A common scenario is a POST request (containing HTML form data).
- Single-resource bodies, consisting of a single file. The body of the type is defined by two headers: Content-type and Content-length.
- Multiple-resource bodies, consisting of a multi-part body, each containing a different bit of information. is usually linked to HTML Forms.
Response of 3.2 HTTP messages
An HTTP response message consists of a status line, a message header, a blank line, and a response body.
<version<status><reason-phrase>
Status line
The starting line of the HTTP response is called the status line, and contains the following information:
- The protocol version, usually http/1.1.
- Status code, which indicates that the request was successful or failed. The common status code is 200,404, or 302.
- Status text. A brief, purely informational, text description of the status code that helps people understand the HTTP message.
Header
The HTTP headers of the response follows the same structure as any other header: A case-insensitive string, followed by a colon (': ') and a structure that depends on the value of the header type. The entire header, including its value, is represented as a single-line form.
There are a number of response headers available, which can be grouped into several groups:
- General headers, for example Via, applies to the entire message.
- Response headers, such as Vary and accept-ranges, provide additional information about the server that does not match the status line.
- Entity headers, such as Content-length, applies to the requested body. Obviously, if there is no body in the request, such a header file will not be sent.
Body
The last part of the response is body. Not all responses have a body: a response with a status code (such as 201 or 204), usually without a body.
Body can be broadly divided into three categories:
- Single-resource bodies, consisting of a single file of known length. The body of the type is defined by two headers: Content-type and Content-length.
- Single-resource bodies, consisting of a single file of unknown length, uses chunks encoding by setting transfer-encoding to chunked.
- Multiple-resource bodies, consisting of a multi-part body, each containing a different piece of information. But this is relatively rare.
Iv. HTTP/2
http/1.x messages have some performance drawbacks:
- The Header is not like body, it will not be compressed.
- Headers between two messages are usually very similar, but they are still repeated in a connection.
- cannot be reused. When several connections are open on the same server: a TCP hot connection is more efficient than a cold connection.
HTTP/2 introduces an additional step: it divides the http/1.x message into frames and embeds it into the stream. Data frames and header frames are detached, which allows header compression. Combining multiple streams, this is a process called multiplexing (multiplexing) that allows for more efficient underlying TCP connections.
HTTP Protocol Note