The process that a browser goes through from entering a link to rendering the page.
First, the browser performs a DNS
resolution on the entered link, which is the process of converting a domain name to an IP
address, obtaining the specific IP
address of the server and enabling TCP
connection and data transmission.
In the specific DNS
resolution process, the browser first checks its own DNS
cache for the IP
address of the domain name. In Chrome
, the caching time for domain name resolution is 60s
, and the DNS
cache can be cleared by entering chrome://net-internals/#dns
in the address bar. If the browser's cache does not have a hit, it then checks the domain-name-to-IP
mapping in the operating system's hosts
file. If there is no hit in the hosts
file, it then requests resolution from the local domain name server, which is generally provided by the internet service provider (ISP
) and typically sends UDP
datagram requests to the server for DNS
resolution through port 53
. If the local server resolution does not yield a hit, there are two resolution methods: iterative resolution and recursive resolution. Generally, the host's query to the local domain name server is a recursive query, and the local domain name server's query to the root domain name server is typically an iterative query, querying from the root domain name server, top-level domain name server, primary domain name server, etc., level by level, until it finds the IP
address.
The HTTP
protocol uses the TCP
protocol as its transport layer protocol. After obtaining the server's IP
address, the client browser establishes a TCP
connection with the server, a process that involves a three-way handshake.
client server
actively open → SYN=1,seq=x → passively open, receive
(synchronize sent) (synchronize received)
receive ← SYN=1,ACK=1,seq=y,ack=x+1 ← send
(connection established) (synchronize received)
send → ACK=1,seq=x+1,ack=y+1 → receive
(connection established) (connection established)
- First handshake: The client actively connects to the server, sends the initial sequence number
seq=x
and theSYN=1
synchronization request flag, and enters theSYN_SENT
state, waiting for confirmation from the server. - Second handshake: After receiving the message, the server sends the acknowledgment flag
ACK=1
and the synchronization request flagSYN=1
, sends its own sequence numberseq=y
and the client's acknowledgment numberack=x+1
, at this point the server enters theSYN_RECV
state. - Third handshake: After receiving the message, the client sends the acknowledgment flag
ACK=1
, sends its own sequence numberseq=x+1
and the server's acknowledgment numberack=y+1
, and after sending, the connection is confirmed to be in theESTABLISHED
state, and the server enters theESTABLISHED
state upon receiving the confirmation information.
The establishment of SSL
is for the encrypted transmission of HTTPS
. HTTPS
adds the SSL
layer on the basis of HTTP
, and the security foundation of HTTPS
is SSL
, so the details of the encryption require SSL
.
- First, the
TCP
three-way handshake establishes the connection, which is the foundation of data transmission, and thenSSL
starts. - The client first sends
Client Hello
to start theSSL
communication. The message contains theSSL
version supported by the client, a random valueRandom1
, encryption algorithm, and key length. - The server sends
Server Hello
, which, like the client, includes theSSL
version, random valueRandom2
, and encryption components. The server then sends the certificate to the client. - At this point, the client needs to verify the certificate sent by the server. By using the built-in
CA
certificate of the operating system, the client decrypts the digital signature of the server's certificate and compares the public key of the certificate with the same algorithm'sHASH
and the decrypted content of the digital signature, verifying whether the certificate is valid and legitimate or whether it has been hijacked and replaced. - The client verifies the certificate as valid, then generates a random value
Random3
, encrypts it with the public key, generates thePre-Master Key
, and sends thePre-Master Key
to the server in aClient Key Exchange
message. Then it sends aChange Cipher Spec
message to indicate that subsequent data transmission will be encrypted. - The server decrypts the
Pre-Master Key
with its private key to getRandom3
, and then sends aChange Cipher Spec
message to indicate that subsequent data transmission will be encrypted. - At this point, both the client and the server have three random strings, and
Random3
is transmitted in ciphertext, which is in a secure state, and these three strings can then be used for symmetrically encrypted transmission. Asymmetric encryption is slow and cannot be used for every data transmission, so it is used to negotiate the key and then use symmetric encryption for data transmission. - At this point, normal
HTTP
data transmission can occur, but due to the effect ofSSL
encryption, theHTTP
transmission at this point is secure. This is the process ofHTTPS
transmission, where steps2
,3
,5
, and6
are also known as theSSL
four-way handshake.
The browser constructs an HTTP
request message and transmits it to the server's specified port via the TCP
protocol. An HTTP
request message consists of three parts: the message header, usually containing the request line and various request header fields; a blank line (telling the server that the request header ends here); and the message body, which contains the transmitted data and is not necessarily required.
<!-- Message header -->
GET / HTTP/1.1 <!-- Request line -->
accept: text/html
accept-encoding: gzip, deflate, br
accept-language: zh-CN, zh;q=0.9
... <!-- Request header -->
<!-- Blank line -->
<!-- Message body -->
u=1&t=1587699008
The server responds to the HTTP
request by returning a response message, which consists of four parts: the response line, response header, blank line, and response body.
HTTP/1.1 200 OK <!-- Response line -->
content-encoding: gzip
content-type: text/html; charset=utf-8
date: Fri, 24 Apr 2020 03:34:50 GMT
... <!-- Response header -->
<!-- Blank line -->
<!-- Response body -->
{"status":1, "msg": "success"}
- From top to bottom, the
HTML
tags are first parsed to generate theDOM Tree
. - When parsing encounters a
<link>
or<style>
tag, theCSS
is parsed to generate theCSSOM
. It's worth noting that at this point, parsing ofHTML
tags andCSS
is executed in parallel. - When a
<script>
tag is encountered, the browser immediately begins script parsing, halting the document parsing. This is because scripts may modify theDOM
andCSS
, and continuing parsing would waste resources. Therefore,<script>
tags should be placed after the<body></body>
. - Once the
DOM Tree
andCSSOM
are generated, they are merged to perform layout, calculating their size, position, and layout information to form an internal representation model that can represent all this information, known as the render tree. - The entire page is drawn based on the calculated information. The system traverses the render tree and invokes the
paint
method to display the content on the screen.
client server
Active close → FIN=1,seq=u → Passive close, receive
(LAST-ACK) (CLOSE-WAIT)
Receive ← ACK=1,seq=v,ack=u+1 ← Send
(TIME-WAIT) (CLOSED)
Receive ← FIN=1,ACK=1,seq=w,ack=u+1 ← Send
(CLOSED) (CLOSED)
Send → ACK=1,seq=u+1,ack=w+1 → Receive
(CLOSED) (CLOSED)
- First handshake: The client sends a release indication
FIN=1
, its own sequence numberseq=u
, and enters the terminated waitingFIN-WAIT-1
state. - Second handshake: After receiving the message, the server sends
ACK=1
acknowledgment flag and the client's acknowledgment numberack=u+1
, its own sequence numberseq=v
, and enters the closed waitingCLOSE-WAIT
state. The client enters the terminated waitingFIN-WAIT-2
state upon receiving the message. - Third handshake: The server sends the release indication
FIN=1
signal, acknowledgment flagACK=1
, acknowledgment numberack=u+1
, its own sequence numberseq=w
, and transitions to the last acknowledgmentLAST-ACK
state. - Fourth handshake: Upon receiving the reply, the client sends the acknowledgment flag
ACK=1
, acknowledgment numberack=w+1
, its own sequence numberseq=u+1
, and enters the time waitingTIME-WAIT
state. After2
maximum segment lifetimes, the clientCLOSE
s. Upon receiving the acknowledgment, the server immediately enters theCLOSE
state.
TCP Three-Way Handshake and Four-Wave Goodbye https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/TCP%E4%B8%89%E6%AC%A1%E6%8F%A1%E6%89%8B.md
Overview of HTTP Protocol https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/HTTP%E5%8D%8F%E8%AE%AE%E6%A6%82%E8%BF%B0.md
Process of HTTPS Encrypted Transmission https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/HTTPS%E5%8A%A0%E5%AF%86%E4%BC%A0%E8%BE%93%E8%BF%87%E7%A8%8B.md
Browser Rendering and Kernel https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B8%B2%E6%9F%93%E4%B8%8E%E5%86%85%E6%A0%B8.md
Symmetric Encryption and Asymmetric Encryption https://github.com/WindrunnerMax/EveryDay/blob/master/Browser/%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86%E4%B8%8E%E9%9D%9E%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86.md
https://github.com/WindrunnerMax/EveryDay
https://www.jianshu.com/p/d616d887953a
https://www.cnblogs.com/lhh520/p/10232738.html
https://blog.csdn.net/bjweimengshu/article/details/78978314
https://blog.csdn.net/wlk2064819994/article/details/79756669
https://blog.csdn.net/weixin_40659167/article/details/86510745