# What happens when you request a website?

**High level view:**\
When you enter "example.com" into your web browser and press enter, several things happen behind the scenes to bring up the website in your browser. Here is an overview of the process:

1. The browser looks up the IP address associated with "example.com" using the Domain Name System (DNS). DNS is a system that converts human-friendly domain names like "example.com" into machine-friendly IP addresses like "192.168.1.1". This step is similar to how a phonebook works, it converts name to numbers.
2. Once the browser has the IP address, it sends a request to the server located at that address using the Hypertext Transfer Protocol (HTTP). HTTP is the protocol used for exchanging data between a web browser and a web server.
3. The server receives the request and processes it. The server may check for the necessary parameters, authenticate the user and validate the request.
4. The server then sends a response to the browser, which is typically in the form of an HTML, CSS and JavaScript files that the browser can interpret and render as a webpage.
5. The browser receives the response and renders the webpage on the user's screen. The browser uses the HTML to define the structure of the page, the CSS to define the layout and styling, and the JavaScript to define the interactive functionality.
6. Depending on the webpage, it may make additional requests to the server to retrieve additional resources like images, videos or other media.
7. Once the webpage is completely loaded, the user can interact with it, such as clicking links, filling out forms, or making purchases. These interactions will generate new requests to the server, which will be processed and responded to in a similar way as the initial request.

**Low Level View:**\
When you enter "example.com" into your web browser, here is a more detailed, low-level explanation of the steps that happen behind the scenes:

1. The browser first checks its local cache to see if the IP address for "example.com" has been recently looked up. If it finds a match, it uses that IP address to connect to the server. If it does not find a match, it moves on to the next step.
2. The browser then checks its local hosts file, which is a simple text file stored on your computer that maps domain names to IP addresses. If it finds a match in the hosts file, it uses that IP address to connect to the server. If it does not find a match, it moves on to the next step.
3. The browser then sends a request to the configured DNS resolver, it could be configured on your computer or your router, which is responsible for resolving domain names to IP addresses. The DNS resolver first checks its local cache to see if it has recently looked up the IP address for "example.com". If it finds a match, it returns that IP address to the browser.
4. If the DNS resolver does not have the IP address in its cache, it starts to query the DNS servers on the internet in a specific order, known as the DNS resolution process. The DNS resolver will send a request to a root server, then to a top-level domain (TLD) server such as .com, .org or .net. The TLD server will then direct the request to the correct authoritative name server for the domain. The authoritative name server for "example.com" is the server that has the most up-to-date information about the IP address for that domain.
5. Once the webpage is completely loaded, the user can interact with it, such as clicking links, filling out forms, or making purchases. These interactions will generate new requests to the server, which will be processed and responded to in a similar way as the initial request.
6. The browser receives the response and uses the data in it to render the webpage on the user's screen. The browser uses the HTML to define the structure of the page, the CSS to define the layout and styling, and the JavaScript to define the interactive functionality.
7. Once the server has processed the request, it sends an HTTP response message back to the browser over the established TCP connection. The response message includes details such as the HTTP status code (e.g. 200 OK, 404 Not Found), any additional headers (e.g. Content-Type, Content-Length), and the body of the message which contains the actual data such as HTML, CSS, JavaScript, images, videos etc.
8. The server receives the request and processes it. The server may check for the necessary parameters, authenticate the user, and validate the request. Depending on the request, the server may query a database, execute a script, or retrieve a file from the file system.
9. The browser then sends an HTTP request message to the server over the established TCP connection. The request message includes details such as the HTTP method (e.g. GET, POST, PUT), the requested resource (e.g. /index.html), and any additional headers (e.g. Accept-Language, User-Agent) that provide more information about the request.

With the IP address in hand, the browser opens a TCP connection to the server using the IP address and port number 80 for HTTP traffic, or 443 for HTTPS traffic. The TCP protocol is used to establish a reliable, end-to-end connection between the browser and the server.

<br>
