13#include <unordered_map>
34 if constexpr (std::is_same_v<T, std::string>) {
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;
40 }
else if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>) {
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;
46 static_assert(!
sizeof(T),
"HttpRequest::param<T>() supports std::string, bool, and integral or "
58 std::unordered_map<std::string, std::string>
query;
60 std::unordered_map<std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual>
headers;
62 std::unordered_map<std::string, std::string>
params;
63 std::unordered_map<std::string, std::string>
cookies;
67 std::shared_ptr<middleware::Session>
session;
92 const T*
get()
const {
96 template <
typename T,
typename... Args>
137 auto it =
headers.find(stored_name);
139 it->second = stored_value;
141 headers.emplace(stored_name, stored_value);
152 if (
body.empty())
return nlohmann::json::object();
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);
201 auto ct =
headers.find(
"Content-Type");
202 if (ct !=
headers.end() && ct->second.starts_with(
"multipart/form-data")) {
a class to store JSON values
Definition json.hpp:19401
bool empty() const noexcept
checks whether the container is empty.
Definition json.hpp:22208
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
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