Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_server.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
13#include <string>
14#include <map>
15#include <functional>
16#include <memory>
17#include <regex>
18#include <vector>
19#include <chrono>
20
22{
28 {
29 std::vector<uint8_t> data;
30 bool headers_complete = false;
31 std::size_t headers_end_pos = 0;
32 std::size_t content_length = 0;
33
34 static constexpr std::size_t MAX_REQUEST_SIZE = 10 * 1024 * 1024; // 10MB
35 static constexpr std::size_t MAX_HEADER_SIZE = 64 * 1024; // 64KB
36
42 auto append(const std::vector<uint8_t>& chunk) -> bool;
43
48 auto is_complete() const -> bool;
49
54 static auto find_header_end(const std::vector<uint8_t>& data) -> std::size_t;
55
62 static auto parse_content_length(const std::vector<uint8_t>& data,
63 std::size_t headers_end) -> std::size_t;
64 };
65
71 {
73 std::map<std::string, std::string> path_params; // Extracted path parameters (e.g., /users/:id)
74
80 auto get_query_param(const std::string& name) const -> std::optional<std::string>
81 {
82 auto it = request.query_params.find(name);
83 if (it != request.query_params.end())
84 {
85 return it->second;
86 }
87 return std::nullopt;
88 }
89
95 auto get_path_param(const std::string& name) const -> std::optional<std::string>
96 {
97 auto it = path_params.find(name);
98 if (it != path_params.end())
99 {
100 return it->second;
101 }
102 return std::nullopt;
103 }
104 };
105
114
123
129 {
131 std::string pattern; // e.g., "/users/:id"
132 std::regex regex_pattern; // Compiled regex for matching
133 std::vector<std::string> param_names; // Parameter names extracted from pattern
135
143 auto matches(internal::http_method method, const std::string& path,
144 std::map<std::string, std::string>& path_params) const -> bool;
145 };
146
206 class http_server : public std::enable_shared_from_this<http_server>
207 {
208 public:
213 explicit http_server(const std::string& server_id);
214
218 ~http_server();
219
225 auto start(unsigned short port) -> VoidResult;
226
231 auto stop() -> VoidResult;
232
236 auto wait_for_stop() -> void;
237
243 auto get(const std::string& pattern, http_handler handler) -> void;
244
250 auto post(const std::string& pattern, http_handler handler) -> void;
251
257 auto put(const std::string& pattern, http_handler handler) -> void;
258
264 auto del(const std::string& pattern, http_handler handler) -> void;
265
271 auto patch(const std::string& pattern, http_handler handler) -> void;
272
278 auto head(const std::string& pattern, http_handler handler) -> void;
279
285 auto options(const std::string& pattern, http_handler handler) -> void;
286
291 auto set_not_found_handler(http_handler handler) -> void;
292
297 auto set_error_handler(http_handler handler) -> void;
298
304 auto set_error_handler(internal::http_error_code code, error_handler handler) -> void;
305
310 auto set_default_error_handler(error_handler handler) -> void;
311
316 auto set_request_timeout(std::chrono::milliseconds timeout) -> void;
317
322 auto set_json_error_responses(bool enable) -> void;
323
328 auto set_compression_enabled(bool enable) -> void;
329
334 auto set_compression_threshold(size_t threshold_bytes) -> void;
335
336 private:
343 auto register_route(internal::http_method method,
344 const std::string& pattern,
345 http_handler handler) -> void;
346
354 auto find_route(internal::http_method method,
355 const std::string& path,
356 std::map<std::string, std::string>& path_params) const
357 -> const http_route*;
358
364 auto handle_request(const std::vector<uint8_t>& request_data) -> std::vector<uint8_t>;
365
371 auto process_http_request(const internal::http_request& request) -> internal::http_response;
372
379 auto should_close_connection(const internal::http_request& request,
380 const internal::http_response& response) const -> bool;
381
388 auto create_error_response(int status_code, const std::string& message)
390
397 static auto pattern_to_regex(const std::string& pattern,
398 std::vector<std::string>& param_names) -> std::string;
399
405 auto apply_compression(const internal::http_request& request,
406 internal::http_response& response) -> void;
407
413 auto choose_compression_algorithm(const std::string& accept_encoding) const
415
421 auto build_error_response(const internal::http_error& error) -> internal::http_response;
422
423 std::shared_ptr<messaging_server> tcp_server_;
424 std::vector<http_route> routes_;
425 std::mutex routes_mutex_;
428
429 // Request buffering support
430 std::map<std::shared_ptr<session::messaging_session>, http_request_buffer> session_buffers_;
431 std::mutex buffers_mutex_;
432
433 // Compression settings
434 bool compression_enabled_ = false;
435 size_t compression_threshold_ = 1024; // 1KB default
437
438 // Error handling settings
439 std::map<internal::http_error_code, error_handler> error_handlers_;
442 bool use_json_errors_ = false;
443 std::chrono::milliseconds request_timeout_{30000}; // 30 seconds default
444 };
445
446} // namespace kcenon::network::core
HTTP/1.1 server built on top of messaging_server.
std::map< internal::http_error_code, error_handler > error_handlers_
std::vector< http_route > routes_
std::map< std::shared_ptr< session::messaging_session >, http_request_buffer > session_buffers_
std::shared_ptr< messaging_server > tcp_server_
TCP server implementation.
std::function< internal::http_response(const http_request_context &ctx)> http_handler
Handler function for HTTP requests.
std::function< internal::http_response(const internal::http_error &error)> error_handler
Handler function for HTTP errors.
http_error_code
Standard HTTP error codes (RFC 7231)
Definition http_error.h:20
http_method
HTTP request methods (verbs)
Definition http_types.h:24
compression_algorithm
Supported compression algorithms.
Result< T > error(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
Network-specific error and result type definitions.
Buffer for accumulating HTTP request data received in chunks.
Definition http_server.h:28
auto append(const std::vector< uint8_t > &chunk) -> bool
Append new data chunk to buffer.
static auto parse_content_length(const std::vector< uint8_t > &data, std::size_t headers_end) -> std::size_t
Parse Content-Length from headers.
auto is_complete() const -> bool
Check if complete HTTP request has been received.
static constexpr std::size_t MAX_REQUEST_SIZE
Definition http_server.h:34
static auto find_header_end(const std::vector< uint8_t > &data) -> std::size_t
Find end of HTTP headers (\r\n\r\n marker)
static constexpr std::size_t MAX_HEADER_SIZE
Definition http_server.h:35
Context for an HTTP request with parsed components.
Definition http_server.h:71
auto get_path_param(const std::string &name) const -> std::optional< std::string >
Get path parameter value.
Definition http_server.h:95
auto get_query_param(const std::string &name) const -> std::optional< std::string >
Get query parameter value.
Definition http_server.h:80
std::map< std::string, std::string > path_params
Definition http_server.h:73
Route definition with pattern matching and handler.
std::vector< std::string > param_names
internal::http_method method
Structured HTTP error information.
Definition http_error.h:78
Represents an HTTP request message.
Definition http_types.h:112
std::map< std::string, std::string > query_params
Definition http_types.h:118
Represents an HTTP response message.
Definition http_types.h:162