20 const std::string &protocol = request.
getProtocol();
21 const std::string &host = request.
getHost();
22 const std::string &path = request.
getUri();
23 const std::string &method = request.
getMethod();
24 const std::string &body = request.
getBody();
28 const bool useTls = (protocol ==
"https");
29 const uint16_t port = useTls ? 443 : 80;
33 TRACE(
"Connecting to %s:%d\n", host.c_str(), port);
34 TRACE(
"Using TLS: %s\n", useTls ?
"true" :
"false");
35 TRACE(
"Root CA: %s\n", cert.empty() ?
"none" :
"set");
36 TRACE(
"Method: %s\n", method.c_str());
37 TRACE(
"Path: %s\n", path.c_str());
38 TRACE(
"Body: %s\n", body.c_str());
40 for (
const auto &[key, value] : headers)
42 TRACE(
" %s: %s\n", key.c_str(), value.c_str());
54 if (!socket.
connect(host.c_str(), port, useTls))
59 std::ostringstream req;
60 req << method <<
" " << path <<
" HTTP/1.1\r\n";
61 req <<
"Host: " << host <<
"\r\n";
63 for (
const auto &[key, value] : headers)
65 req << key <<
": " << value <<
"\r\n";
70 req <<
"Content-Length: " << body.length() <<
"\r\n";
79 const std::string requestStr = req.str();
80 if (!socket.
send(requestStr.c_str(), requestStr.length()))
86 if (rawHeader.empty())
91 TRACE(
"Raw header: %s\n", rawHeader.c_str());
92 TRACE(
"Leftover: %s\n", leftover.c_str());
96 for (
const auto &[key, value] : parsedHeaders)
101 bool truncated =
false;
110 [&](
const char* data,
size_t len) {
111 return storage->
appendToFile(path,
reinterpret_cast<const uint8_t*
>(data), len);
117 [&](
const char* data,
size_t len) {
118 return storage->
appendToFile(path,
reinterpret_cast<const uint8_t*
>(data), len);
130 response.
setHeader(
"X-Body-Saved", path);
135 std::string bodyData;
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
#define TRACE(...)
Default trace (INFO level).
Defines the HttpRequest class for handling HTTP requests: headers, method, path, query string,...
HTTP HttpResponse class for managing status, headers, body, and streaming support.
General-purpose TCP socket abstraction with optional TLS support for both client and server use.
static constexpr std::uintptr_t getTypeKey()
static std::pair< std::string, std::string > receiveHeaderAndLeftover(Tcp &socket)
Receive raw headers from the socket until \r \r .
static bool receiveFixedLengthBodyToFile(Tcp &socket, const std::map< std::string, std::string > &headers, const std::string &leftover, std::function< bool(const char *, size_t)> writeFn, size_t maxLength, bool *wasTruncated)
static bool isChunkedEncoding(const std::map< std::string, std::string > &headers)
static int parseStatusCode(const std::string &statusLine)
Parses the HTTP status line to extract the status code.
static bool receiveBody(Tcp &socket, const std::map< std::string, std::string > &headers, const std::string &leftoverBody, std::string &outBody, size_t maxLength, bool *wasTruncated)
Receive the HTTP body from socket, using Content-Length or connection close.
static std::map< std::string, std::string > parseHeaders(const std::string &rawHeaders)
Parses the HTTP headers from a raw header string.
static bool receiveChunkedBodyToFile(Tcp &socket, const std::string &leftover, std::function< bool(const char *, size_t)> writeFn, size_t maxLength, bool *wasTruncated)
Forward declaration for potential routing needs.
const std::string & getBody() const
Get the request body (copy).
const std::map< std::string, std::string > & getHeaders() const
Get all request headers.
std::string getOutputFilePath() const
const std::string & getMethod() const
Get the HTTP method.
const std::string & getRootCACertificate() const
Get the root CA certificate string, if set.
const std::string & getHost() const
Get the Host header value.
const std::string & getUri() const
Get the original URL from the request line.
const std::string & getProtocol() const
Represents an HTTP response object.
HttpResponse & setStatus(int code)
Alias for status().
void markBodyTruncated()
Mark the response body as truncated.
HttpResponse & setBody(const std::string &body)
Set the body of the response (string).
void reset()
Clear the response status, headers, and body.
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.
virtual bool appendToFile(const std::string &path, const uint8_t *data, size_t size)=0
Append data to a file.
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).
bool connect(const char *host, int port, bool useTls=false)
Connect to a remote host.
void setRootCACertificate(const std::string &pem)
Set the Root CA certificate to be used for client TLS connections (PEM format).
void setHostname(const char *name)
int send(const char *buffer, size_t size)
Send data over the connection.
Delegates to user or system configuration.
#define MAX_HTTP_BODY_LENGTH
Maximum HTTP body size in bytes.