Edited at

えっ?アドベントカレンダーって25日目があるの?からの curl で HTTP2

More than 3 years have passed since last update.

うーん、ネタが思い浮かばない・・・

が、しかし、アドベントカレンダーって一般的には24日間らしいぞ

https://en.wikipedia.org/wiki/Advent_calendar

そういうことなので皆様良いお年を!


でも、中には25日のもあるっぽいし、小心者だから小ネタを少々。

curl って

curl -O http://www.example.com/[001-025].html

って実行すると 001.html から 025.html までゲットできるんですよ。[1-25].html ならゼロパディングされません。

え? man pages の 1 ページ目に書いてある? {a,b,c}.html も使えるって?知らなかった...

では次はこれ

curl -Lso /dev/null -w 'http_code: %{http_code}

time_connect: %{time_connect}
time_namelookup: %{time_namelookup}
time_starttransfer: %{time_starttransfer}
time_redirect: %{time_redirect}
time_total: %{time_total}
' http://www.google.com/

って実行すると

http_code: 200

time_connect: 0.042
time_namelookup: 0.029
time_starttransfer: 0.188
time_redirect: 0.056
time_total: 0.327

っていう出力が得られます。どこに時間がかかってるのかわかって便利ですね。

あぁ...しょぼい...

最後につい先日公開された 7.46.0 でついに HTTP2 に対応した (間違いでしたもっと前から対応してました、お使いのバイナリが対応しているかどうかは別として) ということなので早速これを試してみます

http://curl.haxx.se/changes.html#7_46_0

環境は Ubuntu 15.10 です

$ cat /etc/os-release 

NAME="Ubuntu"
VERSION="15.10 (Wily Werewolf)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 15.10"
VERSION_ID="15.10"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

まだパッケージでの提供はないので nghttp2 と curl の最新ソースコードをダウンロードしてビルドします


configure

  curl version:     7.46.0

Host setup: x86_64-pc-linux-gnu
Install prefix: /home/ytera
Compiler: gcc
SSL support: enabled (OpenSSL)
SSH support: no (--with-libssh2)
zlib support: enabled
GSS-API support: no (--with-gssapi)
TLS-SRP support: enabled
resolver: default (--enable-ares / --enable-threaded-resolver)
IPv6 support: enabled
Unix sockets support: enabled
IDN support: no (--with-{libidn,winidn})
Build libcurl: Shared=yes, Static=yes
Built-in manual: enabled
--libcurl option: enabled (--disable-libcurl-option)
Verbose errors: enabled (--disable-verbose)
SSPI support: no (--enable-sspi)
ca cert bundle: /etc/ssl/certs/ca-certificates.crt
ca cert path: no
LDAP support: no (--enable-ldap / --with-ldap-lib / --with-lber-lib)
LDAPS support: no (--enable-ldaps)
RTSP support: enabled
RTMP support: no (--with-librtmp)
metalink support: no (--with-libmetalink)
PSL support: no (libpsl not found)
HTTP2 support: enabled (nghttp2)
Protocols: DICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP

$ ~/bin/curl --version

curl 7.46.0 (x86_64-pc-linux-gnu) libcurl/7.46.0 OpenSSL/1.0.2d zlib/1.2.8 nghttp2/1.6.1-DEV
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

HTTP/2 対応できてるっぽいですね。

       --http2

(HTTP) Tells curl to issue its requests using HTTP 2. This
requires that the underlying libcurl was built to support it.
(Added in 7.33.0)

え? 7.33.0 で http2 使えたの?

$ /usr/bin/curl --version

curl 7.43.0 (x86_64-pc-linux-gnu) libcurl/7.43.0 GnuTLS/3.3.15 zlib/1.2.8 libidn/1.28 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

7.43.0 だから --http2 はあるぞ。でも Features に HTTP2 がない、libnghttp2 へのリンクもない。ubuntu のパッケージで入る奴は HTTP2 対応してないようだ

$ /usr/bin/curl --http2 -vo /dev/null https://www.google.co.jp/

curl: (1) Unsupported protocol

やっぱり。

それでは気を取り直して

$ ~/bin/curl --http2 -svo /dev/null https://www.google.co.jp/

* Trying 2404:6800:4004:814::2003...
* Connected to www.google.co.jp (2404:6800:4004:814::2003) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [100 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [3700 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [148 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS change cipher, Client hello (1):
{ [1 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
* subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=*.google.com
* start date: Dec 10 17:52:51 2015 GMT
* expire date: Mar 9 00:00:00 2016 GMT
* subjectAltName: www.google.co.jp matched
* issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* TCP_NODELAY set
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
} [5 bytes data]
* Using Stream ID: 1 (easy handle 0x23a7dd0)
} [5 bytes data]
> GET / HTTP/1.1
> Host: www.google.co.jp
> User-Agent: curl/7.46.0
> Accept: */*
>
{ [5 bytes data]
< HTTP/2.0 200
< date:Fri, 25 Dec 2015 14:17:38 GMT
< expires:-1
< cache-control:private, max-age=0
< content-type:text/html; charset=Shift_JIS
< p3p:CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
< server:gws
< x-xss-protection:1; mode=block
< x-frame-options:SAMEORIGIN
< set-cookie:NID=74=Mn2oEfF0V7xa41Ehje7AW2rDFU3Gf0HqF8WbSZmf01NfiMCOcef0Z8DJQUeQ0QOf-LfNplRMrvYRi76IKRam-EWPxLF-pN1l6i-JpK-nzCGKKP3AH_hb40G-HBymT0im; expires=Sat, 25-Jun-2016 14:17:38 GMT; path=/; domain=.google.co.jp; HttpOnly
< alternate-protocol:443:quic,p=1
< alt-svc:quic="www.google.com:443"; ma=600; v="30,29,28,27,26,25",quic=":443"; ma=600; v="30,29,28,27,26,25"
< accept-ranges:none
< vary:Accept-Encoding
<
{ [5 bytes data]
* Connection #0 to host www.google.co.jp left intact

キター!

それでは良いお年を。