Three handshake and four waves (HTTP request process)

Popular explanation: https://blog.csdn.net/mi2shao/article/details/99707415?depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1&utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH -1

1. HTTP request and response steps

The above completely shows the 7 steps of HTTP request and response. Let's understand how HTTP request and response are transmitted from the perspective of TCP / IP protocol model.

2. TCP / IP protocol

The TCP / IP protocol model (Transmission Control Protocol / Internet Protocol) contains a series of network protocols that form the basis of the Internet. It is the core protocol of the Internet and is widely used in wide area networks and local area networks.

The HTTP protocol is based on the TCP / IP protocol model to transmit information

(1). Link layer

Also known as the data link layer or network interface layer (the network interface layer and the hardware layer in the first figure), it usually includes the device driver in the operating system and the corresponding network interface card in the computer. Together they handle the details of the physical interface with the cable (or any other transmission medium). ARP (Address Resolution Protocol) and RARP (Reverse Address Resolution Protocol) are special protocols used by some network interfaces (such as Ethernet and Token Ring) to convert the addresses used by the IP layer and the network interface layer.

(2). Network layer

Also known as the Internet layer (the Internet layer in the first diagram), it handles the activities of packets in the network, such as the routing of packets. In the TCP / IP protocol family, the network layer protocols include IP protocol (Internet Protocol), ICMP protocol (Internet Internet Control Message Protocol), and IGMP protocol (Internet Group Management Protocol).

IP is a network layer protocol that provides an unreliable service. It just sends packets from the source node to the destination node as quickly as possible, but does not provide any reliability guarantee. Used by both TCP and UDP. Each set of data for TCP and UDP is transmitted over the Internet through the end system and the IP layer in each intermediate router.

ICMP is a subsidiary protocol of the IP protocol. The IP layer uses it to exchange error messages and other important information with other hosts or routers.

IGMP is an Internet group management protocol. It is used to multicast a UDP datagram to multiple hosts.

(3). Transport layer

Mainly provides end-to-end communication for applications on two hosts. In the TCP / IP protocol family, there are two different transmission protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).

TCP provides high reliability data communication for two hosts. The work it does includes dividing the data handed over to it by the application into suitable small blocks and handing it to the underlying network layer, confirming the received packets, and setting the timeout clock for sending the last confirmation packet. Since the transport layer provides highly reliable end-to-end communication, the application layer can ignore all these details. In order to provide reliable services, TCP uses mechanisms such as timeout retransmission, sending and receiving end-to-end acknowledgement packets.

UDP provides a very simple service for the application layer. It just sends packets called datagrams from one host to another, but it does not guarantee that the datagram can reach the other end. A datagram refers to an information unit transmitted from the sender to the receiver (for example, a certain number of bytes of information specified by the sender). Any necessary reliability of the UDP protocol must be provided by the application layer.

(4). Application layer

The application layer determines the communication activities when providing application services to users. Various general application services are pre-stored in the TCP / IP protocol family. Including HTTP, FTP (File Transfer Protocol, file transfer protocol), DNS (Domain Name System, domain name system) services.

 

When an application uses TCP to transfer data, the data is sent to the protocol stack, and then passes through each layer one by one until it is sent to the network as a string of bit streams. Each layer must add some header information (sometimes add tail information) to the received data. The process is shown in the figure.

 

When the destination host receives an Ethernet data frame, the data starts to rise from the bottom of the protocol stack, and at the same time removes the header of the message added by each layer of protocol. Each protocol box must check the protocol identifier in the header of the packet to determine the upper protocol of the received data. This process is called demultiplexing. The protocol is unpacked by destination port number, source IP address and source port number.

Through the above steps, we understand the process of an HTTP request and response from the perspective of the TCP / IP model.

The following picture is more clear:

Three, TCP three-way handshake

TCP is connection-oriented. Before sending data to the other party, a connection must be established between the two parties. In the TCP / IP protocol, the TCP protocol provides a reliable connection service, and the connection is initialized through a three-way handshake. The purpose of the three-way handshake is to synchronize the serial number and confirmation number of the two sides of the connection and exchange TCP window size information.

The first handshake: establish a connection. The client sends the connection request segment, setting the SYN position to 1 and Sequence Number to x; then, the client enters the SYN_SEND state and waits for confirmation from the server

