📜 ⬆️ ⬇️

What actually happens when a user types google.com into a browser



This article is an attempt to answer the old question for interviews: “What happens when you type google.com in the address bar and press Enter?” We will try to understand this in as much detail as possible without missing a single detail.

Note: the publication is based on the content of the repository What happens when ...
')
Submitted content is replete with a large number of terms, some of them may contain various inaccuracies. If you find any mistake in our translation - write a personal message, and we will fix it.

We moved the translation to the GitHub repository and sent the Pull Request to the author of the material - leave your changes to the text, and together we can significantly improve it.

1. "g" key pressed


The rest of this article contains information about how the physical keyboard works and how the operating system interrupts. But a lot of things are happening and besides this - when you press the "g" key, the browser receives an event and the auto-substitution mechanism starts. Depending on the algorithm of the browser and its mode (whether the function is incognito) in the drop-down box under the URL line, the user will be offered a certain number of options for automatic substitution.

Most auto-substitution algorithms rank recommendations based on search history and bookmarks left. Some browsers (for example, Rockmelt) even offer profiles of friends on Facebook. When the user plans to type “google.com” in the address bar, none of the above is important, but a large amount of code will be executed, and the recommendations will be updated with each new letter. The browser may offer to go to google.com before the user enters the entire address.

2. “Enter” key pressed to the end


As a certain zero point, you can choose the moment when the Enter key on the keyboard is pressed to the end and is in the lower position. At this point, the electrical circuit of this key closes and a small amount of current is sent through the keyboard's electrical circuit, which scans the state of each key switch and converts the signal to the integer key code (in this case, 13). The keyboard controller then converts the key code for transmission to the computer. As a rule, the transfer now takes place via USB or Bluetooth, and earlier the keyboard was connected to a computer using PS / 2 or ADB connectors.

In the case of a USB keyboard:


In the case of a virtual keyboard (touchscreen):


2.1 An interrupt has occurred [not for USB keyboards]


The keyboard sends signals to its “interrupt request line” (IRQ), which is then mapped to an “interrupt vector” (integer) interrupt controller. The processor uses the “Interrupt Descriptor Table” (IDT) to map interrupt vectors to functions (“interrupt handlers”) of the kernel. When an interrupt occurs, the processor (CPU) updates the IDT with the interrupt vector and starts the corresponding handler. Thus, the core comes into play.

2.2 (On Windows) WM_KEYDOWN message sent to application


HID sends the event of the key pressed to the KBDHID.sys driver, which converts it into a scan code (scancode). In this particular case, the scan code is VK_RETURN ( 0x0D ). The KDBHID.sys driver KDBHID.sys associated with the KBDCLASS.sys driver (keyboard class driver). He is responsible for the safe handling of all keyboard input. In the future, this driver causes Win32K.sys (after a possible message transfer through installed third-party keyboard filters). All this happens in kernel mode.

Win32K.sys determines which window is currently active using the GetForegroundWindow() function. This API provides processing of the address bar window in the browser. Then the main “message pump” of Windows calls SendMessage(hWnd, WM_KEYDOWN, VK_RETURN, lParam) . lParam is a bitmask that indicates further information about a key press: a repeat counter (0 in this case), the current scan code (may depend on the OEM, but VK_RETURN usually does not depend on it), whether or not the additional keys (for example, Alt, Shift, Ctrl - in our case were not) and some other data.

The Windows API has the SendMessage function, which places a message in a queue for a particular window handler ( hWnd ). After that, the main message handling function ( WindowProc ) assigned to the hWnd handler is hWnd to process all messages in the queue.

The window ( hWnd ) currently active is the processing control, and in this case, WindowsProc has a handler for the WM_KEYDOWN messages. This code examines the third parameter that entered SendMessage (wParam) and, since it is VK_RETURN , understands that the user has pressed the ENTER key.

2.3 (On OS X) NSEVent KeyDown sent to application


