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

Represents an HTTP response message. More...

#include <http_types.h>

Collaboration diagram for kcenon::network::internal::http_response:
Collaboration graph

Public Member Functions

auto get_header (const std::string &name) const -> std::optional< std::string >
 Get the value of a header (case-insensitive)
 
auto set_header (const std::string &name, const std::string &value) -> void
 Set a header value.
 
auto get_body_string () const -> std::string
 Get body as string.
 
auto set_body_string (const std::string &content) -> void
 Set body from string.
 
auto set_cookie (const std::string &name, const std::string &value, const std::string &path="/", int max_age=-1, bool http_only=true, bool secure=false, const std::string &same_site="") -> void
 Set a cookie in the response.
 

Public Attributes

int status_code = 200
 
std::string status_message = "OK"
 
http_version version = http_version::HTTP_1_1
 
std::map< std::string, std::string > headers
 
std::vector< uint8_t > body
 
std::vector< cookieset_cookies
 
bool use_chunked_encoding = false
 

Detailed Description

Represents an HTTP response message.

Structure

  • status_code: HTTP status code (200, 404, etc.)
  • status_message: Status message ("OK", "Not Found", etc.)
  • version: HTTP version (1.0, 1.1, 2.0)
  • headers: Map of header name to value
  • body: Response body as raw bytes

Definition at line 161 of file http_types.h.

Member Function Documentation

◆ get_body_string()

auto kcenon::network::internal::http_response::get_body_string ( ) const -> std::string

Get body as string.

Returns
Body content as UTF-8 string

Definition at line 102 of file http_types.cpp.

103 {
104 return std::string(body.begin(), body.end());
105 }

References body.

◆ get_header()

auto kcenon::network::internal::http_response::get_header ( const std::string & name) const -> std::optional<std::string>

Get the value of a header (case-insensitive)

Parameters
nameHeader name
Returns
Optional header value

Definition at line 69 of file http_types.cpp.

70 {
71 auto lower_name = to_lower(name);
72 for (const auto& [key, value] : headers)
73 {
74 if (to_lower(key) == lower_name)
75 {
76 return value;
77 }
78 }
79 return std::nullopt;
80 }
std::map< std::string, std::string > headers
Definition http_types.h:166

Referenced by kcenon::network::core::http_server::handle_request(), and kcenon::network::core::http_server::process_http_request().

Here is the caller graph for this function:

◆ set_body_string()

auto kcenon::network::internal::http_response::set_body_string ( const std::string & content) -> void

Set body from string.

Parameters
contentBody content as UTF-8 string

Definition at line 107 of file http_types.cpp.

108 {
109 body.assign(content.begin(), content.end());
110 }

Referenced by kcenon::network::internal::http_error_response::build_html_error(), and kcenon::network::internal::http_error_response::build_json_error().

Here is the caller graph for this function:

◆ set_cookie()

auto kcenon::network::internal::http_response::set_cookie ( const std::string & name,
const std::string & value,
const std::string & path = "/",
int max_age = -1,
bool http_only = true,
bool secure = false,
const std::string & same_site = "" ) -> void

Set a cookie in the response.

Parameters
nameCookie name
valueCookie value
pathCookie path (default: "/")
max_ageMaximum age in seconds (default: -1 for session cookie)
http_onlyHttpOnly flag (default: true)
secureSecure flag (default: false)
same_siteSameSite attribute (default: empty)

Definition at line 267 of file http_types.cpp.

271 {
272 cookie c;
273 c.name = name;
274 c.value = value;
275 c.path = path;
276 c.max_age = max_age;
277 c.http_only = http_only;
278 c.secure = secure;
279 c.same_site = same_site;
280 set_cookies.push_back(c);
281 }

References kcenon::network::internal::cookie::http_only, kcenon::network::internal::cookie::max_age, kcenon::network::internal::cookie::name, kcenon::network::internal::cookie::path, kcenon::network::internal::cookie::same_site, kcenon::network::internal::cookie::secure, and kcenon::network::internal::cookie::value.

◆ set_header()

auto kcenon::network::internal::http_response::set_header ( const std::string & name,
const std::string & value ) -> void

Set a header value.

Parameters
nameHeader name
valueHeader value

Definition at line 82 of file http_types.cpp.

83 {
84 // Remove existing header with same name (case-insensitive)
85 auto lower_name = to_lower(name);
86 for (auto it = headers.begin(); it != headers.end();)
87 {
88 if (to_lower(it->first) == lower_name)
89 {
90 it = headers.erase(it);
91 }
92 else
93 {
94 ++it;
95 }
96 }
97
98 // Add new header
99 headers[name] = value;
100 }

Referenced by kcenon::network::internal::http_error_response::build_html_error(), kcenon::network::internal::http_error_response::build_json_error(), kcenon::network::core::http_server::handle_request(), kcenon::network::core::http_server::process_http_request(), and kcenon::network::internal::adapters::http_server_adapter::setup_internal_routes().

Here is the caller graph for this function:

Member Data Documentation

◆ body

◆ headers

std::map<std::string, std::string> kcenon::network::internal::http_response::headers

Definition at line 166 of file http_types.h.

◆ set_cookies

std::vector<cookie> kcenon::network::internal::http_response::set_cookies

Definition at line 168 of file http_types.h.

◆ status_code

◆ status_message

◆ use_chunked_encoding

bool kcenon::network::internal::http_response::use_chunked_encoding = false

Definition at line 169 of file http_types.h.

◆ version

http_version kcenon::network::internal::http_response::version = http_version::HTTP_1_1

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