44 HttpRequest(
const char *rawHeaders,
const std::string &reqMethod,
const std::string &reqPath);
71#if defined(PICO_HTTP_ENABLE_STORAGE)
78 bool setRootCACertificateFromFile(
const char *
path);
117 return (it !=
headers.end()) ? it->second :
"";
134 for (
const auto &header :
headers)
136 std::cout << header.first <<
": " << header.second << std::endl;
180 auto it =
headers.find(
"content-type");
181 return it !=
headers.end() && it->second.find(
"application/x-www-form-urlencoded") != std::string::npos;
189 auto it =
headers.find(
"content-type");
190 return it !=
headers.end() && it->second.find(
"application/json") != std::string::npos;
208 auto boundaryPos = contentType.find(
"boundary=");
209 if (boundaryPos != std::string::npos)
211 return contentType.substr(boundaryPos + 9);
221 auto contentType =
getHeader(
"content-type");
222 return contentType.find(
"multipart/form-data") != std::string::npos;
253 std::string content_length_str =
getHeader(
"content-length");
254 int contentLength = 0;
255 if (!content_length_str.empty())
257 contentLength = std::stoi(content_length_str);
259 return contentLength;
269 inline nlohmann::json
json()
const
271 return nlohmann::json::parse(
getBody(),
nullptr,
false);
345 const std::unordered_map<std::string, std::string>
getCookies()
const;
352 const std::string
getCookie(
const std::string &name)
const;
357 const std::unordered_multimap<std::string, std::string>
getQueryParams();
362 const std::unordered_multimap<std::string, std::string>
getFormParams();
437#if defined(PICO_HTTP_ENABLE_STORAGE)
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.
Forward declaration for potential routing needs.
HttpResponse get()
Send a GETPOST/PUT/DEL request.
const std::string & getBody() const
Get the request body (copy).
const std::map< std::string, std::string > & getHeaders() const
Get all request headers.
bool isFormUrlEncoded() const
Check if content-type is application/x-www-form-urlencoded.
HttpRequest & setUserAgent(const std::string &userAgent)
std::string getOutputFilePath() const
void setQueryString(const std::string &query)
HttpRequest & setUri(const std::string &uri)
Set the URI for the request.
void appendToBody(const char *data, size_t len)
HttpRequest(const std::string &raw, const std::string &method, const std::string &path)
const std::string & getMethod() const
Get the HTTP method.
static bool getMethodAndPath(const std::string &data, std::string &method, std::string &path)
Parse the HTTP method and path from the first request line.
std::string outputFilePath
HttpRequest & setRootCACertificate(const std::string &certData)
Set the root CA certificate to use for TLS.
HttpRequest & setHost(const std::string &host)
HttpRequest & setMethod(const std::string &method)
Set the HTTP method (e.g., GET, POST).
HttpRequest & setPath(const std::string &path)
Set the request path.
void printHeaders() const
Print all headers to the standard output.
const std::string & getRootCACertificate() const
Get the root CA certificate string, if set.
void parseHeaders(const char *raw)
HttpRequest & toFile(const std::string &path)
const std::string & getHost() const
Get the Host header value.
const std::unordered_multimap< std::string, std::string > getFormParams()
Get parsed form fields (application/x-www-form-urlencoded).
const std::unordered_multimap< std::string, std::string > getQueryParams()
Get parsed query string parameters.
static HttpRequest receive(Tcp *tcp)
Receive and parse an HTTP request from a socket.
int handle_multipart(HttpResponse &res)
Handle multipart/form-data uploads.
HttpRequest(const HttpRequest &)=default
const std::unordered_map< std::string, std::string > getCookies() const
Get all parsed cookies.
size_t getHeaderEnd()
Get the header end offset (used for body parsing).
static std::optional< std::pair< std::string, std::string > > receiveUntilHeadersComplete(Tcp *conn)
const std::string & getPath() const
Get the parsed request path (without query string).
const std::string getContentType() const
Get the raw Content-Type string.
HttpRequest & setProtocol(const std::string &protocol)
const std::string & getQuery() const
Get the parsed query string from the URL.
std::map< std::string, std::string > headers
nlohmann::json json() const
Safely parse the request body as JSON (non-throwing).
bool isJson() const
Check if content-type is application/json.
const std::string getCookie(const std::string &name) const
Get a specific cookie value.
int getContentLength() const
Get the Content-Length header as integer.
HttpResponse send()
Send the request and return the response.
void setHeaderEnd(size_t end)
Set the position marking the end of headers.
HttpRequest & setHeaders(const std::map< std::string, std::string > &headers)
const std::string getBoundary() const
Get the boundary string (for multipart/form-data).
bool isMultipart() const
Check whether the request is multipart/form-data.
std::string rootCACertificate
HttpRequest & setAcceptEncoding(const std::string &encoding)
std::string getHeader(const std::string &field) const
Get a specific header field (case-insensitive).
HttpRequest(Tcp *connection, const std::string rawHeaders, const std::string &method, const std::string &path)
static HttpRequest create()
HttpRequest & setHeader(const std::string &key, const std::string &value)
const std::string & getUri() const
Get the original URL from the request line.
bool appendRemainingBody(int expectedLength)
bool isBodyTruncated() const
Check if the request body was truncated due to memory limits.
const std::string & getProtocol() const
HttpRequest & setBody(const std::string &body)
Set the body of the request.
Represents an HTTP response object.
The central router for handling HTTP requests and middleware.
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).
std::string toLower(std::string str)
System utilities for diagnostics, memory, stack usage, and tracing.