The HTTP protocol

What is the HTTP protocol?

The Hyper Text Transfer Protocol (HTTP) is a Transfer Protocol that obtains network resources, such as texts, layout descriptions, images, and videos. HTTP is also a client-server protocol. For example, a browser serving as an HTTP client can send a request to the HTTP server (that is, the Web server) through the URL. The Web server sends response information to the client based on the received request. HTTP is at the top application layer and is transmitted over TCP or TLS, a secure TCP connection that can theoretically be used by any reliable transport protocol.

HTTP component system

The client the user-agent

User-agent is any tool that can initiate behavior for the user, usually played by the browser, although there are exceptions, such as debugging applications. The browser is responsible for making the HTTP request, further parsing the HTTP message back, and then giving the user an explicit response.

The Web server

The other end of the HTTP request mentioned above is the Web Server, which is served by the Web Server and provides the resources requested by the client. The Server simply represents a machine in a virtual sense. It can be a computer cluster consisting of a group of servers sharing load (load balancing), or it can be a complex software. By communicating to other computers (such as caches, database servers, e-commerce servers…) Make a request to obtain some or all of the resources. A Server does not have to be a single machine. There can be many servers on a single machine, and in HTTP/1.1 and host headers, they can even share an IP address.

Agent Proxies

Between the client and server, there are many computers and other devices involved in forwarding HTTP messages. Because of the Web stack hierarchy, they are mostly distributed at the transport, network, and physical layers, and they are transparent to HTTP, although they may play an important role in the application layer. Proxies can be Proxies that can be both transparent and opaque (change requests will go through them), Proxies can be Proxies at the application layer, Proxies can be Proxies that can be either transparent or opaque (change requests will go through them), Proxies can serve the following purposes:

  • Caches (either public or private, such as browser caches)
  • Filtering (e.g. anti-virus scanning, parental controls)
  • Load balancing (having multiple servers serve different requests)
  • Authentication (Permission management for different resources)
  • Log control (allows storage of historical information)

Basic properties of HTTP

  • HTTP is simple
  • HTTP is extensible
  • HTTP is unordered and sessionable
  • HTTP and connection

What can HTTP control

The extensibility of HTTP has brought more and more Web functions under its control. Caching and authentication have long been controlled by HTTP, and the same-origin restriction was not changed until 2010. Common features that can be controlled by HTTP are:

  • The cache
  • Open homology restriction
  • certification
  • Agent and tunnel
  • The session

HTTP flows

The interaction between the client and server is as follows:

  1. Open a TCP connection: TCP connections are used to send one or more requests and receive response messages. The client may open a new connection, reuse an existing connection, or open several new TCP connections to the server
  2. Send an HTTP message: AN HTTP message (prior to HTTP/2) is semantically readable. In HTTP/2, these simple messages are wrapped in frames, which make them unreadable, but the principle is the same.
  3. Read the packet returned by the server
  4. Close the TCP connection or reuse the connection for subsequent requests.

The HTTP message

There are two types of HTTP messages, request and response, each with its own specific format.

request

A request consists of the following elements:

  • Method is usually defined by a verb like GET or POST or a noun like OPTIONS or HEAD. Typically, the client-side action is to GET a resource (GET method) or send an HTML form value (POST method), although in some cases there are other actions as well.
  • The path to the resource is usually the URL of the element resource that is obvious in the context. It has no protocol, domain name, or TCP port number (HTTP is usually port 80, HTTPS is port 443).
  • HTTP version number.
  • Optional headers for the server to express additional information.
  • For some methods, such as POST, the body of the message contains the sent resource, similar to the body of the response message.

The response

The response message contains the following elements:

  • HTTP version number.
  • A status code that informs you of the success or failure of the request and the reason for the failure.
  • A status message, which is an authoritative description of the status code and can be set by the server.
  • HTTP Headers, similar to request headers.
  • Optional. The body of the obtained resource is more commonly contained in a response message than in a request message.

Http-based APIs

The most common is the XMLHttpRequest API, which is used to exchange data between the user-Agent and the server. The Fetch API provides the same functionality with a more powerful and flexible set of features. Another API, server-sent events, is a one-way service that allows the server to send events to the client using HTTP as a transport mechanism. Using the EventSource interface, the client opens the connection and establishes an event handle. The client browser automatically converts the messages arriving at the HTTP flow into the appropriate Event objects and passes them to a handle that specializes in handling events of this type, if one exists. But if the corresponding event handler is not established at all, it is passed to the onMessage event handler.