Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
http_types.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
7#include <string>
8#include <map>
9#include <vector>
10#include <cstdint>
11#include <optional>
12
14
16{
35
40 enum class http_version
41 {
45 };
46
62 struct cookie
63 {
64 std::string name;
65 std::string value;
66 std::string path;
67 std::string domain;
68 std::string expires;
69 int max_age = -1; // -1 = session cookie
70 bool secure = false;
71 bool http_only = false;
72 std::string same_site; // "Strict", "Lax", "None"
73
78 auto to_header_value() const -> std::string;
79 };
80
92 {
93 std::string field_name;
94 std::string filename;
95 std::string content_type;
96 std::vector<uint8_t> content;
97 };
98
112 {
113 http_method method = http_method::HTTP_GET;
114 std::string uri;
115 http_version version = http_version::HTTP_1_1;
116 std::map<std::string, std::string> headers;
117 std::vector<uint8_t> body;
118 std::map<std::string, std::string> query_params;
119 std::map<std::string, std::string> cookies; // Parsed cookies from Cookie header
120 std::map<std::string, std::string> form_data; // Form fields from multipart/form-data
121 std::map<std::string, multipart_file> files; // Uploaded files from multipart/form-data
122
128 auto get_header(const std::string& name) const -> std::optional<std::string>;
129
135 auto set_header(const std::string& name, const std::string& value) -> void;
136
141 auto get_body_string() const -> std::string;
142
147 auto set_body_string(const std::string& content) -> void;
148 };
149
162 {
163 int status_code = 200;
164 std::string status_message = "OK";
165 http_version version = http_version::HTTP_1_1;
166 std::map<std::string, std::string> headers;
167 std::vector<uint8_t> body;
168 std::vector<cookie> set_cookies; // Cookies to set in Set-Cookie headers
169 bool use_chunked_encoding = false; // Use Transfer-Encoding: chunked
170
176 auto get_header(const std::string& name) const -> std::optional<std::string>;
177
183 auto set_header(const std::string& name, const std::string& value) -> void;
184
189 auto get_body_string() const -> std::string;
190
195 auto set_body_string(const std::string& content) -> void;
196
207 auto set_cookie(const std::string& name, const std::string& value,
208 const std::string& path = "/", int max_age = -1,
209 bool http_only = true, bool secure = false,
210 const std::string& same_site = "") -> void;
211 };
212
218 auto http_method_to_string(http_method method) -> std::string;
219
228 auto string_to_http_method(const std::string& method_str) -> ::kcenon::network::internal::Result<http_method>;
229
235 auto http_version_to_string(http_version version) -> std::string;
236
245 auto string_to_http_version(const std::string& version_str) -> ::kcenon::network::internal::Result<http_version>;
246
252 auto get_status_message(int status_code) -> std::string;
253
254} // namespace kcenon::network::internal
http_version
HTTP protocol version.
Definition http_types.h:41
http_method
HTTP request methods (verbs)
Definition http_types.h:24
constexpr const char * version() noexcept
Get the network system version string.
Definition network.cppm:111
Network-specific error and result type definitions.
Represents an HTTP cookie.
Definition http_types.h:63
auto to_header_value() const -> std::string
Convert cookie to Set-Cookie header value.
Represents an HTTP request message.
Definition http_types.h:112
std::map< std::string, multipart_file > files
Definition http_types.h:121
std::map< std::string, std::string > headers
Definition http_types.h:116
std::map< std::string, std::string > cookies
Definition http_types.h:119
std::map< std::string, std::string > query_params
Definition http_types.h:118
std::map< std::string, std::string > form_data
Definition http_types.h:120
Represents an HTTP response message.
Definition http_types.h:162
std::map< std::string, std::string > headers
Definition http_types.h:166
Represents a file uploaded via multipart/form-data.
Definition http_types.h:92