Linux命令Curl支持HTTP 2.0

简介: Curl命令不一定支持HTTP 2.0,但某些服务必须需要HTTP2.0,如Apple的推送服务若使用HTTP/1.x协议进行请求,则会返回“Unexpected HTTP/1.x request”的错误。因此就有了让Curl命令支持HTTP/2的实践,其实质就是重新编译Curl命令。 # 验证curl对HTTP 2.0的支持 为了验证默认情况下curl使用的协议,执行命令: ```

Curl命令不一定支持HTTP 2.0,但某些服务必须需要HTTP2.0,如Apple的推送服务若使用HTTP/1.x协议进行请求,则会返回“Unexpected HTTP/1.x request”的错误。因此就有了让Curl命令支持HTTP/2的实践,其实质就是重新编译Curl命令。

验证curl对HTTP 2.0的支持

为了验证默认情况下curl使用的协议,执行命令:

[root@host root]# curl -I https://nghttp2.org/
HTTP/1.1 200 OK
Date: Tue, 27 Oct 2020 07:53:18 GMT
Content-Type: text/html
Last-Modified: Tue, 02 Jun 2020 12:23:35 GMT
Etag: "5ed644c7-19d8"
Accept-Ranges: bytes
Content-Length: 6616
X-Backend-Header-Rtt: 0.00096
Strict-Transport-Security: max-age=31536000
Server: nghttpx
Via: 2 nghttpx
alt-svc: h3-29=":443"; ma=3600
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
x-content-type-options: nosniff

HTTP/1.1 200 OK可知当前计算机的curl默认使用HTTP 1.1。

HTTP 2.0的启用需要使用--http2开关,看当前curl命令是否支持这个参数,执行命令:

[root@host root]# curl --http2 -I https://nghttp2.org/
curl: option --http2: is unknown
curl: try 'curl --help' or 'curl --manual' for more information

从上面的结果可见,当前计算机的curl不支持HTTP 2.0。

看curl命令支持的特性,从Features里可以看到curl命令是否真的不支持http2:

[root@host root]# curl --version
curl 7.29.0 (x86_64-koji-linux-gnu) libcurl/7.29.0 NSS/3.28.4 zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets

因为Features里没有HTTP2,因此无法支持HTTP2.0。

增加对HTTP2.0的支持

增加对HTTP2.0的支持说白了,就是重新编译一个curl,其中一个重要的依赖是nghttp2。下面就来实际操作一下。注意用root用户的身份安装系统软件。

  1. 用yum把当前已安装包升级到最新,并安装依赖的包。
[root@host root]# yum -y update
[root@host root]# yum -y install make binutils autoconf automake libtool \
    python-setuptools python-devel python3-devel git openssl openssl-devel curl gcc gcc-c++ \
    pkgconfig zlib-devel zlib libxml2-devel libev-devel libevent-devel libevdev libevdev-devel \
    jansson-devel jemalloc-devel libcurl libicu libmetalink libpsl libssh2 c-ares-devel \
    libnghttp2 systemd
  1. 安装nghttp2。因为每个命令输出内容较长,这里把命令执行输出部分略去。
[root@host root]# git clone https://github.com/tatsuhiro-t/nghttp2.git
[root@host root]# cd nghttp2/
[root@host nghttp2]# autoreconf -i
[root@host nghttp2]# automake
[root@host nghttp2]# autoconf
[root@host nghttp2]# ./configure
[root@host nghttp2]# make
[root@host nghttp2]# make install
[root@host nghttp2]# ldconfig
[root@host nghttp2]# cd ..

如果上面能正确编译和安装,则可直接到下一步。如果make过程中出现错误:#error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. 说明系统的编译器版本过旧,需要进行更多的操作。

[root@host nghttp2]# yum install centos-release-scl
[root@host nghttp2]# yum-config-manager --enable rhel-server-rhscl-7-rpms
[root@host nghttp2]# yum install devtoolset-7
[root@host nghttp2]# scl enable devtoolset-7 bash

上面4个命令执行后,再执行gcc --version就可以看到gcc已经升级到7.0以上。升级完后,继续编译和安装nghttp2。

  1. 重新编译curl命令。
[root@host root]# wget https://curl.haxx.se/download/curl-7.73.0.tar.gz
[root@host root]# tar -zxvf curl-7.73.0.tar.gz
[root@host root]# cd curl-7.73.0/
[root@host curl-7.73.0]# ./configure --with-nghttp2=/usr/local --with-ssl --enable-tls-srp
[root@host curl-7.73.0]# make && make install
[root@host curl-7.73.0]# ldconfig
[root@host curl-7.73.0]# cd ..

需要注意,执行configure后,需要确认输出中含有HTTP2: enabled (nghttp2)字样。

  1. 确认curl命令已经支持HTTP2.0
[root@host root]# curl --version
curl 7.73.0 (x86_64-pc-linux-gnu) libcurl/7.73.0 OpenSSL/1.0.2k-fips zlib/1.2.7 nghttp2/1.42.0-DEV
Release-Date: 2020-10-14
Protocols: dict file ftp ftps gopher http https imap imaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets

这里显示了HTTP2字样,说明curl已经支持了HTTP 2.0。
上述操作在CentOS Linux release 7.8系统中验证通过。

目录
相关文章
|
29天前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
105 16
|
21天前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
3月前
|
监控 Linux 网络安全
Linux命令大全:从入门到精通
日常使用的linux命令整理
672 13
|
4月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
565 21
|
2月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
405 0
|
4月前
|
监控 Linux
Linux系统中使用df命令详解磁盘使用情况。
`df`命令是Linux系统管理员和用户监控和管理磁盘空间使用的重要工具。掌握它的基本使用方法和选项可以帮助在必要时分析和解决空间相关问题。简洁但功能丰富,`df`命令确保了用户可以快速有效地识别和管理文件系统的空间使用情况。
275 13
|
2月前
|
安全 Linux Shell
Linux系统中sudo命令的高效运用技巧。
用户可以通过sudo -l来列出自己目前可执行的命令列表,这有助于用户了解自己的权限范围。
123 0
|
2月前
|
监控 Linux Shell
linux命令
常用 Linux 命令汇总
|
4月前
|
Linux
Linux命令拓展:为cp和mv添加进度显示
好了,就这样,让你的Linux复制体验充满乐趣吧!记住,每一个冷冰冰的命令背后,都有方法让它变得热情起来。
306 8
|
3月前
|
Linux C++
每天一个linux命令(8):cp 命令
cp 命令是 Linux 中用于复制文件或目录的命令。它的名字来源于英文单词 copy。这个命令非常常用,特别是在需要备份文件或创建文件副本时。
125 0