awesome-interview/book1/topic-enter-url-display-xx....

24 lines
32 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Docusaurus v2.0.0-beta.5">
<link rel="search" type="application/opensearchdescription+xml" title="HZFE - 剑指前端 Offer" href="/awesome-interview/opensearch.xml">
<link rel="preconnect" href="https://hm.baidu.com">
<script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?c7cd0fd77ac518cc6ef46461cdc9524b";var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(e,c)}()</script><title data-react-helmet="true">浏览器从输入网址到页面展示的过程 | HZFE - 剑指前端 Offer</title><meta data-react-helmet="true" name="twitter:card" content="summary_large_image"><meta data-react-helmet="true" property="og:url" content="https://febook.hzfe.org/awesome-interview/book1/topic-enter-url-display-xx"><meta data-react-helmet="true" name="docsearch:language" content="en"><meta data-react-helmet="true" name="docsearch:version" content="current"><meta data-react-helmet="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-react-helmet="true" property="og:title" content="浏览器从输入网址到页面展示的过程 | HZFE - 剑指前端 Offer"><meta data-react-helmet="true" name="description" content="回答关键点"><meta data-react-helmet="true" property="og:description" content="回答关键点"><link data-react-helmet="true" rel="shortcut icon" href="/awesome-interview/img/favicon.ico"><link data-react-helmet="true" rel="canonical" href="https://febook.hzfe.org/awesome-interview/book1/topic-enter-url-display-xx"><link data-react-helmet="true" rel="alternate" href="https://febook.hzfe.org/awesome-interview/book1/topic-enter-url-display-xx" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://febook.hzfe.org/awesome-interview/book1/topic-enter-url-display-xx" hreflang="x-default"><link data-react-helmet="true" rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/awesome-interview/assets/css/styles.304d13b7.css">
<link rel="preload" href="/awesome-interview/assets/js/runtime~main.e4536af0.js" as="script">
<link rel="preload" href="/awesome-interview/assets/js/main.52f9823f.js" as="script">
</head>
<body>
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div><a href="#" class="skipToContent_1oUP">Skip to main content</a></div><nav class="navbar navbar--fixed-top navbarHideable_2qcr"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ navbar__logo"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m navbar__logo"><b class="navbar__title">剑指前端 Offer</b></a></div><div class="navbar__items navbar__items--right"><a href="https://github.com/hzfe/awesome-interview" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link"><span>GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_3J9K"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a><div class="searchBox_1Doo"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docs-wrapper docs-doc-page"><div class="docPage_31aa"><button class="clean-btn backToTopButton_35hR" type="button"><svg viewBox="0 0 24 24" width="28"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" fill="currentColor"></path></svg></button><aside class="docSidebarContainer_3Kbt"><div class="sidebar_15mo sidebarWithHideableNavbar_267A"><a tabindex="-1" class="sidebarLogo_3h0W" href="/awesome-interview/"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--light_3UqQ"><img src="/awesome-interview/img/badge.svg" alt="HZFE" class="themedImage_1VuW themedImage--dark_hz6m"><b>剑指前端 Offer</b></a><nav class="menu thin-scrollbar menu_Bmed menuWithAnnouncementBar_2WvA"><ul class="menu__list"><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/about">关于我们</a></li><li class="menu__list-item"><a class="menu__link" href="/awesome-interview/">前言</a></li><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#">模拟题一</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-cross-origin">浏览器:浏览器跨域</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/browser-repain-reflow">浏览器:浏览器的重排重绘</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/engineer-webpack-workflow">工程化Webpack 工作流程</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-data-binding">框架Vue 的数据绑定机制</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/frame-vue-computed-watch">框架Vue 的 computed 和 watch 的区别</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-closures">基础:闭包的作用和原理</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/js-module-specs">基础:前端模块化规范</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/css-bfc">样式BFC 的形成和作用</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/network-security">网络:前端安全</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/coding-promise">编码:实现一个 Promises/A+</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/awesome-interview/book1/algorithm-balanced-binary-trees">算法:平衡二叉树</a></li><li class="menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/awesome-interview/book1/topic-enter-url-display-xx">综合:浏览器从输入网址到页面展现的过程</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题二</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">模拟题三</a></li></ul></nav><button type="button" title="Collapse sidebar" aria-label="Collapse sidebar" class="button button--secondary button--outline collapseSidebarButton_1CGd"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_3E-R"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></aside><main class="docMainContainer_3ufF"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_3FnS"><div class="docItemContainer_33ec"><article><div class="tocCollapsible_1PrD tocMobile_3Hoh"><button type="button" class="clean-btn tocCollapsibleButton_2O1e">On this page</button></div><div class="markdown"><header><h1>浏览器从输入网址到页面展示的过程</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="回答关键点"></a>回答关键点<a class="hash-link" href="#回答关键点" title="Direct link to heading">#</a></h2><p><code>URL</code> <code>DNS</code> <code>TCP</code> <code>渲染</code></p><p>浏览器从输入网址到渲染页面主要分为以下几个过程</p><ul><li>URL 输入</li><li>DNS 解析</li><li>建立 TCP 连接</li><li>发送 HTTP / HTTPS 请求(建立 TLS 连接)</li><li>服务器响应请求</li><li>浏览器解析渲染页面</li><li>HTTP 请求结束,断开 TCP 连接</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="知识点深入"></a>知识点深入<a class="hash-link" href="#知识点深入" title="Direct link to heading">#</a></h2><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="1-url-输入"></a>1. URL 输入<a class="hash-link" href="#1-url-输入" title="Direct link to heading">#</a></h3><p><img src="https://user-images.githubusercontent.com/15681693/131242120-9965c535-cc8b-41fe-8db1-b3aa228e9d04.png" alt="URL地址"></p><p>URL统一资源定位符Uniform Resource Locator用于定位互联网上资源俗称网址。</p><p>我们在地址栏输入 HZFE 官方网址 hzfe.org 后敲下回车,浏览器会对输入的信息进行以下判断:</p><ol><li>检查输入的内容是否是一个合法的 URL 链接。</li><li>是,则判断输入的 URL 是否完整。如果不完整,浏览器可能会对域进行猜测,补全前缀或者后缀。</li><li>否,将输入内容作为搜索条件,使用用户设置的默认搜索引擎来进行搜索。</li></ol><p>大部分浏览器会从历史记录、书签等地方开始查找我们输入的网址,并给出智能提示。</p><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="2-dnsdomain-name-system解析"></a>2. DNSDomain Name System解析<a class="hash-link" href="#2-dnsdomain-name-system解析" title="Direct link to heading">#</a></h3><p>因为浏览器不能直接通过域名找到对应的服务器 IP 地址,所以需要进行 DNS 解析,查找到对应的 IP 地址进行访问。</p><p>DNS 解析流程如下:</p><p><img src="https://user-images.githubusercontent.com/17002181/131223396-d385b4a9-1952-402f-90e8-ab98e5f3f5e6.png" alt="DNS 解析"></p><ol><li>在浏览器中输入 hzfe.org 域名,操作系统检查浏览器缓存和本地的 hosts 文件中,是否有这个网址记录,有则从记录里面找到对应的 IP 地址,完成域名解析。</li><li>查找本地 DNS 解析器缓存中,是否有这个网址记录,有则从记录里面找到对应的 IP 地址,完成域名解析。</li><li>使用 TCP/IP 参数中设置的 DNS 服务器进行查询。如果要查询的域名包含在本地配置区域资源中,则返回解析结果,完成域名解析。</li><li>检查本地 DNS 服务器是否缓存该网址记录,有则返回解析结果,完成域名解析。</li><li>本地 DNS 服务器发送查询报文至根 DNS 服务器,根 DNS 服务器收到请求后,用顶级域 DNS 服务器地址进行响应。</li><li>本地 DNS 服务器发送查询报文至顶级域 DNS 服务器。顶级域 DNS 服务器收到请求后,用权威 DNS 服务器地址进行响应。</li><li>本地 DNS 服务器发送查询报文至权威 DNS 服务器,权威 DNS 服务器收到请求后,用 hzfe.org 的 IP 地址进行响应,完成域名解析。</li></ol><p>查询通常遵循以上流程,从请求主机到本地 DNS 服务器的查询是递归查询DNS 服务器获取到所需映射的查询过程是迭代查询。</p><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="3-建立-tcp-连接"></a>3. 建立 TCP 连接<a class="hash-link" href="#3-建立-tcp-连接" title="Direct link to heading">#</a></h3><blockquote><p>世界上几乎所有的 HTTP 通信都是由 TCP/IP 承载的TCP/IP 是全球计算机及网络设备都在使用的一种常用的分组交换网络分层。 HTTP 的连接实际上就是 TCP 连接以及其使用规则。 《HTTP 权威指南》</p></blockquote><p>当浏览器获取到服务器的 IP 地址后浏览器会用一个随机的端口1024 &lt; 端口 &lt; 65535向服务器 80 端口发起 TCP 连接请求HTTP 默认约定 80 端口HTTPS 为 443 端口)。这个连接请求到达服务端后,通过 TCP 三次握手,建立 TCP 的连接。</p><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="31-分层模型"></a>3.1 分层模型<a class="hash-link" href="#31-分层模型" title="Direct link to heading">#</a></h4><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly"><pre tabindex="0" class="prism-code language-undefined codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token plain"> ----------------------------------</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 7| 应用层 | | HTTP |</span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 6| 表示层 | 应用层 |</span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 5| 会话层 | | |</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> ---------------------------------</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 4| 传输层 | 传输层 | TCP TLS |</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> ---------------------------------</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 3| 网络层 | 网络层 | IP |</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> ---------------------------------</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 2| 数据链路层</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> | 链路层</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 1| 物理层</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> --------------------------------</span></span><span class="token-line" style="color:#393A34"><span class="token plain">   [OSI] | [TCP/IP]</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="32-tcp-三次握手"></a>3.2 TCP 三次握手<a class="hash-link" href="#32-tcp-三次握手" title="Direct link to heading">#</a></h4><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly"><pre tabindex="0" class="prism-code language-undefined codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token plain"># SYN 是建立连接时的握手信号TCP 中发送第一个 SYN 包的为客户端,接收的为服务端</span></span><span class="token-line" style="color:#393A34"><span class="token plain"># TCP 中,当发送端数据到达接收端时,接收端返回一个已收到消息的通知。这个消息叫做确认应答 ACK</span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> 假设有客户端A服务端B。我们要建立可靠的数据传输。</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> SYN(=j) // SYN: A 请求建立连接</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> A ----------&gt; B</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> |</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> ACK(=j+1) | // ACK: B 确认应答 A 的 SYN</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> SYN(=k) | // SYN: B 发送一个 SYN</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> A &lt;-----------</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> |</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> | ACK(=k+1)</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> -----------&gt; B // ACK: A 确认应答 B 的包</span></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block">
</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><ol><li>客户端发送 SYN 包seq = j到服务器并进入 SYN_SEND 状态,等待服务器确认。</li><li>服务器收到 SYN 包,必须确认客户的 SYNACK = k + 1同时自己也发送一个 SYN 包seq = k即 SYN+ACK 包,此时服务器进入 SYN_RECV 状态。</li><li>客户端收到服务器的 SYN+ACK 包,向服务器发送确认包 ACKACK = k + 1此包发送完毕客户端和服务器进入 ESTABLISHED 状态,完成三次握手。</li></ol><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="4-tls-协商"></a>4. TLS 协商<a class="hash-link" href="#4-tls-协商" title="Direct link to heading">#</a></h3><p><img src="https://user-images.githubusercontent.com/15681693/125198502-bbe02c00-e294-11eb-8764-1e4674977b40.png" alt="TLS协商"></p><p>建立连接后就可以通过 HTTP 进行数据传输。如果使用 HTTPS会在 TCP 与 HTTP 之间多添加一层协议做加密及认证的服务。HTTPS 使用 SSLSecure Socket Layer 和 TLSTransport Layer Security 协议,保障了信息的安全。</p><ul><li><p>SSL</p><ul><li>认证用户和服务器,确保数据发送到正确的客户端和服务器。</li><li>加密数据防止数据中途被窃取。</li><li>维护数据的完整性,确保数据在传输过程中不被改变。</li></ul></li><li><p>TLS</p><ul><li>用于在两个通信应用程序之间提供保密性和数据完整性。该协议由两层组成TLS 记录协议TLS Record和 TLS 握手协议TLS Handshake。较低的层为 TLS 记录协议,位于某个可靠的传输协议(例如 TCP上面。</li></ul></li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="41-tls-握手协议"></a>4.1 TLS 握手协议<a class="hash-link" href="#41-tls-握手协议" title="Direct link to heading">#</a></h4><p><img src="https://user-images.githubusercontent.com/17002181/131222941-02fc347d-32c1-41ff-80b1-4f47a23f833f.png" alt="TLS握手协议"></p><ol><li>客户端发出一个 client hello 消息,携带的信息包括:所支持的 SSL/TLS 版本列表;支持的与加密算法;所支持的数据压缩方法;随机数 A。</li><li>服务端响应一个 server hello 消息,携带的信息包括:协商采用的 SSL/TLS 版本号;会话 ID随机数 B服务端数字证书 serverCA由于双向认证需求服务端需要对客户端进行认证会同时发送一个 client certificate request表示请求客户端的证书。</li><li>客户端校验服务端的数字证书;校验通过之后发送随机数 C该随机数称为 pre-master-key使用数字证书中的公钥加密后发出由于服务端发起了 client certificate request客户端使用私钥加密一个随机数 clientRandom 随客户端的证书 clientCA 一并发出。</li><li>服务端校验客户端的证书,并成功将客户端加密的随机数 clientRandom 解密;根据随机数 A/随机数 B/随机数 Cpre-master-key 产生动态密钥 master-key加密一个 finish 消息发至客户端。</li><li>客户端根据同样的随机数和算法生成 master-key加密一个 finish 消息发送至服务端。</li><li>服务端和客户端分别解密成功,至此握手完成,之后的数据包均采用 master-key 进行加密传输。</li></ol><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="5-服务器响应"></a>5. 服务器响应<a class="hash-link" href="#5-服务器响应" title="Direct link to heading">#</a></h3><p>当浏览器到 web 服务器的连接建立后,浏览器会发送一个初始的 HTTP GET 请求,请求目标通常是一个 HTML 文件。服务器收到请求后,将发回一个 HTTP 响应报文,内容包括相关响应头和 HTML 正文。</p><div class="codeBlockContainer_K1bP"><div class="codeBlockContent_hGly HTML"><pre tabindex="0" class="prism-code language-HTML codeBlock_23N8 thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_39YC"><span class="token-line" style="color:#393A34"><span class="token plain">&lt;html&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;head&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;meta charset=&quot;UTF-8&quot;/&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;title&gt;我的博客&lt;/title&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;link rel=&quot;stylesheet&quot; src=&quot;styles.css&quot;/&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;scrIPt src=&quot;index.js&quot;&gt;&lt;/scrIPt&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain">&lt;/head&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain">&lt;body&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;h1 class=&quot;heading&quot;&gt;首页&lt;/h1&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;p&gt;A paragraph with a &lt;a href=&quot;https://hzfe.org/&quot;&gt;link&lt;/a&gt;&lt;/p&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> &lt;scrIPt src=&quot;index.js&quot;&gt;&lt;/scrIPt&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain">&lt;/body&gt;</span></span><span class="token-line" style="color:#393A34"><span class="token plain">&lt;/html&gt;</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_Ue-o clean-btn">Copy</button></div></div><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="51-状态码"></a>5.1 状态码<a class="hash-link" href="#51-状态码" title="Direct link to heading">#</a></h4><p>状态码是由 3 位数组成,第一个数字定义了响应的类别,且有五类可能取值</p><ul><li>1xx指示信息——表示请求已接收继续处理</li><li>2xx成功——表示请求已被成功接收、理解、接受</li><li>3xx重定向——要完成请求必须进行更进一步的操作</li><li>4xx客户端错误——请求有语法错误或请求无法实现</li><li>5xx服务器端错误——服务器未能实现合法的请求</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="52-常见的请求头和字段"></a>5.2 常见的请求头和字段<a class="hash-link" href="#52-常见的请求头和字段" title="Direct link to heading">#</a></h4><ul><li>Cache-Controlmust-revalidate、no-cache、private是否需要缓存资源</li><li>Connectionkeep-alive保持连接</li><li>Content-Encodinggzipweb 服务器支持的返回内容压缩编码类型)</li><li>Content-Typetext/htmlcharset=UTF-8文件类型和字符编码格式</li><li>DateSun 21 Sep 2021 06:18:21 GMT服务器消息发出的时间</li><li>Transfer-Encodingchunked服务器发送的资源的方式是分块发送</li></ul><h4><a aria-hidden="true" tabindex="-1" class="anchor anchor__h4 anchorWithHideOnScrollNavbar_3R7-" id="53-http-响应报文"></a>5.3 HTTP 响应报文<a class="hash-link" href="#53-http-响应报文" title="Direct link to heading">#</a></h4><p>响应报文由四部分组成(响应行 + 响应头 + 空行 + 响应体)</p><ul><li>状态行HTTP 版本 + 空格 + 状态码 + 空格 + 状态码描述 + 回车符CR + 换行符LF</li><li>响应头:字段名 + 冒号 + 值 + 回车符 + 换行符</li><li>空行:回车符 + 换行符</li><li>响应体:由用户自定义添加,如 post 的 body 等</li></ul><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="6-浏览器解析并绘制"></a>6. 浏览器解析并绘制<a class="hash-link" href="#6-浏览器解析并绘制" title="Direct link to heading">#</a></h3><p>不同的浏览器引擎渲染过程都不太一样,这里以 Chrome 浏览器渲染方式为例。</p><p><img src="https://user-images.githubusercontent.com/15681693/131242132-b299c8ec-1c61-4dc0-86b8-62b3bbdae612.png" alt="webkit render"></p><ol><li>处理 HTML 标记并构建 DOM 树。</li><li>处理 CSS 标记并构建 CSSOM 树。</li><li>将 DOM 与 CSSOM 合并成一个渲染树。</li><li>根据渲染树来布局,以计算每个节点的几何信息。</li><li>将各个节点绘制到屏幕上。</li></ol><h3><a aria-hidden="true" tabindex="-1" class="anchor anchor__h3 anchorWithHideOnScrollNavbar_3R7-" id="7-tcp-断开连接"></a>7. TCP 断开连接<a class="hash-link" href="#7-tcp-断开连接" title="Direct link to heading">#</a></h3><p>现在的页面为了优化请求的耗时默认都会开启持久连接keep-alive那么一个 TCP 连接确切关闭的时机,是这个 tab 标签页关闭的时候。这个关闭的过程就是<strong>四次挥手</strong>。关闭是一个全双工的过程,发包的顺序是不一定的。一般来说是客户端主动发起的关闭,过程如下图所示:
<img src="https://user-images.githubusercontent.com/15681693/131242424-462d98ac-3e6b-4004-b494-8e29efc584c3.png" alt="TCP 四次挥手"></p><ol><li>主动关闭方发送一个 FIN用来关闭主动方到被动关闭方的数据传送也就是主动关闭方告诉被动关闭方我已经不会再给你发数据了在 FIN 包之前发送出去的数据,如果没有收到对应的 ACK 确认报文,主动关闭方依然会重发这些数据),但此时主动关闭方还可以接受数据。</li><li>被动关闭方收到 FIN 包后,发送一个 ACK 给对方,确认序号为收到序号+1与 SYN 相同,一个 FIN 占用一个序号)。</li><li>被动关闭方发送一个 FIN用来关闭被动关闭方到主动关闭方的数据传送也就是告诉主动关闭方我的数据也发送完了不会再给你发数据了。</li><li>主动关闭方收到 FIN 后,发送一个 ACK 给被动关闭方,确认序号为收到序号+1至此完成四次挥手。</li></ol><h2><a aria-hidden="true" tabindex="-1" class="anchor anchor__h2 anchorWithHideOnScrollNavbar_3R7-" id="参考资料"></a>参考资料<a class="hash-link" href="#参考资料" title="Direct link to heading">#</a></h2><ol><li><a href="HTTPS://developer.mozilla.org/en-US/docs/Web/Performance/How_browsers_work" target="_blank" rel="noopener noreferrer">How_browsers_work</a></li><li><a href="HTTPS://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList" target="_blank" rel="noopener noreferrer">DOMTokenList</a></li><li><a href="HTTP://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html" target="_blank" rel="noopener noreferrer">图解 SSL/TLS 协议 </a></li><li><a href="HTTPS://zh.wikIPedia.org/wiki/%E5%9F%9F%E5%90%8D%E7%B3%BB%E7%BB%9F" target="_blank" rel="noopener noreferrer">DNS 域名系统</a></li></ol></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"><a class="pagination-nav__link" href="/awesome-interview/book1/algorithm-balanced-binary-trees"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">« 算法:平衡二叉树</div></a></div><div class="pagination-nav__item pagination-nav__item--next"><a class="pagination-nav__link" href="/awesome-interview/book2/browser-render-mechanism"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">浏览器:浏览器渲染机制 »</div></a></div></nav></div></div><div class="col col--3"><div class="tableOfContents_35-E thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#回答关键点" class="table-of-contents__link">回答关键点</a></li><li><a href="#知识点深入" class="table-of-contents__link">知识点深入</a><ul><li><a href="#1-url-输入" class="table-of-contents__link">1. URL 输入</a></li><li><a href="#2-dnsdomain-name-system解析" class="table-of-contents__link">2. DNSDomain Name System解析</a></li><li><a href="#3-建立-tcp-连接" class="table-of-contents__link">3. 建立 TCP 连接</a></li><li><a href="#4-tls-协商" class="table-of-contents__link">4. TLS 协商</a></li><li><a href="#5-服务器响应" class="table-of-contents__link">5. 服务器响应</a></li><li><a href="#6-浏览器解析并绘制" class="table-of-contents__link">6. 浏览器解析并绘制</a></li><li><a href="#7-tcp-断开连接" class="table-of-contents__link">7. TCP 断开连接</a></li></ul></li><li><a href="#参考资料" class="table-of-contents__link">参考资料</a></li></ul></div></div></div><div class="row"><div class="col"><div class="react-utterences"><div>Loading script...</div></div></div><div class="col col--3"></div></div></div></main></div></div></div>
<script src="/awesome-interview/assets/js/runtime~main.e4536af0.js"></script>
<script src="/awesome-interview/assets/js/main.52f9823f.js"></script>
</body>
</html>