Next, I want to review systematically TCP/IP Something about the protocol family , Of course, most of these things were learned in college , But that sentence , The basic things should be reviewed from time to time . The next few blogs are all about TCP/IP The agreement family , In this blog, let's talk about it TCP/IP Protocol family , Then talk about it HTTP agreement , And then talk about it SSL Upper HTTP（ That is to say HTTPS） 了 . Of course TCP/IP The protocol family is a cliche , There are a lot of articles about this content on the Internet , But given its importance , It is necessary to summarize systematically .
TCP/IP Brief description of protocol group
Talking about HTTP And HTTPS Before that , Let's have a brief talk TCP/IP Protocol family .TCP/IP It's not just about TCP and IP These two agreements , It's about the agreements that are related to it . such as HTTP, FTP, DNS, TCP, UDP, IP, SNMP And so on TCP/IP The category of protocol family .
TCP/IP Layers of agreement
TCP/IP Protocol family is hierarchical management , stay OSI Standards can be divided into 7 layer （ application layer 、 The presentation layer 、 The session layer 、 Transport layer 、 The network layer 、 Data link layer 、 The physical layer , It can be recorded as ： Should watch can transmit network data ）, In this blog we use TCP/IP Four layers of the protocol family （ application layer 、 Transport layer 、 The network layer 、 The link layer ）. Below is a brief introduction to each of the four floors ：
application layer ： This layer is user oriented , That is to say, users can operate the layer directly , This layer determines the communication activities when providing application services to users . This blog will talk about HTTP（HyperText Transfer Protocol： Hypertext transfer protocol ） It's on this floor . We use it a lot FTP(File Transfer Protocol: File transfer protocol ) and DNS (Domain Name System: The domain name system ) It's all on this floor .FTP In short, it is used for file transfer . and DNS The domain name resolution is responsible for , adopt DNS You can put the domain name （ such as ：www.cnblogs.com） And IP Address （201.33.xx.09） To make a mutual conversion . stay 7 Layer , This layer is divided into ： application layer 、 Presentation layer and session layer .
Transport layer ： Below the application layer is the transport layer , The application layer delivers the data to the transport layer for transmission .TCP(Transmission Control Prococol: Transmission control protocol ) and UDP(User Data Protocol: User data protocol ) On this floor , Of course, the name knows the meaning , This layer is used to provide direct data transmission between two computers in a network connection .TCP Establishing a connection requires three handshakes to confirm the connection , and UDP There's no three handshakes . I'll introduce you later .
The network layer ： Below the transport layer is the network layer , The network layer is used to process packets that flow over the network ,IP(Internet Protocol: Internet protocol ) It's on this floor . This layer is responsible for selecting one transmission line among many network lines . Of course, the process of selecting transmission lines requires IP Address and MAC Address support .
The link layer ： stay 7 In the layer agreement , The link layer is divided into data link layer and physical layer . This part is mainly used to deal with the hardware part of the network , What we often say NIC（Net Work Card）, That is, the network card is located in this part , Of course, fiber is also part of the link layer .
stay TCP/IP A cooperative relationship in a protocol family that directly transmits data , And the interaction process , Or reference 《 The illustration HTTP》 A picture from a book . The figure below shows how these four layer protocols work in the process of data transmission . The picture below is quite intuitive . At the sending end is the application layer –> This direction of the link layer packet process , Each pass through a layer increases the head of that layer . The receiver is from the link layer –> Application layer unpacking process , After each layer, the corresponding header will be removed .
TCP Three handshakes of the agreement
Talking about HTTP Before the agreement , Let's have a brief talk TCP Three handshakes , We'll talk about TCP and IP The agreement details , In this blog, I'd like to talk about it HTTP The basis of the agreement .
TCP The protocol is on the transport layer , To ensure the reliability of the transmission ,TCP The protocol requires three handshakes to establish a connection （Three-way handshaking）. The diagram below is TCP The process of three handshakes when a protocol establishes a connection .
The first handshake ： The sender sends a tape SYN(Synchronize) Flag the packet to the receiver , Used to ask if the receiver can receive . If possible , Just a second handshake .
The second handshake ： The receiver sends back to the sender a message with SYN/ACK(Acknowledgement) Data packets of , Say to the sender , I received a message from you SYN It marks , I'll send you another ACK sign , Can you get it ？ If the sender receives SYN/ACK This packet , You can confirm that the receiver has received the previous SYN, Then make a third handshake .
The third handshake ： The sender will send to the receiver a message with ACK Packets for flags , Tell the receiver that I can receive the SYN/ACK sign . If the receiver receives this from the client ACK sign , It means three handshakes , Connection is established , You can start transmitting data .
HTTP Message structure
HTTP The full name of the agreement is HyperText Transfer Protocol, Hypertext transfer protocol , Communication between client and server , At present, the commonly used version is HTTP/1.1. Agreements are essentially norms , We mentioned before “ An interface ” Programming , In fact, that is “ Protocol oriented ” Programming . First define the protocol of the class , That's the interface , The related classes follow the protocol , In this way, we can standardize the way these classes are called . and HTTP Protocol is a protocol that standardizes the communication between client and server . That is to say, all clients or servers follow HTTP This communication protocol , So it means that their interface for external data transmission is always , You can connect a pipe in the middle of it , So you can transfer it .
These protocols are interfaces , There's a common communication protocol , Multiple terminals can communicate with each other . Use the same protocol , It is convenient for communication between devices .HTTP The role of the agreement is as follows .
HTTP The function of the protocol is to standardize the communication content , stay HTTP The protocol can be divided into request message and response message . seeing the name of a thing one thinks of its function , A request message is a message sent by the requesting party , The response message is the content of response after receiving the request . Let's take a look at the overall structure of the request message and the response message .
Request message （Request Message） structure
Below is the overall structure of the request message . The request message is mainly divided into two parts , One is the request header （Request Headers） The other is the request body （Request Body）. There is a blank line between the two . In the request header, it is divided into request line （Request Line）, Request header field , General header fields and entity header fields, etc , This will be described in detail later . Below is the structure of the request message .
The screenshot below is when you request a page in the blog Garden Request Headers. The first one in the request line “GET” Is the method of the current request , I'll introduce it later . In the middle is the path to request resources , the last one HTTP/1.1 Is the current use of request protocol and its version . Here are the request headers , The common request headers are explained later . The request body is the data you send to the server , such as form The form is divine .
response message （Response Message） structure
After chatting about the request message , Next, let's talk about the response message , The structure of response message is similar to that of request message , It is also divided into message header and newspaper style . Below is the structure of the response message . Response head （Response Headers） It's divided into status lines （State Line）, Response header field , General header field 、 Entity header fields, etc . There is also a blank line between the response head and the response body .
The screenshot below is the response header after the above request message is sent , The response body is right HTML Wait for the front-end resources . In the response header , The first line is the status line ,“HTTP/1.1” Indicates the HTTP Agreed 1.1 edition , state 200 Indicates a successful response ,"OK" It's the state reason phrase . Common state , It will be described in detail later .
HTTP Request method and response status code
As mentioned above in the introduction request message “GET” It's the request request method , In the response message “200” Status code , This is the response status code to be talked about later . The request method and response status code are in HTTP The agreement is more important . We were using Perfect Framework development server side , I talked about the request method GET、POST、PUT as well as DELETE, And these four methods can be combined with REST Use . This part is based on HTTP From the perspective of protocol, we can talk about the request method , So it's a little different from before . In this part, we'll talk about HTTP Protocol request method and response status code .
The next request method we're going to talk about is GET、POST、PUT、HEAD、DELETE、OPTIONS、TRACE、CONNECT. Of course, the above method is based on HTTP/1.1 Of ,HTTP/1.0 I don't want to talk about the unique method in .
GET---- Access to resources GET Methods are generally used to get resources from the server . The server ends up with GET After the request , You will understand that the client is to get the corresponding resources from the server , Then according to the corresponding parameters in the request message , Return the required resources to the client . Use GET Method request , The transmitted parameters are spliced in URI Upper .
POST---- Data submission POST Method is generally used for form submission , The client's data is pushed into the request body and sent to the server .
PUT---- Upload files PUT Method is mainly used to upload files , Put the contents of the document into the style of the request , Transfer to server . because HTTP/1.1 Of PUT The method itself does not have a validation mechanism , So anyone can upload files , Security exists , So it is not recommended to use when uploading files . But before, we were designing interfaces to use REST Standard time , have access to PUT To update the content .
HEAD---- Get response header The response received HEAD After the request , Only the corresponding response header will be returned , The response body will not be returned .
DELETE---- Delete file DELETE Used to delete URI Designated resources , And PUT equally , There is no verification mechanism , But in the REST Standards can be used to do the corresponding API Delete function of .
OPTIONS---- Query supported methods OPTIONS Method is used to query the server to make corresponding response to those request methods , The return content is the method supported by the response side .
TRACE---- Trace path TRACE Method to trace the proxy path through which the request passes , When sending a request, it will be Max-Forwards Fill in the header field with numbers , Every time through an agent transfer Max-Forwards The value of is reduced by one , until Max-Forwards After zero , Will return 200. Because this method is easy to cause XST(Cross-Site Tracing, Cross Station tracking ) attack , So it's not commonly used .
CONNECT---- Require a tunnel protocol to connect to the agent CONNECT Method requires a tunnel to be built when communicating with the proxy server , Implementation with tunnel protocol TCP signal communication . The main use of SSL(Secure Sockets Layer, Secure socket layer ) and TLS(Transport Layer Security, Transmission security layer ) The protocol encrypts the communication content and transmits it through the network tunnel .
Response status code
After talking about the request method , Now let's talk about it HTTP Response status code of the protocol . seeing the name of a thing one thinks of its function , The response status code is used to mark HTTP Responding to the state of , Response state consists of response state code and response reason phrase , Of course, there are many state codes , This section will pick out the commonly used status code to discuss . Below are several categories of response status codes ：
1xx ---- Informational（ Informational status code ）, Indicates that the accepted request is being processed .
2xx ---- Success ( success ), Indicates that the request has been processed normally .
3xx ---- Redirection ( Redirect ), Indicates that you want to redirect the request , Of course, one of them 304 With the exception of .
4xx ---- Client Error ( Client error ), The server was unable to process the request .
5xx ---- Server Error ( Server error ), An error occurred while the server was processing the request .
Above is the overall classification of response status codes , Next, some common response status codes are introduced .
(01)、200 OK : Indicates that the server has correctly processed the request sent by the client .
(02)、204 No Content : Indicates that the server processes the request correctly , But there is no message entity to return .
(03)、206 Partial Content ： Indicates that the server has correctly processed the range request of the client , And return the entity content within the specified scope according to the request scope .
(04)、301 Moved Permanently： Permanent redirection , If the previous URI Saved to Bookmarks , Then update the bookmark URI.
(05)、302 Found： Temporary redirection , The redirection does not change the content of the bookmark .
(06)、303 See Other： Temporary redirection , And 302 Function the same , however 303 State? Explicitly indicates that the client should use GET Method to get resources .
(07)、304 Not Modified: The resources have not changed , The status code has nothing to do with redirection , When the status code is returned , Tell the client that the requested resource has not been updated , The content requested will not be returned in the response style .
(08)、400 Bad Request： Wrong request , Indicates that the request message contains a syntax error .
(09)、401 Unauthorized： Request not authenticated , Indicates that the request sent needs to be made by the client HTTP authentication （ I'll talk about it later ）.
(10)、404 Not Found： No corresponding resources can be found , Indicates that the server cannot find the resource requested by the client .
(11)、500 Internal Server Error： Server internal error , Indicates that the server encountered an error while processing the request , Something is wrong .
(12)、503 Service Unavailable： Service not available , Indicates that the server is down , Unable to process request from client .