博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
http请求的工作原理_HTTP请求如何工作
阅读量:2506 次
发布时间:2019-05-11

本文共 9488 字,大约阅读时间需要 31 分钟。

http请求的工作原理

This article describes how browsers perform page requests using the HTTP/1.1 protocol

本文介绍了浏览器如何使用HTTP / 1.1协议执行页面请求

If you ever did an interview, you might have been asked: “what happens when you type something into the Google search box and press enter”.

如果您曾经接受过采访,那么您可能会被问到:“当您在Google搜索框中输入内容并按Enter键时,会发生什么情况”。

It’s one of the most popular questions you get asked. People just want to see if you can explain some rather basic concepts and if you have any clue how the internet actually works.

这是您提出的最受欢迎的问题之一。 人们只是想看看您是否可以解释一些相当基本的概念,以及您是否对互联网的实际工作有任何了解。

In this post, I’ll analyze what happens when you type an URL in the address bar of your browser and press enter.

在本文中,我将分析当您在浏览器的地址栏中键入URL并按Enter时会发生什么。

It’s a very interesting topic to dissect in a blog post, as it touches many technologies I can dive into in separate posts.

在博客文章中进行剖析是一个非常有趣的话题,因为它涉及到许多我可以在单独的文章中探讨的技术。

This is tech that is very rarely changed, and powers one the most complex and wide ecosystems ever built by humans.

这项技术很少更改,可以为人类建立的最复杂,最广泛的生态系统提供动力。

HTTP协议 (The HTTP protocol)

First, I mention HTTPS in particular because things are different from an HTTPS connection.

首先,我特别提到HTTPS,因为它与HTTPS连接有所不同。

我仅分析网址请求 (I analyze URL requests only)

Modern browsers have the capability of knowing if the thing you wrote in the address bar is an actual URL or a search term, and they will use the default search engine if it’s not a valid URL.

现代浏览器具有知道您在地址栏中写的内容是实际URL还是搜索词的功能,并且如果不是有效URL,它们将使用默认搜索引擎。

I assume you type an actual URL.

我假设您键入一个实际的URL。

When you enter the URL and press enter, the browser first builds the full URL.

输入URL并按Enter键后,浏览器会首先构建完整的URL。

If you just entered a domain, like flaviocopes.com, the browser by default will prepend HTTP:// to it, defaulting to the HTTP protocol.

如果您只是输入了flaviocopes.com之类的域,则默认情况下,浏览器会将HTTP:// flaviocopes.com在该域之前,默认为HTTP协议。

事情与macOS / Linux有关 (Things relate to macOS / Linux)

Just FYI. Windows might do some things slightly differently.

仅供参考。 Windows的某些功能可能会略有不同。

DNS查找阶段 (DNS Lookup phase)

The browser starts the DNS lookup to get the server IP address.

浏览器启动DNS查找以获取服务器IP地址。

The domain name is a handy shortcut for us humans, but the internet is organized in such a way that computers can look up the exact location of a server through its IP address, which is a set of numbers like 222.324.3.1 (IPv4).

域名对于我们人类来说是一个方便的快捷方式,但是互联网的组织方式使得计算机可以通过其IP地址查找服务器的确切位置,该IP地址是一组数字,例如222.324.3.1 (IPv4)。

First, it checks the DNS local cache, to see if the domain has already been resolved recently.

首先,它检查DNS本地缓存,以查看该域最近是否已解析。

Chrome has a handy DNS cache visualizer you can see at chrome://net-internals/#dns

Chrome浏览器有一个方便的DNS缓存可视化工具,您可以在chrome:// net-internals /#dns上看到

If nothing is found there, the browser uses the DNS resolver, using the gethostbyname POSIX system call to retrieve the host information.

如果在此未找到任何内容,则浏览器将使用DNS解析器,并使用gethostbyname POSIX系统调用来检索主机信息。

gethostbyname (gethostbyname)

gethostbyname first looks in the local hosts file, which on macOS or Linux is located in /etc/hosts, to see if the system provides the information locally.

