5#include <unordered_map>
111 std::unordered_map<std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual>
headers;
159 void set_body(
const std::string& b,
const std::string& content_type =
"text/plain");
230 void send_file(
const std::string& path,
const std::string& content_type);
a class to store JSON values
Definition json.hpp:19401
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
HttpResponse && json(const nlohmann::json &j, HttpStatus code) &&
Definition HttpResponse.hpp:193
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 Con...
off_t file_offset
Definition HttpResponse.hpp:117
std::unordered_map< std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
Definition HttpResponse.hpp:111
HttpResponse & json(const nlohmann::json &j, HttpStatus code) &
json(j) and status(code) together: res.json(obj, HttpStatus::Created).
Definition HttpResponse.hpp:192
std::string serialize_headers() const
Serializes only the response headers to a string.
void render(const std::string &template_path, const nlohmann::json &data)
Renders a template and sets it as the HTML response body.
HttpResponse & html(const std::string &h) &
Sets the response body as HTML.
Definition HttpResponse.hpp:213
HttpResponse && status(HttpStatus code) &&
Definition HttpResponse.hpp:168
HttpResponse & json(const std::string &j) &
Sets the response body as JSON from a string.
Definition HttpResponse.hpp:181
int file_fd
Definition HttpResponse.hpp:115
void set_body(const std::string &b, const std::string &content_type="text/plain")
Sets the response body and its content type.
std::string serialize() const
Serializes the entire response (headers and body) to a string.
HttpStatus status_code
Definition HttpResponse.hpp:110
HttpResponse && json(const nlohmann::json &j) &&
Definition HttpResponse.hpp:189
HttpResponse & status(HttpStatus code) &
Sets the HTTP status code of the response.
Definition HttpResponse.hpp:167
std::vector< Cookie > cookies
Definition HttpResponse.hpp:109
HttpResponse & operator=(const HttpResponse &)=delete
std::string body
Definition HttpResponse.hpp:112
HttpResponse & send(const std::string &b) &
Sets the response body as plain text.
Definition HttpResponse.hpp:174
static HttpResponse error(HttpStatus code, const std::string &message)
A JSON error response, {"error": message}, with code.
Definition HttpResponse.hpp:205
void send_file(const std::string &path, const std::string &content_type)
Prepares a file to be sent using zero-copy (sendfile).
HttpResponse && json(const std::string &j) &&
Definition HttpResponse.hpp:182
HttpResponse & operator=(HttpResponse &&other) noexcept
HttpResponse && html(const std::string &h) &&
Definition HttpResponse.hpp:214
HttpResponse & set_cookie(const std::string &name, const std::string &value)
Adds a simple key-value cookie to the response.
Definition HttpResponse.hpp:146
HttpResponse(HttpResponse &&other) noexcept
HttpResponse && send(const std::string &b) &&
Definition HttpResponse.hpp:175
HttpResponse(const HttpResponse &)=delete
off_t file_size
Definition HttpResponse.hpp:116
HttpResponse & json(const nlohmann::json &j) &
Sets the response body as JSON from a nlohmann::json object.
Definition HttpResponse.hpp:188
HttpResponse & set_cookie(const Cookie &cookie)
Adds a cookie to the response.
Definition HttpResponse.hpp:135
bool is_valid_header_name(std::string_view name)
True if name is a valid header field name (an RFC 9110 token).
std::string format_http_date(std::time_t t)
Formats t as an IMF-fixdate (RFC 9110 section 5.6.7), e.g. "Sun, 06 Nov 1994 08:49:37 GMT",...
const char * reason_phrase(int status_code)
Reason phrase for a status code. Codes without a registered phrase get a generic one for their class ...
HttpStatus
Standard HTTP status codes.
Definition HttpResponse.hpp:17
@ RequestHeaderFieldsTooLarge
bool is_valid_header_value(std::string_view value)
True if value can be written as a header field value: it contains no CR, LF or NUL,...
std::string_view http_date_now()
The current time as an IMF-fixdate, for the Date header.
Represents an HTTP cookie.
Definition HttpResponse.hpp:93
bool secure
Definition HttpResponse.hpp:99
bool http_only
Definition HttpResponse.hpp:100
std::string value
Definition HttpResponse.hpp:95
long max_age
Definition HttpResponse.hpp:98
std::string name
Definition HttpResponse.hpp:94
std::string same_site
Definition HttpResponse.hpp:101
std::string path
Definition HttpResponse.hpp:96
std::string domain
Definition HttpResponse.hpp:97