19#include <unordered_map>
22#include <nlohmann/json.hpp>
122 HttpResponse &
setCookie(
const std::string &name,
const std::string &value,
const std::string &options);
146 void send(std::nullptr_t) =
delete;
152 void send(
const std::string &
body,
const std::string &contentType);
157 const std::map<std::string, std::string>& context);
170 void start(
int code,
size_t contentLength,
const std::string &contentType =
"application/octet-stream",
171 const std::string &contentEncoding =
"");
178 void writeChunk(
const char *data,
size_t length);
249 bool sendFile(
const std::string &path);
251#if defined(PICO_HTTP_ENABLE_STORAGE)
258 bool saveFile(
const char *path)
const;
267 std::string
renderTemplate(
const std::string &tpl,
const std::map<std::string, std::string> &context);
288 return (it !=
headers.end()) ? it->second :
"";
Abstract base class for all views in the PicoFramework.
Abstract interface for file and directory storage backends.
General-purpose TCP socket abstraction with optional TLS support for both client and server use.
Forward declaration for potential routing needs.
Represents an HTTP response object.
void sendUnauthorized()
Send a 401 Unauthorized JSON response.
int status_code
HTTP status code.
bool isBodyTruncated() const
Check if the response body was truncated.
HttpResponse & setStatus(int code)
Alias for status().
std::string renderTemplate(const std::string &tpl, const std::map< std::string, std::string > &context)
Apply basic variable substitution in a template.
HttpResponse & toFile(const std::string &path, StorageManager *storage)
HttpResponse & setContentType(const std::string &content_type)
Set the Content-Type header.
void writeChunk(const char *data, size_t length)
Send a chunk of the response body.
HttpResponse & text(const std::string &textString)
Send a plain text string with correct content type.
int getStatusCode() const
Get the response status code.
void markBodyTruncated()
Mark the response body as truncated.
int getSocket() const
Return the raw socket descriptor.
HttpResponse & set(const std::string &field, const std::string &value)
Set a generic header field.
void finish()
Finish the response (placeholder for potential finalization).
bool isHeaderSent() const
Check if the headers have already been sent.
HttpResponse & setAuthorization(const std::string &jwtToken)
Set an Authorization header with a JWT token.
HttpResponse & sendCreated(const nlohmann::json &data={}, const std::string &message="")
void sendNotFound()
Send a 404 Not Found JSON response.
void endServerError(const std::string &msg)
Send a 500 Internal Server Error response.
const std::string & getBody() const
Get the response body.
bool ok() const
Check if the response status code indicates success.
HttpResponse & jsonFormatted(const nlohmann::json &jsonObj)
void sendHeaders()
Send only the headers (for chunked/streaming responses).
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.
HttpResponse & status(int code)
Set the HTTP status code.
Tcp * tcp
Pointer to the Tcp object for socket operations.
std::string getContentType() const
Get the current Content-Type header value.
HttpResponse & sendMessage(const std::string &message)
std::string body
Full response body (client-side or buffered server content)
HttpResponse & setBody(const std::string &body)
Set the body of the response (string).
bool sendFile(const std::string &path)
Sends the specified file from mounted storage to the client.
std::vector< std::string > cookies
Set-Cookie headers (server only)
HttpResponse & redirect(const std::string &url, int statusCode)
Redirect the client to another URL.
HttpResponse & sendNoContent()
std::map< std::string, std::string > headers
Response headers (server+client)
void send(std::nullptr_t)=delete
std::string getHeader(const std::string &key) const
Get the value of a specific response header.
void send(const FrameworkView &view)
bool headerSent
Tracks whether headers have already been sent.
void reset()
Clear the response status, headers, and body.
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.
const std::map< std::string, std::string > & getHeaders() const
Get all response headers.
std::string getStatusMessage(int code)
Convert an HTTP status code to its standard message.
HttpResponse & sendSuccess(const nlohmann::json &data={}, const std::string &message="")
HttpResponse & sendError(int statusCode, const std::string &code, const std::string &message)
HttpResponse & setHeader(const std::string &key, const std::string &value)
Alias for set() for custom headers.
Abstract base class for storage access and file operations.
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).