HTTP introduction, request method and response status code
Next, I would like to review the TCP/IP protocol family related things, of course, most of these things were learned in college, but that sentence, the basis of things still need to review the back of the time. The next few blog is about the TCP/IP protocol family, this blog will be a simple chat about the TCP/IP protocol family, and then chat about the HTTP protocol, and then talk about the SSL http (that is, HTTPS). Of course, the TCP/IP protocol family is a commonplace topic, the Internet on the content of the article on the catch a lot of it, but in view of its importance, it is necessary to summarize the system.
1. TCP/IP protocol family Brief
Before we talk about HTTP and HTTPS, let's briefly talk about the TCP/IP protocol family. TCP/IP refers not only to the two Protocols of TCP and IPs, but to the various protocols associated with them. For example, HTTP, FTP, DNS, TCP, UDP, IP, SNMP and so on are all part of the TCP/IP protocol family category.
1.1. Layering of TCP/IP protocols
TCP/IP protocol family is hierarchical management, in the OSI standard can be divided into 7 layers (application layer, presentation layer, session layer, Transport layer, network layer, data link layer, physical layer, can be recorded as: should be the table will be transmitted network number), this blog we use the TCP/IP protocol family of four (application layer, Transport layer, network layer Link layer). Below is a brief description of each layer in layer four:
- Application Layer : This layer is a user-oriented layer, that is, the user can directly manipulate the layer, which determines the user to provide application services when the communication activities. The HTTP (Hypertext Transfer Protocol: Hypertext Transfer Protocol) you want to talk about in this blog is located at that level. The FTP that we use frequently (the file Transfer Protocol: Files Transfer Protocol) and DNS (Domain Name System: DomainName) are located at that level. FTP is simply used for file transfer. DNS is responsible for domain name resolution, through the DNS can be the domain name (such as: www.cnblogs.com) and IP address (201.33.xx.09) to convert each other. In layer 7, the layer is divided into: Application layer, presentation layer, and Session layer.
- Transport Layer : Below the application layer is the transport layer, and the application layer delivers the data to the transport layer for transmission. The TCP (transmission Control Prococol: Transmission protocol) and UDP (user data Protocol: the protocol) are located at this level, which is, of course, known to provide direct data transfer between two computers in a network connection. TCP establishes a connection that requires three handshakes to confirm the connection, while UDP does not have a three-time handshake process. will be introduced later.
- Network layer : Below the transport layer is the network layer, which is used by the network layer to process packets flowing over the network, and IP (Internet Protocol: Internetwork Protocol) is located on this layer. This layer is responsible for selecting a transmission line in many network lines. Of course, the process of selecting a transmission line requires IP address and MAC address support.
- link Layer : In the 7 layer protocol, the link layer is divided into the data link layer and the physical layer. This part is mainly used to deal with the hardware part of the network, we often say the NIC (Net work card), that is, the network card is located in this part, of course, fiber is also part of the link layer.
In the TCP/IP protocol family, each time directly in the transmission of data collaboration, as well as the interaction process, or refer to the book "Graphical http" a picture to explain it. Is the way the four-layer protocol works during data transfer. The following picture is quite straightforward. On the sending side is the application layer----the link layer in this direction of the encapsulation process, each through a layer will increase the head of the layer. The receiving end is the process of unpacking from the link layer to the application layer, and each passing layer will remove the corresponding header.
1.2. Three-time handshake of the TCP protocol
Before we talk about the HTTP protocol, let's briefly talk about the TCP three handshake process, in the following blog we will be on the TCP and IP protocol details, this blog will be a simple chat on the basis of the HTTP protocol.
The TCP protocol is located in the transport layer, and in order to ensure the reliability of the transmission, the TCP protocol requires three handshake (Three-way handshaking) when establishing the connection. The diagram below is the process of three handshake when the TCP protocol establishes a connection.
- First handshake: Send Side sends a SYN (Synchronize [? s??? KR?NA?Z] The packet to the receiving end of the flag that asks the receiving side if it can receive it. If you can, do a second handshake.
- Second handshake: Receive end to send a packet with syn/ack (acknowledgement [? K?n?l?d?m?nt] Confirmation), to the sender said, I received you sent me a SYN flag, I will send you an ACK flag, you can receive it? If the sender receives the Syn/ack packet, it can confirm that the receiving side received the SYN that was sent, and then the third handshake.
- Third handshake: The sender sends a packet with an ACK flag to the receiving end, telling the receiver that I can receive the SYN/ACK flag you sent me. If the receiver receives this ACK flag from the client, it means that the three handshake is complete and the connection is established to begin transmitting the data.
1.3. Four waves of TCP protocol
The TCP three-time handshake is the process of establishing a TCP connection, and a TCP four wave is the process of releasing the TCP connection . Here is a flowchart of the TCP four wave:
When the client does not have the data to be sent to the server, it needs to release the client connection, the whole process is:
- The client sends a message to the server (no data), where fin is set to 1,sequence number to u, and the client enters the fin_wait_1 state
- The server receives a request from the client, sends an ACK to the client, acknowledge to u+1, sends sequence number to V, and the server enters Close_wait state
- The server sends a fin to the client, and the ACK is set to 1,sequence W,acknowledge to U+1, which is used to shut down the service-to-client data transfer, and the server enters the Last_ack state.
- After the client receives fin, it enters the TIME_WAIT state, then sends an ACK to the server, acknowledge W+1,sequence number to u+1, and finally the client and server enter closed state.
One of the problems here is why the TCP connection requires only three handshakes and the release of the TCP connection requires four handshakes:
Because the server is in listen state, after receiving the SYN message of the request, the ACK and SYN are sent to the client in a message. And when the connection is closed, when the other side of the fin message, only to indicate that the other party does not need to send the data, but also can receive data, you may not have all the data sent to each other, so you can immediately close, It is also possible to send all the data that should be sent before sending the fin message to the client to express consent to close the connection now.
From this point of view, the service side of the ACK and fin will generally be divided into development.
2. HTTP message structure
The HTTP protocol is the full name of hypertext Transfer Protocol, the Hypertext Transfer Protocol, the user client and the server before the communication, the current widely used version of http/1.1. The protocol is essentially a specification, and the "interface-oriented" programming we've mentioned before is actually "protocol-oriented" programming. We define the protocol of the class first, that is, the interface, and the related classes follow the protocol, so that we can standardize the way these classes are called. The HTTP protocol is the protocol that regulates the communication between the client and the server. In other words, all the clients or servers follow the HTTP protocol, which means that the interface that transmits the data is always there, and it can be connected to the pipeline so that it can be transferred.
These protocols are interfaces, have a common communication protocol, and can communicate with each other on multiple ends. The same protocol is used to facilitate communication between all devices. The role of the HTTP protocol is as follows.
The function of HTTP protocol is to standardize the content of communication, and it can be divided into request message and Response message in HTTP protocol. As the name implies, the request message is the information sent by the requester, and the response message is the content of the response received upon request. Next we'll look at the overall structure of the request message and the response message.
1.1. Request message Structure
Below is the overall structure of the request message. The request message is mainly divided into two major parts, one is the request Headers and the other is the request body. Between the two is separated by a blank line. In the request header is divided into the request line, the Request header field, the General header field and the Entity header field, which will be described in detail later. Below is the structure of the request message.
This is the request Headers when requesting a page in the blog park. The first "GET" in the request line is the currently requested method, which is described later. The middle is the path to the requested resource, and the last http/1.1 is the current usage request protocol and its version. These are the request headers, which will be explained later in the general request header. The request body is the data that you transmit to the server, such as form form, God horse.
1.2. Response message (Response message) structure
After talking about the request message, we will talk about the response message, the structure of the response message is similar to the structure of the request message, and also divided into the message header and newspaper style. Below is the structure diagram of the response message. The response header (Response Headers) is divided into the status line, the Response header field, the General header field, the Entity header field, and so on. The response head is split with a blank line in the middle of the response body.
Below is the response header after the above request message is issued, the response body is for the HTML and other front-end resources. In the response header, the first line is the status line, "http/1.1" represents the 1.1 version of the HTTP protocol used, status 200 indicates a successful response, and "OK" is the state reason phrase. Common status, which is described in more detail later.
01-http profile-four-layer seven-layer three-time handshake