[Process] Http Http request

The browser sends an HTTP request process

Browser enter the URL: http: //www.baidu.com, after what steps it will be completed?

1, build requests

First, build a browser request line was constructed through the good, the browser is ready for initiating a network request.

GET /index.html HTTP1.1

2, Lookup Cache

Before actually initiating a network request, the browser will first have to check whether the requested file in the browser cache. Among them, the browser cache is a locally stored copy of the resource, technology for direct use when the next request .

When a browser requests a resource discovery already exists in the browser cache copies there, it will intercept the request and return a copy of the end of the resource request. If the cache lookup fails, access to the network request. It would be beneficial:

  • Ease server stress, improve performance
  • For the website, the cache is to achieve an important part of resource load quickly, reducing the time to acquire resources.

3, ready to IP addresses and ports

We prepared earlier by the beginning of knowledge and also learn about the relationship between HTTP and TCP. Browser using HTTP protocol as the application layer protocol used to text information request package ; using TCP / IP as a transport protocol to send it to the network, so before work begins HTTP, browser needs to establish a connection with the server via TCP . That HTTP content is achieved by transmitting data phase of TCP.

TCP and HTTP relationship diagram:

 

 Accordingly, we can know it is to establish an HTTP request to the network, to obtain the IP address resolved through URL and port information to establish server and TCP connections . We previously "TCP protocol" when it comes to the data packet to the receiving side are transmitted through the IP address. And our website address is usually the domain name, the domain name and IP address need to do mapping relationship, that resolution system IP address " domain name system (the DNS) " to resolve the IP address, port number and get the corresponding obtain pre-established connection condition. In other words, the browser requests DNS returns the corresponding IP domain name, and the request will query DNS data cache when the DNS service, to determine whether the domain name has been resolved, if the query is parsed directly, to get the IP after it is determined whether the URL specify the port number, not the HTTP protocol when the default port 80.

 

4, waiting for TCP queue

Chrome has a mechanism for the same domain name simultaneously only create up to six TCP connection , if there are 10 requests occur at the same domain name, of which four requests are queued to enter the state, carried out until the request is completed. Of course, if the current number of requests is less than 6, it will go directly to the next step, to establish a TCP connection.

5, to establish a TCP connection

After the queue waiting for the end, and TCP server implementation "three-way handshake" (TCP protocol previously described), that is, the client and the server sends three packets to confirm the connection, connection to realize the browser and services.

6, transmits an HTTP request

Once the TCP connection is established, the browser and the server can communicate. The data is transmitted in HTTP communication in this process.

HTTP request data format:

 

 

 

First, the browser sends to the server request line , which includes a request method, request URI (Uniform Resource Identifier) protocol and HTTP version .

There are ways in which the request GET, POST, PUT, Delete, etc., which are commonly used for POST will send some data to the server, such as visit the website to send user information to the server, usually these data will pass the request body sent.

After the browser sends a request command line, but also to send some additional information to request headers form, some basic information about the browser tells the server. For example, it contains the operating system, browser kernel information such as browser you are using, and the domain name information about the current request, Cookie and so on.

 

 

 

HTTP requests server-side processing flow

1, return request

curl -i https://www.baidu.com/

By curl tool (or network panel), we can understand the server returns data format:

 

 

 

First, the server returns a response line , including protocol version and status code.

If an error occurs, the server will pass the request line status code to return the processing result, for example:

  • The most common status code 200 indicating the success of the processing;
  • 404 not found page
  • 500, indicating that the server error

As the browser will send the request along with the request header, the server will send a response headers along with the response to the browser. Response header contains some information of the server itself, such as a server returns data generation time, data type (JSON, HTML, streaming media type) is returned, the server and the client to save information such as Cookie.

After the first response, the server sends a response body of data, typically it contains the actual HTML content. The above is the server responds browser process.

2. Disconnect

Once the client returns to the server request data, it is necessary to close the TCP connection. However, if the browser or server to join in its header information:

Connection:Keep-Alive

After sending the TCP connection remains open, so the browser can continue to send the same TCP connection request. To maintain TCP connections require time to establish connections may be omitted when the next request, to enhance resource loading speed. If a page embedded pictures are from the same web site, you can initialize a persistent connection multiplexing to reduce the TCP connection.

3, redirection

And returns a response redirect response header rows:

 

 301 state is to tell the browser, I need to redirect to another URL, is the need to redirect URLs included in the Location field in the response header, and then, get the address of the browser Location field, and use that address again navigation, which is a complete redirection of the implementation process.

to sum up

Http request through the complete process, we know that the DNS request process slowly and page resources are cached browser cache up to reduce resource request to the server, so when the site will once again requests will be faster.

Browser cache processing resources:

 

 

As can be seen from the figure the first request, when the server returns the HTTP response header to the browser, the browser is in response Cache-Control header field to set whether to cache the resource . In general, we also need to set the length of time that a resource cache expires, and this is a long time by Max-age Cache-Control parameter in the set.

Therefore, in the case of the cache resources it has not yet expired, if the resource is requested again, the resources directly back to the browser cache.

If the cache expires, the browser will continue to initiate network requests, and the header tape If-None-Match request HTTP , whether the resource server receives the request header, the request will be judged based on the value of the If-None-Match for updates.

  • If there is no update, returns a 304 status code, the equivalent server tell the browser, the cache can continue to use.
  • If the resource is updated, the server returns the latest resources directly to the browser.

String visit the website, submit information to the server through the POST method. After the server receives the information submitted by the browser, query the user identity authentication information is correct surface is generated written response Set-Cookie header field back to the browser.

Browser parses the response header, if the Set-Cookie field is stored locally when the user accesses again, before an HTTP request browsers read and write request data Cookie headers to the server, the server information determination again, if correct the show user status and user login information.

Finally, summed up the browser to initiate HTTP requests from end experienced a total of eight stages: build requests, look up the cache, ready IP and port, TCP queue waiting to establish a TCP connection, sends an HTTP request, the server processes the request, the server returns the requested and disconnect .

Details HTTP request process:

 

 Transfer - https://cloud.tencent.com/developer/article/1574325

Guess you like

Origin www.cnblogs.com/july-sunny/p/12628874.html