userver: server::http::HttpRequest Class Reference
Loading...
Searching...
No Matches
server::http::HttpRequest Class Referencefinal

#include <userver/server/http/http_request.hpp>

Detailed Description

HTTP Request data.

Note
do not create HttpRequest by hand in tests, use HttpRequestBuilder instead.
Examples
postgresql/functional_tests/basic_chaos/postgres_service.cpp, samples/config_service/main.cpp, samples/easy/4_custom_dependency/main.cpp, samples/easy/5_pg_service_template/src/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/helpers.cpp, samples/scylla_service/src/helpers.hpp, 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/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 35 of file http_request.hpp.

Public Types

using HeadersMap = ::http::headers::HeaderMap
using HeadersMapKeys = decltype(utils::impl::MakeKeysView(HeadersMap()))
using CookiesMap = std::unordered_map<std::string, std::string, utils::StrCaseHash>
using CookiesMapKeys = decltype(utils::impl::MakeKeysView(CookiesMap()))

Public Member Functions

 HttpRequest (HttpRequest &&)=delete
 HttpRequest (const HttpRequest &)=delete
const HttpMethodGetMethod () const
const std::string & GetMethodStr () const
int GetHttpMajor () const
int GetHttpMinor () const
const std::string & GetUrl () const
 Get HTTP request target as provided by the client (see https://www.rfc-editor.org/rfc/rfc7230#section-5.3). May contain the whole URL, but usually it consists of path and query string.
const std::string & GetRequestPath () const
 Get the path part of HTTP request URL.
const std::string & GetHost () const
const engine::io::SockaddrGetRemoteAddress () const
const std::string & GetArg (std::string_view arg_name) const
const std::vector< std::string > & GetArgVector (std::string_view arg_name) const
bool HasArg (std::string_view arg_name) const
size_t ArgCount () const
std::vector< std::string > ArgNames () const
template<typename Predicate>
requires std::is_invocable_r_v<bool, Predicate, std::string_view>
std::string GetMaskedUrl (Predicate is_masked_arg_name) const
 Reconstruct the request URL with selected query args masked.
const FormDataArgGetFormDataArg (std::string_view arg_name) const
const std::vector< FormDataArg > & GetFormDataArgVector (std::string_view arg_name) const
bool HasFormDataArg (std::string_view arg_name) const
size_t FormDataArgCount () const
std::vector< std::string > FormDataArgNames () const
const std::string & GetPathArg (std::string_view arg_name) const
const std::string & GetPathArg (size_t index) const
bool HasPathArg (std::string_view arg_name) const
bool HasPathArg (size_t index) const
size_t PathArgCount () const
const std::string & GetHeader (std::string_view header_name) const
const std::string & GetHeader (const ::http::headers::PredefinedHeader &header_name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
bool HasHeader (std::string_view header_name) const
bool HasHeader (const ::http::headers::PredefinedHeader &header_name) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
size_t HeaderCount () const
void RemoveHeader (std::string_view header_name)
 Removes the header with case insensitive name header_name.
void RemoveHeader (const ::http::headers::PredefinedHeader &header_name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
HeadersMapKeys GetHeaderNames () const
const HeadersMap & GetHeaders () const
const std::string & GetCookie (const std::string &cookie_name) const
bool HasCookie (const std::string &cookie_name) const
size_t CookieCount () const
CookiesMapKeys GetCookieNames () const
const CookiesMap & RequestCookies () const
const std::string & RequestBody () const
std::string ExtractRequestBody ()
void SetResponseStatus (HttpStatus status) const
 Set the response status code.
bool IsBodyCompressed () const
HttpResponseGetHttpResponse () const noexcept
std::chrono::steady_clock::time_point GetStartTime () const
 Get approximate time point of request handling start.

Member Typedef Documentation

◆ CookiesMap

using server::http::HttpRequest::CookiesMap = std::unordered_map<std::string, std::string, utils::StrCaseHash>

Definition at line 41 of file http_request.hpp.

◆ CookiesMapKeys

using server::http::HttpRequest::CookiesMapKeys = decltype(utils::impl::MakeKeysView(CookiesMap()))

Definition at line 43 of file http_request.hpp.

◆ HeadersMap

using server::http::HttpRequest::HeadersMap = ::http::headers::HeaderMap

Definition at line 37 of file http_request.hpp.

◆ HeadersMapKeys

using server::http::HttpRequest::HeadersMapKeys = decltype(utils::impl::MakeKeysView(HeadersMap()))

Definition at line 39 of file http_request.hpp.

Member Function Documentation

◆ ArgCount()

size_t server::http::HttpRequest::ArgCount ( ) const
Returns
Count of arguments. Arguments are extracted from:
  • query part of the URL,
  • the HTTP body (only if parse_args_from_body: true for handler is set).

◆ ArgNames()

std::vector< std::string > server::http::HttpRequest::ArgNames ( ) const
Returns
List of names of arguments. Arguments are extracted from:
  • query part of the URL,
  • the HTTP body (only if parse_args_from_body: true for handler is set).

◆ CookieCount()

size_t server::http::HttpRequest::CookieCount ( ) const
Returns
Number of cookies.

◆ ExtractRequestBody()

std::string server::http::HttpRequest::ExtractRequestBody ( )
Returns
moved out HTTP body. this is modified.

◆ FormDataArgCount()

size_t server::http::HttpRequest::FormDataArgCount ( ) const
Returns
Count of multipart/form-data arguments.

◆ FormDataArgNames()

std::vector< std::string > server::http::HttpRequest::FormDataArgNames ( ) const
Returns
List of names of multipart/form-data arguments.

◆ GetArg()

const std::string & server::http::HttpRequest::GetArg ( std::string_view arg_name) const

◆ GetArgVector()

const std::vector< std::string > & server::http::HttpRequest::GetArgVector ( std::string_view arg_name) const
Returns
Argument values with name arg_name or an empty vector if no such argument. Arguments are extracted from:
  • query part of the URL,
  • the HTTP body (only if parse_args_from_body: true for handler is set).

In both cases, arg keys and values are url-decoded automatically when parsing into the HttpRequest.

◆ GetCookie()

const std::string & server::http::HttpRequest::GetCookie ( const std::string & cookie_name) const
Returns
Value of the cookie with case sensitive name cookie_name, or an empty string if no such cookie exists.

◆ GetCookieNames()

CookiesMapKeys server::http::HttpRequest::GetCookieNames ( ) const
Returns
List of cookies names.

◆ GetFormDataArg()

const FormDataArg & server::http::HttpRequest::GetFormDataArg ( std::string_view arg_name) const
Returns
First argument value with name arg_name from multipart/form-data request or an empty FormDataArg if no such argument.
Examples
samples/multipart_service/main.cpp.

◆ GetFormDataArgVector()

const std::vector< FormDataArg > & server::http::HttpRequest::GetFormDataArgVector ( std::string_view arg_name) const
Returns
Argument values with name arg_name from multipart/form-data request or an empty FormDataArg if no such argument.

◆ GetHeader()

const std::string & server::http::HttpRequest::GetHeader ( std::string_view header_name) const
Returns
Value of the header with case insensitive name header_name, or an empty string if no such header.
Examples
samples/multipart_service/main.cpp, and samples/postgres_auth/auth_bearer.cpp.

◆ GetHeaderNames()

HeadersMapKeys server::http::HttpRequest::GetHeaderNames ( ) const
Returns
List of headers names.

◆ GetHeaders()

const HeadersMap & server::http::HttpRequest::GetHeaders ( ) const
Returns
HTTP headers.

◆ GetHost()

const std::string & server::http::HttpRequest::GetHost ( ) const
Returns
Host from the URL.

◆ GetHttpMajor()

int server::http::HttpRequest::GetHttpMajor ( ) const
Returns
Major version of HTTP. For example, for HTTP 1.0 it returns 1

◆ GetHttpMinor()

int server::http::HttpRequest::GetHttpMinor ( ) const
Returns
Minor version of HTTP. For example, for HTTP 1.0 it returns 0

◆ GetMaskedUrl()

template<typename Predicate>
requires std::is_invocable_r_v<bool, Predicate, std::string_view>
std::string server::http::HttpRequest::GetMaskedUrl ( Predicate is_masked_arg_name) const
inline

Reconstruct the request URL with selected query args masked.

Iterates all query arguments, replacing each value with "***" when is_masked_arg_name(name) returns true.

Definition at line 136 of file http_request.hpp.

◆ GetMethod()

const HttpMethod & server::http::HttpRequest::GetMethod ( ) const

◆ GetMethodStr()

const std::string & server::http::HttpRequest::GetMethodStr ( ) const
Returns
HTTP method as a string (e.g. "GET")

◆ GetPathArg() [1/2]

const std::string & server::http::HttpRequest::GetPathArg ( size_t index) const
Returns
Argument from URL path with wildcards by its 0-based index.
Note
Path args are currently NOT url-decoded automatically.

◆ GetPathArg() [2/2]

const std::string & server::http::HttpRequest::GetPathArg ( std::string_view arg_name) const
Returns
Named argument from URL path with wildcards.
Note
Path args are currently NOT url-decoded automatically.

◆ GetRemoteAddress()

const engine::io::Sockaddr & server::http::HttpRequest::GetRemoteAddress ( ) const
Returns
Request remote (peer's) address

◆ GetRequestPath()

const std::string & server::http::HttpRequest::GetRequestPath ( ) const

Get the path part of HTTP request URL.

Unlike server::handlers::HandlerConfig::path, path parameters are not replaced with placeholders, this is the original highly cardinal path.

◆ HasArg()

bool server::http::HttpRequest::HasArg ( std::string_view arg_name) const
Returns
true if argument with name arg_name exists, false otherwise. Arguments are extracted from:
  • query part of the URL,
  • the HTTP body (only if parse_args_from_body: true for handler is set).

In both cases, arg keys and values are url-decoded automatically when parsing into the HttpRequest.

Examples
samples/mongo_service/main.cpp, and samples/scylla_service/src/main.cpp.

◆ HasCookie()

bool server::http::HttpRequest::HasCookie ( const std::string & cookie_name) const
Returns
true if cookie with case sensitive name cookie_name exists, false otherwise.

◆ HasFormDataArg()

bool server::http::HttpRequest::HasFormDataArg ( std::string_view arg_name) const
Returns
true if argument with name arg_name exists in multipart/form-data request, false otherwise.

◆ HasHeader()

bool server::http::HttpRequest::HasHeader ( std::string_view header_name) const
Returns
true if header with case insensitive name header_name exists, false otherwise.

◆ HasPathArg() [1/2]

bool server::http::HttpRequest::HasPathArg ( size_t index) const
Returns
true if argument with index from URL path with wildcards exists, false otherwise.

◆ HasPathArg() [2/2]

bool server::http::HttpRequest::HasPathArg ( std::string_view arg_name) const
Returns
true if named argument from URL path with wildcards exists, false otherwise.

◆ HeaderCount()

size_t server::http::HttpRequest::HeaderCount ( ) const
Returns
Number of headers.

◆ IsBodyCompressed()

bool server::http::HttpRequest::IsBodyCompressed ( ) const
Returns
true if the body of the request is still compressed. In other words returns true if the static option decompress_request of a handler was set to false and this is a compressed request.

◆ PathArgCount()

size_t server::http::HttpRequest::PathArgCount ( ) const
Returns
Number of wildcard arguments in URL path.

◆ RequestBody()

const std::string & server::http::HttpRequest::RequestBody ( ) const

◆ RequestCookies()

const CookiesMap & server::http::HttpRequest::RequestCookies ( ) const
Returns
HTTP cookies.

◆ SetResponseStatus()

◆ HttpRequestBuilder

friend class HttpRequestBuilder
friend

Definition at line 310 of file http_request.hpp.

◆ HttpRequestHandler

friend class HttpRequestHandler
friend

Definition at line 311 of file http_request.hpp.


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