MantisBase v0.3.4
Loading...
Searching...
No Matches
mb::MantisRequest Class Reference

A wrapper class around httplib::Request offering a consistent API and allowing for easy wrapper methods compatible with Duktape API requirements for scripting. More...

#include <http.h>

Public Member Functions

 MantisRequest (const httplib::Request &_req)
 Wrapper class around the httplib Request object and our context library.
 
std::string getMethod () const
 Get HTTP request method (GET, POST, etc.).
 
std::string getPath () const
 Get request path.
 
std::string getBody () const
 Get raw request body.
 
std::string getRemoteAddr () const
 Get client remote address.
 
int getRemotePort () const
 Get client remote port.
 
std::string getLocalAddr () const
 Get server local address.
 
int getLocalPort () const
 Get server local port.
 
bool hasHeader (const std::string &key) const
 Check if header exists.
 
std::string getHeaderValue (const std::string &key, const char *def, size_t id) const
 Get header value with default and index.
 
size_t getHeaderValueU64 (const std::string &key, size_t def, size_t id) const
 Get header value as unsigned 64-bit integer.
 
size_t getHeaderValueCount (const std::string &key) const
 Get count of header values.
 
bool hasTrailer (const std::string &key) const
 Check if trailer exists.
 
std::string getTrailerValue (const std::string &key, size_t id) const
 Get trailer value.
 
size_t getTrailerValueCount (const std::string &key) const
 Get count of trailer values.
 
httplib::Match matches () const
 Get route match information.
 
bool hasQueryParam (const std::string &key) const
 Check if query parameter exists.
 
std::string getQueryParamValue (const std::string &key) const
 Get query parameter value (first occurrence).
 
std::string getQueryParamValue (const std::string &key, size_t id) const
 Get query parameter value by index.
 
size_t getQueryParamValueCount (const std::string &key) const
 Get count of query parameter values.
 
bool hasPathParams () const
 Check if request has path parameters.
 
bool hasPathParam (const std::string &key) const
 Check if path parameter exists.
 
std::string getPathParamValue (const std::string &key) const
 Get path parameter value.
 
size_t getPathParamValueCount (const std::string &key) const
 Get count of path parameter values.
 
bool isMultipartFormData () const
 Check if request is multipart/form-data.
 
bool hasKey (const std::string &key) const
 Check if context store has key.
 
std::string getBearerTokenAuth () const
 Extract Bearer token from Authorization header.
 
std::pair< nlohmann::json, std::string > getBodyAsJson () const
 Parse request body as JSON.
 
template<typename T >
void set (const std::string &key, T value)
 Store value in request context (for middleware communication).
 
template<typename T >
TgetOr (const std::string &key, T default_value)
 Get value from context store or return default.
 

Static Public Member Functions

static void registerDuktapeMethods ()
 Register MantisRequest methods for JavaScript/Duktape.
 

Detailed Description

A wrapper class around httplib::Request offering a consistent API and allowing for easy wrapper methods compatible with Duktape API requirements for scripting.

Additionally, MantisRequest adds a context object for storing some key-value data for sharing across middlewares and request handlers.

Constructor & Destructor Documentation

◆ MantisRequest()

mb::MantisRequest::MantisRequest ( const httplib::Request &  _req)
explicit

Wrapper class around the httplib Request object and our context library.

Parameters
_reqhttplib::Request& object

Construct request wrapper.

Parameters
_reqReference to httplib::Request object

Member Function Documentation

◆ getBearerTokenAuth()

std::string mb::MantisRequest::getBearerTokenAuth ( ) const

Extract Bearer token from Authorization header.

Returns
Token string (without "Bearer " prefix)

◆ getBody()

std::string mb::MantisRequest::getBody ( ) const

Get raw request body.

Returns
Body string

◆ getBodyAsJson()

std::pair< nlohmann::json, std::string > mb::MantisRequest::getBodyAsJson ( ) const

Parse request body as JSON.

Returns
Pair of (JSON object, error message)
  • If parsing succeeds: (json object, "")
  • If parsing fails: (empty json, error message)

◆ getHeaderValue()

std::string mb::MantisRequest::getHeaderValue ( const std::string &  key,
const char def,
size_t  id 
) const

Get header value with default and index.

Parameters
keyHeader name
defDefault value if not found
idIndex if multiple values (0-based)
Returns
Header value or default

◆ getHeaderValueCount()

size_t mb::MantisRequest::getHeaderValueCount ( const std::string &  key) const

Get count of header values.

Parameters
keyHeader name
Returns
Number of values

◆ getHeaderValueU64()

size_t mb::MantisRequest::getHeaderValueU64 ( const std::string &  key,
size_t  def,
size_t  id 
) const

Get header value as unsigned 64-bit integer.

Parameters
keyHeader name
defDefault value
idIndex if multiple values
Returns
Header value as size_t