The interrupt signal triggers the interrupt event in the keyboard I / O Kit driver. The driver translates the signal into a keyboard code, which is then passed to an OS X process called WindowServer . As a result, WindowsServer sends the event to any suitable (active or “listening”) application via the Mach port in which the event is placed in the queue. Events can then be read from this queue by threads with sufficient privileges to call the mach_ipc_dispatch function. This most often occurs and is processed using the NSApplication main loop via NSEvent in the NSEventype KeyDown .

2.4 (GNU / Linux) Xorg server listens to keyboard codes


In the case of a graphical X server, the generic evdev event driver will be used to get the keystroke. Reassignment of keyboard codes to scan codes is performed using special rules and X Server cards. When the scan-key mapping is completed, the X server sends the symbol to the window manager (DWM, metacity, i3), which then sends it to the active window. The graphics API of the window that received the symbol prints the corresponding character of the font in the desired field.

3. Parsing URL


The browser now has the following URL information:

Protocol "HTTP"
Use "Hyper Text Transfer Protocol"

Resource "/"
Show home (index) page

3.1 Is this a URL or search query?


When a user does not enter a protocol or domain name, the browser feeds what the person typed to the default search engine. Often, a special text is added to the URL, which allows the search engine to understand that the information is transmitted from the URL string of a specific browser.

3.2 HSTS checklist



3.3 Converting Non-ASCII Unicode Characters to a Host Name



4. DNS definition



4.1 The process of sending an ARP request


In order to send a broadcast ARP request, you need to find the target IP address, and also know the MAC address of the interface that will be used to send the ARP request.

The ARP cache is checked for each target IP address - if the address is in the cache, then the library function returns the result: Target IP = MAC .

If there is no cache entry:


ARP request:

Sender MAC: interface:mac:address:here
Sender IP: interface.ip.goes.here
Target MAC: FF:FF:FF:FF:FF:FF (Broadcast)
Target IP: target.ip.goes.here

Depending on what "hardware" is located between the computer and the router (router):

Direct connection:


Between them a hub (hub):


Between them switch (switch):


ARP response:

Sender MAC: target:mac:address:here
Sender IP: target.ip.goes.here
Target MAC: interface:mac:address:here
Target IP: interface.ip.goes.here

Now the network library has the IP address of either the DNS server or the default gateway, which can be used to resolve the domain name:


5. Opening Socket


When the browser receives the IP address of the destination server, it takes this information and data about the port used from the URL (port 80 for HTTP, 443 for HTTPS) and calls the socket function of the system library and requests the TCP socket stream — AF_INET and SOCK_STREAM .


At this point, the packet is ready for transmission via:


In the case of the Internet connection of most private users or small companies, the package will be sent from a computer via a local network and then via a modem ( MOdulator/DEModulator ), which translates digital units and zeros into an analog signal suitable for transmission over a telephone line, cable or wireless phone connections. On the other side of the connection is another modem that converts the analog signal into digital data and sends them to the next network node , where the sender and receiver data are further analyzed.

Eventually, the packet will reach the router managing the local subnet. Then he will continue to travel from one router to another until he gets to the destination server. Each router in the path will extract the destination address from the IP header and send the packet to the next hop. The value of the TTL (time to live) field in the IP header will decrease each time after passing each router. If the value of the TTL field reaches zero, the packet will be dropped (this will also happen if the router does not have a place in the current queue - for example, due to network congestion).

During a TCP connection, there are many such requests and responses.

5.1 TCP Connection Life Cycle


a. The client selects the initial sequence number (ISN) and sends the packet to the server with the SYN bit set to open the connection.

b. The server receives a packet with a SYN bit and, if it is ready to establish a connection, then:


c. The client confirms the connection by sending a packet:


d. Data is transmitted as follows:


e. Connection closure:


6. TLS handshake



7. HTTP protocol


