27 std::cout <<
"=== HTTP/2 Server Example ===" << std::endl;
28 std::cout <<
"Note: This example uses h2c (HTTP/2 cleartext) for simplicity." << std::endl;
29 std::cout <<
" For production, use start_tls() with proper certificates." << std::endl;
30 std::cout << std::endl;
37 auto server = std::make_shared<http2_server>(
"http2-example-server");
42 settings.initial_window_size = 65535;
47 server->set_error_handler([](
const std::string& error_msg)
49 std::cerr <<
"[ERROR] " << error_msg << std::endl;
56 std::cout <<
"[REQUEST] " << request.
method <<
" " << request.
path << std::endl;
59 if (request.
method ==
"GET" && request.
path ==
"/")
62 std::string body = R
"({
63 "message": "Welcome to HTTP/2 Server",
73 {"content-type",
"application/json"},
74 {
"server",
"network-system-http2"}
78 else if (request.
method ==
"GET" && request.
path ==
"/api/health")
82 {
"content-type",
"application/json"}
84 stream.
send_data(R
"({"status": "healthy"})", true);
86 else if (request.
method ==
"GET" && request.
path ==
"/api/echo")
89 std::string response = R
"({"path": ")" + request.path + R"(", "authority": ")" +
92 {"content-type",
"application/json"}
96 else if (request.
method ==
"POST" && request.
path ==
"/api/data")
101 std::string response = R
"({"received_bytes": )" +
102 std::to_string(request.body.size()) + R"(, "content_type": ")" +
103 content_type.value_or("unknown") + R
"("})";
106 {"content-type",
"application/json"}
114 {
"content-type",
"application/json"}
116 stream.
send_data(R
"({"error": "Not Found"})", true);
121 const unsigned short port = 8080;
122 std::cout <<
"Starting HTTP/2 server on port " << port <<
"..." << std::endl;
124 auto result = server->start(port);
127 std::cerr <<
"Failed to start server: " << result.error().message << std::endl;
131 std::cout <<
"Server started successfully!" << std::endl;
132 std::cout << std::endl;
133 std::cout <<
"Available endpoints:" << std::endl;
134 std::cout <<
" GET http://localhost:" << port <<
"/" << std::endl;
135 std::cout <<
" GET http://localhost:" << port <<
"/api/health" << std::endl;
136 std::cout <<
" GET http://localhost:" << port <<
"/api/echo" << std::endl;
137 std::cout <<
" POST http://localhost:" << port <<
"/api/data" << std::endl;
138 std::cout << std::endl;
139 std::cout <<
"Test with curl (requires h2c support):" << std::endl;
140 std::cout <<
" curl --http2-prior-knowledge http://localhost:" << port <<
"/" << std::endl;
141 std::cout <<
" curl --http2-prior-knowledge http://localhost:" << port <<
"/api/health" << std::endl;
142 std::cout <<
" curl --http2-prior-knowledge -X POST -d '{\"test\": 123}' http://localhost:"
143 << port <<
"/api/data" << std::endl;
144 std::cout << std::endl;
145 std::cout <<
"Press Ctrl+C to stop the server..." << std::endl;
150 std::this_thread::sleep_for(std::chrono::milliseconds(100));
153 std::cout <<
"\nShutting down server..." << std::endl;
155 std::cout <<
"Server stopped." << std::endl;
Server-side HTTP/2 stream for sending responses.
auto send_headers(int status_code, const std::vector< http_header > &headers, bool end_stream=false) -> VoidResult
Send response headers.
auto send_data(const std::vector< uint8_t > &data, bool end_stream=false) -> VoidResult
Send response data.
@ settings
SETTINGS frame.
std::atomic< bool > g_running
HTTP/2 request data structure.
auto content_type() const -> std::optional< std::string >
Get Content-Type header value.
std::vector< uint8_t > body
Request body from DATA frames.
std::string path
Request path (:path pseudo-header)
std::string authority
Authority (:authority pseudo-header)
std::string method
HTTP method (:method pseudo-header)
HTTP/2 connection settings.
uint32_t max_concurrent_streams
Max concurrent streams.