Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::http2::http2_request Struct Reference

HTTP/2 request data structure. More...

#include <http2_request.h>

Collaboration diagram for kcenon::network::protocols::http2::http2_request:
Collaboration graph

Public Member Functions

auto get_header (std::string_view name) const -> std::optional< std::string >
 Get header value by name (case-insensitive)
 
auto content_type () const -> std::optional< std::string >
 Get Content-Type header value.
 
auto content_length () const -> std::optional< size_t >
 Get Content-Length header value as size_t.
 
auto get_body_string () const -> std::string
 Get request body as UTF-8 string.
 
auto is_valid () const -> bool
 Check if this is a valid HTTP/2 request.
 

Static Public Member Functions

static auto from_headers (const std::vector< http_header > &parsed_headers) -> http2_request
 Create http2_request from parsed headers.
 

Public Attributes

std::string method
 HTTP method (:method pseudo-header)
 
std::string path
 Request path (:path pseudo-header)
 
std::string authority
 Authority (:authority pseudo-header)
 
std::string scheme
 Scheme (:scheme pseudo-header)
 
std::vector< http_headerheaders
 Regular headers (non-pseudo)
 
std::vector< uint8_t > body
 Request body from DATA frames.
 

Detailed Description

HTTP/2 request data structure.

Represents an incoming HTTP/2 request with all pseudo-headers and regular headers parsed from HEADERS frames.

Pseudo-Headers (RFC 7540 Section 8.1.2.3)

  • :method - HTTP method (GET, POST, etc.)
  • :path - Request path
  • :scheme - URI scheme (http or https)
  • :authority - Authority portion of target URI

Definition at line 32 of file http2_request.h.

Member Function Documentation

◆ content_length()

auto kcenon::network::protocols::http2::http2_request::content_length ( ) const -> std::optional<size_t>
inlinenodiscard

Get Content-Length header value as size_t.

Returns
Content-Length value if present and valid

Definition at line 79 of file http2_request.h.

80 {
81 auto value = get_header("content-length");
82 if (!value) {
83 return std::nullopt;
84 }
85 try {
86 return static_cast<size_t>(std::stoull(*value));
87 } catch (...) {
88 return std::nullopt;
89 }
90 }
auto get_header(std::string_view name) const -> std::optional< std::string >
Get header value by name (case-insensitive)

◆ content_type()

auto kcenon::network::protocols::http2::http2_request::content_type ( ) const -> std::optional<std::string>
inlinenodiscard

Get Content-Type header value.

Returns
Content-Type value if present

Definition at line 70 of file http2_request.h.

71 {
72 return get_header("content-type");
73 }

◆ from_headers()

static auto kcenon::network::protocols::http2::http2_request::from_headers ( const std::vector< http_header > & parsed_headers) -> http2_request
inlinestatic

Create http2_request from parsed headers.

Parameters
parsed_headersHeaders decoded from HPACK
Returns
http2_request with pseudo-headers and regular headers separated

This method separates pseudo-headers (starting with ':') from regular headers and populates the appropriate fields.

Definition at line 131 of file http2_request.h.

133 {
134 http2_request request;
135 for (const auto& header : parsed_headers) {
136 if (header.name.empty()) {
137 continue;
138 }
139 if (header.name[0] == ':') {
140 // Pseudo-header
141 if (header.name == ":method") {
142 request.method = header.value;
143 } else if (header.name == ":path") {
144 request.path = header.value;
145 } else if (header.name == ":scheme") {
146 request.scheme = header.value;
147 } else if (header.name == ":authority") {
148 request.authority = header.value;
149 }
150 // Ignore unknown pseudo-headers
151 } else {
152 // Regular header
153 request.headers.push_back(header);
154 }
155 }
156 return request;
157 }

References authority, headers, method, path, and scheme.

Referenced by kcenon::network::protocols::http2::http2_server_connection::dispatch_request().

Here is the caller graph for this function:

◆ get_body_string()

auto kcenon::network::protocols::http2::http2_request::get_body_string ( ) const -> std::string
inlinenodiscard

Get request body as UTF-8 string.

Returns
Body content as string

Definition at line 96 of file http2_request.h.

97 {
98 return std::string(body.begin(), body.end());
99 }
std::vector< uint8_t > body
Request body from DATA frames.

◆ get_header()

auto kcenon::network::protocols::http2::http2_request::get_header ( std::string_view name) const -> std::optional<std::string>
inlinenodiscard

Get header value by name (case-insensitive)

Parameters
nameHeader name to search for
Returns
Header value if found, empty optional otherwise

Definition at line 46 of file http2_request.h.

48 {
49 auto it = std::find_if(headers.begin(), headers.end(),
50 [&name](const http_header& h) {
51 if (h.name.size() != name.size()) return false;
52 for (size_t i = 0; i < h.name.size(); ++i) {
53 if (std::tolower(static_cast<unsigned char>(h.name[i])) !=
54 std::tolower(static_cast<unsigned char>(name[i]))) {
55 return false;
56 }
57 }
58 return true;
59 });
60 if (it != headers.end()) {
61 return it->value;
62 }
63 return std::nullopt;
64 }
std::vector< http_header > headers
Regular headers (non-pseudo)

References headers.

◆ is_valid()

auto kcenon::network::protocols::http2::http2_request::is_valid ( ) const -> bool
inlinenodiscard

Check if this is a valid HTTP/2 request.

Returns
True if all required pseudo-headers are present

Per RFC 7540 Section 8.1.2.3, requests must include:

  • :method (except for CONNECT)
  • :scheme (except for CONNECT)
  • :path (except for CONNECT with authority-form)

Definition at line 110 of file http2_request.h.

111 {
112 if (method.empty()) {
113 return false;
114 }
115 // CONNECT requests have special rules
116 if (method == "CONNECT") {
117 return !authority.empty();
118 }
119 // Normal requests require :method, :scheme, and :path
120 return !scheme.empty() && !path.empty();
121 }
std::string path
Request path (:path pseudo-header)
std::string authority
Authority (:authority pseudo-header)
std::string scheme
Scheme (:scheme pseudo-header)
std::string method
HTTP method (:method pseudo-header)

Member Data Documentation

◆ authority

std::string kcenon::network::protocols::http2::http2_request::authority

Authority (:authority pseudo-header)

Definition at line 36 of file http2_request.h.

Referenced by from_headers().

◆ body

std::vector<uint8_t> kcenon::network::protocols::http2::http2_request::body

Request body from DATA frames.

Definition at line 39 of file http2_request.h.

◆ headers

std::vector<http_header> kcenon::network::protocols::http2::http2_request::headers

Regular headers (non-pseudo)

Definition at line 38 of file http2_request.h.

Referenced by from_headers(), get_header(), and kcenon::network::protocols::http2::http2_server_stream::headers().

◆ method

std::string kcenon::network::protocols::http2::http2_request::method

HTTP method (:method pseudo-header)

Definition at line 34 of file http2_request.h.

Referenced by from_headers(), and kcenon::network::protocols::http2::http2_server_stream::method().

◆ path

std::string kcenon::network::protocols::http2::http2_request::path

Request path (:path pseudo-header)

Definition at line 35 of file http2_request.h.

Referenced by from_headers(), and kcenon::network::protocols::http2::http2_server_stream::path().

◆ scheme

std::string kcenon::network::protocols::http2::http2_request::scheme

Scheme (:scheme pseudo-header)

Definition at line 37 of file http2_request.h.

Referenced by from_headers().


The documentation for this struct was generated from the following file: