December 08, 2015

Curl With HTTP2 Support

Installing HTTP2 support with the curl command

Installing HTTP2 support with the curl commandJust to get us started (really only need git):

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

Let's first curl request https://nghttp2.org (which uses http2). We'll see that it returns an HTTP1.1 response:

$ 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

If we try http2, it will give us an unsupported protocol error.

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

Install nghttp2

Install nghttp2 ad tools (C library for http2):

# 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

Upgrade to latest 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

Using ldconfig should make the curl command work, but you can also try this out:

# 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

Test out curl:

We'll get something like this:

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

Resources

All Topics