3#include <unordered_map>
54 std::unordered_map<std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual>
headers;
101 void set_body(
const std::string& b,
const std::string& content_type =
"text/plain");
154 void send_file(
const std::string& path,
const std::string& content_type);
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:50
HttpResponse && json(const std::string &j) &&
Definition HttpResponse.hpp:124
HttpResponse & operator=(const HttpResponse &)=delete
void set_body(const std::string &b, const std::string &content_type="text/plain")
Sets the response body and its content type.
Definition HttpResponse.cpp:47
HttpResponse & operator=(HttpResponse &&other) noexcept
Definition HttpResponse.cpp:27
HttpResponse && status(HttpStatus code) &&
Definition HttpResponse.hpp:110
HttpResponse & html(const std::string &h) &
Sets the response body as HTML.
Definition HttpResponse.hpp:137
HttpResponse && html(const std::string &h) &&
Definition HttpResponse.hpp:138
int file_fd
Definition HttpResponse.hpp:58
std::vector< Cookie > cookies
Definition HttpResponse.hpp:52
HttpResponse & json(const nlohmann::json &j) &
Sets the response body as JSON from a nlohmann::json object.
Definition HttpResponse.hpp:130
std::string serialize() const
Serializes the entire response (headers and body) to a string.
Definition HttpResponse.cpp:137
void send_file(const std::string &path, const std::string &content_type)
Prepares a file to be sent using zero-copy (sendfile).
Definition HttpResponse.cpp:64
HttpResponse & json(const std::string &j) &
Sets the response body as JSON from a string.
Definition HttpResponse.hpp:123
HttpResponse && json(const nlohmann::json &j) &&
Definition HttpResponse.hpp:131
std::string body
Definition HttpResponse.hpp:55
HttpResponse && send(const std::string &b) &&
Definition HttpResponse.hpp:117
HttpResponse & send(const std::string &b) &
Sets the response body as plain text.
Definition HttpResponse.hpp:116
HttpResponse(const HttpResponse &)=delete
HttpStatus status_code
Definition HttpResponse.hpp:53
std::string serialize_headers() const
Serializes only the response headers to a string.
Definition HttpResponse.cpp:89
HttpResponse & set_cookie(const Cookie &cookie)
Adds a cookie to the response.
Definition HttpResponse.hpp:77
HttpResponse & status(HttpStatus code) &
Sets the HTTP status code of the response.
Definition HttpResponse.hpp:109
HttpResponse & set_cookie(const std::string &name, const std::string &value)
Adds a simple key-value cookie to the response.
Definition HttpResponse.hpp:88
~HttpResponse()
Definition HttpResponse.cpp:41
void render(const std::string &template_path, const nlohmann::json &data)
Renders a template and sets it as the HTML response body.
Definition HttpResponse.cpp:53
std::unordered_map< std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
Definition HttpResponse.hpp:54
off_t file_size
Definition HttpResponse.hpp:59
a class to store JSON values
Definition json.hpp:19401
Definition Http2Session.hpp:21
HttpStatus
Standard HTTP status codes.
Definition HttpResponse.hpp:14
@ RequestHeaderFieldsTooLarge
Represents an HTTP cookie.
Definition HttpResponse.hpp:36
std::string domain
Definition HttpResponse.hpp:40
bool http_only
Definition HttpResponse.hpp:43
std::string value
Definition HttpResponse.hpp:38
bool secure
Definition HttpResponse.hpp:42
long max_age
Definition HttpResponse.hpp:41
std::string path
Definition HttpResponse.hpp:39
std::string same_site
Definition HttpResponse.hpp:44
std::string name
Definition HttpResponse.hpp:37