Web && Network from Click to render-a detailed HTTP request

Source: Internet
Author: User
Tags apache php php language php mysql

Transferred from: http://zrj.me/archives/589


From click to render-detailed once HTTP request (1)

Actually think of this content for a long time, on the one hand is to review what I learned HTTP knowledge, on the other hand is also to share it, because so far did not see the network like I want to write something.


In general, many of the resources above will say that HTTP is a request/response based work mode, and then draw a browser and Server B/s structure, and then draw two arrows to indicate the request and response, it should be said that the explanation is understandable, is generally clear enough, but for a textual research like me , so simple explanation is obviously not enough, in our click on a URL, to it can be presented in the browser, show in front of us, the process, the computer, the network, what happened, in fact, is very interesting, it is worth to explore.


So I want to follow my own understanding of the process as detailed as possible, in the wording I intend to follow the way of the timeline, so good to write, also good to read, good understanding. This should be written in a series, just recently began to see a lot of people are recommended hot books, HTTP authoritative guide, this, http://book.douban.com/subject/10746113/, and I want to say the same, so on the side as a reading notes it


Then we started, the story is not in the browser's address bar to enter a URL, or we grab the mouse click on a link to start, the beginning of things to go back to the start of the server monitoring service, at some unknown moment, a machine room in the ordinary blade server, plus electricity, activated the operating system, With the operating system ready, the server started the HTTP service process, the HTTP service daemon, (daemon), may be Apache, or it may be nginx, anyway, this HTTP service process began to locate the WWW folder on the server, typically located in/var /www, and then started some of the subordinate modules, such as PHP, or, using the fastcgi way to connect to the FPM management process of PHP, then, to the operating system to request a TCP connection, and then bound to the 80 port, called the Accept function, began silent listening, listening to the Be prepared to respond to requests from anywhere on Earth.


This time, the typical case, the room should also have a database server, perhaps, there is a cache server, if the traffic is huge site, then the dynamic script interpreter may have a separate physical machine to run, if it is small and medium-sized site, then, the above-mentioned services, or even on a physical machine , these services listen to the relationship between, you can refer to here, and then a Apache PHP MySQL environment, anyway, they are ready, waiting to be sent.






From click to render-detailed once HTTP request (2)

Last said the server started the listening service, ready to meet the request from the client. So, we parse with a typical browse request:


When we start to enter the URL in the browser, the browser is actually already in the smart match may have URL, he will be from the history, bookmarks and other places, find the string that has been entered may correspond to the URL, and then give a smart hint, for Google Chrome that kind of perverted browser, He will even show the page directly from the cache, that is, you have not pressed enter, the page is out, this is too wonderful, we do not elaborate.


After entering a domain name such as Baidu or QQ, we can press CTRL + ENTER to auto-complete, generate qq.com or baidu.com URLs, and then initiate the request. Once the request is initiated, the first thing the browser should do is to resolve the domain name, in general, the browser will first look at the local hard disk Hosts file, see if there is no and this domain corresponding rules, if there is a direct use of the Hosts file inside the IP address, said here, you may think, This place there is a security risk, if there is a virus to some commonly used domain names, modify the Hosts file, pointing to some malicious IP, then the browser will not add judgment to connect, yes, this is a lot of virus usage.


If the local Hosts file is not able to find the corresponding IP address, the browser will be to the DNS domain name resolution server to initiate the domain name resolution request, DNS domain name resolution is recursive, (there is another DNS is iterative), the recursive DNS will first look at their own DNS cache, if the cache can hit, then The IP address is returned from the cache to the browser, if the corresponding domain name is not found in the IP address, then forward the request, and then the resulting domain name corresponding to the address of the nameserver, and then to this namserver to request the IP address of the domain name, and finally return the IP addresses to browse , however, this recursive query process, for the browser is transparent, he just wait for the IP address to send back. For further understanding of DNS, you can see here for further understanding of DNS


Once the IP address is obtained, the browser starts to construct an HTTP request, and a typical HTTP requests header typically needs to include the requested method, such as Get or POST, less commonly used and PUT and DELETE methods, and more infrequently, HEAD and OPTION and the TRACE method, the general browser can only initiate a GET or POST request, the Application layer HTTP request is ready, the browser in the transport layer to initiate a TCP connection to the server, this time should start the process of three handshake, TCP packets are encapsulated to the network layer of IP packets Inside, the IP packet is then encapsulated in the data link layer of the data frame structure, and then through the physical layer of bitstream sent out, the significance of these layers is the division of labor, the data link layer through the CSMA/CD protocol to ensure the transmission of data between adjacent two hosts, and the network layer of IP Packets through the router between different subnets routing algorithm and route forwarding, to ensure that the Internet two remote host point-to-point communication, but this transmission is unreliable, so the reliability of the transport layer of the TCP protocol to ensure that TCP through slow start, multiplication to reduce the means to carry out traffic control and congestion avoidance, At the same time, two remote host on the process to the process of communication, and finally ensure that the HTTP request header can be on the remote server is listening to the HTTP server process received, finally, the packet between the jump and jump is split and encapsulated, between the subnet and the subnet is forwarded and forwarded, Finally entered the server's operating system buffer, the server's operating system to a blocked accept function one return, will wake him.




