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

Wrapper around httplib::Response for consistent API. More...

#include <http.h>

Public Member Functions

 MantisResponse (httplib::Response &_resp)
 Construct response wrapper.
 
 ~MantisResponse ()=default
 Destructor.
 
httplib::Response & getResponse () const
 
int getStatus () const
 Get HTTP status code.
 
void setStatus (int s)
 Set HTTP status code.
 
std::string getVersion () const
 Get HTTP version.
 
void setVersion (const std::string &b)
 Set HTTP version.
 
std::string getBody () const
 Get response body.
 
void setBody (const std::string &b)
 Set response body.
 
std::string getLocation () const
 Get redirect location.
 
void setLocation (const std::string &b)
 Set redirect location.
 
std::string getReason () const
 Get status reason phrase.
 
void setReason (const std::string &b)
 Set status reason phrase.
 
bool hasHeader (const std::string &key) const
 Check if header exists.
 
std::string getHeaderValue (const std::string &key, const char *def="", size_t id=0) const
 Get header value.
 
size_t getHeaderValueU64 (const std::string &key, size_t def=0, size_t id=0) const
 Get header value as unsigned 64-bit integer.
 
size_t getHeaderValueCount (const std::string &key) const
 Get count of header values.
 
void setHeader (const std::string &key, const std::string &val) const
 Set response header.
 
bool hasTrailer (const std::string &key) const
 Check if trailer exists.
 
std::string getTrailerValue (const std::string &key, size_t id=0) const
 Get trailer value.
 
size_t getTrailerValueCount (const std::string &key) const
 Get count of trailer values.
 
void setRedirect (const std::string &url, int status=httplib::StatusCode::Found_302) const
 Set redirect response.
 
void setContent (const char *s, size_t n, const std::string &content_type) const
 Set response content from buffer.
 
void setContent (const std::string &s, const std::string &content_type) const
 Set response content from string.
 
void setContent (std::string &&s, const std::string &content_type) const
 Set response content from moved string.
 
void setFileContent (const std::string &path, const std::string &content_type) const
 Set response content from file with MIME type.
 
void setFileContent (const std::string &path) const
 Set response content from file (auto-detect MIME type).
 
void send (int statusCode, const std::string &data="", const std::string &content_type="text/plain") const
 Send response with status, data, and content type.
 
void sendJSON (int statusCode=200, const json &data=json::object()) const
 Send JSON response.
 
void sendText (int statusCode=200, const std::string &data="") const
 Send text response.
 
void sendHtml (int statusCode=200, const std::string &data="") const
 Send HTML response.
 
void sendEmpty (int statusCode=204) const
 Send empty response (no body).
 

Static Public Member Functions

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

Detailed Description

Wrapper around httplib::Response for consistent API.

Provides convenient methods for setting response status, headers, and body with support for JSON, text, HTML, and file responses.

// Send JSON response
res.sendJSON(200, {{"message", "Success"}});
// Send text response
res.sendText(200, "Hello World");
// Set custom header
res.setHeader("X-Custom", "value");

Constructor & Destructor Documentation

◆ MantisResponse()

mb::MantisResponse::MantisResponse ( httplib::Response &  _resp)
explicit

Construct response wrapper.

Parameters
_respReference to httplib::Response object

◆ ~MantisResponse()

mb::MantisResponse::~MantisResponse ( )
default

Destructor.

Member Function Documentation

◆ getBody()

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

Get response body.

Returns
Body string

◆ getHeaderValue()

std::string mb::MantisResponse::getHeaderValue ( const std::string &  key,
const char *  def = "",
size_t  id = 0 
) const

Get header value.

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

◆ getHeaderValueCount()

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

Get count of header values.

Parameters
keyHeader name
Returns
Number of values

◆ getHeaderValueU64()

size_t mb::MantisResponse::getHeaderValueU64 ( const std::string &  key,
size_t  def = 0,
size_t  id = 0 
) const

Get header value as unsigned 64-bit integer.

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

◆ getLocation()

std::string mb::MantisResponse::getLocation ( ) const

Get redirect location.

Returns
Location URL

◆ getReason()

std::string mb::MantisResponse::getReason ( ) const

Get status reason phrase.

Returns
Reason string

◆ getResponse()

httplib::Response & mb::MantisResponse::getResponse ( ) const

◆ getStatus()

int mb::MantisResponse::getStatus ( ) const

