preface

HTTP name in front of er, no one knows, no one knows. Unlock your mystery today, my most familiar stranger.

HTTP

HTTP full name “HyperText Transfer Protocol”, English name: “HyperText Transfer Protocol”, is a distributed, collaborative and hypermedia information system application layer Protocol. HTTP is the basis for data communication on the World Wide Web.

In plain English, it is a set of conventions designed to publish and receive HTML pages (HTTP/0.9).

Then HTTP went viral, and the update (HTTP/1.0) got even better, allowing you to send anything (which laid the foundation for the Internet). Besides basic GET commands, POST and HEAD commands are added, which makes it possible to not only read the contents of the server through HTTP, but also modify the contents of the server! These commands are detailed below.

Because there is so much more to transfer, you need to identify the format of the transfer, so the content of the HTTP/1.0 request response is not just the data part. Each communication must include a header (HTTP header) that describes some metadata. Other new features include Status Code, multi-character set support, multi-part Type, authorization, cache, and Content encoding.

HTTP/1.0, while good, was designed to be “connectionless”, meaning that only one request could be sent per TCP connection. Once the data is sent, the connection is closed, and if additional resources are requested, a new connection must be created.

This is because of the high cost of establishing TCP connections, the need for a three-way handshake between the client and the server, and slow start. As a result, HTTP 1.0 has poor performance. The more external resources a web page loads, the more this problem becomes.

Great things are great by iterating on themselves, and HTTP/1.1 was released in January 1997, just six months after 1.0. It further refined the HTTP protocol, which is still in use 20 years later and is still the most popular version.

The biggest change in HTTP/1.1 is the introduction of persistent connections, which means TCP connections are not closed by default and can be reused by multiple requests without declaring connection: keep-alive (the browser adds it to you by default).

The client and server can close the connection if they find the other side inactive for a period of time. However, it is standard practice for the client to send Connection: close on its last request, explicitly asking the server to close the TCP Connection.

Currently, most browsers allow up to six persistent connections to the same domain name.

HTTP/1.1 also introduced pipelining, which allows clients to send multiple requests simultaneously over the same TCP connection. This further improves the efficiency of the HTTP protocol. However, most browsers turn this feature off by default (because it doesn’t solve real problems well and can cause performance problems).

HTTP/2, based on Google’s experimental project SPDY, became a standard in May 2015. The major change in HTTP/2 over HTTP/1.1 is in performance. Key words: binary frame layer, data stream, message and frame, request and response multiplexing, data stream priority, flow control, server push, header compression

Although HTTP/2 has been around for some time, and HTTP/3 is on its way, HTTP/1.1 is by far the most widely used and compatible version of the Internet.

HTTP/1.1 Request and response packets

The browser initiates the HTTP request process

  1. Build the request (Build the request line information)
  2. Lookup cache (browser cache is a technique that saves a copy locally for immediate use on the next request)
  3. The IP address and port number are available
  4. TCP queue (A maximum of six TCP connections can be established for a domain name at the same time. If 10 requests occur for a domain name at the same time, four of the requests are queued.)
  5. Establishing a TCP Connection
  6. Sending an HTTP request

HTTP requests and responses have a similar structure, consisting of the following parts

  • The starting line describes the request to be executed, or its corresponding status, success or failure. The starting line is always a single line.
  • An optional set of HTTP headers indicates the request or description message body.
  • A blank line indicates that all metadata about the request has been sent.
  • An optional body containing data related to the request, such as HTML form content, or a document related to the response. The size of the body is specified by the HTTP header of the starting line.
  • The starting line and the HTTP headers in the HTTP message are collectively referred to as the request headers, and the payload is referred to as the message body.

}

The HTTP request

The request line

An HTTP request is a message sent by a client to cause the server to perform an action. The start line contains three elements:

An HTTP method, a verb (like GET, PUT, or POST) or a noun (like HEAD or OPTIONS), describes the action to be performed. For example, GET means to fetch a resource, and POST means to push data to the server (to create or modify a resource, or to produce a temporary file to return).

A request target, usually a URL or an absolute path to a protocol, port, or domain name, is typically characterized by the requested environment. The format of the request varies with different HTTP methods. It could be:

An absolute path followed by a ‘? ‘and the query string. This is the most common form, called the Origin form, and is used by the GET, POST, HEAD and OPTIONS methods.