If the browser used was created by Google, then instead of sending an HTTP request to get the page, it will send a request to try to “negotiate” with the server about the “upgrade” of the protocol from HTTP to SPDY (“speed”).

If the client uses the HTTP protocol and does not support SPDY, then it sends to the server a request of the following form:

GET / HTTP/1.1
Host: google.com
Connection: close
[ ]

where [ ] is a series of key: value pairs broken by line breaks. (Here it is assumed that there are no errors in the browser used that violate the HTTP specification. It is also assumed that the browser uses HTTP/1.1 , otherwise it may not include the Host header in the request and the version sent in response to the GET request may be HTTP/1.0 or HTTP/0.9 ).

HTTP/1.1 defines the option to close the connection ("close") for the sender - with its help, the connection is closed after the response is completed. For example:

Connection: close

HTTP/1.1 applications that do not support persistent connections are required to include the “close” option in each message.

After sending the request and headers, the browser sends a single blank line to the server, signaling that the content of the message has ended.

The server responds with a special code that indicates the status of the request and includes the response of the following form:

200 OK
[ ]

After that, an empty line is sent, and then the remaining content of the HTML page of www.google.com . The server can then close the connection, or, if requested by the headers sent by the client, keep the connection open for use by the following requests.

If the HTTP headers sent by the web browser include information that is enough for the server to determine the version of the file cached in the browser and this file has not changed since the last request, the response can take the following form:

304 Not Modified
[ ]

and, accordingly, no content is sent to the client, instead the browser “gets” HTML from the cache.

After parsing HTML, the browser (and server) repeats the download process for each resource (images, styles, scripts, favicon.ico, etc.) referenced by the HTML page, but the address of each request changes with GET / HTTP/1.1 on GET /$( URL www.google.com) HTTP/1.1 GET /$( URL www.google.com) HTTP/1.1 .

If HTML refers to a resource hosted on a domain other than google.com, the browser returns to steps that include resolving the domain name, and then re-runs the process to its current state, but for a different domain. The Host header in the request instead of google.com will be set to the desired domain name.

7.1 Processing HTTP requests on the server


HTTPD (HTTP Daemon) is one of the server-side request / response processing tools. The most popular HTTPD servers are Apache or Nginx for Linux and IIS for Windows.

- HTTPD (HTTP Daemon) receives a request.

- The server parses the request according to the following parameters:


- The server checks the existence of a virtual host that corresponds to google.com.

- The server verifies that google.com can accept GET requests.

- The server checks if the client has the right to use this method (based on IP address, authentication, etc.).

- If a rewrite module is installed on the server ( mod_rewrite for Apache or URL Rewrite for IIS), then it matches the request with one of the configured rules. If a matching rule is found, the server uses it to rewrite the request.

- The server finds the content that matches the request, in our case it will examine the index file.

- Next, the server parses ("parsit") the file using a handler. If Google is working in PHP, the server uses PHP to interpret the index file and sends the result to the client.

8. Behind the scenes of the browser


The task of the browser is to show the user selected web resources, requesting them from the server and displaying it in the browser window. Typically, these resources are HTML documents, but it can be PDF, images or other content. The location of the resources is determined by the URL.

The way that the browser uses to interpret and display HTML files is described in the HTML and CSS specifications. These documents are developed and maintained by the World Wide Wib Consortium (W3C) consortium, which deals with web standardization.

Browser interfaces are very similar to each other. They have a large number of identical elements:


High-level browser structure


The browser includes the following components:


9. HTML


. , 8. HTML- .

(«parse tree») — DOM- . DOM — Document Object Model . HTML- HTML- « » (, JavaScript-). «».


HTML- «» ( ). There are several reasons for this:


, HTML. HTML5 .

: .


, (, , ).

, , «» : , . « complete » (« load »).

: «Invalid Syntax» , «» .

10. CSS



11.



12. GPU



13. -


, JavaScript- ( Google) ( ). Flash Java ( Google). , , .

Source: https://habr.com/ru/post/254825/


All Articles