gethostbyname首先查找本地主机文件,该文件在macOS或Linux上位于/etc/hosts ,以查看系统是否在本地提供信息。

If this does not give any information about the domain, the system makes a request to the DNS server.

如果这没有提供有关域的任何信息,则系统会向DNS服务器发出请求。

The address of the DNS server is stored in the system preferences.

DNS服务器的地址存储在系统偏好设置中。

Those are 2 popular DNS servers:

这些是2种流行的DNS服务器:

  • 8.8.8.8: the Google public DNS server

    8.8.8.8公共DNS服务器

  • 1.1.1.1: the CloudFlare DNS server

    1.1.1.1 :在CloudFlare的DNS服务器

Most people use the DNS server provided by their internet provider.

大多数人使用其互联网提供商提供的DNS服务器。

The browser performs the DNS request using the UDP protocol.

浏览器使用UDP协议执行DNS请求。

TCP and UDP are two of the foundational protocols of computer networking. They sit at the same conceptual level, but TCP is connection-oriented, while UDP is a connectionless protocol, more lightweight, used to send messages with little overhead.

TCP和UDP是计算机网络的两个基本协议。 它们处于相同的概念级别,但是TCP面向连接,而UDP是一种无连接协议,更轻量级,用于发送消息而开销很小。

How the UDP request is performed is not in the scope of this tutorial

UDP请求的执行方式不在本教程的范围之内

The DNS server might have the domain IP in the cache. If not, it will ask the root DNS server. That’s a system (composed of 13 actual servers, distributed across the planet) that drives the entire internet.

DNS服务器可能在缓存中具有域IP。 如果没有,它将询问根DNS服务器 。 那是一个驱动整个互联网的系统(由13台实际服务器组成,分布在地球上)。

The DNS server does not know the address of each and every domain name on the planet.

DNS服务器知道每个地址和地球上的每个域名。

What it knows is where the top-level DNS resolvers are.

它所知道的是顶级DNS解析器的位置。

A top-level domain is the domain extension: .com, .it, .pizza and so on.

顶级域名是域名扩展名: .com.it.pizza等。

Once the root DNS server receives the request, it forwards the request to that top-level domain (TLD) DNS server.

根DNS服务器收到请求后,会将请求转发到该顶级域(TLD)DNS服务器。

Say you are looking for flaviocopes.com. The root domain DNS server returns the IP of the .com TLD server.

假设您正在寻找flaviocopes.com 。 根域DNS服务器返回.com TLD服务器的IP。

Now our DNS resolver will cache the IP of that TLD server, so it does not have to ask the root DNS server again for it.

现在,我们的DNS解析器将缓存该TLD服务器的IP,因此它不必再次询问根DNS服务器。

The TLD DNS server will have the IP addresses of the authoritative Name Servers for the domain we are looking for.

TLD DNS服务器将具有我们要查找的域的权威名称服务器的IP地址。

How? When you buy a domain, the domain registrar sends the appropriate TDL the name servers. When you update the name servers (for example, when you change the hosting provider), this information will be automatically updated by your domain registrar.

怎么样? 当您购买域名时,域名注册商会向相应的TDL发送名称服务器。 当您更新名称服务器时(例如,当您更改托管服务提供商时),该信息将由您的域名注册商自动更新。

Those are the DNS servers of the hosting provider. They are usually more than 1, to serve as backup.

这些是托管服务提供商的DNS服务器。 它们通常大于1,以用作备份。

For example:

例如:

  • ns1.dreamhost.com

    ns1.dreamhost.com

  • ns2.dreamhost.com

    ns2.dreamhost.com

  • ns3.dreamhost.com

    ns3.dreamhost.com

The DNS resolver starts with the first, and tries to ask the IP of the domain (with the subdomain, too) you are looking for.

DNS解析器从第一个开始,然后尝试询问您要查找的域的IP(也包含子域)。

That is the ultimate source of truth for the IP address.

这是IP地址真实性的最终来源。

Now that we have the IP address, we can go on in our journey.

