Forward declaration for potential routing needs. More...
#include <HttpRequest.h>
Public Member Functions | |
HttpRequest (const char *rawHeaders, const std::string &reqMethod, const std::string &reqPath) | |
Construct a HttpRequest from raw headers, method, and path. | |
HttpRequest (const std::string &raw, const std::string &method, const std::string &path) | |
HttpRequest (Tcp *connection, const std::string rawHeaders, const std::string &method, const std::string &path) | |
HttpRequest ()=default | |
HttpRequest (const HttpRequest &)=default | |
HttpRequest & | setRootCACertificate (const std::string &certData) |
Set the root CA certificate to use for TLS. | |
HttpResponse | send () |
Send the request and return the response. | |
HttpResponse | get () |
Send a GETPOST/PUT/DEL request. | |
HttpResponse | get (const std::string &url) |
HttpResponse | post () |
HttpResponse | post (const std::string &url, const std::string &body) |
HttpResponse | put () |
HttpResponse | put (const std::string &url, const std::string &body) |
HttpResponse | del () |
HttpResponse | del (const std::string &url) |
std::string | getHeader (const std::string &field) const |
Get a specific header field (case-insensitive). | |
const std::map< std::string, std::string > & | getHeaders () const |
Get all request headers. | |
void | printHeaders () const |
Print all headers to the standard output. | |
void | setHeaderEnd (size_t end) |
Set the position marking the end of headers. | |
size_t | getHeaderEnd () |
Get the header end offset (used for body parsing). | |
const std::string & | getHost () const |
Get the Host header value. | |
const std::string & | getProtocol () const |
bool | isFormUrlEncoded () const |
Check if content-type is application/x-www-form-urlencoded. | |
bool | isJson () const |
Check if content-type is application/json. | |
const std::string | getContentType () const |
Get the raw Content-Type string. | |
const std::string | getBoundary () const |
Get the boundary string (for multipart/form-data). | |
bool | isMultipart () const |
Check whether the request is multipart/form-data. | |
const std::string & | getBody () const |
Get the request body (copy). | |
bool | isBodyTruncated () const |
Check if the request body was truncated due to memory limits. | |
void | markBodyTruncated () |
HttpRequest & | setBody (const std::string &body) |
Set the body of the request. | |
int | getContentLength () const |
Get the Content-Length header as integer. | |
nlohmann::json | json () const |
Safely parse the request body as JSON (non-throwing). | |
HttpRequest & | setMethod (const std::string &method) |
Set the HTTP method (e.g., GET, POST). | |
HttpRequest & | setPath (const std::string &path) |
Set the request path. | |
const std::string & | getMethod () const |
Get the HTTP method. | |
const std::string & | getPath () const |
Get the parsed request path (without query string). | |
const std::string & | getUri () const |
Get the original URL from the request line. | |
const std::string & | getQuery () const |
Get the parsed query string from the URL. | |
Tcp * | getTcp () const |
const std::unordered_map< std::string, std::string > | getCookies () const |
Get all parsed cookies. | |
const std::string | getCookie (const std::string &name) const |
Get a specific cookie value. | |
const std::unordered_multimap< std::string, std::string > | getQueryParams () |
Get parsed query string parameters. | |
const std::unordered_multimap< std::string, std::string > | getFormParams () |
Get parsed form fields (application/x-www-form-urlencoded). | |
bool | appendRemainingBody (int expectedLength) |
int | handle_multipart (HttpResponse &res) |
Handle multipart/form-data uploads. | |
HttpRequest & | setUri (const std::string &uri) |
Set the URI for the request. | |
HttpRequest & | setHost (const std::string &host) |
HttpRequest & | setProtocol (const std::string &protocol) |
HttpRequest & | setHeaders (const std::map< std::string, std::string > &headers) |
HttpRequest & | setHeader (const std::string &key, const std::string &value) |
HttpRequest & | setUserAgent (const std::string &userAgent) |
HttpRequest & | setAcceptEncoding (const std::string &encoding) |
HttpRequest & | toFile (const std::string &path) |
std::string | getOutputFilePath () const |
bool | wantsToFile () const |
const std::string & | getRootCACertificate () const |
Get the root CA certificate string, if set. | |
Static Public Member Functions | |
static HttpRequest | receive (Tcp *tcp) |
Receive and parse an HTTP request from a socket. | |
static std::optional< std::pair< std::string, std::string > > | receiveUntilHeadersComplete (Tcp *conn) |
static bool | getMethodAndPath (const std::string &data, std::string &method, std::string &path) |
Parse the HTTP method and path from the first request line. | |
static HttpRequest | create () |
Private Member Functions | |
void | parseHeaders (const char *raw) |
void | appendToBody (const char *data, size_t len) |
void | setQueryString (const std::string &query) |
Private Attributes | |
Tcp * | tcp = nullptr |
std::string | clientIp |
std::string | method |
std::string | uri |
std::string | path |
std::string | query |
std::string | host |
std::string | protocol |
std::map< std::string, std::string > | headers |
std::string | body |
std::string | rootCACertificate |
size_t | headerEnd = 0 |
bool | bodyTruncated = false |
std::string | outputFilePath |
Represents a parsed HTTP request, providing access to headers, method, path, body, etc.
Definition at line 31 of file HttpRequest.h.
HttpRequest::HttpRequest | ( | const char * | rawHeaders, |
const std::string & | reqMethod, | ||
const std::string & | reqPath | ||
) |
Construct a HttpRequest object and parse the URL and headers.
rawHeaders | Raw HTTP headers as a C-string. |
reqMethod | HTTP method. |
reqPath | HttpRequest path. |
rawHeaders | Raw header string. |
reqMethod | HTTP method (GET, POST, etc.). |
reqPath | HttpRequest URL path. |
Definition at line 120 of file HttpRequest.cpp.
References parseHeaders(), path, query, and uri.
|
inline |
|
inline |
Definition at line 51 of file HttpRequest.h.
References parseHeaders().
|
default |
|
default |
bool HttpRequest::appendRemainingBody | ( | int | expectedLength | ) |
Definition at line 210 of file HttpRequest.cpp.
References body, HTTP_BUFFER_SIZE, HTTP_RECEIVE_TIMEOUT, markBodyTruncated(), MAX_HTTP_BODY_LENGTH, Tcp::recv(), tcp, and TRACE.
Referenced by receive().
|
private |
|
static |
Definition at line 398 of file HttpRequest.cpp.
References HttpRequest().
HttpResponse HttpRequest::del | ( | ) |
Definition at line 71 of file HttpRequest.cpp.
References send(), and setMethod().
HttpResponse HttpRequest::del | ( | const std::string & | url | ) |
Definition at line 93 of file HttpRequest.cpp.
References send(), setMethod(), and setUri().
HttpResponse HttpRequest::get | ( | ) |
url | The URL to send the request to. |
Definition at line 46 of file HttpRequest.cpp.
References send(), and setMethod().
Referenced by TimeManager::fetchAndApplyTimezoneFromOpenMeteo(), and TimeManager::getLocationFromIp().
HttpResponse HttpRequest::get | ( | const std::string & | url | ) |
Definition at line 52 of file HttpRequest.cpp.
References send(), setMethod(), and setUri().
|
inline |
Definition at line 232 of file HttpRequest.h.
References body.
Referenced by JsonRequestHelper::getFullJson(), HttpServer::handleClient(), MultipartParser::handleMultipart(), and json().
|
inline |
Definition at line 205 of file HttpRequest.h.
References getContentType().
Referenced by HttpServer::handleClient().
|
inline |
Definition at line 251 of file HttpRequest.h.
References getHeader().
Referenced by HttpServer::handleClient(), and receive().
|
inline |
Definition at line 196 of file HttpRequest.h.
References getHeader().
Referenced by getBoundary(), and HttpServer::handleClient().
const std::string HttpRequest::getCookie | ( | const std::string & | name | ) | const |
Get a specific cookie by name.
name | Cookie name. |
name | Cookie name. |
Definition at line 367 of file HttpRequest.cpp.
References getCookies().
const std::unordered_map< std::string, std::string > HttpRequest::getCookies | ( | ) | const |
Extract and return all cookies from the Cookie header.
Definition at line 340 of file HttpRequest.cpp.
References getHeader().
Referenced by getCookie(), and HttpServer::handleClient().
const std::unordered_multimap< std::string, std::string > HttpRequest::getFormParams | ( | ) |
Parse the request body as form-urlencoded content.
Definition at line 389 of file HttpRequest.cpp.
References body, and parseUrlEncoded().
Referenced by HttpServer::handleClient().
|
inline |
field | Header field name. |
Definition at line 114 of file HttpRequest.h.
References headers, and toLower().
Referenced by Router::getAuthorizationToken(), getContentLength(), getContentType(), getCookies(), HttpServer::handleClient(), MultipartParser::handleMultipart(), isMultipart(), and FrameworkManager::warmUp().
|
inline |
Definition at line 153 of file HttpRequest.h.
References headerEnd.
Referenced by HttpServer::handleClient().
|
inline |
Definition at line 124 of file HttpRequest.h.
References headers.
Referenced by HttpServer::handleClient(), and receive().
|
inline |
Definition at line 161 of file HttpRequest.h.
References host.
|
inline |
Definition at line 299 of file HttpRequest.h.
References method.
Referenced by HttpServer::handleClient(), and Router::handleRequest().
|
static |
Extract HTTP method and path from request line.
buffer | Raw request buffer. |
method | Output buffer for method. |
path | Output buffer for path. |
buffer | The raw request buffer. |
clientSocket | The client socket (not used, included for consistency). |
method | Output buffer for method. |
path | Output buffer for path. |
Definition at line 160 of file HttpRequest.cpp.
References method, path, and TRACE.
Referenced by receive().
std::string HttpRequest::getOutputFilePath | ( | ) | const |
|
inline |
Definition at line 307 of file HttpRequest.h.
References path.
Referenced by HttpFileserver::handle_list_directory(), HttpFileserver::handle_static_request(), HttpServer::handleClient(), and Router::handleRequest().
|
inline |
|
inline |
Definition at line 323 of file HttpRequest.h.
References query.
Referenced by HttpServer::handleClient().
const std::unordered_multimap< std::string, std::string > HttpRequest::getQueryParams | ( | ) |
Parse the query string into a key-value map.
Definition at line 379 of file HttpRequest.cpp.
References parseUrlEncoded(), and query.
Referenced by HttpServer::handleClient(), and GpioController::handleGetMultipleGpios().
|
inline |
Definition at line 432 of file HttpRequest.h.
References rootCACertificate.
|
inline |
Definition at line 332 of file HttpRequest.h.
References tcp.
Referenced by MultipartParser::handleMultipart().
|
inline |
Definition at line 315 of file HttpRequest.h.
References uri.
Referenced by HttpServer::handleClient().
int HttpRequest::handle_multipart | ( | HttpResponse & | res | ) |
Handle multipart/form-data content using MultipartParser.
res | Reference to the response. |
clientSocket | The client socket. |
req | Reference to this request. |
Definition at line 329 of file HttpRequest.cpp.
References MultipartParser::handleMultipart().
Referenced by DashboardController::uploadHandler().
|
inline |
|
inline |
Definition at line 178 of file HttpRequest.h.
References headers.
|
inline |
Definition at line 187 of file HttpRequest.h.
References headers.
Referenced by JsonRequestHelper::getFullJson().
|
inline |
Definition at line 219 of file HttpRequest.h.
References getHeader().
Referenced by HttpServer::handleClient(), and receive().
|
inline |
Returns an empty object if parsing fails.
Definition at line 269 of file HttpRequest.h.
References getBody().
|
inline |
Definition at line 239 of file HttpRequest.h.
References bodyTruncated.
Referenced by appendRemainingBody().
|
private |
Definition at line 146 of file HttpRequest.cpp.
References headers, and HttpParser::parseHeaders().
Referenced by HttpRequest(), and HttpRequest().
HttpResponse HttpRequest::post | ( | ) |
Definition at line 59 of file HttpRequest.cpp.
References send(), and setMethod().
HttpResponse HttpRequest::post | ( | const std::string & | url, |
const std::string & | body | ||
) |
Definition at line 77 of file HttpRequest.cpp.
References body, send(), setBody(), setMethod(), and setUri().
|
inline |
Definition at line 132 of file HttpRequest.h.
References headers.
HttpResponse HttpRequest::put | ( | ) |
Definition at line 65 of file HttpRequest.cpp.
References send(), and setMethod().
HttpResponse HttpRequest::put | ( | const std::string & | url, |
const std::string & | body | ||
) |
|
static |
Receive raw bytes from the client socket into a buffer.
Instance | of tcp |
Tcp* | The TCP connection to read from. |
Definition at line 252 of file HttpRequest.cpp.
References appendRemainingBody(), body, BUFFER_SIZE, getContentLength(), getHeaders(), getMethodAndPath(), Tcp::getSocketFd(), headers, HttpRequest(), isMultipart(), method, path, receiveUntilHeadersComplete(), setBody(), setPath(), setQueryString(), tcp, and TRACE.
Referenced by HttpServer::handleClient().
|
static |
Definition at line 180 of file HttpRequest.cpp.
References headerEnd, headers, HTTP_BUFFER_SIZE, HTTP_RECEIVE_TIMEOUT, MAX_HEADER_BYTES, and Tcp::recv().
Referenced by receive().
HttpResponse HttpRequest::send | ( | ) |
Definition at line 100 of file HttpRequest.cpp.
Referenced by del(), del(), get(), get(), post(), post(), put(), and put().
HttpRequest & HttpRequest::setAcceptEncoding | ( | const std::string & | encoding | ) |
Definition at line 471 of file HttpRequest.cpp.
References headers.
HttpRequest & HttpRequest::setBody | ( | const std::string & | body | ) |
HttpRequest & HttpRequest::setHeader | ( | const std::string & | key, |
const std::string & | value | ||
) |
Definition at line 459 of file HttpRequest.cpp.
References headers.
Referenced by FrameworkManager::warmUp().
|
inline |
end | Offset into the raw request. |
Definition at line 144 of file HttpRequest.h.
References headerEnd.
HttpRequest & HttpRequest::setHeaders | ( | const std::map< std::string, std::string > & | headers | ) |
Definition at line 450 of file HttpRequest.cpp.
References headers.
HttpRequest & HttpRequest::setHost | ( | const std::string & | host | ) |
Definition at line 438 of file HttpRequest.cpp.
References host.
|
inline |
|
inline |
Definition at line 290 of file HttpRequest.h.
References path.
Referenced by receive(), and FrameworkManager::warmUp().
HttpRequest & HttpRequest::setProtocol | ( | const std::string & | protocol | ) |
Definition at line 444 of file HttpRequest.cpp.
References protocol.
|
inlineprivate |
HttpRequest & HttpRequest::setRootCACertificate | ( | const std::string & | certData | ) |
certData | PEM-encoded certificate. |
Definition at line 476 of file HttpRequest.cpp.
References rootCACertificate.
HttpRequest & HttpRequest::setUri | ( | const std::string & | uri | ) |
Parses the uri into protocol, host, and path. But not if it is just a /path
uri | The URI to set. |
Definition at line 409 of file HttpRequest.cpp.
References host, protocol, and uri.
Referenced by del(), get(), post(), and put().
HttpRequest & HttpRequest::setUserAgent | ( | const std::string & | userAgent | ) |
Definition at line 465 of file HttpRequest.cpp.
References headers.
HttpRequest & HttpRequest::toFile | ( | const std::string & | path | ) |
Definition at line 507 of file HttpRequest.cpp.
References outputFilePath, and path.
bool HttpRequest::wantsToFile | ( | ) | const |
Definition at line 512 of file HttpRequest.cpp.
References outputFilePath.
|
private |
Definition at line 460 of file HttpRequest.h.
Referenced by appendRemainingBody(), getBody(), getFormParams(), post(), put(), receive(), and setBody().
|
private |
Definition at line 463 of file HttpRequest.h.
Referenced by isBodyTruncated(), and markBodyTruncated().
|
private |
Definition at line 452 of file HttpRequest.h.
|
private |
Definition at line 462 of file HttpRequest.h.
Referenced by getHeaderEnd(), receiveUntilHeadersComplete(), and setHeaderEnd().
|
private |
Definition at line 459 of file HttpRequest.h.
Referenced by getHeader(), getHeaders(), isFormUrlEncoded(), isJson(), parseHeaders(), printHeaders(), receive(), receiveUntilHeadersComplete(), setAcceptEncoding(), setHeader(), setHeaders(), and setUserAgent().
|
private |
Definition at line 457 of file HttpRequest.h.
|
private |
Definition at line 453 of file HttpRequest.h.
Referenced by getMethod(), getMethodAndPath(), receive(), and setMethod().
|
private |
Definition at line 464 of file HttpRequest.h.
Referenced by getOutputFilePath(), toFile(), and wantsToFile().
|
private |
Definition at line 455 of file HttpRequest.h.
Referenced by getMethodAndPath(), getPath(), HttpRequest(), receive(), setPath(), and toFile().
|
private |
Definition at line 458 of file HttpRequest.h.
Referenced by getProtocol(), setProtocol(), and setUri().
|
private |
Definition at line 456 of file HttpRequest.h.
Referenced by getQuery(), getQueryParams(), HttpRequest(), and setQueryString().
|
private |
Definition at line 461 of file HttpRequest.h.
Referenced by getRootCACertificate(), and setRootCACertificate().
|
private |
Definition at line 450 of file HttpRequest.h.
Referenced by appendRemainingBody(), getTcp(), and receive().
|
private |
Definition at line 454 of file HttpRequest.h.
Referenced by getUri(), HttpRequest(), and setUri().