userver: server::request::RequestContext Class Reference
Loading...
Searching...
No Matches
server::request::RequestContext Class Referencefinal

#include <userver/server/request/request_context.hpp>

Detailed Description

Stores request-specific data during request processing.

For example: you can store some data in HandleRequestThrow() method and access this data in GetResponseDataForLogging() method.

Use typed tags to store and retrieve per-request custom data.

The context can be used to pass data between middleware, authentication checkers, handlers, and logging hooks.

Example usage:

Define a shared tag in a header:

inline const utils::AnyStorageDataTag<server::request::StorageContext, std::string> kUsername;

Store the value in an authentication checker:

request_context.EmplaceData(kUsername, info.name);

Read it in a handler:

return "Hello world, " + ctx.GetData(kUsername) + "!\n";
Examples
postgresql/functional_tests/basic_chaos/postgres_service.cpp, samples/config_service/main.cpp, samples/flatbuf_service/main.cpp, samples/hello_service/src/hello_handler.cpp, samples/hello_service/src/hello_handler.hpp, samples/kafka_service/src/producer_handler.cpp, samples/kafka_service/src/producer_handler.hpp, samples/mongo_service/main.cpp, samples/multipart_service/main.cpp, samples/postgres_auth/auth_bearer.cpp, samples/postgres_auth/postgres_service.cpp, samples/postgres_service/main.cpp, samples/redis_service/main.cpp, samples/scylla_service/src/main.cpp, samples/testsuite-support/src/logcapture.cpp, samples/testsuite-support/src/metrics.cpp, samples/testsuite-support/src/metrics.hpp, samples/testsuite-support/src/now.cpp, samples/testsuite-support/src/testpoint.cpp, samples/websocket_client/main.cpp, samples/websocket_service/main.cpp, samples/ydb_service/views/bson-reading/post/view.cpp, samples/ydb_service/views/bson-upserting/post/view.cpp, samples/ydb_service/views/select-rows/post/view.cpp, samples/ydb_service/views/upsert-2rows/post/view.cpp, samples/ydb_service/views/upsert-row/post/view.cpp, samples/ydb_service/views/upsert-rows/post/view.cpp, samples/ydb_topic_writer_service/src/write_handler.cpp, and samples/ydb_topic_writer_service/src/write_handler.hpp.

Definition at line 45 of file request_context.hpp.

Public Member Functions

 RequestContext (RequestContext &&) noexcept
 RequestContext (const RequestContext &)=delete
const dynamic_config::Snapshot & GetInitialDynamicConfig () const
 Returns the dynamic config snapshot captured at the start of HTTP request processing.
template<typename Data>
Data & SetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag, Data data)
 Stores data under the tag if no value was previously stored.
template<typename Data, typename... Args>
Data & EmplaceData (const utils::AnyStorageDataTag< StorageContext, Data > &tag, Args &&... args)
 Emplaces data under the tag if no value was previously stored.
template<typename Data>
Data & GetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag)
template<typename Data>
const Data & GetData (const utils::AnyStorageDataTag< StorageContext, Data > &tag) const
template<typename Data>
Data * GetDataOptional (const utils::AnyStorageDataTag< StorageContext, Data > &tag) noexcept
template<typename Data>
const Data * GetDataOptional (const utils::AnyStorageDataTag< StorageContext, Data > &tag) const noexcept
template<typename Data>
void EraseData (const utils::AnyStorageDataTag< StorageContext, Data > &tag) noexcept
 Erases data for the tag. Erasing an empty slot is harmless.
template<typename Data>
Data & SetUserData (Data data)
 Stores user data if it was not previously stored in this.
template<typename Data, typename... Args>
Data & EmplaceUserData (Args &&... args)
 Emplaces user data if it was not previously stored in this.
template<typename Data>
Data & GetUserData ()
template<typename Data>
const Data & GetUserData () const
template<typename Data>
std::remove_reference_t< Data > * GetUserDataOptional ()
template<typename Data>
const std::remove_reference_t< Data > * GetUserDataOptional () const
void EraseUserData () noexcept
 Erases the user data.
