Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
HttpRequest.hpp
Go to the documentation of this file.
1#pragma once
3#ifdef _WIN32
4#undef DELETE
5#endif
6#include <charconv>
7#include <deque>
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <unordered_map>
14#include <orbit/http/json.hpp>
18
20class Session;
21}
22
23namespace orbit::http {
24
29
32template <typename T>
33std::optional<T> parse_param(const std::string& text) {
34 if constexpr (std::is_same_v<T, std::string>) {
35 return text;
36 } else if constexpr (std::is_same_v<T, bool>) {
37 if (text == "true" || text == "1") return true;
38 if (text == "false" || text == "0") return false;
39 return std::nullopt;
40 } else if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>) {
41 T value{};
42 const auto [ptr, ec] = std::from_chars(text.data(), text.data() + text.size(), value);
43 if (ec != std::errc() || ptr != text.data() + text.size()) return std::nullopt;
44 return value;
45 } else {
46 static_assert(!sizeof(T), "HttpRequest::param<T>() supports std::string, bool, and integral or "
47 "floating-point T");
48 }
49}
50
56 std::string uri;
57 std::string target; // Raw request-target as received, including any query string
58 std::unordered_map<std::string, std::string> query;
59 std::string http_version;
60 std::unordered_map<std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual> headers;
61 std::string_view body;
62 std::unordered_map<std::string, std::string> params;
63 std::unordered_map<std::string, std::string> cookies;
64 std::string client_ip;
65 std::string peer_ip;
66 std::string session_id; // Set by the session middleware
67 std::shared_ptr<middleware::Session> session;
68 std::string request_id;
69 std::string trace_id;
70 std::string span_id;
71 nlohmann::json user; // Populated by JwtAuth middleware
72
80
82 template <typename T>
83 T& set(T value) {
84 return context.set<T>(std::move(value));
85 }
87 template <typename T>
88 T* get() {
89 return context.get<T>();
90 }
91 template <typename T>
92 const T* get() const {
93 return context.get<T>();
94 }
96 template <typename T, typename... Args>
97 T& ensure(Args&&... args) {
98 return context.ensure<T>(std::forward<Args>(args)...);
99 }
100
101 mutable nlohmann::json json_body; // Cached parsed JSON
102
108 std::deque<std::string> owned_header_storage;
109
120 std::weak_ptr<void> storage_owner;
121
124 std::shared_ptr<void> keep_alive() const { return storage_owner.lock(); }
125
134 void set_header(std::string_view name, std::string value) {
135 const std::string& stored_name = owned_header_storage.emplace_back(name);
136 const std::string& stored_value = owned_header_storage.emplace_back(std::move(value));
137 auto it = headers.find(stored_name);
138 if (it != headers.end()) {
139 it->second = stored_value;
140 } else {
141 headers.emplace(stored_name, stored_value);
142 }
143 }
144
151 if (!json_body.empty()) return json_body;
152 if (body.empty()) return nlohmann::json::object();
153 json_body = nlohmann::json::parse(body, nullptr, false); // false = no exceptions
154 return json_body;
155 }
156
181 template <typename T>
182 std::optional<T> param(const std::string& name) const {
183 auto it = params.find(name);
184 if (it == params.end()) return std::nullopt;
185 return parse_param<T>(it->second);
186 }
187
194 std::unordered_map<std::string, std::string> form_fields() const;
195
201 auto ct = headers.find("Content-Type");
202 if (ct != headers.end() && ct->second.starts_with("multipart/form-data")) {
203 return MultipartForm::parse(ct->second, body);
204 }
205 return {};
206 }
207};
208
209} // namespace http
a class to store JSON values
Definition json.hpp:19401
bool empty() const noexcept
checks whether the container is empty.
Definition json.hpp:22208
Represents parsed multipart/form-data content.
Definition MultipartForm.hpp:23
static MultipartForm parse(std::string_view content_type_header, std::string_view body)
Parses a multipart/form-data request body.
Type-indexed, per-request storage: middleware that computes something for later middleware or the han...
Definition RequestContext.hpp:65
T & ensure(Args &&... args)
Definition RequestContext.hpp:99
T & set(T value)
Sets (replacing any previous value) and returns a reference to it.
Definition RequestContext.hpp:69
T * get()
The value for T, or null if none is set.
Definition RequestContext.hpp:80
The current request's session (req.session).
Definition SessionManager.hpp:89
Definition Task.hpp:10
HttpMethod
Represents standard HTTP methods.
Definition HttpRequest.hpp:28
std::optional< T > parse_param(const std::string &text)
Definition HttpRequest.hpp:33
Definition HttpRequest.hpp:19
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
RequestContext context
Type-indexed storage for values middleware computes for later middleware or the handler (a tenant,...
Definition HttpRequest.hpp:79
HttpMethod method
Definition HttpRequest.hpp:55
std::unordered_map< std::string, std::string > params
Definition HttpRequest.hpp:62
std::optional< T > param(const std::string &name) const
A path parameter (from params; not query) parsed as T.
Definition HttpRequest.hpp:182
std::string client_ip
Client address; may be replaced by middleware::trusted_proxies()
Definition HttpRequest.hpp:64
std::string trace_id
W3C trace id (32 hex), set by middleware::tracing()
Definition HttpRequest.hpp:69
T * get()
context.get<T>(): see RequestContext::get.
Definition HttpRequest.hpp:88
std::shared_ptr< void > keep_alive() const
Definition HttpRequest.hpp:124
T & set(T value)
context.set<T>(value): see RequestContext::set.
Definition HttpRequest.hpp:83
T & ensure(Args &&... args)
context.ensure<T>(args...): see RequestContext::ensure.
Definition HttpRequest.hpp:97
MultipartForm form() const
Parses the request body as multipart/form-data.
Definition HttpRequest.hpp:200
std::unordered_map< std::string, std::string > form_fields() const
Parses an application/x-www-form-urlencoded body (an HTML form submitted without files).
std::string request_id
Set by middleware::request_id()
Definition HttpRequest.hpp:68
std::unordered_map< std::string, std::string > cookies
Definition HttpRequest.hpp:63
std::string peer_ip
Address of the socket peer (the client, or the last proxy)
Definition HttpRequest.hpp:65
std::weak_ptr< void > storage_owner
Whatever owns this request's storage, when it is shared.
Definition HttpRequest.hpp:120
nlohmann::json user
Definition HttpRequest.hpp:71
std::string http_version
Definition HttpRequest.hpp:59
std::string span_id
This request's span id (16 hex), set by middleware::tracing()
Definition HttpRequest.hpp:70
std::string uri
Definition HttpRequest.hpp:56
std::unordered_map< std::string, std::string > query
Definition HttpRequest.hpp:58
nlohmann::json json() const
Parses and returns the request body as a JSON object.
Definition HttpRequest.hpp:150
void set_header(std::string_view name, std::string value)
Sets a header, copying the name and value into storage owned by this request.
Definition HttpRequest.hpp:134
nlohmann::json json_body
Definition HttpRequest.hpp:101
const T * get() const
Definition HttpRequest.hpp:92
std::string_view body
Definition HttpRequest.hpp:61
std::deque< std::string > owned_header_storage
Storage for header names and values that did not come from the raw request buffer....
Definition HttpRequest.hpp:108
std::unordered_map< std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
Definition HttpRequest.hpp:60
std::string session_id
Definition HttpRequest.hpp:66
std::string target
Definition HttpRequest.hpp:57
std::shared_ptr< middleware::Session > session
Set by the session middleware.
Definition HttpRequest.hpp:67