用 curl 验证 http2 http/2 mac osx

Using cURL with HTTP/2 on Mac OS X

curl 从7.43.0 开始支持http2,需要加参数 –http2。

curl -I --http2 https://www.cloudflare.com/
HTTP/2.0 200
server:cloudflare-nginx
date:Sun, 24 Jan 2016 21:53:48 GMT
content-type:text/html
set-cookie:__cfduid=d73309ac5d32f18d2ca9efb414cc0fd111453672428; expires=Mon, 23-Jan-17 21:53:48 GMT; path=/; domain=.cloudflare.com; HttpOnly
last-modified:Thu, 21 Jan 2016 18:44:44 GMT
etag:W/"56a1271c-3342"
strict-transport-security:max-age=31536000
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
content-security-policy-report-only:default-src 'self' https://*; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://*; img-src 'self' https://* data:; style-src 'self' 'unsafe-inline' https://*; font-src 'self' https://* data:; frame-src https://*; connect-src 'self' https://*; report-uri https://www.cloudflare.com/csp-report
cf-cache-status:HIT
expires:Mon, 25 Jan 2016 01:53:48 GMT
cache-control:public, max-age=14400
cf-ray:269ef92389950e12-MXP

如果发现如下:不支持的协议,需要升级curl。

curl -I --http2 https://www.cloudflare.com/
curl: (1) Unsupported protocol

curl升级:

brew install curl --with-nghttp2

创建软链接:

➜  brew link curl
Warning: curl is keg-only and must be linked with --force
Note that doing so can interfere with building software.
➜  brew link curl --force
Linking /usr/local/Cellar/curl/7.50.1... 348 symlinks created

整个过程的日志如下:

brew install curl --with-nghttp2
==> Installing dependencies for curl: pkg-config, nghttp2
==> Installing curl dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.1_1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.1_1.yosemite.bottle.tar.gz
/usr/local/Cellar/pkg-config/0.29.1_1: 10 files, 627.3K
==> Installing curl dependency: nghttp2
==> Downloading https://homebrew.bintray.com/bottles/nghttp2-1.14.0.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nghttp2-1.14.0.yosemite.bottle.tar.gz
/usr/local/Cellar/nghttp2/1.14.0: 228 files, 12.8M
==> Installing curl
==> Using the sandbox
==> Downloading https://curl.haxx.se/download/curl-7.50.1.tar.bz2
######################################################################## 100.0%
==> ./configure --disable-silent-rules --prefix=/usr/local/Cellar/curl/7.50.1 --with-ssl=/usr/local/opt/openssl --with-ca-bundle=/usr/local/etc/openssl/cert.pem --without-libssh2 --without-libidn --w
==> make install
== Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

LDFLAGS: -L/usr/local/opt/curl/lib
CPPFLAGS: -I/usr/local/opt/curl/include

==> Summary
/usr/local/Cellar/curl/7.50.1: 366 files, 2.6M, built in 6 minutes 43 seconds
MacBook-Pro:soft user$ curl -V
-bash: /usr/local/bin/curl: No such file or directory
MacBook-Pro:soft user$ brew link curl
Warning: curl is keg-only and must be linked with --force
Note that doing so can interfere with building software.
MacBook-Pro:soft user$ brew link curl --force
Book-Pro:soft user$ brew link curl --force
Linking /usr/local/Cellar/curl/7.50.1... 353 symlinks created

验证:

curl -V
curl 7.50.1 (x86_64-apple-darwin14.5.0) libcurl/7.50.1 OpenSSL/1.0.2h zlib/1.2.5 nghttp2/1.14.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

再用本文最初的命令即可看到响应头是http/2.0.

参考资料:

https://simonecarletti.com/blog/2016/01/http2-curl-macosx/

http2 protocol : https://tools.ietf.org/pdf/rfc7540.pdf
hpack : https://tools.ietf.org/pdf/rfc7541.pdf

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>