HTTP-2-lib
HTTP-2-lib Documentation

HTTP-2-lib

More efficient use of network resources, reduced perception of latency and multiple concurrent exchanges on the same connection.

Travis CI

Travis Build Status

Table of contents

Introduction

HTTP-2-lib is an implementation of the RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2) in C++. It is important to notice that this library is a work in progress, so there will be some missing HTTP/2-functionalities.

Prerequisites

Installing dependencies

Dependencies needed to use HTTP-2-lib:

Compiling and running

If you want to try the server without an IDE, use the following commands:

git clone https://github.com/M46N3/HTTP-2-lib.git
cd HTTP-2-lib/
mkdir build
cd build/
cmake ..
make
./HTTP-2-lib-server 8443

Go to https://localhost:8443 to visit the example site.

It is recommended to use a C++ IDE, you then have to follow the instruction for the chosen IDE.

How to use

Initialize server instance with path to private key and certificate. Remember to generate your own private key and certificate for security reasons.

h2_server server = h2_server("../key.pem", "../cert.pem");

Set path to the directory you want to serve.

Add routes to specific files, the files should be located in the public directory.

h2_server::addRoute("/", "/index.html");

Start the server on a specified port.

server.run("443");

Testing

If you want to run the tests from the terminal, use the following commands to print detailed information about the tests:

cd HTTP-2-lib/build/
./Tests --log_level=all --report_sink=./report --report_format=HRF --report_level=detailed

With the server running, you can also test the connection with curl. The following command will show detailed output from curl's communication with the server:

curl -k -v https://localhost:8443

The flags does the following:

Short introduction to HTTP2

HTTP/2 has the same purpose as earlier versions of HTTP: to provide a standard way for web browsers and servers to talk to each other. The HTTP-protocol is in the application layer in the OSI-model. HTTP/2 provides an optimized transport for HTTP semantics and aims to be more efficient than earlier version of HTTP.

HTTP/2 enables more efficient processing of messages with binary message framing. The original request response messages are divided into header-frames and data-frames. Header-frames is the same as headers in earlier versions, and data-frames is the same as body, but is sent as individually frames on the same stream.

Head-of-line-blocking have been a problem in earlier versions of HTTP, HTTP/2 introduced multiplexing, which reduced the amount of head-of-line-blocking. Multiplexing of requests is achieved by each having HTTP request/response exchange associated with its own stream. Streams are largely independent of each other, so a blocked or stalled request or response does not prevent progress on other streams.

Flow control and prioritization is also important for HTTP/2, these features ensure that it is possible to efficiently use multiplexed streams.

Server push is a new feature in HTTP/2 to make request/response more efficient. Server push allows a server to speculatively send data to a client that the server anticipates the client will need. For example, if the client is requesting index.html, the server push style.css and script.js to the client, because it predict that it will need it soon.

In earlier version of HTTP we did not compress header fields, HTTP/2 is the first version to introduce header field compression. Because HTTP header fields used in a connection can contain large amounts of redundant data, we sent unnecessary amount of data instead of compressing it and reduce the amount of data sent over the connection.

What's included

Within the download you'll find the following directories and files:

HTTP-2-lib/
├── include/
| └── HTTP-2-lib/
| └── h2_server.hpp
|
├── public/
| ├── index.html
| ├── index2.html
| ├── script.js
| └── style.css
|
├── src/
| ├── h2_callbacks.cpp
| ├── h2_callback.hpp
| ├── h2_config.cpp
| ├── h2_config.hpp
| ├── h2_enums.hpp
| ├── h2_frame_handlers.cpp
| ├── h2_frame_handlers.hpp
| ├── h2_global.hpp
| ├── h2_server.cpp
| ├── h2_structs.hpp
| ├── h2_utils.cpp
| └── h2_utils.hpp
├── tests/
| ├── h2_server.test.cpp
| ├── h2_utils.test.cpp
| └── main.test.cpp
|
├── cert.pem
├── CMakeLists.txt
├── main.cpp
├── key.pem
└── README.md

main.cpp, cert.pem and key.pem are included for demonstrative purposes only. Users of this library should generate their own cerificate and key files for security reasons.

Implemented functionality

HTTP-2-lib supports https, this is done by using TLS with ALPN extension. ALPN extension is used to negotiate the use of HTTP/2 with the client. Not all web browsers supports HTTP/2 over TLS, you can check which web browsers that are supported here.

Asynchronous reading and writing of frames are implemented with libevent. This feature makes it possible to read and write frames back and forth between the server and multiple clients simultaneously.

Currently the HTTP-2-lib server only supports GET requests, with the filetypes requested being .html, .css or .js.

Future work

HTTP-2-lib is far from finished. The library is expected to include more features in the future such as flow control, server push, and its own implementation of the HPACK-algorithm. We also want to support bigger payloads with multiple frames. To have a complete HTTP/2 library it would also be essential to implement support for POST, PUT and DELETE requests. The GET request should also support more filetypes, such as images.

List of missing features:

External information