93 return "size_exceeded";
95 return "null_byte_detected";
97 return "invalid_format";
99 return "invalid_character";
101 return "header_count_exceeded";
133 return size <= max_size;
146 if (size > max_size) {
147 throw std::length_error(
148 "Message size " + std::to_string(size) +
149 " exceeds limit " + std::to_string(max_size));
169 size_t src_size)
noexcept {
170 if (!dest || !src || dest_size == 0 || src_size == 0) {
174 size_t copy_size = std::min(dest_size, src_size);
175 std::memcpy(dest, src, copy_size);
190 const char* src)
noexcept {
191 if (!dest || dest_size == 0 || !src) {
195 size_t src_len = std::strlen(src);
196 size_t copy_len = std::min(dest_size - 1, src_len);
198 std::memcpy(dest, src, copy_len);
199 dest[copy_len] =
'\0';
216 std::string_view header)
noexcept {
223 if (header.find(
'\0') != std::string_view::npos) {
228 for (
char c : header) {
229 if (c < 0x20 && c !=
'\r' && c !=
'\n' && c !=
'\t') {
255 size_t payload_length,
257 return payload_length <= max_size;
267 std::string_view url)
noexcept {
272 if (url.find(
'\0') != std::string_view::npos) {
289 std::string_view data)
noexcept {
291 if (data.find(
'\0') != std::string_view::npos) {
296 if (data.find(
"\r\n\r\n") != std::string_view::npos) {
311 result.reserve(input.size());
313 for (
char c : input) {
315 if (c >= 0x20 || c ==
'\t' || c ==
'\n' || c ==
'\r') {
333 size_t requested_size,
335 return std::min(requested_size, max_size);
Message validator for network input validation.
static validation_result validate_http_header(std::string_view header) noexcept
Validate HTTP header.
static size_t safe_strcpy(char *dest, size_t dest_size, const char *src) noexcept
Safe string copy with null termination.
static bool contains_suspicious_pattern(std::string_view data) noexcept
Check if data contains potential injection patterns.
static bool validate_header_count(size_t count) noexcept
Validate HTTP header count.
static size_t safe_copy(void *dest, size_t dest_size, const void *src, size_t src_size) noexcept
Safe buffer copy with size validation.
static void validate_size_or_throw(size_t size, size_t max_size=message_limits::MAX_MESSAGE_SIZE)
Validate and throw if size exceeds limit.
static std::string sanitize_string(std::string_view input)
Sanitize string by removing control characters.
static bool validate_websocket_frame(size_t payload_length, size_t max_size=message_limits::MAX_WEBSOCKET_FRAME) noexcept
Validate WebSocket frame payload size.
static bool validate_size(size_t size, size_t max_size=message_limits::MAX_MESSAGE_SIZE) noexcept
Validate message size against limit.
static validation_result validate_url(std::string_view url) noexcept
Validate URL length.
static size_t safe_buffer_size(size_t requested_size, size_t max_size=message_limits::MAX_MESSAGE_SIZE) noexcept
Calculate safe buffer size for operations.
Main namespace for all Network System components.
const char * to_string(validation_result result)
Convert validation result to string.
validation_result
Result type for validation operations.
@ size_exceeded
Size limit exceeded.
@ null_byte_detected
NULL byte found in string.
@ invalid_format
Invalid data format.
@ header_count_exceeded
Too many headers.
@ invalid_character
Invalid character detected.
Configurable message size limits.
static constexpr size_t MAX_HEADER_SIZE
Maximum HTTP header size (default: 8KB - Apache default)
static constexpr size_t MAX_URL_LENGTH
Maximum URL length (default: 2KB)
static constexpr size_t MAX_HEADER_COUNT
Maximum number of HTTP headers (default: 100)
static constexpr size_t MAX_HTTP_LINE
Maximum HTTP request line length (default: 8KB)
static constexpr size_t MAX_WEBSOCKET_FRAME
Maximum WebSocket frame payload (default: 1MB)
static constexpr size_t MAX_MESSAGE_SIZE
Maximum allowed message size (default: 16MB)
static constexpr size_t MAX_COOKIE_SIZE
Maximum cookie size (default: 4KB)