Second handshake: The server receives the SYN segment. The server receives the client's SYN message segment, and needs to confirm this SYN message segment, set the Acknowledgment Number to x + 1 (Sequence Number + 1); at the same time, it also needs to send its own SYN request information, and set the SYN position to 1. , Sequence Number is y; the server puts all the above information into a message segment (ie SYN + ACK message segment) and sends it to the client together, at this time the server enters the SYN_RECV state;

The third handshake: the client receives the SYN + ACK segment of the server. Then set the Acknowledgment Number to y + 1, and send an ACK segment to the server. After the segment is sent, both the client and the server enter the ESTABLISHED state, completing the three TCP handshake.

Why should we shake hands three times

In order to prevent the invalid connection request segment from being sent to the server suddenly, an error is generated.

Specific example: The generation of the "failed connection request segment" occurs in such a situation: the first connection request segment sent by the client is not lost, but it stays for a long time at a network node , So that the delay does not reach the server until some time after the connection is released. This was originally a segment that had long since expired. However, after receiving this invalid connection request segment, the server mistakenly believes that it is a new connection request sent by the client again. So it sends a confirmation segment to the client and agrees to establish a connection. Assuming that the "three-way handshake" is not used, as soon as the server sends out an acknowledgment, a new connection is established. Since the client has not issued a request to establish a connection, it will not ignore the server's confirmation or send data to the server. However, the server thought that a new transport connection had been established and had been waiting for data from the client. In this way, many resources of the server are wasted. The "three-way handshake" method can prevent the above phenomenon. For example, in the case just now, the client will not send confirmation to the server's confirmation. Since the server cannot receive the confirmation, it knows that the client has not requested to establish a connection. "

Four, HTTP protocol

What is Http?

In layman's terms, it is the rule that computers communicate through the network. It is a stateless, application-layer protocol based on requests and responses. It is often based on TCP / IP protocol to transmit data. At present, any kind of communication between any terminal (mobile phone, laptop ...) must be carried out according to the Http protocol, otherwise it cannot be connected.

The four are based on:

Request and response: the client sends a request, and the server responds with data

Stateless: The protocol has no memory for transaction processing. When the client first establishes a connection with the server and sends a request, a series of security authentication matches are required. Therefore, the page waiting time is increased. After the end response is completed, the two are disconnected, and the connection status is not saved. Unyielding! From now on passersby! The next time the client sends a request to the same server, because they have forgotten each other before, they need to re-establish the connection.

Application layer: Http is a protocol belonging to the application layer, used with TCP / IP.

TCP / IP: Http uses TCP as its supporting transport protocol. The HTTP client initiates a TCP connection with the server. Once the connection is established, the browser (client) and server process can access TCP through the socket interface.

Some solutions to statelessness:

Sometimes it is necessary to save the user's previous HTTP communication state, such as performing a login operation, and all requests do not need to log in again within 30 minutes. So the cookie technology was introduced.

HTTP / 1.1 came up with a method of HTTP keep-alive. Its characteristic is that as long as no connection is explicitly proposed at either end, the TCP connection state is maintained. Connection: keep-alive in the request header field indicates that a persistent connection is used.

There are many more. . . . . .

The following starts to explain the highlights: HTTP request message, response message, corresponding to the above steps 2, 3, 4, 5, 6.

HTTP messages are text-oriented. Each field in the message is some ASCII code string, and the length of each field is uncertain. There are two types of HTTP messages: request messages and response messages.

Five, HTTP request message

An HTTP request message consists of a request line, a request header, a blank line, and request data. The following figure shows the general format of the request message.

1. Request line

The request line is divided into three parts: request method, request address and protocol version

Request method

There are 8 types of request methods defined by HTTP / 1.1: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE.

The two most common types of GET and POST, if it is a RESTful interface, generally use GET, POST, DELETE, PUT.

Request address

URL: Uniform Resource Locator, is an abstract unique identification method of voluntary location.

Composition: <protocol>: // <host>: <port> / <path>

Port and path can sometimes be omitted (HTTP default port number is 80)

The following example:

Sometimes with parameters, GET request

Protocol version

The format of the protocol version is: HTTP / major version number. Minor version number, commonly used are HTTP / 1.0 and HTTP / 1.1

2. Request header

The request header adds some additional information to the request message, which consists of "name / value" pairs, one pair per line, with a colon separating the name and value.

Common request headers are as follows:

There will be a blank line at the end of the request header, indicating the end of the request header, followed by the request data, this line is very important and essential.

3. Request data

