What is HTTP?
Basic Information
HTTP (HyperText Transfer Protocol) is an application layer protocol used for distributed, collaborative, and hypermedia information systems.
Development History
The development of HTTP began in 1989, initiated by Tim Berners-Lee at the European Organization for Nuclear Research (CERN). The standardization was coordinated by the World Wide Web Consortium (W3C) and the Internet Engineering Task Force (IETF). RFC2616, published in June 1999, defined the widely used HTTP/1.1 version. In May 2015, the HTTP/2 standard was officially published as RFC 7540, replacing HTTP/1.1 as the new implementation standard.
Overview of the HTTP Protocol
HTTP is the standard for requests and responses between the client (user) and the server (website), based on TCP. The client initiates an HTTP request to the server via a user agent (such as a browser) by specifying a path/port (default port 80). The server stores resources such as HTML files and images and responds as the origin server. There may be intermediary layers such as proxy servers, gateways, or tunnels between them. Although the HTTP protocol is often based on TCP/IP, it does not mandate its use, assuming that the lower layer protocols provide reliable transmission.
How HTTP Works
The HTTP protocol uses a request/response model, defining how the client requests web pages from the server and how the server delivers the pages to the client.
Connection Process Between Client and Server
The client initiates a request, establishing a TCP connection (default port 80). After the server listens and receives the request, it returns a status (e.g., “HTTP/1.1 200 OK”) and the corresponding content (files, error messages, etc.).
Format of Request and Response Messages
The formats of request and response messages are similar, both containing a request line/status line, request headers/response headers, and a request body/response body. For example, the following are HTTP request and response messages captured using a packet capture tool:
Request Message
POST /mmtls/00005f18 HTTP/1.1
Accept: */*
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 491
Content-Type: application/octet-stream
Host: szextshort.weixin.qq.com
Upgrade: mmtls
User-Agent: MicroMessenger Client
X-Online-Host: szextshort.weixin.qq.com
File Data: 491 bytes
Response Message
HTTP/1.1 200 OK
Connection: close
Content-Type: application/octet-stream
Content-Length: 248
File Data: 248 bytes
Specific Connection Between Client and Server
Process of Client Sending Request and Receiving Response
For example, when entering “baidu.com” in the browser’s address bar, the specific process is as follows:
-
Request the DNS server to resolve the URL and obtain the IP address.
-
Establish a connection to the web server using the IP address and default port 80.
-
The server receives the request message, parses it, locates the resource, and sends a response message back to the client.
-
The client receives the response message, parses the response body, and displays the Baidu homepage.
-
Release the TCP connection.
How the Server Receives Request Messages
The server securely receives request messages through TCP’s three-way handshake.
Server Response Mechanism When the Client Requests the Same Resource
HTTP is a stateless protocol, meaning it does not save the communication state between the client and server. Each request generates a new response, and not maintaining state facilitates handling a large number of transactions, ensuring the protocol’s scalability and simplicity. However, HTTP/1.1 introduced cookie technology to manage state.
Handling of Connections After the Client Receives the Response Message
In HTTP/1.1, the connection is not immediately closed; instead, it waits a few seconds to see if the user has subsequent actions. If there is a new request, messages continue to be sent and received through the previous connection channel; if there are no new requests within a few seconds, the connection is closed to improve efficiency and reduce the number of connections established in a short time.
HTTP Request Methods and Response Status Codes
You can refer to this article for status codes, and this article for request methods.