Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


🍊 HTTP message

HTTP messages are the way data is exchanged between the server and the client. There are two types of messages: requests — sent by clients to trigger an action on a server; Responses — responses from the server.

HTTP messages consist of multiple lines of text encoded in ASCII. In HTTP/1.1 and earlier versions, these messages were sent openly over a connection. In HTTP/2, messages that were once human-readable were split into multiple HTTP frames for optimization and performance improvements.

Web developers or webmasters rarely create these raw HTTP messages by hand themselves: by software, browsers, proxies, or servers. They serve HTTP messages through configuration files (for proxy servers or servers), apis (for browsers), or other interfaces.

The HTTP/2 binary framework mechanism is designed to be applied without changing any API or configuration files: it is largely transparent to the user.

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

  1. The starting line describes the request to be executed, or its corresponding status, success or failure. The starting line is always a single line.
  2. An optional set of HTTP headers indicates the request or description message body.
  3. A blank line indicates that all metadata about the request has been sent.
  4. 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.


🏳 ️ 🌈 HTTP requests

The starting line

The HTTP request is made by the client and contains three elements of the start line for the server to perform the action: 1. An HTTP method, a verb (like GET, PUT, or POST), or a noun (like HEAD or OPTIONS) describing the action to perform. For example, GET gets resources, and POST sends data to the server (to create or create resources, or to produce temporary files to return).

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

  • One absolute path, followed by ‘? ‘and the query string. This is the most common form, the original form (the original form), used by the GET, POST, HEAD, and OPTIONS methods.

PNG HTTP/1.0 HEAD /test.html? Query =alibaba HTTP/1.1 OPTIONS /anypage.html HTTP/1.0

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

The GET developer.mozilla.org/en-US/docs/… HTTP / 1.1

  • The authorization of a URL that consists of a domain name and an optional port prefixed with a ‘:’ is called the authority form. This is used when only CONNECT is used to establish an HTTP tunnel.

CONNECT to developer.mozilla.org: 80 HTTP / 1.1

  • The asterisk form (asterisk form), a simple asterisk (‘*’), is used with the OPTIONS method to represent the entire server.

The OPTIONS * HTTP / 1.1

3. HTTP Version: defines the structure of the destination packet as an indicator of the response version to the request.

Headers

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.

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).

Bodies can be roughly 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.