curl 支持 HTTP2

yexiaobai

curl 命令支持 HTTP2

执行以下命令:

sudo apt-get install -y tmux curl vim wget htop git

首先使用 curl 请求 https://nghttp2.org(使用了 http2)。但是结果却是返回了 HTTP1.1 响应:

$ curl -I https://nghttp2.org/

HTTP/1.1 200 OK
Date: Fri, 04 Dec 2015 00:00:06 GMT
Content-Type: text/html
Content-Length: 6680
Last-Modified: Thu, 26 Nov 2015 15:28:33 GMT
Etag: "56572521-1a18"
Accept-Ranges: bytes
X-Backend-Header-Rtt: 0.000642
Server: nghttpx nghttp2/1.5.1-DEV
Via: 1.1 nghttpx
strict-transport-security: max-age=31536000

如果使用 http2,会给我们返回一个协议不支持的错误。

$ curl --http2 -I https://nghttp2.org/
# Unsupported protocol error

安装 nghttp2

安装 nghttp2 ad 工具(http2 的 C 库支持):

# Get build requirements
# Some of these are used for the Python bindings
# this package also installs
sudo apt-get install g++ make binutils autoconf automake autotools-dev libtool pkg-config \
  zlib1g-dev libcunit1-dev libssl-dev libxml2-dev libev-dev libevent-dev libjansson-dev \
  libjemalloc-dev cython python3-dev python-setuptools

# Build nghttp2 from source
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure
make
sudo make install

更新到最新的 curl 版本:

cd ~
sudo apt-get build-dep curl
wget http://curl.haxx.se/download/curl-7.46.0.tar.bz2
tar -xvjf curl-7.46.0.tar.bz2
cd curl-7.46.0
./configure --with-nghttp2=/usr/local --with-ssl
make
sudo make install
sudo ldconfig

通过 ldconfig 命令使得 curl 命令可以正常工作,但是你也可以尝试如下命令:

# Try this out first
curl --http2 -I nghttp2.org

# If you get errors, try setting this constant
# to tell curl where to find shared libraries
LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/curl --http2 -I nghttp2.org

测试 curl

LD_LIBRARY_PATH=/usr/local/lib /usr/local/bin/curl --http2 -k -I -H "Host: example.com" https://localhost
> HTTP/2.0 200
> server:nginx/1.9.7
> date:Fri, 04 Dec 2015 02:20:54 GMT
> content-type:text/html
> content-length:12
> last-modified:Fri, 04 Dec 2015 02:11:11 GMT
> etag:"5660f63f-c"
> accept-ranges:bytes

参考资源

原文:https://serversforhackers.com/video/curl-with-http2-support?utm_campaign=Servers%2Bfor%2BHackers&utm_medium=email&utm_source=Servers_for_Hackers_1

阅读 9k

yexiaoxiaobai
刀锋上的跳舞的 SRE,喜爱 Perl, Python, Go,常被正则婊戏耍,热衷于鼓捣一些小玩具。

就是不告诉你 O(∩_∩)O哈哈~。

4.7k 声望
854 粉丝
0 条评论
你知道吗?

就是不告诉你 O(∩_∩)O哈哈~。

4.7k 声望
854 粉丝
宣传栏