TCP/IP: How to send a complete page to the browser

Description: The browser obtains page data from the server to ensure data integrity. So how do you set up connections and transmissions.

  • The IP protocol is responsible for addressing. In the IP header, the addresses of hosts A and B are included
  • The TCP protocol is responsible for establishing a connection, including three handshakes to establish the connection, data transmission, and four waves to disconnect the connection
  • There are two types of data packets, one is UDP and the other is TCP. UDP does not need to establish a long connection, so the efficiency is high. But it’s easy to make mistakes
  • TCP packets contain serial numbers to facilitate the receiving end’s reorganization. The amount of data to be sent is large, but the reliability of data transmission is ensured

The flow of HTTP requests

Description: All the steps that a browser goes through when making an HTTP request. Browser cache and cookies, etc

Question:

  1. How is HTTP related to TCP?

  2. How does the browser send the request?

  3. How does the server respond to HTTP requests?

  4. What caches are available in HTTP?

  5. How is the login state saved?

  6. How is HTTP related to TCP?

TCP is the transport layer and HTTP is the application layer. HTTP relies on the DATA transfer phase of TCP to transfer data

  1. How does the browser send the request?

The first step is to find the IP address through the URL and resolve the IP address through the DNS. The requested URL is cached locally by DNS. After you have the IP address, establish a TCP connection. Then it starts sending HTTP requests: request lines, request headers, and request bodies

  1. How does the server respond to HTTP requests?

First the server returns a response line, including the protocol version and status code. Status code 2 begins with success, and status code 3 begins with redirect. If a redirect is required, the server sends a response line telling the browser to request a different address

  1. What caches are available in HTTP?

A: DNS cache and page resource cache

Page resources are cached in such a way that the returned data is first cached for the time being, and if the cache does not expire on the next request, the data can be used directly. If it expires, you can ask the server if the resource has been modified, or you can use it and update the cache time if it has not.

  1. How is the login state saved?

The server identifies the data that needs to be stored in the cookie for the next request

Message queues and event loops

Browser processes need to handle various events, so good news queues and event loops need to be designed to ensure that the task is executed.

To design a good model, consider the following requirements:

  • Transactions are executed sequentially
  • New events can be inserted during thread execution
  • The current thread can receive tasks from other threads
  • The current process can receive tasks sent by other processes and send them to the main thread for execution
  • Improve the efficiency and real-time performance of message queues

Message queue: A first-in, first-out data structure that can receive tasks sent by other threads and process them in order

Event loop: Tasks can be continuously pulled from the message queue and executed

IO thread: Receives messages from network and browser processes and sends them to the render main thread

(To be continued)