Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
HttpResponse.hpp
Go to the documentation of this file.
1#pragma once
3#include <string>
4#include <string_view>
5#include <unordered_map>
6#include <cstddef>
7#include <cstdint>
8#include <ctime>
9#include <orbit/http/json.hpp>
11
12namespace orbit::http {
13
17enum class HttpStatus {
18 Continue = 100,
20 OK = 200,
21 Created = 201,
22 Accepted = 202,
23 NoContent = 204,
24 PartialContent = 206,
25 MovedPermanently = 301,
26 Found = 302,
27 SeeOther = 303,
28 NotModified = 304,
31 BadRequest = 400,
32 Unauthorized = 401,
33 Forbidden = 403,
34 NotFound = 404,
35 MethodNotAllowed = 405,
36 NotAcceptable = 406,
37 RequestTimeout = 408,
38 Conflict = 409,
39 Gone = 410,
40 LengthRequired = 411,
42 PayloadTooLarge = 413,
43 URITooLong = 414,
48 UpgradeRequired = 426,
49 TooManyRequests = 429,
52 NotImplemented = 501,
53 BadGateway = 502,
55 GatewayTimeout = 504
56};
57
62const char* reason_phrase(int status_code);
63
67bool is_valid_header_name(std::string_view name);
68
73bool is_valid_header_value(std::string_view value);
74
79std::string format_http_date(std::time_t t);
80
88std::string_view http_date_now();
89
93struct Cookie {
94 std::string name;
95 std::string value;
96 std::string path = "/";
97 std::string domain;
98 long max_age = -1;
99 bool secure = false;
100 bool http_only = false;
101 std::string same_site; // "Strict", "Lax", "None"
102};
103
108public:
109 std::vector<Cookie> cookies;
111 std::unordered_map<std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual> headers;
112 std::string body;
113
114 // Zero-copy file descriptors. Bytes [file_offset, file_size) are sent.
115 int file_fd{-1};
116 off_t file_size{0};
117 off_t file_offset{0};
118
119 HttpResponse() = default;
120
121 // Move semantics to manage the file_fd lifecycle safely
122 HttpResponse(HttpResponse&& other) noexcept;
125
126 // Disable copy to prevent double-closing FDs
127 HttpResponse(const HttpResponse&) = delete;
129
136 cookies.push_back(cookie);
137 return *this;
138 }
139
146 HttpResponse& set_cookie(const std::string& name, const std::string& value) {
147 Cookie c;
148 c.name = name;
149 c.value = value;
150 cookies.push_back(c);
151 return *this;
152 }
153
159 void set_body(const std::string& b, const std::string& content_type = "text/plain");
160
161 // Ergonomic fluent helpers
167 HttpResponse& status(HttpStatus code) & { status_code = code; return *this; }
168 HttpResponse&& status(HttpStatus code) && { status_code = code; return std::move(*this); }
169
174 HttpResponse& send(const std::string& b) & { set_body(b, "text/plain"); return *this; }
175 HttpResponse&& send(const std::string& b) && { set_body(b, "text/plain"); return std::move(*this); }
176
181 HttpResponse& json(const std::string& j) & { set_body(j, "application/json"); return *this; }
182 HttpResponse&& json(const std::string& j) && { set_body(j, "application/json"); return std::move(*this); }
183
188 HttpResponse& json(const nlohmann::json& j) & { set_body(j.dump(), "application/json"); return *this; }
189 HttpResponse&& json(const nlohmann::json& j) && { set_body(j.dump(), "application/json"); return std::move(*this); }
190
192 HttpResponse& json(const nlohmann::json& j, HttpStatus code) & { return status(code).json(j); }
193 HttpResponse&& json(const nlohmann::json& j, HttpStatus code) && { return std::move(*this).status(code).json(j); }
194
205 static HttpResponse error(HttpStatus code, const std::string& message) {
206 return HttpResponse().json(nlohmann::json{{"error", message}}, code);
207 }
208
213 HttpResponse& html(const std::string& h) & { set_body(h, "text/html"); return *this; }
214 HttpResponse&& html(const std::string& h) && { set_body(h, "text/html"); return std::move(*this); }
215
216 // Server-Side Rendering
222 void render(const std::string& template_path, const nlohmann::json& data);
223
224 // Opens the file and sets up headers for sendfile()
230 void send_file(const std::string& path, const std::string& content_type);
231
237 void set_file_range(off_t start, off_t end);
238
243 std::string serialize() const;
244
249 std::string serialize_headers() const;
250};
251
252} // namespace http
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
Definition Task.hpp:10
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
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