Curl:单行测试 http/2 支持

问题描述 投票:0回答:7

我目前正在编写一个单元测试来检查是否支持

http/2

是否有一个

curl
单行代码可以检查
http/2
是否受支持并输出易于解析的响应?

unit-testing curl http2
7个回答
90
投票

支持HTTP/2:

$ curl -sI https://curl.se -o/dev/null -w '%{http_version}\n'
2

不支持 HTTP/2(在本例中为 1.1):

$ curl -sI http://curl.se -o/dev/null -w '%{http_version}\n'
1.1

(此命令行需要curl 7.50.0或更高版本才能工作)

HTTP/3

从curl 7.88.1开始,如果你构建curl来支持HTTP/3,上面的一行代码可以扩展为也检查HTTP/3支持,如下所示:

$ curl -sI --http3 https://curl.se -o/dev/null -w '%{http_version}\n'
3

11
投票

奔跑

curl --version

并在功能列表中查找 HTTP2


5
投票

在这里您可以找到用于调试、测试和使用 HTTP/2 的工具列表

命令行中最简单的可能是:

$ is-http2 www.cloudflare.com

但这需要

npm install -g is-http2-cli

要使用

curl
进行测试,您可能需要使用 nghttp 库进行编译,在 macOS 中,可以使用
brew
来完成,您可以使用:

$ brew install curl --with-nghttp2

然后你可以使用@daniel-stenberg 在他的回答中建议的内容

$ curl -sI https://curl.haxx.se -o/dev/null -w '%{http_version}\n'

如果支持 http2,您将在其中得到 2。


4
投票

我用过

curl -kvso /dev/null --http2 https://app.domain.com:443

返回了

...
> GET / HTTP/2
...
< HTTP/2 302
...

这不是检查是否支持 HTTP2,而是检查 HTTP2 是否确实有效。


1
投票

你可以简单地这样做:

curl --http2 --head domain.com

0
投票

在我正在查看的示例中,接受的答案不适用于 h2c。而是使用:

curl --http2 -sI https://curl.se -o/dev/null -w '%{http_version}\n'


0
投票

在Mac中,我们可以使用以下命令检查curl支持的HTTP版本

curl --manual | grep "\-\-http" -A 10

--http0.9 - 告诉curl可以使用HTTP版本0.9响应

--http1.0 - 告诉curl使用HTTP版本1.0而不是使用它的 内部首选 HTTP 版本。

--http1.1 - 告诉curl 使用 HTTP 版本 1.1。

--http2 - 告诉curl 使用 HTTP 版本 2。

--http3 - 告诉curl 尝试使用HTTP/3 连接到URL 中的主机,但如果HTTP/3 连接建立失败,则回退到早期的HTTP 版本。 HTTP/3 仅适用于 HTTPS,不适用于 HTTP URL。

$ curl -sI --http1.0 https://example.com -o/dev/null -w '%{http_version}\n'
1
$ curl -sI --http1.1 https://example.com -o/dev/null -w '%{http_version}\n'
1.1
$ curl -sI --http2 https://example.com -o/dev/null -w '%{http_version}\n'
2

由于 example.com 配置为支持 HTTP .0、HTTP .1 和 HTTP ,因此它会根据来自 curl 的传入请求响应相应的 HTTP 版本。

© www.soinside.com 2019 - 2024. All rights reserved.