POST / HTTP / 1.1
Copy the code
GET /background.png HTTP / 1.0
Copy the code
HEAD /test.html? query=alibaba HTTP / 1.1
Copy the code
OPTIONS /anypage.html HTTP / 1.0
Copy the code

A complete URL, called absolute form, is used primarily when connecting to a proxy using the GET method.

GET http://developer.mozilla.org/en-US/docs/Web/HTTP/Messages HTTP / 1.1
Copy the code

The Authority Component of a URL that consists of a domain name and an optional port prefixed with a ‘:’ is called the Authority Form. Used only when using CONNECT to establish an HTTP tunnel.

CONNECT developer.mozilla.org:80 HTTP / 1.1
Copy the code

The asterisk form, a simple asterisk (‘*’) used with the OPTIONS method, represents the entire server.

OPTIONS * HTTP / 1.1
Copy the code

HTTP version, which defines the structure of the remaining message as an indicator of the expected response version.

Request header

HTTP Headers from the request follows the same basic structure as HTTP headers: a case-insensitive string followed by a colon (‘:’), and a structure that depends on the value of the header. The entire header (including the value) consists of a single line, which can be quite long.

There are many request headers available, which can be grouped into several groups:

General headers, such as Via, applies to the entire packet.

Request headers, such as user-agent or accept-type, are defined further (such as accept-language) or given a context (such as Referer). Or modify the request with a conditional restriction (such as if-None).

Entity Headers, such as Content-Length, applies to the body of the request. Obviously, if there is no body in the request, no such header file will be sent.

Example:

Request body

The last part of the request is its body. Not all requests have a body: for example, resource requests, GET, HEAD, DELETE, and OPTIONS, usually do not require a body. Some requests send data to the server to update it: a common case is a POST request (containing HTML form data).

Request bodies can be broadly divided into two categories:

Single-resource bodies, consisting of a Single file. The body Type is defined by two headers: content-type and Content-Length.

Multiple-resource bodies, consisting of Multiple body parts, each containing different bits of information. It is usually associated with HTML Forms.

The HTTP response

Response line

The starting line of the HTTP response is called a status line and contains the following information:

Protocol version, usually HTTP/1.1. Status code: indicates whether the request succeeded or failed. Common status codes are 200,404, or 302. Status text. A short, pure piece of information that helps people understand the HTTP message through a textual description of the status code. A typical status line would look like this: HTTP/1.1 404 Not Found.

Response headers

The HTTP headers for 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, appears as a single line.

There are many response headers available, which can be grouped into several groups:

General headers, such as Via, applies to the entire packet.

Response Headers, such as Vary and Accept-ranges, provide other information about the server that does not match the status line.

Entity Headers, such as Content-Length, applies to the body of the request. Obviously, if there is no body in the request, no such header file will be sent.

Example:

Response body

The final part of the response is the body. Not all responses have a body: Responses with a status code (such as 201 or 204) usually do not.

The response body can be roughly divided into three categories:

Single-resource bodies, consisting of Single files of known length. The body Type is defined by two headers: content-type and Content-Length.

Single-resource bodies, consisting of Single files of unknown length, use chunk-encoding by setting transfer-Encoding to chunked.

Multiple-resource bodies, consisting of Multiple body parts, each containing different information segments. But this is relatively rare.

Common HTTP request modes

The serial number methods describe note
1 The GET ⭐ Requests the specified page information and returns the entity body. Power etc.
2 POST ⭐ Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources. The power etc.
3 PUT ⭐ Data transferred from the client to the server replaces the contents of the specified document. Power etc.
4 PATCH ⭐ Is a complement to the PUT method and is used to perform local updates on known resources. The power etc.
5 The DELETE ⭐ Asks the server to delete the specified page. Power etc.
6 HEAD Similar to a GET request, except that there is no concrete content in the response returned, which is used to retrieve the header Power etc.
7 CONNECT Reserved in HTTP/1.1 for proxy servers that can pipe connections. The power etc.
8 OPTIONS Allows clients to view server performance. Power etc.
9 TRACE The command output displays the requests received by the server for testing or diagnosis. Power etc.

HTTP response code

HTTP status code Classification The HTTP status code consists of three decimal digits. The first decimal digit defines the type of the status code, and the last two digits do not classify the status code. HTTP status codes are classified into five types:

HTTP status code classification

