Starting with today’s article, we will formally introduce the HTTP protocol. This document mainly introduces the overall structure of HTTP and related concepts, so that you can have an overall understanding of HTTP.

Communication between client and server

The HTTP protocol is a stateless request/response protocol that exchanges messages by establishing a reliable session-layer or transport-layer connection. An HTTP client is a program that connects to a server in order to send one or more HTTP requests. An HTTP server is a program that receives connections in order to send an HTTP response.

Sometimes we think of the client as a browser, but this is one-sided. In general, we use a user agent to represent any client program that initiates a request. The user agents here include but are not limited to: browsers, web crawlers, command-line tools, custom applications, mobile applications, etc.

So, in general, we call the end that requests access to resources such as text or images the client, and the end that provides the resources the server.

During HTTP communication, one end of a communication line must be a client and the other end must be a server. It is important to note, however, that the roles of the client and server may be swapped. In a connection, a program may act as a client; On another connection, the program might act as a server.

Let’s take a look at an HTTP protocol with GET request, where the content of request message and response message is as follows:

The request message

GET/HTTP/1.1 Host: 127.0.0.1:9090 Connection: keep-alive accept-language: zh-cn,zh; Q = 0.9, en. Q = 0.8Copy the code

The response message

HTTP/1.1 200 OK Connection: keep-alive Content-Length: 164 Content-Type: text/ HTML Date: Sat, 06 Jun 2020 01:53:57 GMT <html> ...Copy the code

The details of request and response messages will be covered in the next article.

A middleman

HyperText Transfer Protocol (HTTP) is translated into Chinese as HyperText Transfer Protocol. Transport means that data is passed between the client and the server, but the HTTP protocol does not specify that data can only be passed directly from the client to the server, or only from the server to the client. Instead, there can be many “middlemen” in the delivery process.

For example, the common CDN can act as a “middleman”. It exists between the client and server and provides a series of capabilities such as network acceleration, load balancing and security protection, which can effectively improve the entire transmission service.

There are three common “man-in-the-middle” forms in HTTP: proxy, gateway (reverse proxy), and tunnel. In some cases, an independent “middleman” may act as a source server, proxy, gateway, and tunnel, depending on the nature of each request.

I’ll cover source servers, proxies, gateways, and tunnels in more detail in a later article.

Stateless and persistent connections over HTTP

The stateless nature of the HTTP protocol was introduced in the previous article, so here’s a quick review. The stateless nature of the HTTP protocol means that it does not persist the states of requests and responses that have occurred. That is, the HTTP protocol cannot process this request based on the status of the previous request.

In its early days, the stateless nature of THE HTTP protocol was a major advantage because it greatly improved the efficiency of the server because it did not store state. However, with the development of The Times, in more and more scenarios, we need to save the state to facilitate the processing of the corresponding business, so the Cookie technology is introduced.

To save the status, the server sends a response packet with a header field called set-cookie. After the client obtains the packet, the client can save the Cookie. In the next request, the client will carry the saved Cookie in the request packet and send it to the server. After the server gets the Cookie and compares it, it can know which client sent the request.

summary

This article introduces the basic structure of the HTTP protocol:

1.HTTP is a transport protocol, so it must have at least two parties involved, namely the client and the server.

2. The core of HTTP is the packet it transmits. The client sends request packets and the server sends response packets.

3. The HTTP protocol starts when the client initiates a request and ends when the server sends a response. But the HTTP protocol does not allow other participants in the middle. In fact, in the transmission process, there are many “middlemen” involved in the transmission process.

4.HTTP protocol is stateless. In order to meet the needs of modern business, Cookie technology appears, which can save the state in the client.

In future articles, I will build on these points in turn. Stay tuned for the next article, which covers packets over HTTP.

The last word

Your “like” will give me a good mood, and it will be even more perfect if I can get a star.

The resources

1. The RFC 7230 documents

2. Illustrated HTTP

3. Geek Time perspective HTTP Protocol