在浏览器中键入URL时会发生什么[关闭]

本文翻译自:what happens when you type in a URL in browser [closed]

Can somebody tell me what all happens behind the scenes from the time I type in a URL in the browser to the time when I get to see the page on the browser? 从我在浏览器中输入URL到浏览器上的页面时,有人能告诉我幕后发生的一切吗? A detailed account of the process would be of great help. 详细介绍该过程将有很大帮助。


#1楼

参考:https://stackoom.com/question/8Mmr/在浏览器中键入URL时会发生什么-关闭


#2楼

Look up the specification of HTTP. 查找HTTP的规范。 Or to get started, try http://www.jmarshall.com/easy/http/ 或者要开始使用, 请访问http://www.jmarshall.com/easy/http/


#3楼

First the computer looks up the destination host. 首先,计算机查找目标主机。 If it exists in local DNS cache, it uses that information. 如果它存在于本地DNS缓存中,则使用该信息。 Otherwise, DNS querying is performed until the IP address is found. 否则,执行DNS查询,直到找到IP地址。

Then, your browser opens a TCP connection to the destination host and sends the request according to HTTP 1.1 (or might use HTTP 1.0, but normal browsers don't do it any more). 然后,您的浏览器打开与目标主机的TCP连接,并根据HTTP 1.1发送请求(或者可能使用HTTP 1.0,但普通浏览器不再执行此操作)。

The server looks up the required resource (if it exists) and responds using HTTP protocol, sends the data to the client (=your browser) 服务器查找所需的资源(如果存在)并使用HTTP协议响应,将数据发送到客户端(=您的浏览器)

The browser then uses HTML parser to re-create document structure which is later presented to you on screen. 然后,浏览器使用HTML解析器重新创建文档结构,稍后将在屏幕上显示该文档结构。 If it finds references to external resources, such as pictures, css files, javascript files, these are is delivered the same way as the HTML document itself. 如果它找到对外部资源的引用,例如图片,css文件,javascript文件,则它们的传递方式与HTML文档本身相同。


#4楼

Attention: this is an extremely rough and oversimplified sketch, assuming the simplest possible HTTP request (no HTTPS, no HTTP2, no extras), simplest possible DNS, no proxies, single-stack IPv4, one HTTP request only, a simple HTTP server on the other end, and no problems in any step. 注意:这是一个非常粗略和过于简化的草图,假设最简单的HTTP请求(没有HTTPS,没有HTTP2,没有额外的),最简单的DNS,没有代理,单栈IPv4,只有一个HTTP请求,一个简单的HTTP服务器另一端,任何一步都没有问题。 This is, for most contemporary intents and purposes, an unrealistic scenario; 对于大多数当代意图和目的而言,这是一个不切实际的场景; all of these are far more complex in actual use, and the tech stack has become an order of magnitude more complicated since this was written. 所有这些在实际使用中都要复杂得多,而且自编写以来,技术堆栈已变得更加复杂。 With this in mind, the following timeline is still somewhat valid: 考虑到这一点,以下时间表仍然有效:

  1. browser checks cache; 浏览器检查缓存; if requested object is in cache and is fresh, skip to #9 如果请求的对象在缓存中并且是新鲜的,请跳到#9
  2. browser asks OS for server's IP address 浏览器询问操作系统的服务器IP地址
  3. OS makes a DNS lookup and replies the IP address to the browser 操作系统进行DNS查找并将IP地址回复给浏览器
  4. browser opens a TCP connection to server (this step is much more complex with HTTPS) 浏览器打开到服务器的TCP连接(这一步用HTTPS复杂得多)
  5. browser sends the HTTP request through TCP connection 浏览器通过TCP连接发送HTTP请求
  6. browser receives HTTP response and may close the TCP connection, or reuse it for another request 浏览器接收HTTP响应并可以关闭TCP连接,或者将其重新用于另一个请求
  7. browser checks if the response is a redirect or a conditional response (3xx result status codes), authorization request (401), error (4xx and 5xx), etc.; 浏览器检查响应是重定向还是条件响应(3xx结果状态代码),授权请求(401),错误(4xx和5xx)等; these are handled differently from normal responses (2xx) 这些处理方式与正常答案不同(2xx)
  8. if cacheable, response is stored in cache 如果可缓存,则响应存储在缓存中
  9. browser decodes response (eg if it's gzipped) 浏览器解码响应(例如,如果它被gzip压缩)
  10. browser determines what to do with response (eg is it a HTML page, is it an image, is it a sound clip?) 浏览器确定如何处理响应(例如,它是一个HTML页面,它是一个图像,它是一个声音剪辑吗?)
  11. browser renders response, or offers a download dialog for unrecognized types 浏览器呈现响应,或为无法识别的类型提供下载对话框

Again, discussion of each of these points have filled countless pages; 再一次,对这些要点的讨论已经填满了无数页面; take this only as a summary, abridged for the sake of clarity. 为了清楚起见,仅将其作为总结,删节。 Also, there are many other things happening in parallel to this (processing typed-in address, speculative prefetching, adding page to browser history, displaying progress to user, notifying plugins and extensions, rendering the page while it's downloading, pipelining, connection tracking for keep-alive, cookie management, checking for malicious content etc.) - and the whole operation gets an order of magnitude more complex with HTTPS (certificates and ciphers and pinning, oh my!). 此外,还有许多其他事情与此并行(处理输入的地址,推测预取,添加页面到浏览器历史记录,向用户显示进度,通知插件和扩展,在下载时呈现页面,流水线,连接跟踪保持活跃,cookie管理,检查恶意内容等) - 并且整个操作使用HTTPS(证书和密码和固定,哦,我的!)更复杂一个数量级。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值