High-Performance C++17 HTTP/1.1 & HTTP/3
cpp-http is a modern, lightweight, header-only C++17 HTTP/1.1 and HTTP/3 server and client library. Engineered on top of the asynchronous network engines cpp-tcpnet and cpp-quic, it provides an asynchronous event loop combined with session-affinity multi-threading — allowing thousands of concurrent connections to be routed in parallel across worker threads while guaranteeing strict per-connection packet ordering.
Simply drop cpphttp.hpp into your project, link CMake target cpphttp::cpphttp, and begin building resilient, low-latency microservices, REST APIs, and real-time WebSocket applications.
Quick Teaser
Experience the simplicity of building asynchronous servers and executing non-blocking client requests in modern C++17:
Minimal HTTP Server
#include "cpphttp.hpp"
#include <iostream>
int main() {
cpphttp::HttpServer server(8080);
// Route with dynamic path variable returning JSON
server.Get("/users/:id", [](const cpphttp::HttpRequest& req) {
std::string id = req.path_params.at("id");
return cpphttp::HttpResponse::Json(
"{\"id\":\"" + id + "\",\"status\":\"active\"}");
});
server.Start();
std::cout << "Server listening on http://localhost:8080\n";
std::cin.get();
server.Stop();
return 0;
}
Asynchronous HTTP Client
#include "cpphttp.hpp"
#include <iostream>
#include <future>
int main() {
cpphttp::HttpClient client("127.0.0.1", 8080);
// Send asynchronous non-blocking GET request
std::future<cpphttp::HttpResponse> future = client.GetAsync("/users/42");
// Perform concurrent background work while the request completes...
cpphttp::HttpResponse res = future.get();
std::cout << "HTTP " << res.status_code << " -> " << res.body << "\n";
return 0;
}
Features at a Glance
Core & Transports
Zero-overhead network abstractions designed for modern protocols.
- Header-Only: Drop-in C++17 single header (
cpphttp.hpp). - HTTP/1.1 & HTTP/3: Full HTTP/3 over QUIC support via
cpp-quic. - Hybrid Dual-Stack Server: Binds TCP (HTTPS) and UDP (QUIC) on the same port with automatic
Alt-Svcadvertisement. - SSL/TLS Encryption: HTTPS and WSS support via OpenSSL.
Concurrency & Threading
Multi-threaded scalability with built-in state consistency guarantees.
- Session Affinity: Connections are pinned to worker threads (
session_id % num_threads) to guarantee per-connection ordering without locks. - Non-Blocking I/O: OS selectors (
poll/WSAPoll) monitor sockets on dedicated background polling threads. - Asynchronous Client Pool: Thread pool management for async HTTP requests via
cpp-asyncworker.
Routing & Semantics
Clean, declarative routing with built-in normalization.
- Dynamic Parameters: Named path segments (
/users/:id) and wildcards (/static/*). - Query & URL Parsing: Automatic percent-decoding and key-value parameter extraction.
- Headers & Cookies: Case-insensitive header maps and multi-header support (e.g. multiple
Set-Cookieentries). - Static Files & SPA: Local directory serving with MIME mapping, Range requests (
206 Partial Content), and SPA fallback.
Middleware & Resiliency
Modular pipeline architecture for security and traffic control.
- Middleware Pipeline: Global and path-prefixed pre-routing hooks (
server.Use). - CORS Middleware: Automated preflight handling and header controls.
- Rate Limiting: Fixed-window IP rate limiting with automated
Retry-Afterheaders. - Response Caching: In-memory TTL caching wrapper for idempotent GET requests.
- DoS Protection: Max header limits, body size enforcement, and idle timeouts.
Streaming & Compression
Handle large datasets without unbounded memory buffering.
- Transparent Compression: Priority negotiation for Zstandard (
zstd), Brotli (br), and Gzip (gzip). - Server Streaming: Incremental chunked-upload handling via
PostStreamandPutStream. - Client Streaming: Non-buffering streaming uploads and downloads via
GetStream,PostStream, andPostStreamAsync.
WebSockets & Real-Time
Bidirectional full-duplex communication built right into the stack.
- Server WebSocket Route: Register endpoints via
server.WebSocket("/ws", behavior). - Client WebSocket: Event-driven
WebSocketClientwithOnOpen,OnMessage, andOnClosecallbacks. - Protocol Features: Automatic ping/pong keep-alives, frame fragmentation, and continuation reassembly.
Where to Go Next
Explore the documentation guides to integrate and harness cpp-http: