Represents an HTTP response object. More...
#include <HttpResponse.h>
Public Member Functions | |
HttpResponse ()=default | |
HttpResponse (Tcp *tcp) | |
Construct a new HttpResponse object with a socket. | |
Tcp * | getTcp () const |
HttpResponse & | status (int code) |
Set the HTTP status code. | |
HttpResponse & | setStatus (int code) |
Alias for status(). | |
HttpResponse & | setBody (const std::string &body) |
Set the body of the response (string). | |
HttpResponse & | setContentType (const std::string &content_type) |
Set the Content-Type header. | |
std::string | getContentType () const |
Get the current Content-Type header value. | |
HttpResponse & | set (const std::string &field, const std::string &value) |
Set a generic header field. | |
HttpResponse & | setHeader (const std::string &key, const std::string &value) |
Alias for set() for custom headers. | |
HttpResponse & | setAuthorization (const std::string &jwtToken) |
Set an Authorization header with a JWT token. | |
bool | isHeaderSent () const |
Check if the headers have already been sent. | |
HttpResponse & | setCookie (const std::string &name, const std::string &value, const std::string &options) |
Set a cookie to be sent with the response. | |
HttpResponse & | clearCookie (const std::string &name, const std::string &options) |
Clear a cookie by setting Max-Age=0. | |
void | send (const std::string &body) |
Send a full response including headers and body. | |
void | send () |
void | send (std::nullptr_t)=delete |
void | send (const FrameworkView &view) |
void | send (const std::string &body, const std::string &contentType) |
void | send (const FrameworkView &view, const std::map< std::string, std::string > &context) |
void | sendHeaders () |
Send only the headers (for chunked/streaming responses). | |
void | start (int code, size_t contentLength, const std::string &contentType="application/octet-stream", const std::string &contentEncoding="") |
Begin a streaming response by sending headers. | |
void | writeChunk (const char *data, size_t length) |
Send a chunk of the response body. | |
void | finish () |
Finish the response (placeholder for potential finalization). | |
bool | ok () const |
Check if the response status code indicates success. | |
int | getSocket () const |
Return the raw socket descriptor. | |
void | sendUnauthorized () |
Send a 401 Unauthorized JSON response. | |
void | sendNotFound () |
Send a 404 Not Found JSON response. | |
void | endServerError (const std::string &msg) |
Send a 500 Internal Server Error response. | |
HttpResponse & | json (const std::string &jsonString) |
Send a JSON string/object with correct content type. | |
HttpResponse & | json (const nlohmann::json &jsonObj) |
HttpResponse & | jsonFormatted (const nlohmann::json &jsonObj) |
HttpResponse & | text (const std::string &textString) |
Send a plain text string with correct content type. | |
HttpResponse & | redirect (const std::string &url, int statusCode) |
Redirect the client to another URL. | |
bool | sendFile (const std::string &path) |
Sends the specified file from mounted storage to the client. | |
std::string | renderTemplate (const std::string &tpl, const std::map< std::string, std::string > &context) |
Apply basic variable substitution in a template. | |
int | getStatusCode () const |
Get the response status code. | |
std::string | getHeader (const std::string &key) const |
Get the value of a specific response header. | |
const std::map< std::string, std::string > & | getHeaders () const |
Get all response headers. | |
const std::string & | getBody () const |
Get the response body. | |
void | reset () |
Clear the response status, headers, and body. | |
bool | isBodyTruncated () const |
Check if the response body was truncated. | |
void | markBodyTruncated () |
Mark the response body as truncated. | |
HttpResponse & | sendSuccess (const nlohmann::json &data={}, const std::string &message="") |
HttpResponse & | sendCreated (const nlohmann::json &data={}, const std::string &message="") |
HttpResponse & | sendMessage (const std::string &message) |
HttpResponse & | sendNoContent () |
HttpResponse & | sendError (int statusCode, const std::string &code, const std::string &message) |
HttpResponse & | sendError (int statusCode, const std::string &message) |
HttpResponse & | toFile (const std::string &path, StorageManager *storage) |
Private Member Functions | |
std::string | getStatusMessage (int code) |
Convert an HTTP status code to its standard message. | |
Private Attributes | |
Tcp * | tcp |
Pointer to the Tcp object for socket operations. | |
int | status_code = 200 |
HTTP status code. | |
bool | headerSent = false |
Tracks whether headers have already been sent. | |
bool | bodyTruncated = false |
std::map< std::string, std::string > | headers |
Response headers (server+client) | |
std::vector< std::string > | cookies |
Set-Cookie headers (server only) | |
std::string | body |
Full response body (client-side or buffered server content) | |
Used by the server to construct and send headers, content, and cookies in response to an incoming HTTP request.
Definition at line 36 of file HttpResponse.h.
|
default |
HttpResponse::HttpResponse | ( | Tcp * | tcp | ) |
Construct a new HttpResponse object.
sock | The socket file descriptor. |
sock | Socket descriptor for the client connection. |
Definition at line 39 of file HttpResponse.cpp.
HttpResponse & HttpResponse::clearCookie | ( | const std::string & | name, |
const std::string & | options | ||
) |
Clear a cookie by setting Max-Age=0.
name | Cookie name. |
options | Optional options to include in the Set-Cookie header. |
name | Cookie name. |
options | Optional options to include in the Set-Cookie header. |
Definition at line 174 of file HttpResponse.cpp.
References cookies.
void HttpResponse::endServerError | ( | const std::string & | message | ) |
Send a 500 Internal Server Error response.
msg | Error message to include. |
msg | Error message to include. |
Definition at line 342 of file HttpResponse.cpp.
References send(), setContentType(), and status().
void HttpResponse::finish | ( | ) |
Finish the response (placeholder for potential finalization).
Definition at line 310 of file HttpResponse.cpp.
Referenced by FileHandler::serveFile().
|
inline |
Definition at line 304 of file HttpResponse.h.
References body.
Referenced by TimeManager::fetchAndApplyTimezoneFromOpenMeteo(), and TimeManager::getLocationFromIp().
std::string HttpResponse::getContentType | ( | ) | const |
Get the current Content-Type header value.
Definition at line 108 of file HttpResponse.cpp.
References headers.
|
inline |
key | Header name |
Definition at line 285 of file HttpResponse.h.
References headers.
|
inline |
Definition at line 295 of file HttpResponse.h.
References headers.
int HttpResponse::getSocket | ( | ) | const |
Return the raw socket descriptor.
Definition at line 125 of file HttpResponse.cpp.
References Tcp::getSocketFd(), and tcp.
|
inline |
Definition at line 275 of file HttpResponse.h.
References status_code.
Referenced by TimeManager::fetchAndApplyTimezoneFromOpenMeteo().
|
private |
Return the standard HTTP status message for a code.
code | HTTP status code. |
code | HTTP status code. |
Definition at line 135 of file HttpResponse.cpp.
Referenced by send(), sendHeaders(), and start().
|
inline |
|
inline |
Definition at line 318 of file HttpResponse.h.
References bodyTruncated.
bool HttpResponse::isHeaderSent | ( | ) | const |
Check if the headers have already been sent.
Definition at line 117 of file HttpResponse.cpp.
References headerSent.
HttpResponse & HttpResponse::json | ( | const nlohmann::json & | jsonObj | ) |
Definition at line 359 of file HttpResponse.cpp.
HttpResponse & HttpResponse::json | ( | const std::string & | body | ) |
Send a JSON string/object with correct content type.
jsonString | Valid JSON content. |
jsonString | Valid JSON content. |
Definition at line 352 of file HttpResponse.cpp.
Referenced by DashboardController::getLedState(), GpioController::getState(), DashboardController::getTemperature(), GpioController::handleGetMultipleGpios(), App::initRoutes(), JsonResponse::sendCreated(), JsonResponse::sendError(), JsonResponse::sendJson(), JsonResponse::sendMessage(), JsonResponse::sendSuccess(), DashboardController::setLedState(), and GpioController::setState().
HttpResponse & HttpResponse::jsonFormatted | ( | const nlohmann::json & | jsonObj | ) |
Definition at line 364 of file HttpResponse.cpp.
|
inline |
This is used when the body exceeds the maximum allowed size.
Definition at line 324 of file HttpResponse.h.
References bodyTruncated.
|
inline |
Definition at line 192 of file HttpResponse.h.
References status_code.
HttpResponse & HttpResponse::redirect | ( | const std::string & | url, |
int | statusCode | ||
) |
url | Target URL. |
statusCode | HTTP status code (e.g. 302). |
Definition at line 379 of file HttpResponse.cpp.
References send(), set(), and status().
std::string HttpResponse::renderTemplate | ( | const std::string & | tpl, |
const std::map< std::string, std::string > & | context | ||
) |
Apply basic variable substitution in a template.
tpl | Input template with {{placeholders}}. |
context | Key-value map for substitution. |
tpl | Input template with {{placeholders}}. |
context | Key-value map for substitution. |
Definition at line 390 of file HttpResponse.cpp.
void HttpResponse::reset | ( | ) |
Definition at line 434 of file HttpResponse.cpp.
References body, headers, and status_code.
|
inline |
|
inline |
Definition at line 148 of file HttpResponse.h.
References send().
void HttpResponse::send | ( | const FrameworkView & | view, |
const std::map< std::string, std::string > & | context | ||
) |
Definition at line 419 of file HttpResponse.cpp.
References FrameworkView::applyHeaders(), body, FrameworkView::getContentType(), FrameworkView::render(), send(), and setContentType().
void HttpResponse::send | ( | const std::string & | body | ) |
body | HttpResponse body as a string. |
Definition at line 193 of file HttpResponse.cpp.
References body, cookies, getStatusMessage(), headers, headerSent, Tcp::send(), status_code, tcp, and TRACE.
Referenced by endServerError(), MultipartParser::handleMultipart(), App::initRoutes(), DashboardController::initRoutes(), json(), redirect(), HtmlTemplateView::render(), JsonResponse::sendNoContent(), sendNotFound(), sendUnauthorized(), and text().
void HttpResponse::send | ( | const std::string & | body, |
const std::string & | contentType | ||
) |
Definition at line 412 of file HttpResponse.cpp.
References body, send(), and setHeader().
|
delete |
HttpResponse & HttpResponse::sendCreated | ( | const nlohmann::json & | data = {} , |
const std::string & | message = "" |
||
) |
Definition at line 69 of file JsonResponse.cpp.
References JsonResponse::sendCreated().
HttpResponse & HttpResponse::sendError | ( | int | statusCode, |
const std::string & | code, | ||
const std::string & | message | ||
) |
Definition at line 84 of file JsonResponse.cpp.
References JsonResponse::sendError().
Referenced by DashboardController::deleteFile(), and HttpFileserver::handle_list_directory().
HttpResponse & HttpResponse::sendError | ( | int | statusCode, |
const std::string & | message | ||
) |
Definition at line 446 of file HttpResponse.cpp.
References JsonResponse::sendError().
bool HttpResponse::sendFile | ( | const std::string & | path | ) |
path | Relative path to the file (e.g. "/index.html"). |
Definition at line 440 of file HttpResponse.cpp.
References FileHandler::serveFile().
Referenced by DashboardController::initRoutes().
void HttpResponse::sendHeaders | ( | ) |
Send only the headers (for chunked/streaming responses).
Definition at line 231 of file HttpResponse.cpp.
References cookies, getStatusMessage(), headers, headerSent, Tcp::send(), status_code, and tcp.
HttpResponse & HttpResponse::sendMessage | ( | const std::string & | message | ) |
Definition at line 74 of file JsonResponse.cpp.
References JsonResponse::sendMessage().
HttpResponse & HttpResponse::sendNoContent | ( | ) |
Definition at line 79 of file JsonResponse.cpp.
References JsonResponse::sendNoContent().
void HttpResponse::sendNotFound | ( | ) |
Send a 404 Not Found JSON response.
Definition at line 332 of file HttpResponse.cpp.
References send(), setContentType(), and status().
HttpResponse & HttpResponse::sendSuccess | ( | const nlohmann::json & | data = {} , |
const std::string & | message = "" |
||
) |
Definition at line 64 of file JsonResponse.cpp.
References JsonResponse::sendSuccess().
Referenced by DashboardController::deleteFile(), and HttpFileserver::handle_list_directory().
void HttpResponse::sendUnauthorized | ( | ) |
HttpResponse & HttpResponse::set | ( | const std::string & | field, |
const std::string & | value | ||
) |
Set a generic header field.
field | Header name. |
value | Header value. |
field | Header name. |
value | Header value. |
Definition at line 69 of file HttpResponse.cpp.
References headers.
Referenced by json(), redirect(), sendUnauthorized(), FileHandler::serveFile(), and text().
HttpResponse & HttpResponse::setAuthorization | ( | const std::string & | jwtToken | ) |
Set an Authorization header with a JWT token.
jwtToken | The JWT string. |
jwtToken | The JWT string. |
Definition at line 96 of file HttpResponse.cpp.
References headers.
HttpResponse & HttpResponse::setBody | ( | const std::string & | body | ) |
body | Response body content |
Definition at line 429 of file HttpResponse.cpp.
References body.
HttpResponse & HttpResponse::setContentType | ( | const std::string & | ct | ) |
Set the Content-Type header.
content_type | MIME type string. |
content_type | MIME type string. |
Definition at line 87 of file HttpResponse.cpp.
References headers.
Referenced by endServerError(), DashboardController::initRoutes(), HtmlTemplateView::render(), send(), and sendNotFound().
HttpResponse & HttpResponse::setCookie | ( | const std::string & | name, |
const std::string & | value, | ||
const std::string & | options | ||
) |
Set a cookie to be sent with the response.
name | Cookie name. |
value | Cookie value. |
options | Additional options (e.g., Path, HttpOnly). |
name | Cookie name. |
value | Cookie value. |
options | Additional options (e.g., Path, HttpOnly). |
Definition at line 159 of file HttpResponse.cpp.
References cookies.
HttpResponse & HttpResponse::setHeader | ( | const std::string & | key, |
const std::string & | value | ||
) |
Alias for set() for custom headers.
Definition at line 78 of file HttpResponse.cpp.
References headers.
Referenced by HttpServer::handleClient(), and send().
HttpResponse & HttpResponse::setStatus | ( | int | code | ) |
void HttpResponse::start | ( | int | code, |
size_t | contentLength, | ||
const std::string & | contentType = "application/octet-stream" , |
||
const std::string & | contentEncoding = "" |
||
) |
Begin a streaming response by sending headers.
code | HTTP status code. |
contentLength | Total length of body. |
contentType | MIME type. |
code | HTTP status code. |
contentLength | Total length of body. |
contentType | MIME type. |
Definition at line 261 of file HttpResponse.cpp.
References cookies, getStatusMessage(), headers, headerSent, Tcp::send(), status_code, and tcp.
Referenced by FileHandler::serveFile().
HttpResponse & HttpResponse::status | ( | int | code | ) |
Set the HTTP status code.
code | Status code (e.g., 200, 404). |
code | Status code (e.g., 200, 404). |
Definition at line 51 of file HttpResponse.cpp.
References status_code.
Referenced by endServerError(), MultipartParser::handleMultipart(), redirect(), JsonResponse::sendCreated(), JsonResponse::sendError(), JsonResponse::sendJson(), JsonResponse::sendMessage(), JsonResponse::sendNoContent(), sendNotFound(), JsonResponse::sendSuccess(), and sendUnauthorized().
HttpResponse & HttpResponse::text | ( | const std::string & | body | ) |
Send a plain text string with correct content type.
textString | Text content. |
textString | Text content. |
Definition at line 372 of file HttpResponse.cpp.
HttpResponse & HttpResponse::toFile | ( | const std::string & | path, |
StorageManager * | storage | ||
) |
Definition at line 452 of file HttpResponse.cpp.
References body, and StorageManager::writeFile().
void HttpResponse::writeChunk | ( | const char * | data, |
size_t | length | ||
) |
Send a chunk of the response body.
data | Pointer to data buffer. |
length | Size of the data. |
data | Pointer to data buffer. |
length | Size of the data. |
Definition at line 291 of file HttpResponse.cpp.
References headerSent, Tcp::send(), and tcp.
Referenced by FileHandler::serveFile().
|
private |
|
private |
Definition at line 349 of file HttpResponse.h.
Referenced by isBodyTruncated(), and markBodyTruncated().
|
private |
Definition at line 352 of file HttpResponse.h.
Referenced by clearCookie(), send(), sendHeaders(), setCookie(), and start().
|
private |
Definition at line 351 of file HttpResponse.h.
Referenced by getContentType(), getHeader(), getHeaders(), reset(), send(), sendHeaders(), set(), setAuthorization(), setContentType(), setHeader(), and start().
|
private |
Definition at line 348 of file HttpResponse.h.
Referenced by isHeaderSent(), send(), sendHeaders(), start(), and writeChunk().
|
private |
Definition at line 347 of file HttpResponse.h.
Referenced by getStatusCode(), ok(), reset(), send(), sendHeaders(), setStatus(), start(), and status().
|
private |
Definition at line 346 of file HttpResponse.h.
Referenced by getSocket(), getTcp(), send(), sendHeaders(), start(), and writeChunk().