Optional part, such as GET request, there is no request data.

The following is a POST method request message:

POST /index.php HTTP / 1.1 request line

Host: localhost

User-Agent: Mozilla / 5.0 (Windows NT 5.1; rv: 10.0.2) Gecko / 20100101 Firefox / 10.0.2 request header

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Language: zh-cn,zh;q=0.5

Accept-Encoding: gzip, deflate

Connection: keep-alive

Referer: http://localhost/

Content-Length:25

Content-Type:application/x-www-form-urlencoded

Blank line

username = aa & password = 1234 request data

Six, HTTP response message

HTTP response message mainly consists of status line, response header, blank line and response data.

1. Status line

It consists of 3 parts, namely: protocol version, status code, and status code description.

The protocol version is consistent with the request message, and the status code description is a simple description of the status code, so only the status code is introduced here.

status code

The status code is 3 digits.

1xx: Indication message--Indicates that the request has been received and processing continues.

2xx: Success--Indicates that the request has been successfully received, understood, and accepted.

3xx: Redirection--Further operations are required to complete the request.

4xx: Client error-The request has a syntax error or the request cannot be fulfilled.

5xx: Server-side error-the server failed to fulfill a legitimate request.

Here are a few common ones:

2. Response header

Similar to the request header, some additional information is added to the response message

Common response headers are as follows:

3. Response data

Used to store data information that needs to be returned to the client.

The following is an example of a response message:

HTTP / 1.1 200 OK status line

Date: Sun, 17 Mar 2013 08:12:54 GMT response header

Server: Apache/2.2.8 (Win32) PHP/5.2.5

X-Powered-By: PHP/5.2.5

Set-Cookie: PHPSESSID=c0huq7pdkmm5gg6osoe3mgjmm3; path=/

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Pragma: no-cache

Content-Length: 4393

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html; charset=utf-8

Blank line

  Response data

HTTP response example <title>

Hello HTTP!

There are many knowledge points about request headers and response headers, so here is just a brief introduction.

Through the above steps, the data has been transferred, and HTTP / 1.1 will maintain a persistent connection, but there will always be a time to close the connection for a period of time. At this time, the TCP connection is disconnected as needed.

Seven, TCP waved four times

After the client and the server establish a TCP connection through the three-way handshake, when the data transfer is completed, the TCP connection must be disconnected. For the disconnection of TCP, there is a mysterious "four breakups".

The first breakup: Host 1 (can be a client or a server), set the Sequence Number, and send a FIN message segment to Host 2; at this time, Host 1 enters the FIN_WAIT_1 state; this means that Host 1 has no data to Sent to host 2;

Second breakup: Host 2 receives the FIN segment sent by Host 1, returns an ACK segment to Host 1, Acknowledgment Number is Sequence Number plus 1; Host 1 enters FIN_WAIT_2 state; Host 2 tells Host 1, I ’ Agree "your request to close;

The third breakup: Host 2 sends a FIN segment to Host 1, requesting to close the connection, and at the same time Host 2 enters the LAST_ACK state;

The fourth breakup: Host 1 receives the FIN segment sent by Host 2 and sends an ACK segment to Host 2, then Host 1 enters the TIME_WAIT state; Host 2 closes the connection after receiving the ACK segment from Host 1 ; At this time, host 1 has not received a reply after waiting for 2MSL, it proves that the server has been shut down normally, then, host 1 can also close the connection.

Why break up four times

The TCP protocol is a connection-oriented, reliable, and transport layer communication protocol based on byte streams. TCP is full-duplex mode, which means that when Host 1 sends out a FIN segment, it only means that Host 1 has no data to send. Host 1 tells Host 2 that its data has all been sent; however, At this time, host 1 can still receive data from host 2; when host 2 returns an ACK segment, it indicates that it has known that host 1 has no data to send, but host 2 can still send data to host 1; when host 2 also When the FIN message segment is sent, this time it means that host 2 has no data to send, and it will tell host 1 that I have no data to send, and then each other will happily interrupt the TCP connection.

Through the above steps, the HTTP request and response were completed, and the data was transferred. Among them, the knowledge points involved were all understood one by one.

Author: Ruheng

Link: http://www.jianshu.com/p/c1d6a294d3c0

Source: Brief Book

The copyright belongs to the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

 

Published 29 original articles · Like1 · Visits 587

Guess you like

Origin blog.csdn.net/wennie11/article/details/104985263