From click to render-detailed once HTTP request (3)

After the request enters the server, the HTTP listener process on the server gets the request, and a new child process is normally started to process the request, and the parent process continues to listen. The HTTP server will first look at the rewrite rules, and then if the file is real, such as some pictures, or CSS JS, such as static files, it will directly return the file, if it is a dynamic request, then according to the URL rewrite module rules, the request will be rewritten to a rest-style URL , and then depending on the script of the dynamic language, decide what type of dynamic file script interpreter to call to handle the request.


We use the PHP language for example, the request to a PHP MVC framework, the framework should first initialize some parameters of the environment, such as the remote IP, request parameters and so on, and then according to the requested URL sent to a router class to match the route, the route from the top to the next match, once encountered URL can Match, and the requested method can be hit, the request is handled by the processing method defined by the route.


After the request enters the processing function, if the client requests that the content that needs to browse is a dynamic content, then the processing function will take out the data from the data source accordingly, this place usually has a cache, for example memcached to reduce the pressure of the DB, if the ORM framework is introduced, Then the processing function directly to the ORM framework to request data, by the ORM framework to determine whether to use in-memory cache or from the DB to fetch data, the general cache will have an expiration time, and the ORM framework will take the data back, the data stored in the memory cache.


The ORM Framework is responsible for translating object-oriented requests into standard SQL statements, which are then sent to the backend db to execute, DB here in the case of MySQL, then a SQL comes in, the DB itself is also cached, but the DB cache is generally used in SQL language hash to access, also In other words, if you want the cache to hit, except for the fields and methods of the query, the parameters of the query must be exactly the same to be able to use the DB itself query cache, SQL through the query cache, and then the query parser, where the DB will be based on the index of the data table being searched, and the SQL The language itself, which determines the index of which field to use, is worth mentioning that even if a data table is indexed in multiple fields at the same time, it is only possible to use one index for an SQL statement, so there is a need to analyze which index is most efficient to use, in general, SQL Optimization At this point is also a very important aspect.


After SQL returns the result set from DB, the ORM Framework transforms the result into a model object, which is then logically processed by the ORM framework, which sends the prepared data to the rendering engine of the view layer to render, the rendering engine is responsible for the management of the template, the friendly display of the fields, and also the tasks such as taking charge of some multinational languages. For a request in MVC life cycle, you can refer to here, copied a PHP MVC framework, after the view layer to prepare the page, and then sent back to the HTTP server from the dynamic Script interpreter, the HTTP server to add these bodies to a response header, encapsulated into a standard HTTP response packet, and then through the TCP IP protocol, sent back to the client browser.




From Click to render-a detailed HTTP request (4) After a long time, we request the response finally reached the client's browser, the response to the browser, the browser first determine the status code, if it is 200 to start the good, directly into the rendering process, if it is 300 beginning to go to the corresponding head inside to find Location domain, according to the guidance of this location, to jump, here jumps need to open a jump counter, is to avoid two or more pages formed between the cycle of jump, when the number of jumps, the browser will error, and stop. If it is a status code beginning with 400 or 500, the browser will also give you an error page.


When the browse to get a correct 200 response, the next problem is the multi-language encoding parsing, the response header is an ASCII standard character set of text, this is OK, but the body of the response is essentially a stream of bytes, for this lump of the stream of bytes, the browser how to deal with it, First, the browser will look at the response header within the specified encoding domain, if there is this thing, then according to the specified encoding to parse the character, if not, then the browser will use some more intelligent way to guess and determine what character set to use the byte stream to decode. Related notes can be seen here, browser-to-code determination


Solve the problem of the character set, the next step is to build the DOM tree, in the HTML language nesting normal and normative situation, this XML markup language is relatively easy to build a DOM tree out, of course, for the Internet a large number of irregular pages, Different browsers should have their own different fault-tolerant to handle. The building of the DOM is essentially an abstract logical tree, the process of building a DOM tree, if encountered by script tags wrapped in the JS dynamic script code, then the code will be sent to the JS engine to run, if encountered a style tag surrounded by CSS code, will be saved, with Rendering at a later time. If you encounter the label of external files such as IMG, then the browser will launch a new HTTP request according to the specified URL, to pull back this file, it is worth mentioning that for the same domain name of the download process, the browser generally allow the concurrent request is limited, usually controlled at two or so, So if there are a lot of pictures, generally for the purpose of optimization, they will use a static file of the server to save, responsible for response, thereby reducing the pressure on the primary server.


After the DOM tree is constructed, the render tree is constructed based on the DOM tree and CSS stylesheet, which is the real one for rendering to a rectangle on the page, and for each box on the render tree, you need to determine his x y coordinate, size, border, font, shape, etc. Things, the render tree once built, the entire page is ready to serve.


Need to explain is, download page, build DOM tree, build render tree These three steps, actually not is strict order, in order to speed up, improve efficiency, let the user not wait so long, now generally parallel forward, modern browser is one side to download, Download to a little bit of data and start building the DOM tree, and start building the render tree, and build a little bit to show it, so the user doesn't seem to have to wait that long.


Web && Network from Click to render-a detailed HTTP request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.