More efficient use of network resources, reduced perception of latency and multiple concurrent exchanges on the same connection.
- Introduction
- Prerequisites
- Installing dependencies
- Compiling and running
- How to use
- Testing
- Short introduction to HTTP2
- What's included
- Implemented functionality
- Future work
- External information
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.
- Linux
- C++ compiler
- CMake
Dependencies needed to use HTTP-2-lib:
- Boost: Used for unit testing.
- libevent: Used for asynchronous buffer read and write.
- nghttp2: HPACK to compress and decompress header frames.
- OpenSSL: TLS and ALPN support.
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 8443Go 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.
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.
h2_server::setPublicDir("../public");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");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=detailedWith 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:8443The flags does the following:
- k: Supports self-signed certificate
- v: Verbose, detailed output
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.
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.
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.
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:
- Better management of stream states: 5.1. Stream States
- Automated use of stream identifiers: 5.1.1. Stream Identifiers
- Stream concurrency management: 5.1.2. Stream Concurrency
- Flow-control scheme to avoid destructively interference between streams: 5.2. Flow Control
- Management of stream priority: 5.3. Stream Priority
- More advanced error handling: 5.4. Error Handling
- Server push feature: 8.2. Server Push
- Own implementation of HPACK: RFC 7541
- Keep state of flow-control on WINDOW_UPDATE frames by expanding ClientSessionData object for each connection: 6.9. WINDOW_UPDATE
- RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2)
- RFC 7541 - HPACK: Header Compression for HTTP/2
- RFC 5246 - The Transport Layer Security (TLS) Protocol Version 1.2
- RFC 7301 - Transport Layer Security (TLS), Application-Layer Protocol Negotiation Extension
- Tutorial:HTTP/2 server
- Tutorial:HPACK API
- Book: O'Reilly Media, Learning HTTP/2, A practical guide for beginners