template<typename Data>
Data & SetData (std::string name, Data data)
 Stores the data with specified name if it was not previously stored in this.
template<typename Data, typename... Args>
Data & EmplaceData (std::string name, Args &&... args)
 Emplaces the data with specified name if it was not previously stored in this.
template<typename Data>
Data & GetData (std::string_view name)
template<typename Data>
const Data & GetData (std::string_view name) const
template<typename Data>
std::remove_reference_t< Data > * GetDataOptional (std::string_view name)
template<typename Data>
const std::remove_reference_t< Data > * GetDataOptional (std::string_view name) const
void EraseData (std::string_view name) noexcept
 Erase data with specified name.
void SetHandlerMetricsShard (std::string_view path, utils::statistics::LabelsSpan labels)
 Set the metrics shard (path + labels) for this request. When set, handler metrics will be accumulated on a new subpath "http.handler.path.*" and the provided labels would be set to each metric.
impl::InternalRequestContext & GetInternalContext () noexcept

Member Function Documentation

◆ EmplaceData() [1/2]

template<typename Data, typename... Args>
Data & server::request::RequestContext::EmplaceData ( const utils::AnyStorageDataTag< StorageContext, Data > & tag,
Args &&... args )

Emplaces data under the tag if no value was previously stored.

Returns
Reference to the stored data.
Exceptions
std::runtime_errorif data for the tag is already stored.
Examples
samples/postgres_auth/auth_bearer.cpp.

Definition at line 221 of file request_context.hpp.

◆ EmplaceData() [2/2]

template<typename Data, typename... Args>
Data & server::request::RequestContext::EmplaceData ( std::string name,
Args &&... args )

Emplaces the data with specified name if it was not previously stored in this.

Exceptions
std::runtime_errorif data with such name was already stored.

Definition at line 324 of file request_context.hpp.

◆ EmplaceUserData()

template<typename Data, typename... Args>
Data & server::request::RequestContext::EmplaceUserData ( Args &&... args)

Emplaces user data if it was not previously stored in this.

Exceptions
std::runtime_errorif user data was already stored.

Definition at line 276 of file request_context.hpp.

◆ EraseData() [1/2]

template<typename Data>
void server::request::RequestContext::EraseData ( const utils::AnyStorageDataTag< StorageContext, Data > & tag)
noexcept

Erases data for the tag. Erasing an empty slot is harmless.

Definition at line 254 of file request_context.hpp.

◆ EraseData() [2/2]

void server::request::RequestContext::EraseData ( std::string_view name)
inlinenoexcept

Erase data with specified name.

Definition at line 353 of file request_context.hpp.

◆ EraseUserData()

void server::request::RequestContext::EraseUserData ( )
inlinenoexcept

Erases the user data.

Definition at line 305 of file request_context.hpp.

◆ GetData() [1/4]

template<typename Data>
Data & server::request::RequestContext::GetData ( const utils::AnyStorageDataTag< StorageContext, Data > & tag)
Returns
Stored data for the tag.
Exceptions
std::runtime_errorif no data was stored.
Examples
samples/postgres_auth/postgres_service.cpp.

Definition at line 231 of file request_context.hpp.

◆ GetData() [2/4]

template<typename Data>
const Data & server::request::RequestContext::GetData ( const utils::AnyStorageDataTag< StorageContext, Data > & tag) const
Returns
Read-only access to stored data for the tag.
Exceptions
std::runtime_errorif no data was stored.

Definition at line 236 of file request_context.hpp.

◆ GetData() [3/4]

template<typename Data>
Data & server::request::RequestContext::GetData ( std::string_view name)
Returns
Stored data with specified name.
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 331 of file request_context.hpp.

◆ GetData() [4/4]

template<typename Data>
const Data & server::request::RequestContext::GetData ( std::string_view name) const
Returns
Stored data with specified name.
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 336 of file request_context.hpp.

◆ GetDataOptional() [1/4]

template<typename Data>
const Data * server::request::RequestContext::GetDataOptional ( const utils::AnyStorageDataTag< StorageContext, Data > & tag) const
noexcept
Returns
Read-only pointer to stored data for the tag or nullptr if absent.