现在我们有了IP地址,就可以继续前进了。

TCP请求握手 (TCP request handshaking)

With the server IP address available, now the browser can initiate a TCP connection to that.

在服务器IP地址可用的情况下,现在浏览器可以启动与之的TCP连接。

A TCP connection requires a bit of handshaking before it can be fully initialized and you can start sending data.

TCP连接需要进行一些握手才能完全初始化,然后才能开始发送数据。

Once the connection is established, we can send the request

建立连接后,我们可以发送请求

发送请求 (Sending the request)

The request is a plain text document structured in a precise way determined by the communication protocol.

该请求是以通信协议确定的精确方式构造的纯文本文档。

It’s composed of 3 parts:

它由3部分组成:

  • the request line

    请求行
  • the request header

    请求头
  • the request body

    请求主体

请求线 (The request line)

The request line sets, on a single line:

请求行设置为一行:

  • the HTTP method

    HTTP方法
  • the resource location

    资源位置
  • the protocol version

    协议版本

Example:

例:

GET / HTTP/1.1

请求标头 (The request header)

The request header is a set of field: value pairs that set certain values.

请求标头是一组field: value设置某些值的field: value对。

There are 2 mandatory fields, one of which is Host, and the other is Connection, while all the other fields are optional:

有2个必填字段,其中一个是Host ,另一个是Connection ,而其他所有字段都是可选的:

Host: flaviocopes.comConnection: close

Host indicates the domain name which we want to target, while Connection is always set to close unless the connection must be kept open.

Host表示我们要定位的域名,而“ Connection始终设置为close除非必须保持打开状态。

Some of the most used header fields are:

一些最常用的标头字段是:

  • Origin

    Origin

  • Accept

    Accept

  • Accept-Encoding

    Accept-Encoding

  • Cookie

    Cookie

  • Cache-Control

    Cache-Control

  • Dnt

    Dnt

but many more exist.

但还有更多。

The header part is terminated by a blank line.

标头部分由空白行终止。

请求主体 (The request body)

The request body is optional, not used in GET requests but very much used in POST requests and sometimes in other verbs too, and it can contain data in format.

请求主体是可选的,不在GET请求中使用,但在POST请求中(有时也在其他动词中使用)非常多,它可以包含格式的数据。

Since we’re now analyzing a GET request, the body is blank and we’ll not look more into it.

由于我们现在正在分析GET请求,因此主体为空白,因此我们不再赘述。

响应 (The response)

Once the request is sent, the server processes it and sends back a response.

发送请求后,服务器将对其进行处理并发送回响应。

The response starts with the status code and the status message. If the request is successful and returns a 200, it will start with:

响应以状态代码和状态消息开头。 如果请求成功并返回200,它将以:

200 OK

The request might return a different status code and message, like one of these:

该请求可能返回不同的状态代码和消息,例如以下之一:

404 Not Found403 Forbidden301 Moved Permanently500 Internal Server Error304 Not Modified401 Unauthorized

The response then contains a list of HTTP headers and the response body (which, since we’re making the request in the browser, is going to be HTML)

然后,响应包含HTTP标头和响应正文的列表(由于我们正在浏览器中进行请求,因此将为HTML)

解析HTML (Parse the HTML)

The browser now has received the HTML and starts to parse it, and will repeat the exact same process we did for all the resources required by the page:

现在,浏览器已收到HTML并开始对其进行解析,并将对页面所需的所有资源重复与我们完全相同的过程:

  • CSS files

    CSS文件
  • images

    图片
  • the favicon

    网站图标
  • JavaScript files

    JavaScript文件

How browsers render the page then is out of the scope, but it’s important to understand that the process I described is not just for the HTML pages, but for any item that’s served over HTTP.

然后,浏览器如何呈现页面是超出范围的,但重要的是要了解,我描述的过程不仅针对HTML页面,而且针对通过HTTP服务的任何项目。

翻译自:

http请求的工作原理

转载地址:http://ivqgb.baihongyu.com/

你可能感兴趣的文章
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>