Boost.HTTP
HTTP powers the web, but implementing it correctly is surprisingly hard. Boost.HTTP is a portable C++ library that provides containers and algorithms for the HTTP/1.1 protocol, giving you RFC-compliant message handling without the usual implementation headaches.
What This Library Does
-
Provides modifiable containers for HTTP requests and responses
-
Parses incoming HTTP messages with configurable limits
-
Serializes outgoing messages with automatic chunked encoding
-
Handles content encodings (gzip, deflate, brotli)
-
Offers an Express.js-style router for request dispatch
-
Enforces RFC 9110 compliance to prevent common security issues
What This Library Does Not Do
-
Network I/O — this is a Sans-I/O library by design
-
HTTP/2 or HTTP/3 protocol support
-
TLS/SSL handling
-
Cookie management or session state
-
Full HTTP client/server implementation (see Boost.Beast2 for I/O)
Target Audience
This library is for C++ developers who need precise control over HTTP message handling. You should have:
-
Familiarity with TCP/IP networking concepts
-
Understanding of the HTTP request/response model
-
Experience with C++ move semantics and memory management
Design Philosophy
The library follows a Sans-I/O architecture that separates protocol logic from network operations. This design choice yields several benefits:
Reusability. The same protocol code works with any I/O framework — Asio, io_uring, or platform-specific APIs. Write the HTTP logic once, integrate it anywhere.
Testability. Tests run as pure function calls without sockets, timers, or network delays. Coverage is higher, execution is faster, results are deterministic.
Security. The parser is strict by default. Malformed input that could enable request smuggling or header injection is rejected immediately.
Code Conventions
Code examples in this documentation assume these declarations are in effect:
#include <boost/http.hpp>
using namespace boost::http;
Quick Example
#include <boost/http.hpp>
using namespace boost::http;
int main()
{
// Build a request
request req(method::get, "/api/users");
req.set(field::host, "example.com");
req.set(field::accept, "application/json");
// Build a response
response res(status::ok);
res.set(field::content_type, "application/json");
res.set(field::content_length, "42");
// Access the serialized form
std::cout << req.buffer();
std::cout << res.buffer();
}
Output:
GET /api/users HTTP/1.1
Host: example.com
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 42
Next Steps
-
Introduction to HTTP — understand HTTP sessions and message flow
-
Containers — work with requests, responses, and fields
-
Parsing — parse incoming HTTP messages
-
Serializing — produce outgoing HTTP messages
-
Router — dispatch requests to handlers
Acknowledgments
This library wouldn’t be where it is today without the help of Peter Dimov for design advice and general assistance.