天天看點

Linux指令Curl支援HTTP 2.0驗證curl對HTTP 2.0的支援增加對HTTP2.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使用的協定,執行指令:

[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

系統中驗證通過。