http协议
HTTP (Hyper Text Transfer Protocol) is one of the application protocols of TCP/IP, the suite of protocols that powers the Internet.
HTTP( 超文本传输协议 )是TCP / IP的应用协议之一,TCP / IP是为Internet提供支持的协议套件。
Let me fix that: it’s not one of the protocols, it’s the most successful and popular one, by all means.
让我来解决这个问题:它不是协议之一 ,它是最成功的和流行的一个,通过各种手段。
HTTP is what makes the World Wide Web work, giving browsers a language to communicate to remote servers that host web pages.
HTTP是使万维网起作用的原因,它为浏览器提供了一种语言,可以与承载网页的远程服务器进行通信。
HTTP was first standardized in 1991, as a result of the work that Tim Berners-Lee did at CERN, the European Center of Nuclear Research, since 1989.
由于1989年以来蒂姆·伯纳斯·李(Tim Berners-Lee)在欧洲核研究中心CERN所做的工作,HTTP于1991年首次标准化。
The goal was to allow researchers to easily exchange and interlink their papers. It was meant as a way for the scientific community to work better.
目的是使研究人员可以轻松地交换和链接他们的论文。 这是为了让科学界更好地工作。
Back then the internet main applications basically consisted in FTP (the File Transfer Protocol), Email and Usenet (newsgroups, today almost abandoned).
那时,Internet的主要应用程序主要包括FTP(文件传输协议),Email和Usenet(新闻组,今天几乎被废弃)。
In 1993 Mosaic, the first graphical web browser, was released, and things skyrocketed from there.
1993年,第一个图形化Web浏览器Mosaic发行了,事情从那里暴涨。
The Web became the killer app of the Internet.
Web成为Internet的杀手级应用。
Over time the Web and the ecosystem around it have dramatically evolved, but the basics still remain. One example of evolution: HTTP now powers, in addition to web pages, REST APIs, one common way to programmatically access a service over the Internet.
随着时间的流逝,Web及其周围的生态系统已经发生了巨大的发展,但基础仍然存在。 演进的一个例子:HTTP现在不仅为网页提供了REST API,而且还提供了REST API,这是一种通过Internet编程访问服务的常用方法。
HTTP got a minor revision in 1997 with HTTP/1.1, and in 2015 its successor, HTTP/2, was standardized and it’s now being implemented by the major Web Servers used across the globe.
HTTP在1997年以HTTP / 1.1进行了较小的修订,并在2015年对HTTP / 2的后继版本HTTP / 2进行了标准化,现在由全球使用的主要Web服务器来实现。
The HTTP protocol is considered insecure, just like any other protocol (SMTP, FTP..) not served over an encrypted connection. This is why there is a big push nowadays towards using HTTPS, which is HTTP served over TLS.
就像没有通过加密连接提供服务的其他任何协议(SMTP,FTP ..)一样,HTTP协议被认为是不安全的。 这就是为什么如今大力推动使用HTTPS的原因,HTTPS是通过TLS服务的HTTP。
That said, the building blocks of HTTP/2 and HTTPS have their roots in HTTP, and in this article I’ll introduce how HTTP works.
就是说,HTTP / 2和HTTPS的构建基块都起源于HTTP,在本文中,我将介绍HTTP的工作原理。
HTML文件 (HTML documents)
HTTP is the way web browsers like Chrome, Firefox, Edge and many others (also called clients from here on) communicate with web servers.
HTTP是Web浏览器(例如Chrome,Firefox,Edge和许多其他浏览器,在这里也称为客户端 )与Web服务器通信的方式。
The name Hyper Text Transfer Protocol derives from the need of transferring not just files, like in FTP - the “File Transfer Protocol”, but hypertexts, which would be written using HTML, and then represented graphically by the browser with a nice presentation and interactive links.
超文本传输协议的名称源于不仅需要传输文件(例如在FTP中称为“文件传输协议”),还需要传输超文本,该超文本将使用HTML编写,然后由浏览器以图形化的方式呈现,并具有精美的外观和交互性。链接。
Links were the driving force that drove adoption, along with the ease of creation of new web pages.
链接是推动采用的驱动力,同时易于创建新的网页。
HTTP is what transfer those hypertext files (and as we’ll see also images and other file types) over the network.
HTTP是通过网络传输那些超文本文件(以及我们还将看到的图像和其他文件类型)的工具。
超连结 (Hyperlinks)
Inside a web browser, a document can point to another document using links.
在Web浏览器中,文档可以使用链接指向另一个文档。
A link is composed by a first part that determines the protocol and the server address, either through a domain name or an IP.
链接由第一部分组成,该第一部分通过域名或IP确定协议和服务器地址。
This part is not unique to HTTP, of course.
当然,这部分不是HTTP独有的。
Then there’s the document part. Anything appended to the address part represents the document path.
然后是文档部分。 地址部分的任何内容都代表文档路径。
For example, this document address is https://flaviocopes.com/http/
:
例如,此文档地址为https://flaviocopes.com/http/
:
https
is the protocol.https
是协议。flaviocopes.com
is the domain name that points to my serverflaviocopes.com
是指向我的服务器的域名/http/
is the document URL relative to the server root path./http/
是相对于服务器根路径的文档URL。
The path can be nested: https://flaviocopes.com/page/privacy/
and in this case the document URL is /page/privacy
.
该路径可以嵌套: https://flaviocopes.com/page/privacy/
: https://flaviocopes.com/page/privacy/
,在这种情况下,文档URL为/page/privacy
。
The web server is responsible for interpreting the request and, once analyzed, serving the correct response.
Web服务器负责解释请求,并在分析后提供正确的响应。
一个要求 (A request)
What’s in a request?
请求中有什么?
The first thing is the URL, which we’ve already seen before.
首先是URL ,我们之前已经看到过。
When we enter an address and press enter in our browser, under the hoods the server sends to the correct IP address a request like this:
当我们输入一个地址并在浏览器中按Enter时,服务器会将其请求发送到正确的IP地址,如下所示:
GET /a-page
where /a-page is the URL you requested.
/ a-page是您请求的URL。
The second thing is the HTTP method (also called verb).
第二件事是HTTP方法 (也称为动词)。
HTTP in the early days defined 3 of them:
早期的HTTP定义了其中的三个:
GET
GET
POST
POST
HEAD
HEAD
and HTTP/1.1 introduced
和HTTP / 1.1引入
PUT
PUT
DELETE
DELETE
OPTIONS
OPTIONS
TRACE
TRACE
We’ll see them in detail in a minute.
我们将在一分钟内详细介绍它们。
The third thing that composes a request is a set of HTTP headers.
组成请求的第三件事是一组HTTP标头 。
Headers are a set of key: value
pairs that are used to communicate to the server-specific information that is predefined, so the server can know what we mean.
标头是一组key: value
对,用于与预定义的服务器特定信息进行通信,因此服务器可以了解我们的意思。
I described them in detail in the HTTP request headers list.
我在HTTP请求标头列表中详细介绍了它们。
Give that list a quick look. All of those headers are optional, except Host
.
快速浏览该列表。 所有这些标头都是可选的,但Host
除外。
HTTP方法 (HTTP methods)
GET
(GET
)
GET is the most used method here. It’s the one that’s used when you type an URL in the browser address bar, or when you click a link.
GET是此处最常用的方法。 这是在浏览器地址栏中键入URL或单击链接时使用的URL。
It asks the server to send the requested resource as a response.
它要求服务器发送请求的资源作为响应。
HEAD
(HEAD
)
HEAD is just like GET, but tells the server to not send the response body back. Just the headers.
HEAD就像GET一样,但是告诉服务器不要将响应主体发送回去。 只是标题。
POST
(POST
)
The client uses the POST method to send data to the server. It’s typically used in forms, for example, but also when interacting with a REST API.
客户端使用POST方法将数据发送到服务器。 例如,它通常以形式使用,但在与REST API交互时也是如此。
PUT
(PUT
)
The PUT method is intended to create a resource at that specific URL, with the parameters passed in the request body. Mainly used in REST APIs
PUT方法旨在使用在请求正文中传递的参数在该特定URL创建资源。 主要用于REST API
DELETE
(DELETE
)
The DELETE method is called against an URL to request deletion of that resource. Mainly used in REST APIs
针对URL调用DELETE方法以请求删除该资源。 主要用于REST API
OPTIONS
(OPTIONS
)
When a server receives an OPTIONS request it should send back the list of HTTP methods allowed for that specific URL.
当服务器收到OPTIONS请求时,它应该发回该特定URL允许的HTTP方法列表。
TRACE
(TRACE
)
Returns back to the client the request that has been received. Used for debugging or diagnostic purposes.
将已收到的请求返回给客户端。 用于调试或诊断目的。
HTTP客户端/服务器通信 (HTTP Client/Server communication)
HTTP, as most of the protocols that belong to the TCP/IP suite, is a stateless protocol.
作为大多数属于TCP / IP套件的协议,HTTP是无状态协议。
Servers have no idea what’s the current state of the client. All they care about is that they get request and they need to fulfill them.
服务器不知道客户端的当前状态是什么。 他们关心的只是得到请求,他们需要满足他们。
Any prior request is meaningless in this context, and this makes it possible for a web server to be very fast, as there’s less to process, and also it gives it bandwidth to handle a lot of concurrent requests.
在这种情况下,任何先前的请求都是没有意义的,并且这使得Web服务器变得非常快,因为处理的资源更少,并且还为它提供了处理大量并发请求的带宽。
HTTP is also very lean, and communication is very fast in terms of overhead. This contrasts with the protocols that were the most used at the time HTTP was introduced: TCP and POP/SMTP, the mail protocols, which involve lots of handshaking and confirmations on the receiving ends.
HTTP也非常精简,并且在开销方面通讯非常快。 这与引入HTTP时最常用的协议形成对比:TCP和POP / SMTP,即邮件协议,在接收端涉及很多握手和确认。
Graphical browsers abstract all this communication, but we’ll illustrate it here for learning purposes.
图形浏览器将所有这些通信抽象化,但是出于学习目的,我们将在此处进行说明。
A message is composed by a first line, which starts with the HTTP method, then contains the resource relative path, and the protocol version:
消息由第一行组成,该行以HTTP方法开头,然后包含资源相对路径和协议版本:
GET /a-page HTTP/1.1
After that, we need to add the HTTP request headers. As mentioned above, there are many headers, but the only mandatory one is Host
:
之后,我们需要添加HTTP请求标头。 如上所述,头有很多,但唯一的强制性头是Host
:
GET /a-page HTTP/1.1
Host: flaviocopes.com
How can you test this? Using telnet. This is a command-line tool that lets us connect to any server and send it commands.
您如何测试呢? 使用telnet 。 这是一个命令行工具,可让我们连接到任何服务器并向其发送命令。
Open your terminal, and type telnet flaviocopes.com 80
打开您的终端,然后键入telnet flaviocopes.com 80
This will open a terminal, that tells you
这将打开一个终端,告诉您
Trying 178.128.202.129...
Connected to flaviocopes.com.
Escape character is '^]'.
You are connected to the Netlify web server that powers my blog. You can now type:
您已连接到支持我的博客的Netlify Web服务器。 您现在可以输入:
GET /axios/ HTTP/1.1
Host: flaviocopes.com
and press enter on an empty line to fire the request.
然后在空行上按Enter键以触发请求。
The response will be:
响应将是:
HTTP/1.1 301 Moved Permanently
Cache-Control: public, max-age=0, must-revalidate
Content-Length: 46
Content-Type: text/plain
Date: Sun, 29 Jul 2018 14:07:07 GMT
Location: https://flaviocopes.com/axios/
Age: 0
Connection: keep-alive
Server: Netlify
Redirecting to https://flaviocopes.com/axios/
See, this is an HTTP response we got back from the server. It’s a 301 Moved Permanently request. See the HTTP status codes list to know more about the status codes.
瞧,这是我们从服务器返回的HTTP响应。 这是“永久移动301”请求。 请参阅HTTP状态代码列表以了解有关状态代码的更多信息。
It basically tells us the resource has permanently moved to another location.
它基本上告诉我们资源已永久移动到另一个位置。
Why? Because we connected to port 80, which is the default for HTTP, but on my server I set up an automatic redirection to HTTPS.
为什么? 因为我们连接到端口80,这是HTTP的默认端口,但是在我的服务器上,我设置了自动重定向到HTTPS的功能。
The new location is specified in the Location
HTTP response header.
在Location
HTTP响应标头中指定了新位置。
There are other headers, all described in the HTTP response headers list.
还有其他标头,所有标头均在HTTP响应标头列表中进行了描述。
In both the request and the response, an empty line separates the request header from the request body. The response body in this case contains the string
在请求和响应中,空行都将请求标头与请求正文分开。 在这种情况下,响应主体包含字符串
Redirecting to https://flaviocopes.com/axios/
which is 46 bytes long, as specified in the Content-Length
header. It is shown in the browser when you open the page, while it automatically redirects you to the correct location.
Content-Length
为46个字节,如Content-Length
标头中所指定。 当您打开页面时,它会显示在浏览器中,同时它会自动将您重定向到正确的位置。
In this case we’re using telnet, the low-level tool that we can use to connect to any server, so we can’t have any kind of automatic redirect.
在这种情况下,我们使用的是telnet,这是我们可用于连接到任何服务器的低级工具,因此我们无法进行任何类型的自动重定向。
Let’s do this process again, now connecting to port 443, which is the default port of the HTTPS protocol. We can’t use telnet because of the SSL handshake that must happen.
让我们再次执行此过程,现在连接到端口443,这是HTTPS协议的默认端口。 由于必须进行SSL握手,因此我们无法使用telnet。
Let’s keep things simple and use curl
, another command-line tool. We cannot directly type the HTTP request, but we’ll see the response:
让我们保持简单,并使用另一个命令行工具curl
。 我们无法直接输入HTTP请求,但会看到响应:
curl -i https://flaviocopes.com/axios/
this is what we’ll get in return:
这就是我们的回报:
HTTP/1.1 200 OK
Cache-Control: public, max-age=0, must-revalidate
Content-Type: text/html; charset=UTF-8
Date: Sun, 29 Jul 2018 14:20:45 GMT
Etag: "de3153d6eacef2299964de09db154b32-ssl"
Strict-Transport-Security: max-age=31536000
Age: 152
Content-Length: 9797
Connection: keep-alive
Server: Netlify
<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>HTTP requests using Axios</title>
....
I cut the response, but you can see that the HTML of the page is being returned now.
我剪切了响应,但是您可以看到现在正在返回页面HTML。
其他资源 (Other resources)
An HTTP server will not just transfer HTML files, but typically it will also serve other files: CSS, JS, SVG, PNG, JPG, lots of different file types.
HTTP服务器不仅会传输HTML文件,而且通常还会提供其他文件服务:CSS,JS,SVG,PNG,JPG,以及许多其他文件类型。
This depends on the configuration.
这取决于配置。
HTTP is perfectly capable of transferring those files as well, and the client will know about the file type, thus interpret them in the right way.
HTTP也完全能够传输这些文件,并且客户端将知道文件类型,从而以正确的方式解释它们。
This is how the web works: when an HTML page is retrieved by the browser, it’s interpreted and any other resource it needs to display property (CSS, JavaScript, images..) is retrieved through additional HTTP requests to the same server.
Web的工作方式是:浏览器检索HTML页面时,将对其进行解释,并通过对同一服务器的其他HTTP请求来检索其显示属性(CSS,JavaScript,图像等)所需的任何其他资源。
http协议