Unfortunately, I have been busy with my wedding recently, and I cannot guarantee timely updates on WeChat during this period. Updates will be irregular until after the New Year. If you have any issues, please leave a message, and I will respond when I see it. Thank you all for your support.
Introduction HTTP (Hypertext Transfer Protocol) is a protocol for obtaining network resources, such as retrieving an HTML page, an image, or a JavaScript file, all of which must comply with this protocol. The HTTP protocol is the foundation of data exchange on the web.
Client and Server The client is usually a browser. When a URL is entered, the browser initiates the first request to obtain the HTML document. The server receives the request, generates the corresponding HTML document, and returns it to the browser. The browser parses the returned HTML document and sends additional requests to obtain resources such as CSS files, JavaScript scripts, images, etc., based on the resource information in the document. The browser then renders the page using these resources.
Page Rendering Process
-
Parsing HTML to construct the DOM tree
-
Parsing CSS and constructing the render tree based on styles calculated from CSS selectors
-
Layout of the render tree
-
Painting the render tree
Proxy Server There may be a proxy server between the browser and the server, which primarily serves the following functions:
-
Cache functionality to improve access speed
-
Filtering (such as antivirus scanning, parental control)
-
Load balancing to allow multiple servers to handle different requests
-
Access control for different resources
-
Login, allowing storage of historical information
HTTP is Stateless and Session-Based The HTTP protocol is stateless, meaning there is no relationship between two successfully executed requests within the same connection. The server does not know that these two requests come from the same connection. To address this issue, cookies and sessions can be used to create stateful sessions, or tokens can be added to the request headers to solve this problem.
var request = new XMLHttpRequest();
request.open('GET', '', true);
request.setRequestHeader('Authorization','')
request.send();
HTTP Flow The data exchange process between a client and a server is as follows:
-
Open a TCP connection (or reuse a previous one): The TCP connection is used to send one or more requests and to receive response messages. The client may reuse an existing connection or open several new TCP connections to the server.
-
Send an HTTP message: The HTTP message (before HTTP/2) is semantically readable. In HTTP/2, these simple messages are encapsulated in frames, making them impossible to read directly, but the rules remain the same.
GET / HTTP/1.1
Host: developer.mozilla.org
Accept-Language: fr
-
Read the message returned by the server:
HTTP/1.1 200 OK
Date: Sat, 09 Oct 2010 14:28:02 GMT
Server: Apache
Last-Modified: Tue, 01 Dec 2009 20:18:22 GMT
ETag: "51142bc1-7449-479b075b2891b"
Accept-Ranges: bytes
Content-Length: 29769
Content-Type: text/html
<!DOCTYPE html... )
-
Close the connection or reuse it for future requests.
HTTP Message There are two types of HTTP messages: requests and responses. Request Message
GET / HTTP/1.1
Host: 127.0.0.1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
The first line GET / HTTP/1.1 represents the request method, resource path, and HTTP protocol version, followed by the headers.
Response Message
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 07 Sep 2015 07:37:37 GMT
Content-Type: text/html
Last-Modified: Mon, 07 Sep 2015 07:18:00 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Content-Encoding: gzip
<!DOCTYPE html... )
The first line HTTP/1.1 200 OK represents the HTTP protocol version, status code, and status message, followed by the headers. After an empty line, the response body follows.
The HTTP protocol defines many methods for interacting with the server, with the four basic ones being GET, POST, PUT, and DELETE. A URL describes a resource on the web, and the HTTP methods GET, POST, PUT, and DELETE correspond to querying, modifying, adding, and deleting that resource, respectively. The most common methods are GET and POST. GET is generally used to retrieve/query resource information, while POST is typically used to update resource information.
Issues with HTTP/1.0 The most criticized aspect of HTTP/1.0 is the inability to reuse connections and head-of-line blocking.
The inability to reuse connections directly leads to each request needing to undergo three-way handshakes and slow starts. The three-way handshake significantly affects performance under high latency, while slow start has a greater impact on large file requests. Although the Connection: Keep-Alive setting can be used to reuse connections within a short time, it still cannot handle requests with a longer time span.
Head-of-line blocking means that in HTTP/1.0, requests are processed in order, which causes subsequent requests to be affected if the previous request takes a long time or encounters an error.
HTTP/2 Protocol HTTP/2 is a binary protocol. The use of binary in HTTP/2 makes frame usage more convenient. The HTTP/2 specification defines a total of 10 different frame types, each with a unique 8-byte type code. In the establishment and management of the entire TCP connection or individual streams, each frame type serves a specific purpose, with the two most basic types corresponding to HTTP/1.1’s DATA and HEADERS.
Multiplexed Streams Each frame transmitted over an HTTP/2 connection is associated with a “stream”. Once a stream is completed, its lifecycle ends.
Each individual HTTP/2 connection can contain multiple concurrent streams, interleaving frames from both ends. Streams can be established and used unilaterally by either the client or server, shared by both parties, or closed by either side. The order of frames sent within a stream is crucial. The receiver processes frames in the order they are received.
Priority and Dependency Each stream contains a priority (or “weight”) that informs the other end which stream is more important. When resources are limited, the server will choose which streams to send first based on priority.
Header Compression In HTTP/1.1, the status line and headers are transmitted in plain text without any compression. As the number of resources requested on a page increases, the size of cookies and requests also grows, and each request typically contains nearly identical cookies, leading to unnecessary resource waste.
Reset In HTTP/1.1, once an HTTP message is sent, it is difficult to interrupt. In HTTP/2, an HTTP message can be interrupted by sending an RST_STREAM frame, thus avoiding bandwidth waste and interrupting the TCP connection (which can be done by cutting off the TCP connection to interrupt the HTTP message).
Server Push This occurs when the server proactively pushes resources to the client’s cache before the client requests them, effectively reducing the time spent on network requests when the user needs those resources.
Flow Control In HTTP/2, each stream has its own advertised flow control window, which can limit the amount of data sent by the other end. The purpose of flow control is to give the receiver full authority to control the amount of flow it wants to accept under the constraints of the initial value of the flow control window.
Reference link: https://daniel.haxx.se/http2/