Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTTP Protocol

HTTP Protocol

HTTP (HyperText Transfer Protocol) is one of the most widely used protocols on the Internet, used to transfer hypertext (such as web pages) between clients (such as browsers) and servers.

HTTP is the foundation of the World Wide Web (WWW), supporting application scenarios such as web browsing, file downloads, and API calls.


How HTTP Works

HTTP uses a client-server model and transmits data through request-response. Its core function is that the client sends a request to the server and the server returns a response.

1. HTTP Request-Response Flow

2. HTTP Request Structure

An HTTP request consists of the following parts:

  1. Request line: Includes the request method (such as GET, POST), the requested resource (such as /index.html) and the protocol version (such as HTTP/1.1).
  2. Request headers: Contains additional information (such as Host、User-Agent、Accept)。
  3. Request body: Optional, used to transfer data (such as form data in a POST request).

Example:

GET /index.html HTTP/1.1

Host: www.example.com

User-Agent: Mozilla/5.0

Accept: text/html


3. HTTP Response Structure

An HTTP response consists of the following parts:

  1. Status line: Includes the protocol version (such as HTTP/1.1), status code (such as 200), and status message (such as OK).
  2. Response headers: Contains additional information (such as Content-Type、Content-Length)。
  3. Response body: Contains the actual data (such as HTML content).

Example:

HTTP/1.1 200 OK

Content-Type: text/html

Content-Length: 1234



...


Key Features of HTTP

  1. Stateless Protocol:

  2. Each request is independent, and the server does not save the client's state.
  3. State management is achieved through Cookie or Session.
  4. - Each request is independent, and the server does not save the client's state. - State management is achieved through Cookie or Session.
    1. Supports Multiple Request Methods:

    2. GET: Retrieve resources.
    3. POST: Submit data.
    4. PUT: Update resources.
    5. DELETE: Delete resources.
    6. - **GET**: Retrieve resources. - **POST**: Submit data. - **PUT**: Update resources. - **DELETE**: Delete resources.
      1. Supports Multiple Data Types:

      2. Pass Content-Type Specify the data type through a header (such as text/html、application/json)。
      3. - Specify the data type through the `Content-Type` header (such as `text/html`, `application/json`).
        1. Caching mechanism:

        2. Pass Cache-Control and ETag Headers enable caching to improve performance.
        3. - Implement caching through `Cache-Control` and `ETag` headers to improve performance.
          1. Extensibility:

          2. Supports custom request headers and response headers to extend functionality.
          3. - Supports custom request headers and response headers to extend functionality.

            Application Scenarios of HTTP

            HTTP is widely used in the following scenarios:


            Security Issues of HTTP

            HTTP itself is insecure because it transmits data in plaintext during transmission, making it vulnerable to the following attacks:

            1. Eavesdropping: Attackers can eavesdrop on transmitted data.
            2. Tampering: Attackers can tamper with transmitted data.
            3. Spoofing: Attackers can impersonate a server or client.

            To improve security, you can use HTTPS (HTTP Secure), i.e., HTTP over TLS/SSL, which protects data transmission through encrypted communication.


            Versions of HTTP

            HTTP has multiple versions, mainly differing in performance and features:

            1. HTTP/1.0:
            2. Each request requires establishing a new connection, resulting in poor performance.
            3. - Each request requires establishing a new connection, resulting in poor performance.
              1. HTTP/1.1:
              2. Supports persistent connections and pipelining, improving performance.
              3. - Supports persistent connections and pipelining, improving performance.
                1. HTTP/2:
                2. Supports multiplexing, binary framing, and header compression, significantly improving performance.
                3. - Supports multiplexing, binary framing, and header compression, significantly improving performance.
                  1. HTTP/3:
                  2. Based on the QUIC protocol, further optimizing performance and security.
                  3. - Based on the QUIC protocol, further optimizing performance and security.