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

Represents an incoming HTTP request. More...

#include <HttpRequest.hpp>

Collaboration diagram for orbit::http::HttpRequest:

Public Member Functions

template<typename T >
T & set (T value)
 context.set<T>(value): see RequestContext::set.
 
template<typename T >
T * get ()
 context.get<T>(): see RequestContext::get.
 
template<typename T >
const T * get () const
 
template<typename T , typename... Args>
T & ensure (Args &&... args)
 context.ensure<T>(args...): see RequestContext::ensure.
 
std::shared_ptr< void > keep_alive () const
 
void set_header (std::string_view name, std::string value)
 Sets a header, copying the name and value into storage owned by this request.
 
nlohmann::json json () const
 Parses and returns the request body as a JSON object.
 
template<typename T >
std::optional< T > param (const std::string &name) const
 A path parameter (from params; not query) parsed as T.
 
std::unordered_map< std::string, std::string > form_fields () const
 Parses an application/x-www-form-urlencoded body (an HTML form submitted without files).
 
MultipartForm form () const
 Parses the request body as multipart/form-data.
 

Public Attributes

HttpMethod method {HttpMethod::UNKNOWN}
 
std::string uri
 
std::string target
 
std::unordered_map< std::string, std::string > query
 
std::string http_version
 
std::unordered_map< std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
 
std::string_view body
 
std::unordered_map< std::string, std::string > params
 
std::unordered_map< std::string, std::string > cookies
 
std::string client_ip
 Client address; may be replaced by middleware::trusted_proxies()
 
std::string peer_ip
 Address of the socket peer (the client, or the last proxy)
 
std::string session_id
 
std::shared_ptr< middleware::Session > session
 Set by the session middleware.
 
std::string request_id
 Set by middleware::request_id()
 
std::string trace_id
 W3C trace id (32 hex), set by middleware::tracing()
 
std::string span_id
 This request's span id (16 hex), set by middleware::tracing()
 
nlohmann::json user
 
RequestContext context
 Type-indexed storage for values middleware computes for later middleware or the handler (a tenant, a DB transaction, timing) with no fixed HttpRequest field or global needed. See RequestContext; set/get/ensure below forward to it.
 
nlohmann::json json_body
 
std::deque< std::string > owned_header_storage
 Storage for header names and values that did not come from the raw request buffer. A deque never moves its elements, so the string_views in headers stay valid as it grows.
 
std::weak_ptr< void > storage_owner
 Whatever owns this request's storage, when it is shared.
 

Detailed Description

Member Function Documentation

◆ ensure()

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

context.ensure<T>(args...): see RequestContext::ensure.

◆ form()

MultipartForm orbit::http::HttpRequest::form ( ) const
inline

Parses the request body as multipart/form-data.

Returns
MultipartForm object containing the parsed fields and files. Returns an empty form if the content type is not multipart/form-data.

◆ form_fields()

std::unordered_map< std::string, std::string > orbit::http::HttpRequest::form_fields ( ) const

Parses an application/x-www-form-urlencoded body (an HTML form submitted without files).

Returns
The decoded fields; empty if the Content-Type is not application/x-www-form-urlencoded or the body is malformed.

◆ get() [1/2]

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

context.get<T>(): see RequestContext::get.

◆ get() [2/2]

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

◆ json()

nlohmann::json orbit::http::HttpRequest::json ( ) const
inline

Parses and returns the request body as a JSON object.

Caches the parsed JSON object for subsequent calls.

Returns
nlohmann::json object containing the parsed body, or an empty object if the body is empty or invalid.

◆ keep_alive()

std::shared_ptr< void > orbit::http::HttpRequest::keep_alive ( ) const
inline

A reference that keeps this request's storage alive; null if storage_owner is empty or already gone.

◆ param()

template<typename T >
std::optional< T > orbit::http::HttpRequest::param ( const std::string &  name) const
inline

A path parameter (from params; not query) parsed as T.

