This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Related articles:
- Have you really figured out HTTP? (a)
- Have you really figured out HTTP? (2)
- Have you really figured out HTTP? (3)
What are the differences between Http1.0/1.1/2.0?
1.1 HTTP1.0
The second version of the HTTP protocol. The first VERSION of the HTTP protocol to specify a version number in a communication
The HTTP 1.0 browser maintains only a brief connection to the server, and each request requires a TCP connection to the server
The server disconnects the TCP connection as soon as it finishes processing the request. The server does not keep track of each client or record past requests
In short, each time you interact with the server, you need to open a new connection
For example, parse an HTML file and create a separate link when a resource file is found in the file
As a result, access to an HTML file consists of multiple requests and responses, with each request requiring the creation of a connection, a relational connection
This form obviously creates a performance flaw
To establish a persistent Connection, you need to set a non-standard Connection field: Connection: keep-alive
1.2 HTTP1.1
In HTTP1.1, keep-alive connections are supported by default, which means that multiple HTTP requests and responses can be transmitted over a TCP Connection, reducing the cost and latency of establishing and closing connections
A connection is established and multiple requests are made through this connection
This allows multiple requests and responses from the HTML file to be transferred over a single connection when the file is loaded
HTTP 1.1 also allows a client request results back don’t have to wait for the last time, you can send out the next request, but the server must be in order to receive the order of the client request of echo response as a result, to ensure that the client can distinguish between the response content of each request, it also significantly reduce the time required to download the entire process
At the same time, HTTP1.1 on the basis of HTTP1.0, add more request headers and response headers to improve the function, as follows:
- More cache control policies have been introduced, such as if-unmodified-since, if-match, if-none-match and other cache headers to control cache policies
- Introduce range, which allows values to request a part of the resource
- By introducing host, multiple virtual WEB sites can be created using different host names on the same IP address and port number on a WEB server
It also adds other request methods: put, DELETE, options…
1.3 HTTP2.0
HTTP2.0 offers significant performance improvements over previous versions, such as the addition of a feature:
- multiplexing
- Binary framing
- The first compression
- Server push
multiplexing
HTTP/2 multiplexes TCP connections. Within a connection, both the client and browser can send multiple requests or responses at the same time, and they don’t have to be sequentially addressed, thus avoiding “queue head congestion.”
Binary framing
A frame is the smallest unit of information in HTTP2 communication
HTTP/2 transmits data in a binary format, rather than the text format of HTTP 1.x, which is more efficient to parse
The request and response data is split into smaller frames, and they are binary encoded
In HTTP2, all communication with a domain name is done on a single connection, which can host any number of two-way data streams
Each data stream is sent as a message, which in turn consists of one or more frames. Multiple frames can be sent out of order and can be reassembled according to the stream identifier at the beginning of the frame. This is also a condition for multiplexing to send data simultaneously
The first compression
HTTP/2 uses “header tables” on both the client and server to track and store previously sent key-value pairs. The same data is no longer sent on each request and response
The header table exists for the lifetime of the HTTP/2 connection and is updated incrementally by both the client and the server
For example, in the following two requests, request one sends all header fields, and request two sends only differential data, which reduces redundant data and overhead
Server push
HTTP2 introduces server push, which allows servers to push resources to clients
The server will push some of the required resources to the client in passing, such as in response to a page request, along with other resources on the page
Lest the client create a connection again to send the request to the server
This is a great way to load static resources
1.4 summarize
HTTP1.0:
- The connection between the browser and the server is only temporary, and each request of the browser needs to establish a TCP connection with the server
HTTP1.1:
- Persistent connections were introduced, meaning that TCP connections are not closed by default and can be reused by multiple requests
- Within the same TCP connection, a client can send multiple requests simultaneously
- Although the multiplexing of TCP connections is allowed, all data communications within the same TCP connection proceed sequentially, with the server processing one request before proceeding to the next. If processing up front is particularly slow, there will be a lot of requests waiting in the queue
- Some request methods have been added
- Some request and response headers have been added
HTTP2.0:
- Use binary format instead of text format
- Full multiplexing, rather than ordered and blocked parallelism with only one connection
- Use header compression to reduce overhead
- Server push
What are the common HTTP status codes and their applicable scenarios?
2.1 what is
HTTP Status Code (HTTP Status Code), a three-digit Code used to indicate the Status of a Web server’s hypertext transfer protocol response
It is defined by RFC 2616 and extended by RFC 2518, RFC 2817, RFC 2295, RFC 2774 and RFC 4918
In simple terms, the HTTP status code is used by the server to tell the client the status of the current request and response. The status code can be used to determine and analyze the running status of the server
2.2 classification
The first digit of the status code determines the different response states, as follows:
- 1: message
- 2: Successful
- 3 indicates redirection
- 4 indicates that the request is incorrect
- 5 indicates that the server is faulty
1xx
The representative request has been accepted and needs to continue processing. This type of response is a temporary response that contains only the status line and some optional response header information and ends with a blank line
Common examples are:
- 100 (Client continues to send request, this is temporary response) : This temporary response is used to inform the client that part of its request has been received by the server and has not yet been rejected. The client should continue sending the rest of the request, or ignore the response if the request has completed. The server must send a final response to the client after the request is completed
- 101: The server switches protocols based on client requests, mainly for WebSocket or HTTP2 upgrades
2xx
Indicates that the request is successfully received, understood, and accepted by the server
Common examples are:
- 200 (Success) : The request was successful and the desired response header or body is returned with this response
- 201 (created) : The request was successful and the server created a new resource
- 202 (created) : The server has received the request but has not yet processed it
- 203 (unauthorized information) : The server successfully processed the request, but the information returned may have come from another source
- 204 (no content) : The server successfully processed the request but did not return any content
- 205 (reset content) : The server successfully processed the request but did not return anything
- 206 (partial) : The server successfully processed part of the request
3xx
Indicates that further action is required to complete the request. Typically, these status codes are used for redirection
Common examples are:
- 300 (Multiple options) : The server can perform multiple operations on a request. The server can select an operation based on the requester (User Agent) or provide a list of operations for the requester to select
- 301 (Permanently moved) : The requested page has been permanently moved to a new location. When the server returns this response (a response to a GET or HEAD request), the requester is automatically forwarded to the new location
- 302 (Temporary mobility) : The server is currently responding to requests from a web page in a different location, but the requester should continue to use the original location for future requests
- 303 (Look at other locations) : The server returns this code when the requester should use a separate GET request for a different location to retrieve the response
- 305 (Using a proxy) : The requester can only use the proxy to access the requested web page. If the server returns this response, it also indicates that the requester should use a proxy
- 307 (Temporary redirects) : The server is currently responding to requests from a web page in a different location, but the requester should continue to use the original location for future requests
4xx
Represents an error that appears to have occurred on the client, hindering server processing
Common examples are:
- 400 (Error request) : The server does not understand the request syntax
- 401 (unauthorized) : Request requires authentication. The server may return this response for a web page that requires login.
- 403 (Forbidden) : The server rejects the request
- 404 (not found) : The server could not find the requested page
- 405 (Method disabled) : Disables the method specified in the request
- 406 (not accepted) : Could not respond to the requested web page using the requested content feature
- 407 (Proxy authorization required) : This status code is similar to 401 (unauthorized), except that the requester specifies that the proxy should be authorized
- 408 (Request timeout) : A timeout occurs while the server is waiting for a request
5xx
Indicates that the server was unable to complete an obviously valid request. This type of status code indicates that an error or exception occurred while the server was processing the request
Common examples are:
- 500 (server internal error) : The server encountered an error and was unable to complete the request
- 501 (not implemented) : The server does not have the capability to complete the request. For example, the server might return this code when it cannot recognize the request method
- 502 (Error gateway) : The server acting as a gateway or proxy received an invalid response from the upstream server
- 503 (Service unavailable) : The server is currently unavailable (due to overloading or down maintenance)
- 504 (Gateway timeout) : The server acts as a gateway or proxy, but does not receive the request in time from the upstream server
- 505 (HTTP version not supported) : The server does not support the HTTP protocol version used in the request
2.3 Application Scenarios
Here are some application scenarios for status codes:
- 100: Before sending THE POST data to the server, the client consults the server to check whether the server processes the POST data. If the server does not process the POST data, the client does not upload the POST data. If the client processes the POST data, the POST uploads the data. It is used for POST big data transmission
- 206: Generally used to do breakpoint continuation, or video files and other large file loading
- 301: Permanent redirects are cached. The new domain name replaces the old domain name. If the old domain name is no longer used, users can use 301 when accessing the old domain name to redirect to the new domain name
- 302: Temporary redirection is not cached. It is used when an unlogged user accesses the user center and redirects to the login page
- 304: Negotiate the cache, tell the client there is a cache, directly use the cache data, return only the header information, is not the content of the page
- 400: The parameter is incorrect and the request cannot be recognized by the server
- 403: Tells the client to access the site or resource in base mode, for example, in the Extranet environment, and then returns when only the Intranet IP address is available
- 404: When the server cannot find the resource, or when the server rejects the request and does not want to give a reason
- 503: When the server is down for maintenance, 503 is used to respond to requests or nginx to set the rate limit. If the rate limit is exceeded, 503 is returned
- 504: The gateway times out