当您在浏览器中输入 URL 时会发生什么 [英] what happens when you type in a URL in browser

查看:19
本文介绍了当您在浏览器中输入 URL 时会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我,从我在浏览器中输入 URL 到我在浏览器上看到页面,幕后发生了什么吗?详细说明该过程会大有帮助.

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? A detailed account of the process would be of great help.

推荐答案

注意:这是一个非常粗略和过度简化的草图,假设最简单的 HTTP 请求(没有 HTTPS,没有 HTTP2,没有额外的),最简单的 DNS,没有代理,单栈 IPv4,只有一个 HTTP 请求,另一端有一个简单的 HTTP 服务器,任何步骤都没有问题.对于大多数当代意图和目的而言,这是一个不切实际的场景;所有这些在实际使用中都要复杂得多,而且技术堆栈自编写以来已经变得复杂了一个数量级.考虑到这一点,以下时间表仍然有效:

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. 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. 浏览器检查缓存;如果请求的对象在缓存中并且是新鲜的,请跳到#9
  2. 浏览器向操作系统询问服务器的 IP 地址
  3. 操作系统进行 DNS 查找并将 IP 地址回复给浏览器
  4. 浏览器打开到服务器的 TCP 连接(使用 HTTPS 时此步骤要复杂得多)
  5. 浏览器通过 TCP 连接发送 HTTP 请求
  6. 浏览器收到 HTTP 响应并可能关闭 TCP 连接,或将其重新用于另一个请求
  7. 浏览器检查响应是重定向还是条件响应(3xx 结果状态码)、授权请求(401)、错误(4xx 和 5xx)等;这些与正常响应的处理方式不同 (2xx)
  8. 如果可缓存,则响应存储在缓存中
  9. 浏览器解码响应(例如,如果它是 gzipped)
  10. 浏览器确定如何处理响应(例如,它是 HTML 页面、图像还是声音剪辑?)
  11. 浏览器呈现响应,或为无法识别的类型提供下载对话框

同样,对这些观点中的每一个的讨论都填满了无数页;仅将此作为摘要,为了清楚起见而进行了删节.此外,还有许多其他事情与此并行发生(处理输入的地址、推测性预取、将页面添加到浏览器历史记录、向用户显示进度、通知插件和扩展、在下载页面时呈现页面、流水线、连接跟踪保持活动状态、cookie 管理、检查恶意内容等)- 使用 HTTPS(证书、密码和固定,哦,天哪!),整个操作变得复杂了一个数量级.

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!).

这篇关于当您在浏览器中输入 URL 时会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