This is an implementation of the Hypertext Transfer Protocol version 2 in C.
The framing layer of HTTP/2 is implemented as a reusable C library. On top of that, we have implemented an HTTP/2 client, server and proxy. We have also developed load test and benchmarking tools for HTTP/2 and SPDY.
An HPACK encoder and decoder are available as a public API.
An experimental high level C++ library is also available.
We have Python bindings of this library, but we do not have full code coverage yet.
We have implemented RFC 7540 <https://tools.ietf.org/html/rfc7540>_
HTTP/2 and RFC 7541 <https://tools.ietf.org/html/rfc7541>_ HPACK -
Header Compression for HTTP/2
The nghttp2 code base was forked from the spdylay (https://github.com/tatsuhiro-t/spdylay) project.
The following endpoints are available to try out our nghttp2 implementation.
https://nghttp2.org/ (TLS + ALPN/NPN)
This endpoint supports h2, h2-16, h2-14, spdy/3.1
and http/1.1 via ALPN/NPN and requires TLSv1.2 for HTTP/2
connection.
http://nghttp2.org/ (HTTP Upgrade and HTTP/2 Direct)
h2c and http/1.1.
The following package is required to build the libnghttp2 library:
To build and run the unit test programs, the following package is required:
To build the documentation, you need to install:
If you need libnghttp2 (C library) only, then the above packages are
all you need. Use --enable-lib-only to ensure that only
libnghttp2 is built. This avoids potential build error related to
building bundled applications.
To build and run the application programs (nghttp, nghttpd,
nghttpx and h2load) in the src directory, the following packages
are required:
ALPN support requires OpenSSL >= 1.0.2 (released 22 January 2015). LibreSSL >= 2.2.0 can be used instead of OpenSSL, but OpenSSL has more features than LibreSSL at the time of this writing.
To enable the SPDY protocol in the application program nghttpx and
h2load, the following package is required:
We no longer recommend to build nghttp2 with SPDY protocol support enabled. SPDY support will be removed soon.
To enable -a option (getting linked assets from the downloaded
resource) in nghttp, the following package is required:
To enable systemd support in nghttpx, the following package is required:
The HPACK tools require the following package:
To build sources under the examples directory, libevent is required:
To mitigate heap fragmentation in long running server programs
(nghttpd and nghttpx), jemalloc is recommended:
jemalloc
.. note::
Alpine Linux currently does not support malloc replacement
due to musl limitations. See details in issue #762 <https://github.com/nghttp2/nghttp2/issues/762>_.
libnghttp2_asio C++ library requires the following packages:
The Python bindings require the following packages:
If you are using Ubuntu 16.04 LTS (Xenial Xerus) or Debian 8 (jessie) and above, run the following to install the required packages:
.. code-block:: text
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 \
libc-ares-dev libjemalloc-dev libsystemd-dev libspdylay-dev \
cython python3-dev python-setuptools
Since Ubuntu 15.10, spdylay has been available as a package named
libspdylay-dev. For the earlier Ubuntu release, you need to build
it yourself: http://tatsuhiro-t.github.io/spdylay/
To enable mruby support for nghttpx, mruby <https://github.com/mruby/mruby>_ is required. We need to build
mruby with C++ ABI explicitly turned on, and probably need other
mrgems, mruby is manged by git submodule under third-party/mruby
directory. Currently, mruby support for nghttpx is disabled by
default. To enable mruby support, use --with-mruby configure
option. Note that at the time of this writing, libmruby-dev and mruby
packages in Debian/Ubuntu are not usable for nghttp2, since they do
not enable C++ ABI. To build mruby, the following packages are
required:
nghttpx supports neverbleed <https://github.com/h2o/neverbleed>_,
privilege separation engine for OpenSSL / LibreSSL. In short, it
minimizes the risk of private key leakage when serious bug like
Heartbleed is exploited. The neverbleed is disabled by default. To
enable it, use --with-neverbleed configure option.
In order to compile the source code, gcc >= 4.8.3 or clang >= 3.4 is required.
.. note::
To enable mruby support in nghttpx, and use --with-mruby
configure option.
.. note::
Mac OS X users may need the --disable-threads configure option to
disable multi-threading in nghttpd, nghttpx and h2load to prevent
them from crashing. A patch is welcome to make multi threading work
on Mac OS X platform.
.. note::
To compile the associated applications (nghttp, nghttpd, nghttpx
and h2load), you must use the --enable-app configure option and
ensure that the specified requirements above are met. Normally,
configure script checks required dependencies to build these
applications, and enable --enable-app automatically, so you
don't have to use it explicitly. But if you found that
applications were not built, then using --enable-app may find
that cause, such as the missing dependency.
.. note::
In order to detect third party libraries, pkg-config is used
(however we don't use pkg-config for some libraries (e.g., libev)).
By default, pkg-config searches *.pc file in the standard
locations (e.g., /usr/lib/pkgconfig). If it is necessary to use
*.pc file in the custom location, specify paths to
PKG_CONFIG_PATH environment variable, and pass it to configure
script, like so:
.. code-block:: text
$ ./configure PKG_CONFIG_PATH=/path/to/pkgconfig
For pkg-config managed libraries, *_CFLAG and *_LIBS
environment variables are defined (e.g., OPENSSL_CFLAGS,
OPENSSL_LIBS). Specifying non-empty string to these variables
completely overrides pkg-config. In other words, if they are
specified, pkg-config is not used for detection, and user is
responsible to specify the correct values to these variables. For
complete list of these variables, run ./configure -h.
The nghttp2 project regularly releases tar archives which includes
nghttp2 source code, and generated build files. They can be
downloaded from Releases <https://github.com/nghttp2/nghttp2/releases>_ page.
Building nghttp2 from git requires autotools development packages. Building from tar archives does not require them, and thus it is much easier. The usual build step is as follows:
.. code-block:: text
$ tar xf nghttp2-X.Y.Z.tar.bz2
$ cd nghttp2-X.Y.Z
$ ./configure
$ make
Building from git is easy, but please be sure that at least autoconf 2.68 is used:
.. code-block:: text
$ git submodule update --init
$ autoreconf -i
$ automake
$ autoconf
$ ./configure
$ make
The easiest way to build native Windows nghttp2 dll is use cmake <https://cmake.org/>. The free version of Visual C++ Build Tools <http://landinghub.visualstudio.com/visual-cpp-build-tools> works
fine.
cmake.cmake --build to build library.Note that the above steps most likely produce nghttp2 library only. No bundled applications are compiled.
Under Mingw environment, you can only compile the library, it's
libnghttp2-X.dll and libnghttp2.a.
If you want to compile the applications(h2load, nghttp,
nghttpx, nghttpd), you need to use the Cygwin environment.
Under Cygwin environment, to compile the applications you need to compile and install the libev first.
Secondly, you need to undefine the macro __STRICT_ANSI__, if you
not, the functions fdopen, fileno and strptime will not
available.
the sample command like this:
.. code-block:: text
$ export CFLAGS="-U__STRICT_ANSI__ -I$libev_PREFIX/include -L$libev_PREFIX/lib"
$ export CXXFLAGS=$CFLAGS
$ ./configure
$ make
If you want to compile the applications under examples/, you need
to remove or rename the event.h from libev's installation, because
it conflicts with libevent's installation.
After installing nghttp2 tool suite with make install one might experience a similar error:
.. code-block:: text
nghttpx: error while loading shared libraries: libnghttp2.so.14: cannot open shared object file: No such file or directory
This means that the tool is unable to locate the libnghttp2.so shared library.
To update the shared library cache run sudo ldconfig.
.. note::
Documentation is still incomplete.
To build the documentation, run:
.. code-block:: text
$ make html
The documents will be generated under doc/manual/html/.
The generated documents will not be installed with make install.
The online documentation is available at https://nghttp2.org/documentation/
Unit tests are done by simply running make check.
We have the integration tests for the nghttpx proxy server. The tests are
written in the Go programming language <http://golang.org/>_ and uses
its testing framework. We depend on the following libraries:
To download the above packages, after settings GOPATH, run the
following command under integration-tests directory:
.. code-block:: text
$ make itprep
To run the tests, run the following command under
integration-tests directory:
.. code-block:: text
$ make it
Inside the tests, we use port 3009 to run the test subject server.
.. note::
github.com/tatsuhiro-t/spdy is a copy used to be available at golang.org/x/net/spdy, but it is now gone.
nghttp2 v1.0.0 introduced several backward incompatible changes. In this section, we describe these changes and how to migrate to v1.0.0.
ALPN protocol ID is now h2 and h2c
++++++++++++++++++++++++++++++++++++++++++
Previously we announced h2-14 and h2c-14. v1.0.0 implements
final protocol version, and we changed ALPN ID to h2 and h2c.
The macros NGHTTP2_PROTO_VERSION_ID,
NGHTTP2_PROTO_VERSION_ID_LEN,
NGHTTP2_CLEARTEXT_PROTO_VERSION_ID, and
NGHTTP2_CLEARTEXT_PROTO_VERSION_ID_LEN have been updated to
reflect this change.
Basically, existing applications do not have to do anything, just recompiling is enough for this change.
Use word "client magic" where we use "client connection preface" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
We use "client connection preface" to mean first 24 bytes of client connection preface. This is technically not correct, since client connection preface is composed of 24 bytes client magic byte string followed by SETTINGS frame. For clarification, we call "client magic" for this 24 bytes byte string and updated API.
NGHTTP2_CLIENT_CONNECTION_PREFACE was replaced with
NGHTTP2_CLIENT_MAGIC.NGHTTP2_CLIENT_CONNECTION_PREFACE_LEN was replaced with
NGHTTP2_CLIENT_MAGIC_LEN.NGHTTP2_BAD_PREFACE was renamed as NGHTTP2_BAD_CLIENT_MAGICThe already deprecated NGHTTP2_CLIENT_CONNECTION_HEADER and
NGHTTP2_CLIENT_CONNECTION_HEADER_LEN were removed.
If application uses these macros, just replace old ones with new ones. Since v1.0.0, client magic is sent by library (see next subsection), so client application may just remove these macro use.
Client magic is sent by library +++++++++++++++++++++++++++++++
Previously nghttp2 library did not send client magic, which is first
24 bytes byte string of client connection preface, and client
applications have to send it by themselves. Since v1.0.0, client
magic is sent by library via first call of nghttp2_session_send()
or nghttp2_session_mem_send().
The client applications which send client magic must remove the relevant code.
Remove HTTP Alternative Services (Alt-Svc) related code +++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alt-Svc specification is not finalized yet. To make our API stable, we have decided to remove all Alt-Svc related API from nghttp2.
NGHTTP2_EXT_ALTSVC was removed.nghttp2_ext_altsvc was removed.We have already removed the functionality of Alt-Svc in v0.7 series and they have been essentially noop. The application using these macro and struct, remove those lines.
Use nghttp2_error in nghttp2_on_invalid_frame_recv_callback +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Previously nghttp2_on_invalid_frame_recv_cb_called took the
error_code, defined in nghttp2_error_code, as parameter. But
they are not detailed enough to debug. Therefore, we decided to use
more detailed nghttp2_error values instead.
The application using this callback should update the callback
signature. If it treats error_code as HTTP/2 error code, update
the code so that it is treated as nghttp2_error.
Receive client magic by default +++++++++++++++++++++++++++++++
Previously nghttp2 did not process client magic (24 bytes byte
string). To make it deal with it, we had to use
nghttp2_option_set_recv_client_preface(). Since v1.0.0, nghttp2
processes client magic by default and
nghttp2_option_set_recv_client_preface() was removed.
Some application may want to disable this behaviour, so we added
nghttp2_option_set_no_recv_client_magic() to achieve this.
The application using nghttp2_option_set_recv_client_preface()
with nonzero value, just remove it.
The application using nghttp2_option_set_recv_client_preface()
with zero value or not using it must use
nghttp2_option_set_no_recv_client_magic() with nonzero value.
The src directory contains the HTTP/2 client, server and proxy programs.
nghttp - client +++++++++++++++
nghttp is a HTTP/2 client. It can connect to the HTTP/2 server
with prior knowledge, HTTP Upgrade and NPN/ALPN TLS extension.
It has verbose output mode for framing information. Here is sample
output from nghttp client:
.. code-block:: text
$ nghttp -nv https://nghttp2.org
[ 0.190] Connected
The negotiated protocol: h2
[ 0.212] recv SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.212] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.212] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.212] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
(dep_stream_id=0, weight=201, exclusive=0)
[ 0.212] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
(dep_stream_id=0, weight=101, exclusive=0)
[ 0.212] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
(dep_stream_id=0, weight=1, exclusive=0)
[ 0.212] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
(dep_stream_id=7, weight=1, exclusive=0)
[ 0.212] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
(dep_stream_id=3, weight=1, exclusive=0)
[ 0.212] send HEADERS frame <length=39, flags=0x25, stream_id=13>
; END_STREAM | END_HEADERS | PRIORITY
(padlen=0, dep_stream_id=11, weight=16, exclusive=0)
; Open new stream
:method: GET
:path: /
:scheme: https
:authority: nghttp2.org
accept: */*
accept-encoding: gzip, deflate
user-agent: nghttp2/1.0.1-DEV
[ 0.221] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.221] recv (stream_id=13) :method: GET
[ 0.221] recv (stream_id=13) :scheme: https
[ 0.221] recv (stream_id=13) :path: /stylesheets/screen.css
[ 0.221] recv (stream_id=13) :authority: nghttp2.org
[ 0.221] recv (stream_id=13) accept-encoding: gzip, deflate
[ 0.222] recv (stream_id=13) user-agent: nghttp2/1.0.1-DEV
[ 0.222] recv PUSH_PROMISE frame <length=50, flags=0x04, stream_id=13>
; END_HEADERS
(padlen=0, promised_stream_id=2)
[ 0.222] recv (stream_id=13) :status: 200
[ 0.222] recv (stream_id=13) date: Thu, 21 May 2015 16:38:14 GMT
[ 0.222] recv (stream_id=13) content-type: text/html
[ 0.222] recv (stream_id=13) last-modified: Fri, 15 May 2015 15:38:06 GMT
[ 0.222] recv (stream_id=13) etag: W/"555612de-19f6"
[ 0.222] recv (stream_id=13) link: </stylesheets/screen.css>; rel=preload; as=stylesheet
[ 0.222] recv (stream_id=13) content-encoding: gzip
[ 0.222] recv (stream_id=13) server: nghttpx nghttp2/1.0.1-DEV
[ 0.222] recv (stream_id=13) via: 1.1 nghttpx
[ 0.222] recv (stream_id=13) strict-transport-security: max-age=31536000
[ 0.222] recv HEADERS frame <length=166, flags=0x04, stream_id=13>
; END_HEADERS
(padlen=0)
; First response header
[ 0.222] recv DATA frame <length=2601, flags=0x01, stream_id=13>
; END_STREAM
[ 0.222] recv (stream_id=2) :status: 200
[ 0.222] recv (stream_id=2) date: Thu, 21 May 2015 16:38:14 GMT
[ 0.222] recv (stream_id=2) content-type: text/css
[ 0.222] recv (stream_id=2) last-modified: Fri, 15 May 2015 15:38:06 GMT
[ 0.222] recv (stream_id=2) etag: W/"555612de-9845"
[ 0.222] recv (stream_id=2) content-encoding: gzip
[ 0.222] recv (stream_id=2) server: nghttpx nghttp2/1.0.1-DEV
[ 0.222] recv (stream_id=2) via: 1.1 nghttpx
[ 0.222] recv (stream_id=2) strict-transport-security: max-age=31536000
[ 0.222] recv HEADERS frame <length=32, flags=0x04, stream_id=2>
; END_HEADERS
(padlen=0)
; First push response header
[ 0.228] recv DATA frame <length=8715, flags=0x01, stream_id=2>
; END_STREAM
[ 0.228] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
(last_stream_id=2, error_code=NO_ERROR(0x00), opaque_data(0)=[])
The HTTP Upgrade is performed like so:
.. code-block:: text
$ nghttp -nvu http://nghttp2.org
[ 0.011] Connected
[ 0.011] HTTP Upgrade request
GET / HTTP/1.1
Host: nghttp2.org
Connection: Upgrade, HTTP2-Settings
Upgrade: h2c
HTTP2-Settings: AAMAAABkAAQAAP__
Accept: */*
User-Agent: nghttp2/1.0.1-DEV
[ 0.018] HTTP Upgrade response
HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: h2c
[ 0.018] HTTP Upgrade success
[ 0.018] recv SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.018] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
(niv=2)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
[SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
[ 0.018] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
(dep_stream_id=0, weight=201, exclusive=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
(dep_stream_id=0, weight=101, exclusive=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
(dep_stream_id=0, weight=1, exclusive=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
(dep_stream_id=7, weight=1, exclusive=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
(dep_stream_id=3, weight=1, exclusive=0)
[ 0.018] send PRIORITY frame <length=5, flags=0x00, stream_id=1>
(dep_stream_id=11, weight=16, exclusive=0)
[ 0.019] recv (stream_id=1) :method: GET
[ 0.019] recv (stream_id=1) :scheme: http
[ 0.019] recv (stream_id=1) :path: /stylesheets/screen.css
[ 0.019] recv (stream_id=1) host: nghttp2.org
[ 0.019] recv (stream_id=1) user-agent: nghttp2/1.0.1-DEV
[ 0.019] recv PUSH_PROMISE frame <length=49, flags=0x04, stream_id=1>
; END_HEADERS
(padlen=0, promised_stream_id=2)
[ 0.019] recv (stream_id=1) :status: 200
[ 0.019] recv (stream_id=1) date: Thu, 21 May 2015 16:39:16 GMT
[ 0.019] recv (stream_id=1) content-type: text/html
[ 0.019] recv (stream_id=1) content-length: 6646
[ 0.019] recv (stream_id=1) last-modified: Fri, 15 May 2015 15:38:06 GMT
[ 0.019] recv (stream_id=1) etag: "555612de-19f6"
[ 0.019] recv (stream_id=1) link: </stylesheets/screen.css>; rel=preload; as=stylesheet
[ 0.019] recv (stream_id=1) accept-ranges: bytes
[ 0.019] recv (stream_id=1) server: nghttpx nghttp2/1.0.1-DEV
[ 0.019] recv (stream_id=1) via: 1.1 nghttpx
[ 0.019] recv HEADERS frame <length=157, flags=0x04, stream_id=1>
; END_HEADERS
(padlen=0)
; First response header
[ 0.019] recv DATA frame <length=6646, flags=0x01, stream_id=1>
; END_STREAM
[ 0.019] recv (stream_id=2) :status: 200
[ 0.019] recv (stream_id=2) date: Thu, 21 May 2015 16:39:16 GMT
[ 0.019] recv (stream_id=2) content-type: text/css
[ 0.019] recv (stream_id=2) content-length: 38981
[ 0.019] recv (stream_id=2) last-modified: Fri, 15 May 2015 15:38:06 GMT
[ 0.019] recv (stream_id=2) etag: "555612de-9845"
[ 0.019] recv (stream_id=2) accept-ranges: bytes
[ 0.019] recv (stream_id=2) server: nghttpx nghttp2/1.0.1-DEV
[ 0.019] recv (stream_id=2) via: 1.1 nghttpx
[ 0.019] recv HEADERS frame <length=36, flags=0x04, stream_id=2>
; END_HEADERS
(padlen=0)
; First push response header
[ 0.026] recv DATA frame <length=16384, flags=0x00, stream_id=2>
[ 0.027] recv DATA frame <length=7952, flags=0x00, stream_id=2>
[ 0.027] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=0>
(window_size_increment=33343)
[ 0.032] send WINDOW_UPDATE frame <length=4, flags=0x00, stream_id=2>
(window_size_increment=33707)
[ 0.032] recv DATA frame <length=14645, flags=0x01, stream_id=2>
; END_STREAM
[ 0.032] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
; ACK
(niv=0)
[ 0.032] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
(last_stream_id=2, error_code=NO_ERROR(0x00), opaque_data(0)=[])
Using the -s option, nghttp prints out some timing information for
requests, sorted by completion time:
.. code-block:: text
$ nghttp -nas https://nghttp2.org/
***** Statistics *****
Request timing:
responseEnd: the time when last byte of response was received
relative to connectEnd
requestStart: the time just before first byte of request was sent
relative to connectEnd. If '*' is shown, this was
pushed by server.
process: responseEnd - requestStart
code: HTTP status code
size: number of bytes received as response body without
inflation.
URI: request URI
see http://www.w3.org/TR/resource-timing/#processing-model
sorted by 'complete'
id responseEnd requestStart process code size request path
13 +37.19ms +280us 36.91ms 200 2K /
2 +72.65ms * +36.38ms 36.26ms 200 8K /stylesheets/screen.css
17 +77.43ms +38.67ms 38.75ms 200 3K /javascripts/octopress.js
15 +7
Content type
Image
Digest
Size
444.6 MB
Last updated
about 9 years ago
docker pull gitai/nghttp2