Shell 脚本 - 在 curl、nc 或 telnet 中强制 HTTP 1.1 流水线

Shell script - Force HTTP 1.1 pipelining in curl, nc or telnet

我需要在 bash 脚本上使用 curl、telnet 或 netcat 对服务器 GET 请求强制 HTTP 流水线操作 (1.1)。我已经尝试使用 curl 这样做,但到目前为止 as I know 该工具从版本 7.65.0 开始就放弃了对 HTTP 管道的支持,而且我找不到太多关于如何这样做的信息。尽管如此,如果无法使用 telnet 或 netcat,我可以在其他计算机上访问 curl 版本 7.29.0。

来自维基百科:

HTTP pipelining is a technique in which multiple HTTP requests are sent on a single TCP (transmission control protocol) connection without waiting for the corresponding responses.

要使用 netcat 发送多个 GET 请求,像这样的东西应该可以解决问题:

echo -en "GET /index.html HTTP/1.1\r\nHost: example.com\r\n\r\nGET /other.html HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80

这将向 example.com 发送两个 HTTP GET 请求,一个用于 http://example.com/index.html and another for http://example.com/other.html

-e 标志表示解释转义序列(回车 returns 和换行符,或 \r 和 \n)。 -n 表示不要在末尾打印换行符(如果没有 -n 可能会工作)。

我只是 运行 上面的命令并从中得到了两个响应,一个是 200 OK,另一个是 404 Not Found。

如果您执行 HEAD 请求而不是 GET 请求,可能更容易看到多个请求和响应。这样,example.com 的服务器将只响应 headers.

echo -en "HEAD /index.html HTTP/1.1\r\nHost: example.com\r\n\r\nHEAD /other.html HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80

这是我从上面的命令得到的输出:

$ echo -en "HEAD /index.html HTTP/1.1\r\nHost: example.com\r\n\r\nHEAD /other.html HTTP/1.1\r\nHost: example.com\r\n\r\n" | nc example.com 80
HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Age: 355429
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 24 Feb 2020 14:48:39 GMT
Etag: "3147526947"
Expires: Mon, 02 Mar 2020 14:48:39 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (dna/63B3)
X-Cache: HIT
Content-Length: 648

HTTP/1.1 404 Not Found
Accept-Ranges: bytes
Age: 162256
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 24 Feb 2020 14:48:39 GMT
Expires: Mon, 02 Mar 2020 14:48:39 GMT
Last-Modified: Sat, 22 Feb 2020 17:44:23 GMT
Server: ECS (dna/63AD)
X-Cache: 404-HIT
Content-Length: 1256

如果您想查看更多详细信息,运行 这些命令之一,而 wireshark is running. The request is sent in No. 7 (highlighted) and the two responses are received on No. 11 and No. 13.