Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::http::RequestContext Class Reference

Type-indexed, per-request storage: middleware that computes something for later middleware or the handler (the authenticated user, a tenant, a DB transaction, request timing) stashes it here instead of a header, a global, or a new fixed field on HttpRequest. More...

#include <RequestContext.hpp>

Classes

struct  Slot
 

Public Member Functions

template<typename T >
T & set (T value)
 Sets (replacing any previous value) and returns a reference to it.
 
template<typename T >
T * get ()
 The value for T, or null if none is set.
 
template<typename T >
const T * get () const
 
template<typename T >
bool has () const
 True if a value for T is set.
 
template<typename T , typename... Args>
T & ensure (Args &&... args)
 
template<typename T >
bool erase ()
 
void set_attr (std::string name, std::string value)
 Sets a named string attribute (distinct storage from set<T>()).
 
const std::string * attr (std::string_view name) const
 A named attribute, or null if it was never set.
 

Private Member Functions

Slot * find (std::type_index key)
 
const Slot * find (std::type_index key) const
 
Slot & slot_for (std::type_index key)
 

Private Attributes

std::array< Slot, kInlineCapacity > inline_ {}
 
std::optional< std::deque< Slot > > overflow_
 
std::unordered_map< std::string, std::string > attrs_
 

Static Private Attributes

static constexpr size_t kInlineCapacity = 4
 

Detailed Description

Type-indexed, per-request storage: middleware that computes something for later middleware or the handler (the authenticated user, a tenant, a DB transaction, request timing) stashes it here instead of a header, a global, or a new fixed field on HttpRequest.

At most one value per type T. HttpRequest::context holds one of these, and HttpRequest has matching set/get/ensure templates that forward to it, so a middleware writes req.set(AuthInfo{...}) and a handler reads req.get<AuthInfo>().

struct AuthInfo { std::string user_id; };
app.use([](HttpRequest& req, std::shared_ptr<ResponseWriter> w) {
// ... verify a token ...
req.set(AuthInfo{user_id});
return true;
});
app.get("/me", [](HttpRequest& req, std::shared_ptr<ResponseWriter> w) {
if (auto* auth = req.get<AuthInfo>()) {
w->send(HttpResponse().send(auth->user_id));
}
});
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
T & set(T value)
context.set<T>(value): see RequestContext::set.
Definition HttpRequest.hpp:83

The first few distinct types used (4, kept inline) cost no heap allocation beyond whatever std::any itself needs to hold the value (none for a value that fits its small-object buffer: a pointer, a shared_ptr, a small struct). A fifth distinct type moves into a heap-allocated overflow list, grown one slot at a time (std::deque, not std::vector: a pointer or reference once returned by get/ensure stays valid for the rest of the request, including across later set, erase or ensure calls for other types — a growing vector would invalidate it on reallocation). A slot a type was erased from is reused by the next type that needs one, rather than left to grow the storage further.

T must be copy-constructible (a std::any requirement), as most context values are: an id, a small struct, a shared_ptr to something with its own lifetime (a DB transaction, say). A move-only type does not fit here; wrap it in a shared_ptr instead. T = std::any itself is rejected at compile time: assigning it into the slot's own std::any would merge the two rather than nest one inside the other.

Not thread-safe: as with the rest of HttpRequest, only the thread currently handling the request should read or write it.

Member Function Documentation

◆ attr()

const std::string * orbit::http::RequestContext::attr ( std::string_view  name) const
inline

A named attribute, or null if it was never set.

◆ ensure()

template<typename T , typename... Args>
T & orbit::http::RequestContext::ensure ( Args &&...  args)
inline

The value for T, constructing and storing one from args first if none is set yet.

◆ erase()

template<typename T >
bool orbit::http::RequestContext::erase ( )
inline

Removes the value for T, if any, in place (every other stored value keeps its address). Returns whether one was removed.

◆ find() [1/2]

Slot * orbit::http::RequestContext::find ( std::type_index  key)
inlineprivate

◆ find() [2/2]

const Slot * orbit::http::RequestContext::find ( std::type_index  key) const
inlineprivate

◆ get() [1/2]

template<typename T >
T * orbit::http::RequestContext::get ( )
inline

The value for T, or null if none is set.

◆ get() [2/2]

template<typename T >
const T * orbit::http::RequestContext::get ( ) const
inline

◆ has()

template<typename T >
bool orbit::http::RequestContext::has ( ) const
inline

True if a value for T is set.

◆ set()

template<typename T >
T & orbit::http::RequestContext::set ( T  value)
inline

Sets (replacing any previous value) and returns a reference to it.

◆ set_attr()

void orbit::http::RequestContext::set_attr ( std::string  name,
std::string  value 
)
inline

Sets a named string attribute (distinct storage from set<T>()).

◆ slot_for()

Slot & orbit::http::RequestContext::slot_for ( std::type_index  key)
inlineprivate

Member Data Documentation

◆ attrs_

std::unordered_map<std::string, std::string> orbit::http::RequestContext::attrs_
private

◆ inline_

std::array<Slot, kInlineCapacity> orbit::http::RequestContext::inline_ {}
private

◆ kInlineCapacity

constexpr size_t orbit::http::RequestContext::kInlineCapacity = 4
staticconstexprprivate

◆ overflow_

std::optional<std::deque<Slot> > orbit::http::RequestContext::overflow_
private

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