◆ getLocalAddr()

std::string mb::MantisRequest::getLocalAddr ( ) const

Get server local address.

Returns
IP address string

◆ getLocalPort()

int mb::MantisRequest::getLocalPort ( ) const

Get server local port.

Returns
Port number

◆ getMethod()

std::string mb::MantisRequest::getMethod ( ) const

Get HTTP request method (GET, POST, etc.).

Returns
Method string

◆ getOr()

template<typename T >
T & mb::MantisRequest::getOr ( const std::string &  key,
T  default_value 
)
inline

Get value from context store or return default.

Template Parameters
TValue type
Parameters
keyContext key
default_valueDefault value if key not found
Returns
Reference to value (or default if not found)
int count = req.getOr<int>("count", 0);
std::string name = req.getOr<std::string>("name", "Unknown");
void set(const std::string &key, T value)
Store value in request context (for middleware communication).
Definition http.h:267

◆ getPath()

std::string mb::MantisRequest::getPath ( ) const

Get request path.

Returns
Path string

◆ getPathParamValue()

std::string mb::MantisRequest::getPathParamValue ( const std::string &  key) const

Get path parameter value.

Parameters
keyParameter name
Returns
Parameter value
// Route: GET /api/v1/users/:id
// Request: GET /api/v1/users/123
std::string id = req.getPathParamValue("id"); // "123"

◆ getPathParamValueCount()

size_t mb::MantisRequest::getPathParamValueCount ( const std::string &  key) const

Get count of path parameter values.

Parameters
keyParameter name
Returns
Number of values

◆ getQueryParamValue() [1/2]

std::string mb::MantisRequest::getQueryParamValue ( const std::string &  key) const

Get query parameter value (first occurrence).

Parameters
keyParameter name
Returns
Parameter value or empty string

◆ getQueryParamValue() [2/2]

std::string mb::MantisRequest::getQueryParamValue ( const std::string &  key,
size_t  id 
) const

Get query parameter value by index.

Parameters
keyParameter name
idIndex if multiple values (0-based)
Returns
Parameter value

◆ getQueryParamValueCount()

size_t mb::MantisRequest::getQueryParamValueCount ( const std::string &  key) const

Get count of query parameter values.

Parameters
keyParameter name
Returns
Number of values

◆ getRemoteAddr()

std::string mb::MantisRequest::getRemoteAddr ( ) const

Get client remote address.

Returns
IP address string

◆ getRemotePort()

int mb::MantisRequest::getRemotePort ( ) const

Get client remote port.

Returns
Port number

◆ getTrailerValue()

std::string mb::MantisRequest::getTrailerValue ( const std::string &  key,
size_t  id 
) const

Get trailer value.

Parameters
keyTrailer name
idIndex if multiple values
Returns
Trailer value

◆ getTrailerValueCount()

size_t mb::MantisRequest::getTrailerValueCount ( const std::string &  key) const

Get count of trailer values.

Parameters
keyTrailer name
Returns
Number of values

◆ hasHeader()

bool mb::MantisRequest::hasHeader ( const std::string &  key) const

Check if header exists.

Parameters
keyHeader name
Returns
true if header exists

◆ hasKey()

bool mb::MantisRequest::hasKey ( const std::string &  key) const

Check if context store has key.

Parameters
keyKey to check
Returns
true if key exists in context

◆ hasPathParam()

bool mb::MantisRequest::hasPathParam ( const std::string &  key) const

Check if path parameter exists.

Parameters
keyParameter name (from route pattern like /:id)
Returns
true if parameter exists

◆ hasPathParams()

bool mb::MantisRequest::hasPathParams ( ) const

Check if request has path parameters.

Returns
true if path parameters exist

◆ hasQueryParam()

bool mb::MantisRequest::hasQueryParam ( const std::string &  key) const

Check if query parameter exists.

Parameters
keyParameter name
Returns
true if parameter exists

◆ hasTrailer()

bool mb::MantisRequest::hasTrailer ( const std::string &  key) const

Check if trailer exists.

Parameters
keyTrailer name
Returns
true if trailer exists

◆ isMultipartFormData()

bool mb::MantisRequest::isMultipartFormData ( ) const

Check if request is multipart/form-data.

Returns
true if multipart form data

◆ matches()

httplib::Match mb::MantisRequest::matches ( ) const

Get route match information.

Returns
httplib::Match object with path parameters

◆ registerDuktapeMethods()

static void mb::MantisRequest::registerDuktapeMethods ( )
static

Register MantisRequest methods for JavaScript/Duktape.

◆ set()

template<typename T >
void mb::MantisRequest::set ( const std::string &  key,
value 
)
inline

Store value in request context (for middleware communication).

Template Parameters
TValue type
Parameters
keyContext key
valueValue to store
req.set<std::string>("user_id", "123");
req.set<int>("count", 42);

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