Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
http::HttpResponse Class Reference

Represents an HTTP response to be sent to a client. More...

#include <HttpResponse.hpp>

Public Member Functions

 HttpResponse ()=default
 
 HttpResponse (HttpResponse &&other) noexcept
 
HttpResponseoperator= (HttpResponse &&other) noexcept
 
 ~HttpResponse ()
 
 HttpResponse (const HttpResponse &)=delete
 
HttpResponseoperator= (const HttpResponse &)=delete
 
HttpResponseset_cookie (const Cookie &cookie)
 Adds a cookie to the response.
 
HttpResponseset_cookie (const std::string &name, const std::string &value)
 Adds a simple key-value cookie to the response.
 
void set_body (const std::string &b, const std::string &content_type="text/plain")
 Sets the response body and its content type.
 
HttpResponsestatus (HttpStatus code) &
 Sets the HTTP status code of the response.
 
HttpResponse && status (HttpStatus code) &&
 
HttpResponsesend (const std::string &b) &
 Sets the response body as plain text.
 
HttpResponse && send (const std::string &b) &&
 
HttpResponsejson (const std::string &j) &
 Sets the response body as JSON from a string.
 
HttpResponse && json (const std::string &j) &&
 
HttpResponsejson (const nlohmann::json &j) &
 Sets the response body as JSON from a nlohmann::json object.
 
HttpResponse && json (const nlohmann::json &j) &&
 
HttpResponsehtml (const std::string &h) &
 Sets the response body as HTML.
 
HttpResponse && html (const std::string &h) &&
 
void render (const std::string &template_path, const nlohmann::json &data)
 Renders a template and sets it as the HTML response body.
 
void send_file (const std::string &path, const std::string &content_type)
 Prepares a file to be sent using zero-copy (sendfile).
 
std::string serialize () const
 Serializes the entire response (headers and body) to a string.
 
std::string serialize_headers () const
 Serializes only the response headers to a string.
 

Public Attributes

std::vector< Cookiecookies
 
HttpStatus status_code = HttpStatus::OK
 
std::unordered_map< std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqualheaders
 
std::string body
 
int file_fd {-1}
 
off_t file_size {0}
 

Detailed Description

Represents an HTTP response to be sent to a client.

Constructor & Destructor Documentation

◆ HttpResponse() [1/3]

http::HttpResponse::HttpResponse ( )
default

◆ HttpResponse() [2/3]

http::HttpResponse::HttpResponse ( HttpResponse &&  other)
noexcept

◆ ~HttpResponse()

http::HttpResponse::~HttpResponse ( )

◆ HttpResponse() [3/3]

http::HttpResponse::HttpResponse ( const HttpResponse )
delete

Member Function Documentation

◆ html() [1/2]

HttpResponse & http::HttpResponse::html ( const std::string &  h) &
inline

Sets the response body as HTML.

Parameters
hThe HTML string content.

◆ html() [2/2]

HttpResponse && http::HttpResponse::html ( const std::string &  h) &&
inline

◆ json() [1/4]

HttpResponse & http::HttpResponse::json ( const nlohmann::json j) &
inline

Sets the response body as JSON from a nlohmann::json object.

Parameters
jThe nlohmann::json object to send.

◆ json() [2/4]

HttpResponse && http::HttpResponse::json ( const nlohmann::json j) &&
inline

◆ json() [3/4]

HttpResponse & http::HttpResponse::json ( const std::string &  j) &
inline

Sets the response body as JSON from a string.

Parameters
jThe JSON string content.

◆ json() [4/4]

HttpResponse && http::HttpResponse::json ( const std::string &  j) &&
inline

◆ operator=() [1/2]

HttpResponse & http::HttpResponse::operator= ( const HttpResponse )
delete

◆ operator=() [2/2]

HttpResponse & http::HttpResponse::operator= ( HttpResponse &&  other)
noexcept

◆ render()

void http::HttpResponse::render ( const std::string &  template_path,
const nlohmann::json data 
)

Renders a template and sets it as the HTML response body.

Parameters
template_pathThe path to the template file.
dataThe JSON data to inject into the template.

◆ send() [1/2]

HttpResponse & http::HttpResponse::send ( const std::string &  b) &
inline

Sets the response body as plain text.

Parameters
bThe text content to send.

◆ send() [2/2]

HttpResponse && http::HttpResponse::send ( const std::string &  b) &&
inline

◆ send_file()

void http::HttpResponse::send_file ( const std::string &  path,
const std::string &  content_type 
)

Prepares a file to be sent using zero-copy (sendfile).

Parameters
pathThe path to the file to send.
content_typeThe MIME type of the file.

◆ serialize()

std::string http::HttpResponse::serialize ( ) const

Serializes the entire response (headers and body) to a string.

Returns
The serialized HTTP response string.

◆ serialize_headers()

std::string http::HttpResponse::serialize_headers ( ) const

Serializes only the response headers to a string.

Returns
The serialized HTTP response headers string.

◆ set_body()

void http::HttpResponse::set_body ( const std::string &  b,
const std::string &  content_type = "text/plain" 
)

Sets the response body and its content type.

Parameters
bThe response body content.
content_typeThe MIME type of the body (default: "text/plain").

◆ set_cookie() [1/2]

HttpResponse & http::HttpResponse::set_cookie ( const Cookie cookie)
inline

Adds a cookie to the response.

Parameters
cookieThe Cookie object to add.
Returns
A reference to this HttpResponse for method chaining.

◆ set_cookie() [2/2]

HttpResponse & http::HttpResponse::set_cookie ( const std::string &  name,
const std::string &  value 
)
inline

Adds a simple key-value cookie to the response.

Parameters
nameThe name of the cookie.
valueThe value of the cookie.
Returns
A reference to this HttpResponse for method chaining.

◆ status() [1/2]

HttpResponse & http::HttpResponse::status ( HttpStatus  code) &
inline

Sets the HTTP status code of the response.

Parameters
codeThe HttpStatus code to set.
Returns
A reference to this HttpResponse for method chaining.

◆ status() [2/2]

HttpResponse && http::HttpResponse::status ( HttpStatus  code) &&
inline

Member Data Documentation

◆ body

std::string http::HttpResponse::body

◆ cookies

std::vector<Cookie> http::HttpResponse::cookies

◆ file_fd

int http::HttpResponse::file_fd {-1}

◆ file_size

off_t http::HttpResponse::file_size {0}

◆ headers

std::unordered_map<std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual> http::HttpResponse::headers

◆ status_code

HttpStatus http::HttpResponse::status_code = HttpStatus::OK

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