Definition at line 248 of file request_context.hpp.

◆ GetDataOptional() [2/4]

template<typename Data>
Data * server::request::RequestContext::GetDataOptional ( const utils::AnyStorageDataTag< StorageContext, Data > & tag)
noexcept
Returns
Pointer to stored data for the tag or nullptr if absent.

Definition at line 242 of file request_context.hpp.

◆ GetDataOptional() [3/4]

template<typename Data>
std::remove_reference_t< Data > * server::request::RequestContext::GetDataOptional ( std::string_view name)
Returns
Stored data with specified name or nullptr if no data found.
Exceptions
std::bad_any_castif data of different type was stored.

Definition at line 342 of file request_context.hpp.

◆ GetDataOptional() [4/4]

template<typename Data>
const std::remove_reference_t< Data > * server::request::RequestContext::GetDataOptional ( std::string_view name) const
Returns
Stored data with specified name or nullptr if no data found.
Exceptions
std::bad_any_castif data of different type was stored.

Definition at line 348 of file request_context.hpp.

◆ GetInitialDynamicConfig()

const dynamic_config::Snapshot & server::request::RequestContext::GetInitialDynamicConfig ( ) const

Returns the dynamic config snapshot captured at the start of HTTP request processing.

The snapshot is available in the initial section of HTTP middlewares (before the Next() call), and in request logging hooks. It is reset before calling a streaming handler. For non-streaming HTTP handlers, the snapshot is available for the entire duration of request handling, including response logging hooks. For WebSocket handlers, it is available during the HTTP handshake and reset before calling Handle().

Exceptions
std::logic_errorif the snapshot has not been initialized or has already been reset.

◆ GetUserData() [1/2]

template<typename Data>
Data & server::request::RequestContext::GetUserData ( )
Returns
Stored user data
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 283 of file request_context.hpp.

◆ GetUserData() [2/2]

template<typename Data>
const Data & server::request::RequestContext::GetUserData ( ) const
Returns
Stored user data
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 288 of file request_context.hpp.

◆ GetUserDataOptional() [1/2]

template<typename Data>
std::remove_reference_t< Data > * server::request::RequestContext::GetUserDataOptional ( )
Returns
A pointer to data of type Data if it was stored before during current request processing or nullptr otherwise

Definition at line 294 of file request_context.hpp.

◆ GetUserDataOptional() [2/2]

template<typename Data>
const std::remove_reference_t< Data > * server::request::RequestContext::GetUserDataOptional ( ) const
Returns
A pointer to data of type Data if it was stored before during current request processing or nullptr otherwise

Definition at line 300 of file request_context.hpp.

◆ SetData() [1/2]

template<typename Data>
Data & server::request::RequestContext::SetData ( const utils::AnyStorageDataTag< StorageContext, Data > & tag,
Data data )

Stores data under the tag if no value was previously stored.

Returns
Reference to the stored data.
Exceptions
std::runtime_errorif data for the tag is already stored.

Definition at line 215 of file request_context.hpp.

◆ SetData() [2/2]

template<typename Data>
Data & server::request::RequestContext::SetData ( std::string name,
Data data )

Stores the data with specified name if it was not previously stored in this.

Exceptions
std::runtime_errorif data with such name was already stored.

Definition at line 308 of file request_context.hpp.

◆ SetHandlerMetricsShard()

void server::request::RequestContext::SetHandlerMetricsShard ( std::string_view path,
utils::statistics::LabelsSpan labels )

Set the metrics shard (path + labels) for this request. When set, handler metrics will be accumulated on a new subpath "http.handler.path.*" and the provided labels would be set to each metric.

Note
If something (e.g. middleware) interrupts the request before this method is called then the sharded metrics won't be written

◆ SetUserData()

template<typename Data>
Data & server::request::RequestContext::SetUserData ( Data data)

Stores user data if it was not previously stored in this.

Exceptions
std::runtime_errorif user data was already stored.

Definition at line 260 of file request_context.hpp.


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