Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::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
 
HttpResponse & operator= (HttpResponse &&other) noexcept
 
 ~HttpResponse ()
 
 HttpResponse (const HttpResponse &)=delete
 
HttpResponse & operator= (const HttpResponse &)=delete
 
HttpResponse & set_cookie (const Cookie &cookie)
 Adds a cookie to the response.
 
HttpResponse & set_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.
 
HttpResponse & status (HttpStatus code) &
 Sets the HTTP status code of the response.
 
HttpResponse && status (HttpStatus code) &&
 
HttpResponse & send (const std::string &b) &
 Sets the response body as plain text.
 
HttpResponse && send (const std::string &b) &&
 
HttpResponse & json (const std::string &j) &
 Sets the response body as JSON from a string.
 
HttpResponse && json (const std::string &j) &&
 
HttpResponse & json (const nlohmann::json &j) &
 Sets the response body as JSON from a nlohmann::json object.
 
HttpResponse && json (const nlohmann::json &j) &&
 
HttpResponse & json (const nlohmann::json &j, HttpStatus code) &
 json(j) and status(code) together: res.json(obj, HttpStatus::Created).
 
HttpResponse && json (const nlohmann::json &j, HttpStatus code) &&
 
HttpResponse & html (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).
 
void set_file_range (off_t start, off_t end)
 Limits a send_file() response to bytes [start, end) of the file (clamped to its size) and updates Content-Length. The caller sets the 206 status and Content-Range.
 
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.
 

Static Public Member Functions

static HttpResponse error (HttpStatus code, const std::string &message)
 A JSON error response, {"error": message}, with code.
 

Public Attributes

std::vector< Cookie > cookies
 
HttpStatus status_code = HttpStatus::OK
 
std::unordered_map< std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
 
std::string body
 
int file_fd {-1}
 
off_t file_size {0}
 
off_t file_offset {0}
 

Detailed Description

Represents an HTTP response to be sent to a client.

Constructor & Destructor Documentation

◆ HttpResponse() [1/3]

orbit::http::HttpResponse::HttpResponse ( )
default

◆ HttpResponse() [2/3]

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

◆ ~HttpResponse()

orbit::http::HttpResponse::~HttpResponse ( )

◆ HttpResponse() [3/3]

orbit::http::HttpResponse::HttpResponse ( const HttpResponse &  )
delete

Member Function Documentation

◆ error()

static HttpResponse orbit::http::HttpResponse::error ( HttpStatus  code,
const std::string &  message 
)
inlinestatic

A JSON error response, {"error": message}, with code.

The shape every app ends up writing by hand for its own error responses (a route handler's failure, a middleware's rejection):

writer->send(HttpResponse::error(HttpStatus::NotFound, "task not found"));
static HttpResponse error(HttpStatus code, const std::string &message)
A JSON error response, {"error": message}, with code.
Definition HttpResponse.hpp:205

◆ html() [1/2]

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

Sets the response body as HTML.

Parameters
hThe HTML string content.

◆ html() [2/2]

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

◆ json() [1/6]

HttpResponse & orbit::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/6]

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

◆ json() [3/6]

HttpResponse & orbit::http::HttpResponse::json ( const nlohmann::json &  j,
HttpStatus  code 
) &
inline

json(j) and status(code) together: res.json(obj, HttpStatus::Created).

◆ json() [4/6]

HttpResponse && orbit::http::HttpResponse::json ( const nlohmann::json &  j,
HttpStatus  code 
) &&
inline

◆ json() [5/6]

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

Sets the response body as JSON from a string.

Parameters
jThe JSON string content.

◆ json() [6/6]

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ render()

void orbit::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 & orbit::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 && orbit::http::HttpResponse::send ( const std::string &  b) &&
inline

◆ send_file()

void orbit::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 orbit::http::HttpResponse::serialize ( ) const

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

Returns
The serialized HTTP response string.

◆ serialize_headers()

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

Serializes only the response headers to a string.

Returns
The serialized HTTP response headers string.

◆ set_body()

void orbit::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 & orbit::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 & orbit::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.

◆ set_file_range()

void orbit::http::HttpResponse::set_file_range ( off_t  start,
off_t  end 
)

Limits a send_file() response to bytes [start, end) of the file (clamped to its size) and updates Content-Length. The caller sets the 206 status and Content-Range.

◆ status() [1/2]

HttpResponse & orbit::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 && orbit::http::HttpResponse::status ( HttpStatus  code) &&
inline

Member Data Documentation

◆ body

std::string orbit::http::HttpResponse::body

◆ cookies

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

◆ file_fd

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

◆ file_offset

off_t orbit::http::HttpResponse::file_offset {0}

◆ file_size

off_t orbit::http::HttpResponse::file_size {0}

◆ headers

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

◆ status_code

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

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