Returns
The value, or std::nullopt if name is absent or its text does not parse as T (trailing characters included).

A route pattern that constrains the segment's type ({id:int}) already guarantees the text parses as that type's full range, so param<std::int64_t>("id") there is never null for a request the route matched — param<T> for a narrower T (plain int, say) still can be, for a value the route accepted but that overflows T. param is just as useful for an untyped :name segment, where it replaces params.at(name) plus a hand-written std::stoi (and the try/catch an invalid one needs). It does not read query; copy a value from there into params first if you want the same parsing for it.

app.get("/tasks/:id", [](HttpRequest& req, std::shared_ptr<ResponseWriter> w) {
auto id = req.param<int>("id");
if (!id) { w->send(HttpResponse().status(HttpStatus::NotFound)); return; }
// ...
});
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
std::optional< T > param(const std::string &name) const
A path parameter (from params; not query) parsed as T.
Definition HttpRequest.hpp:182

◆ set()

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

context.set<T>(value): see RequestContext::set.

◆ set_header()

void orbit::http::HttpRequest::set_header ( std::string_view  name,
std::string  value 
)
inline

Sets a header, copying the name and value into storage owned by this request.

headers holds string_views. Assigning a temporary or local string to it directly leaves a dangling view once that string is destroyed; use this instead whenever the value does not live in the request buffer.

Member Data Documentation

◆ body

std::string_view orbit::http::HttpRequest::body

◆ client_ip

std::string orbit::http::HttpRequest::client_ip

Client address; may be replaced by middleware::trusted_proxies()

◆ context

RequestContext orbit::http::HttpRequest::context

Type-indexed storage for values middleware computes for later middleware or the handler (a tenant, a DB transaction, timing) with no fixed HttpRequest field or global needed. See RequestContext; set/get/ensure below forward to it.

◆ cookies

std::unordered_map<std::string, std::string> orbit::http::HttpRequest::cookies

◆ headers

std::unordered_map<std::string_view, std::string_view, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual> orbit::http::HttpRequest::headers

◆ http_version

std::string orbit::http::HttpRequest::http_version

◆ json_body

nlohmann::json orbit::http::HttpRequest::json_body
mutable

◆ method

HttpMethod orbit::http::HttpRequest::method {HttpMethod::UNKNOWN}

◆ owned_header_storage

std::deque<std::string> orbit::http::HttpRequest::owned_header_storage

Storage for header names and values that did not come from the raw request buffer. A deque never moves its elements, so the string_views in headers stay valid as it grows.

◆ params

std::unordered_map<std::string, std::string> orbit::http::HttpRequest::params

◆ peer_ip

std::string orbit::http::HttpRequest::peer_ip

Address of the socket peer (the client, or the last proxy)

◆ query

std::unordered_map<std::string, std::string> orbit::http::HttpRequest::query

◆ request_id

std::string orbit::http::HttpRequest::request_id

◆ session

std::shared_ptr<middleware::Session> orbit::http::HttpRequest::session

Set by the session middleware.

◆ session_id

std::string orbit::http::HttpRequest::session_id

◆ span_id

std::string orbit::http::HttpRequest::span_id

This request's span id (16 hex), set by middleware::tracing()

◆ storage_owner

std::weak_ptr<void> orbit::http::HttpRequest::storage_owner

Whatever owns this request's storage, when it is shared.

Set by the HTTP/1.1 connection. A coroutine handler that takes the request as a parameter holds it (see keep_alive()), so the request, its headers and its body stay valid until the coroutine finishes, even after the response is sent and the connection has moved on to the next request. Empty when the request is owned another way (HTTP/2 and HTTP/3 streams are kept alive by their ResponseWriter).

◆ target

std::string orbit::http::HttpRequest::target

◆ trace_id

std::string orbit::http::HttpRequest::trace_id

W3C trace id (32 hex), set by middleware::tracing()

◆ uri

std::string orbit::http::HttpRequest::uri

◆ user

nlohmann::json orbit::http::HttpRequest::user

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