HTTP request Process

Source: Internet
Author: User

Imagine using a browser to open the Imooc.com website, HTTP traversed the link:

1. First, it is the imooc.com domain name resolution,
(1.1) The browser searches the DNS cache of the browser itself. (DNS (domain Name System), a distributed database that maps domain names and IP addresses on the Internet, makes it easier for users to access the Internet without remembering the number of IP strings that can be read directly by the machine)
(1.2) If the browser does not find its own DNS cache or the previous cache is invalidated, the browser searches the operating system for its own DNS cache.
(1.3) If the DNS cache of the operating system is not found, then the system will try to find the local host file. (hosts is a system file without extension, can be opened with Notepad and other tools, its role is to use some of the common URL domain name and its corresponding IP address to establish an association "database", when the user in the browser to enter a need to log in the URL, The system will first automatically find the corresponding IP address from the Hosts file, once found, the system will immediately open the corresponding page, if not found, then the system will submit the URL to the DNS domain name resolution server for IP address resolution)
(1.4) If not found in the host, the browser initiates a DNS system call that typically initiates a domain name resolution request to a local broadband operator. This can be followed by a number of steps, first, the broadband carrier server will first look at its own cache to see if there is a result, if not, then the carrier server will initiate an iterative DNS resolution request (root domain, top-level domain, Domain name registrar), will eventually return the results of DNS resolution. The operator server then returns the results to the operating system kernel (also cached in its own cache), and the operating system returns the results to the browser.
(1.5) The final result of the above is to let the browser get imooc.com IP address, DNS resolution is complete.

2. Then, after the browser obtains the IP address of the domain name, initiate a "three-time handshake" to establish a TCP/IP connection.

3. After the TCP/IP connection is established, the browser can send an HTTP request to the server. For example, using the HTTP GET method to request a domain name in a root domain, the protocol can take HTTP 1.0.

4. The server accepts this request, according to the path parameter, after some processing of the backend, the processed result is returned to the browser in the form of data, and if it is the page of the Imooc.com website, the server will return the full HTML page code to the browser.

5. The browser has imooc.com the full HTML page code of this website, in parsing and rendering this page, the inside of JavaScript, CSS, pictures and other static resources, they are also a HTTP request, all need to go through the above steps to obtain.

6. The browser renders the page according to the resources it has received, and finally renders a complete page.
You can simply split the HTTP into requests and responses, and then they all have HTTP headers and body information, and the HTTP hair sends some additional information: the content type, the date the server sent the response, and the HTTP status code. The text is the form data submitted by the user.

Domain name resolution, domain name, cache, root domain dns-> top-level domain dns-> This domain dns-> server IP
1. Search the browser's own DNS cache if it does not exist or expires (>60s) Discard
2. Search the DNS cache of the operating system itself
3. Read the local Host file
4. Browser initiates a DNS system call for Domain name resolution
5. The client initiates a connection request with a three-time handshake using the TCP protocol server IP 80 port via a random port
6.TCP/IP the browser can initiate an HTTP request to the server after the connection request is established
7.http client initiates the request, creates the port, resolves the user operation, stitching the request header information
8.http client and sends request header information to this port on the server
9. Server listening ports such as 80
10.http Monitor Request header information sent to port 80
11.http Server parsing header information
The 12.http server returns the corresponding response header information according to the request header information response
13. The response header information is sent to the HTTP client, the client resolves the response header information, and completes other operations
14. Complete an HTTP request

Waiting (TTFB): Represents the time that the request takes to receive the first byte in a response.

Request method
Get: Request Resource
Post: Submit Resource
Put: Update
Delete: Remove
Head: similar to get
Trace
Options

Server Request Status Code:
1XX: Indicates that information has been received and is being processed
2XX: Successfully received and processed.
3XX: redirect
4XX: Client Error
5XX: Server-side error

1XX request received, processing XX
2XX, request accepted successfully, processing completed, successful return, 200=0k
3XX,, redirect
4XX, client error, 400 syntax error not understood, 401 request not authorized, 403 denied service, 404 Not Found Address, object does not exist
5XX, server-side error, 500 server unknown error, 503 server side is currently unable to process

Through their own understanding of these problems

1. Callback, that is, the subsequent processing of the function as a parameter in the precursor function, some operations in the precursor function to deal with

2. Synchronous and asynchronous

Synchronization means that the latter task must be executed after the previous task has completed, and the order of execution and the order of the tasks are consistent
Sync call wait query return results hang up phone <br>
Async call Leave number description query hang phone results return call completion <br>
Sync download wait to finish watch <br>
Asynchronous download a download b download c Download play cue sound See the sequence of <br>

3. Single Thread/multithreading
Today Sunday, more people, but you still a person to register yourself, you are single-threaded
In Sunday, the vortex naruto with multiple shadow copies to their own registration, Naruto is multi-threaded

4.i/o
When the disk is written, the nurse puts your information into the register, which is written
Disk read out you hung up to see the doctor, your turn, the doctor put your information out, 82-year-old unmarried, oh, elder single dog Ah, this is read out

5. Blocking/non-blocking
The doctor tells you that you have to check and check, and then a hammering for you, you can not do anything, only passively enjoy, you are blocked
Doctor Make a film let you go home, say look, two days after the phone call you results, you want to do what you do, you are non-blocking

6. Event/Event-driven <br>
Events are not executed immediately, but wait for drivers (such as click) to execute

In order to register a callback function for an event, but this callback function is not executed immediately, only when the event occurs, the callback function is called, this function is called event-driven-the registration callback is an event-driven callback, if these callbacks are related to asynchronous I/O operations, Can be seen as a callback-based asynchronous I/O, except that this callback is driven by events in the Nodejs

7. Event Loop callback function queue
If there is a large number of asynchronous operations and the time-consuming operation of the IO, even the timer-controlled delay operation, it is necessary to call the corresponding callback function, so as to complete the dense task, without blocking the whole process of the program, need a mechanism to manage, this mechanism is the event loop

Scope
The ability to access variables or functions, local scopes can access variables or functions under their external or global scope, and external or global scopes cannot access local scopes. JS is a function-level scope;

Context this
1.this internal use of functions
2.this is pointing to the function owner
3. As an object method, this points to the calling object itself; As a function call, this points to the global

Call apply to change the reference of the object specified by this

B.setmessage.call (A, "a message"): When a executes, the context object invokes the Setmessage method of the B object , which is equivalent to A.setmessage ("a message").

Also, the difference between call and apply is that itpasses parameters to the borrowed method as arguments, and apply directly puts the parameters in an array and then passes them.

Global variables Use this , point to function owner

The constructor uses this to point to the newly constructed function, the instance object.

Both call and apply are intended to change the context in which a function is run , in other words, to change the direction of this within the function body .

The two work exactly the same way, except that they accept the parameters differently.

For example, there is a function func1 defined as follows:

var func1 = function (Arg1, arg2) {};

Can be passed Func1.call (this, arg1, arg2); or Func1.apply (this, [Arg1, arg2]); to invoke. Where this is the context you want to specify, he can have any JavaScript object ( everything in JavaScript), call needs to pass parameters in order, and apply The parameter is placed in the array.

In JavaScript, the number of parameters of a function is not fixed, so to say the applicable conditions, when your parameters are clearly known quantity, with Call , but not sure, with apply, and then put the parameter push into the array to pass in. When the number of arguments is indeterminate, the arguments can also be used to facilitate all parameters within the function.

HTTP request Process

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.