Orbit Framework
An ultra-modern, asynchronous, cross-platform C++ Web Framework.
Loading...
Searching...
No Matches
HttpResponse.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <unordered_map>
4#include <cstddef>
5#include <cstdint>
6#include <orbit/http/json.hpp>
8
9namespace http {
10
14enum class HttpStatus {
16 OK = 200,
17 Created = 201,
18 NoContent = 204,
19 MovedPermanently = 301,
20 Found = 302,
21 NotModified = 304,
22 BadRequest = 400,
23 Unauthorized = 401,
24 Forbidden = 403,
25 NotFound = 404,
26 PayloadTooLarge = 413,
27 TooManyRequests = 429,
31};
32
36struct Cookie {
37 std::string name;
38 std::string value;
39 std::string path = "/";
40 std::string domain;
41 long max_age = -1;
42 bool secure = false;
43 bool http_only = false;
44 std::string same_site; // "Strict", "Lax", "None"
45};
46
51public:
52 std::vector<Cookie> cookies;
54 std::unordered_map<std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual> headers;
55 std::string body;
56
57 // Zero-copy file descriptors
58 int file_fd{-1};
59 off_t file_size{0};
60
61 HttpResponse() = default;
62
63 // Move semantics to manage the file_fd lifecycle safely
64 HttpResponse(HttpResponse&& other) noexcept;
65 HttpResponse& operator=(HttpResponse&& other) noexcept;
67
68 // Disable copy to prevent double-closing FDs
69 HttpResponse(const HttpResponse&) = delete;
71
77 HttpResponse& set_cookie(const Cookie& cookie) {
78 cookies.push_back(cookie);
79 return *this;
80 }
81
88 HttpResponse& set_cookie(const std::string& name, const std::string& value) {
89 Cookie c;
90 c.name = name;
91 c.value = value;
92 cookies.push_back(c);
93 return *this;
94 }
95
101 void set_body(const std::string& b, const std::string& content_type = "text/plain");
102
103 // Ergonomic fluent helpers
109 HttpResponse& status(HttpStatus code) & { status_code = code; return *this; }
110 HttpResponse&& status(HttpStatus code) && { status_code = code; return std::move(*this); }
111
116 HttpResponse& send(const std::string& b) & { set_body(b, "text/plain"); return *this; }
117 HttpResponse&& send(const std::string& b) && { set_body(b, "text/plain"); return std::move(*this); }
118
123 HttpResponse& json(const std::string& j) & { set_body(j, "application/json"); return *this; }
124 HttpResponse&& json(const std::string& j) && { set_body(j, "application/json"); return std::move(*this); }
125
130 HttpResponse& json(const nlohmann::json& j) & { set_body(j.dump(), "application/json"); return *this; }
131 HttpResponse&& json(const nlohmann::json& j) && { set_body(j.dump(), "application/json"); return std::move(*this); }
132
137 HttpResponse& html(const std::string& h) & { set_body(h, "text/html"); return *this; }
138 HttpResponse&& html(const std::string& h) && { set_body(h, "text/html"); return std::move(*this); }
139
140 // Server-Side Rendering
146 void render(const std::string& template_path, const nlohmann::json& data);
147
148 // Opens the file and sets up headers for sendfile()
154 void send_file(const std::string& path, const std::string& content_type);
155
160 std::string serialize() const;
161
166 std::string serialize_headers() const;
167};
168
169} // namespace http
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()=default
~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
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