classification Classification description
1 * * Message, the server receives the request and requires the requester to proceed with the operation
2 * * Success, the operation is received and processed successfully
3 * * Redirect, requiring further action to complete the request
4 * * Client error, request contains syntax error or request cannot be completed
5 * * Server error. The server encountered an error while processing the request

HTTP status code list:

Status code Status code English name Product description
100 Continue To continue. The client should continue with its request
101 Switching Protocols Switch protocol. The server switches protocols based on client requests. You can only switch to a more advanced protocol, for example, the new version of HTTP
200 ⭐ OK The request succeeded. Typically used for GET and POST requests
201 Created Has been created. The new resource was successfully requested and created
202 Accepted Has been accepted. The request has been accepted, but processing is not complete
203 Non-Authoritative Information Unauthorized information. The request succeeded. The meta information returned is not the original server, but a copy
204 ⭐ No Content No content. The server processed successfully, but did not return content. You can ensure that the browser continues to display the current document without updating the web page
205 Reset Content Reset the content. The server is successful, and the user end (for example, browser) should reset the document view. Use this return code to clear the browser’s form field
206 ⭐ Partial Content Part of the content. The server successfully processed some of the GET requests
300 Multiple Choices A variety of options. The requested resource can include multiple locations, and a list of resource characteristics and addresses can be returned for user terminal (e.g., browser) selection
301 ⭐ Moved Permanently Permanently move. The requested resource has been permanently moved to the new URI, the return message will include the new URI, and the browser will automatically redirect to the new URI. Any future new requests should be replaced with a new URI
302 ⭐ Found Temporary move. Similar to 301. But resources are moved only temporarily. The client should continue to use the original URI
303 ⭐ See Other Look at other addresses. Similar to 301. Use GET and POST requests to view
304 ⭐ Not Modified Unmodified. The requested resource is not modified, and the server does not return any resources when it returns this status code. Clients typically cache accessed resources by providing a header indicating that the client wants to return only resources that have been modified after a specified date
305 Use Proxy Use a proxy. The requested resource must be accessed through a proxy
306 Unused An invalid HTTP status code
307 ⭐ Temporary Redirect Temporary redirect. Similar to 302. Use GET to request redirection
400 ⭐ Bad Request Client request syntax error, server cannot understand
401 ⭐ Unauthorized The request requires user authentication
402 Payment Required Reserved for future use
403 ⭐ Forbidden The server understands the request from the requesting client, but refuses to execute the request
404 ⭐ Not Found The server could not find the resource (web page) based on the client’s request. With this code, a web designer can set up a personalized page that says “the resource you requested could not be found.
405 Method Not Allowed The HTTP method used is outside the permissions
406 Not Acceptable The server could not complete the request based on the content nature of the client request
407 Proxy Authentication Required The request requires the identity of the broker, similar to the 401, but the requester should use the broker for authorization
408 Request Time-out The server waited for a request sent by the client for a long time and timed out. Procedure
409 Conflict This code may be returned when the server completes a PUT request from the client, and a conflict occurred when the server processed the request
410 Gone The resource requested by the client does not exist. 410 differs from 404 in that if a resource previously had a 410 code that is now permanently deleted, the site designer can specify a new location for the resource through the 301 code
411 Length Required The server cannot process the content-length message sent by the client
412 Precondition Failed A prerequisite error occurred when the client requested information
413 Request Entity Too Large The request was rejected because the requested entity was too large for the server to process. To prevent continuous requests from clients, the server may close the connection. If the server is temporarily unable to process it, a retry-after response is included
414 Request-URI Too Large The request URI is too long (usually a url) for the server to process
415 ⭐ Unsupported Media Type The client requested that the format returned be unsupported, such as JSON, and the server could not process the media format attached to the request
416 Requested range not satisfiable The scope requested by the client is invalid
417 Expectation Failed The server cannot satisfy Expect’s request headers
429 Too Many Requests The number of client requests exceeds the upper limit. Procedure
500 ⭐ Internal Server Error The server had an internal error and could not complete the request
501 Not Implemented The server did not support the requested functionality and could not complete the request
502 Bad Gateway The server working as a gateway or proxy received an invalid response from the remote server when attempting to execute the request
503 ⭐ Service Unavailable The server is temporarily unable to process client requests due to overloading or system maintenance. The length of the delay can be included in the server’s retry-after header
504 Gateway Time-out The server acting as a gateway or proxy did not get the request from the remote server in time
505 HTTP Version not supported The server did not support the HTTP version of the request and could not complete the processing