The first three tortured soul: talk about presenting enter the URL to the page from what happened? - Network chapter

This is an infinitely difficult problem. The subject of the purpose is to examine your web-depth to what extent. As limited space and, here I will give some important processes we comb it again, which will hopefully give a more stunning answer in most cases.

Here I declare in advance, because it is a very strong comprehensive problems that may dig out a lot of details at a certain point, I personally feel that learning is a gradual process, to understand their own research in the whole process again these details, the entire body of knowledge will have a better understanding. Meanwhile, details about the extension application point out the references I have, after reading this may wish to go deeper into what, expand knowledge.

Well, the topic began.

At this moment, you are in the browser address bar enter the Baidu website:

https://www.baidu.com/
复制代码

Network requests

1. Construction Request

The browser will construct request line:

// 请求方法是GET,路径为根路径,HTTP协议版本为1.1
GET / HTTP/1.1
复制代码

2. Find strong caching

Strong first check the cache, if hit directly, otherwise the next step. On the strong cache, I do not know if you can refer to the previous article.

3. DNS resolve

As we enter a domain name, and the data packets through the IP地址pass each other's. So we need to get the corresponding domain name IP地址. This process relies on a service system that the domain name and IP-one mapping, we will this system is called the DNS (Domain Name System). Get specific IP process is DNSresolved.

Of course, it is worth noting that the browser provides the DNS data caching functionality . That is, if a domain name has been resolved, and that will resolve the cached result, the next process go directly to the cache, without going through DNS解析.

Further, if no port is specified, the default port 80 corresponding to the IP.

4. Establish a TCP connection

To remind here that, Chrome at the same time under the same domain name requirements can only have six TCP connections, more than six, then the rest of the request would have to wait.

Suppose now that do not need to wait, we have entered a phase of establishing a TCP connection. First, explain what is TCP:

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable transport layer protocol based on the byte stream.

Establish TCP连接gone through the following three stages:

  1. By way handshake (i.e., a total of three transmission confirmation packet connection has been established) to establish a connection between the client and the server.
  2. For data transmission. There is an important mechanism that the recipient receives the packet must want to sender 确认, if the sender did not receive this 确认message, it is determined that the loss of data packets and resend the packet. Of course, there is a process of sending the optimization strategy is to 大的数据包拆成一个个小包sequentially transmitted to the receiver side, the receiving side of the parcel in order them 组装into a complete packet.
  3. Phase of the connection is disconnected. Data transfer is complete, the now disconnected, by waving four to disconnect.

Read here, you should understand what TCP connection means to ensure the reliability of data transmission, first 三次握手confirm the connection, and second, 数据包校验to ensure that the data arrives at the recipient Third, through 四次挥手disconnected.

Of course, if you go any further asked, such as why the three-way handshake, the two can not I? Third handshake failure how to do? Why four waving , etc. This series of questions, the basic knowledge related to the computer network, compare the bottom, but also a very important detail, I hope you look carefully, while there are a good article, click into the corresponding recommended article , I believe that this article will give you inspiration.

The HTTP request

Now TCP连接build is completed, the browser can start the communication server, which began sending HTTP requests. Browser to send HTTP requests to carry three things: the request line , request headers and body of the request .

First, the browser sends to the server request line , on the request line , we just finished the first step in building this part of the content posted about:

// 请求方法是GET,路径为根路径,HTTP协议版本为1.1
GET / HTTP/1.1
复制代码

Structure is very simple, the request method , request URI and HTTP protocol version composition.

But also to bring the request headers , such as we have said before, Cache-Control , the If-Modified-Since , the If-None-Match by request header can be placed in a cache of identification information. Of course there are other properties, are listed below:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Cookie: /* 省略cookie信息 */
Host: www.baidu.com
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1
复制代码

Finally, request body, the request body only in POSTthe presence of methods common scenario is a form submission .

Network response

HTTP request arrives at the server, the corresponding server process. Finally, take the data to the browser, which is returned network response.

Similarly with the request part, the response network has three parts: a response line , response headers and response body .

Response line similar to the following:

HTTP/1.1 200 OK
复制代码

It made HTTP协议版本, 状态码and 状态描述composition.

Response header contains the information server and returns some data, the server generates time data, and the data type returned Cookie information is to be written.

For example as follows:

Cache-Control: no-cache
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html;charset=utf-8
Date: Wed, 04 Dec 2019 12:29:13 GMT
Server: apache
Set-Cookie: rsv_i=f9a0SIItKqzv7kqgAAgphbGyRts3RwTg%2FLyU3Y5Eh5LwyfOOrAsvdezbay0QqkDqFZ0DfQXby4wXKT8Au8O7ZT9UuMsBq2k; path=/; domain=.baidu.com
复制代码

How to do after the response is complete? TCP connection is disconnected yet?

Not necessarily. This time to determine the Connectionfield, if the request header or response header contains the Connection: the Keep-Alive , represents the establishment of a lasting connection, this TCPconnection will remain, after the request Uniform Resource site will reuse the connection.

Otherwise disconnect TCPconnection request - response process ends.

to sum up

This, we summarize the main content, that is, the browser web request process:

 

Guess you like

Origin www.cnblogs.com/guchengnan/p/12160648.html