Get HTTP status code.

Returns
Status code (e.g., 200, 404, 500)

◆ getTrailerValue()

std::string mb::MantisResponse::getTrailerValue ( const std::string &  key,
size_t  id = 0 
) const

Get trailer value.

Parameters
keyTrailer name
idIndex if multiple values
Returns
Trailer value

◆ getTrailerValueCount()

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

Get count of trailer values.

Parameters
keyTrailer name
Returns
Number of values

◆ getVersion()

std::string mb::MantisResponse::getVersion ( ) const

Get HTTP version.

Returns
Version string

◆ hasHeader()

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

Check if header exists.

Parameters
keyHeader name
Returns
true if header exists

◆ hasTrailer()

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

Check if trailer exists.

Parameters
keyTrailer name
Returns
true if trailer exists

◆ registerDuktapeMethods()

static void mb::MantisResponse::registerDuktapeMethods ( )
static

Register MantisResponse methods for JavaScript/Duktape.

◆ send()

void mb::MantisResponse::send ( int  statusCode = 200,
const std::string &  data = "",
const std::string &  content_type = "text/plain" 
) const

Send response with status, data, and content type.

Parameters
statusCodeHTTP status code
dataResponse data
content_typeMIME type (default: "text/plain")

◆ sendEmpty()

void mb::MantisResponse::sendEmpty ( int  statusCode = 204) const

Send empty response (no body).

Parameters
statusCodeHTTP status code (default: 204 No Content)

◆ sendHtml()

void mb::MantisResponse::sendHtml ( int  statusCode = 200,
const std::string &  data = "" 
) const

Send HTML response.

Parameters
statusCodeHTTP status code (default: 200)
dataHTML content

◆ sendJSON()

void mb::MantisResponse::sendJSON ( int  statusCode = 200,
const json data = json::object() 
) const

Send JSON response.

Parameters
statusCodeHTTP status code (default: 200)
dataJSON object to send
res.sendJSON(200, {{"success", true}, {"data", result}});

◆ sendText()

void mb::MantisResponse::sendText ( int  statusCode = 200,
const std::string &  data = "" 
) const

Send text response.

Parameters
statusCodeHTTP status code (default: 200)
dataText content

◆ setBody()

void mb::MantisResponse::setBody ( const std::string &  b)

Set response body.

Parameters
bBody string

◆ setContent() [1/3]

void mb::MantisResponse::setContent ( const char *  s,
size_t  n,
const std::string &  content_type 
) const

Set response content from buffer.

Parameters
sContent buffer
nContent length
content_typeMIME type

◆ setContent() [2/3]

void mb::MantisResponse::setContent ( const std::string &  s,
const std::string &  content_type 
) const

Set response content from string.

Parameters
sContent string
content_typeMIME type

◆ setContent() [3/3]

void mb::MantisResponse::setContent ( std::string &&  s,
const std::string &  content_type 
) const

Set response content from moved string.

Parameters
sContent string (moved)
content_typeMIME type

◆ setFileContent() [1/2]

void mb::MantisResponse::setFileContent ( const std::string &  path) const

Set response content from file (auto-detect MIME type).

Parameters
pathFile path

◆ setFileContent() [2/2]

void mb::MantisResponse::setFileContent ( const std::string &  path,
const std::string &  content_type 
) const

Set response content from file with MIME type.

Parameters
pathFile path
content_typeMIME type

◆ setHeader()

void mb::MantisResponse::setHeader ( const std::string &  key,
const std::string &  val 
) const

Set response header.

Parameters
keyHeader name
valHeader value

◆ setLocation()

void mb::MantisResponse::setLocation ( const std::string &  b)

Set redirect location.

Parameters
bLocation URL

◆ setReason()

void mb::MantisResponse::setReason ( const std::string &  b)

Set status reason phrase.

Parameters
bReason string

◆ setRedirect()

void mb::MantisResponse::setRedirect ( const std::string &  url,
int  status = httplib::StatusCode::Found_302 
) const

Set redirect response.

Parameters
urlRedirect URL
statusHTTP status code (default: 302 Found)

◆ setStatus()

void mb::MantisResponse::setStatus ( int  s)

Set HTTP status code.

Parameters
sStatus code

◆ setVersion()

void mb::MantisResponse::setVersion ( const std::string &  b)

Set HTTP version.

Parameters
bVersion string

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