Forward declaration for potential routing needs. More...
#include <HttpRequest.h>
Collaboration diagram for HttpRequest: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. | |
| HttpRequest & | setJson (const nlohmann::json &json) |
| Set the body of the request as JSON. | |
| HttpRequest & | setJson (const std::string &json) |
| Set the body of the request as JSON. | |
| 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.
Here is the call graph for this function:
|
inline |
|
inline |
Definition at line 51 of file HttpRequest.h.
References parseHeaders().
Here is the call graph for this function:
|
default |
|
default |
| bool HttpRequest::appendRemainingBody | ( | int | expectedLength | ) |
Definition at line 224 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().
Here is the call graph for this function:
Here is the caller graph for this function:
|
private |
|
static |
Definition at line 412 of file HttpRequest.cpp.
References HttpRequest().
Here is the call graph for this function:| HttpResponse HttpRequest::del | ( | ) |
Definition at line 71 of file HttpRequest.cpp.
References send(), and setMethod().
Here is the call graph for this function:| HttpResponse HttpRequest::del | ( | const std::string & | url | ) |
Definition at line 93 of file HttpRequest.cpp.
References send(), setMethod(), and setUri().
Here is the call graph for this function:| 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().
Here is the call graph for this function:
Here is the caller graph for this function:| HttpResponse HttpRequest::get | ( | const std::string & | url | ) |
Definition at line 52 of file HttpRequest.cpp.
References send(), setMethod(), and setUri().
Here is the call graph for this function:
|
inline |
Definition at line 232 of file HttpRequest.h.
References body.
Referenced by JsonRequestHelper::getFullJson(), HttpServer::handleClient(), MultipartParser::handleMultipart(), and json().
Here is the caller graph for this function:
|
inline |
Definition at line 205 of file HttpRequest.h.
References getContentType().
Referenced by HttpServer::handleClient().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 263 of file HttpRequest.h.
References getHeader().
Referenced by HttpServer::handleClient(), and receive().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 196 of file HttpRequest.h.
References getHeader().
Referenced by getBoundary(), and HttpServer::handleClient().
Here is the call graph for this function:
Here is the caller graph for this function:| 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 381 of file HttpRequest.cpp.
References getCookies().
Here is the call graph for this function:| const std::unordered_map< std::string, std::string > HttpRequest::getCookies | ( | ) | const |
Extract and return all cookies from the Cookie header.
Definition at line 354 of file HttpRequest.cpp.
References getHeader().
Referenced by getCookie(), and HttpServer::handleClient().
Here is the call graph for this function:
Here is the caller graph for this function:| const std::unordered_multimap< std::string, std::string > HttpRequest::getFormParams | ( | ) |
Parse the request body as form-urlencoded content.
Definition at line 403 of file HttpRequest.cpp.
References body, and parseUrlEncoded().
Referenced by HttpServer::handleClient().
Here is the call graph for this function:
Here is the caller graph for this function:
|
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().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 153 of file HttpRequest.h.
References headerEnd.
Referenced by HttpServer::handleClient().
Here is the caller graph for this function:
|
inline |
Definition at line 124 of file HttpRequest.h.
References headers.
Referenced by HttpServer::handleClient(), and receive().
Here is the caller graph for this function:
|
inline |
Definition at line 161 of file HttpRequest.h.
References host.
|
inline |
Definition at line 311 of file HttpRequest.h.
References method.
Referenced by HttpServer::handleClient(), and Router::handleRequest().
Here is the caller graph for this function:
|
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 174 of file HttpRequest.cpp.
References method, path, and TRACE.
Referenced by receive().
Here is the caller graph for this function:| std::string HttpRequest::getOutputFilePath | ( | ) | const |
|
inline |
Definition at line 319 of file HttpRequest.h.
References path.
Referenced by HttpFileserver::handle_list_directory(), HttpFileserver::handle_static_request(), HttpServer::handleClient(), and Router::handleRequest().
Here is the caller graph for this function:
|
inline |
|
inline |
Definition at line 335 of file HttpRequest.h.
References query.
Referenced by HttpServer::handleClient().
Here is the caller graph for this function:| const std::unordered_multimap< std::string, std::string > HttpRequest::getQueryParams | ( | ) |
Parse the query string into a key-value map.
Definition at line 393 of file HttpRequest.cpp.
References parseUrlEncoded(), and query.
Referenced by HttpServer::handleClient(), and GpioController::handleGetMultipleGpios().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 444 of file HttpRequest.h.
References rootCACertificate.
|
inline |
Definition at line 344 of file HttpRequest.h.
References tcp.
Referenced by MultipartParser::handleMultipart().
Here is the caller graph for this function:
|
inline |
Definition at line 327 of file HttpRequest.h.
References uri.
Referenced by HttpServer::handleClient().
Here is the caller graph for this function:| 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 343 of file HttpRequest.cpp.
References MultipartParser::handleMultipart().
Referenced by DashboardController::uploadHandler().
Here is the call graph for this function:
Here is the caller graph for this function:
|
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().
Here is the caller graph for this function:
|
inline |
Definition at line 219 of file HttpRequest.h.
References getHeader().
Referenced by HttpServer::handleClient(), and receive().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Returns an empty object if parsing fails.
Definition at line 281 of file HttpRequest.h.
References getBody().
Referenced by setJson().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 239 of file HttpRequest.h.
References bodyTruncated.
Referenced by appendRemainingBody().
Here is the caller graph for this function:
|
private |
Definition at line 160 of file HttpRequest.cpp.
References headers, and HttpParser::parseHeaders().
Referenced by HttpRequest(), and HttpRequest().
Here is the call graph for this function:
Here is the caller graph for this function:| HttpResponse HttpRequest::post | ( | ) |
Definition at line 59 of file HttpRequest.cpp.
References send(), and setMethod().
Here is the call graph for this function:| 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().
Here is the call graph for this function:
|
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().
Here is the call graph for this function:| 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 266 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().
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
Definition at line 194 of file HttpRequest.cpp.
References headerEnd, headers, HTTP_BUFFER_SIZE, HTTP_RECEIVE_TIMEOUT, MAX_HEADER_BYTES, and Tcp::recv().
Referenced by receive().
Here is the call graph for this function:
Here is the caller graph for this function:| HttpResponse HttpRequest::send | ( | ) |
Definition at line 100 of file HttpRequest.cpp.
Referenced by del(), del(), get(), get(), post(), post(), put(), and put().
Here is the caller graph for this function:| HttpRequest & HttpRequest::setAcceptEncoding | ( | const std::string & | encoding | ) |
Definition at line 485 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 473 of file HttpRequest.cpp.
References headers.
Referenced by setJson(), setJson(), and FrameworkManager::warmUp().
Here is the caller graph for this function:
|
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 464 of file HttpRequest.cpp.
References headers.
| HttpRequest & HttpRequest::setHost | ( | const std::string & | host | ) |
Definition at line 452 of file HttpRequest.cpp.
References host.
| HttpRequest & HttpRequest::setJson | ( | const nlohmann::json & | json | ) |
| json | JSON object to set as body. |
Definition at line 153 of file HttpRequest.cpp.
References body, and setHeader().
Here is the call graph for this function:| HttpRequest & HttpRequest::setJson | ( | const std::string & | json | ) |
| json | JSON string to set as body. |
Definition at line 146 of file HttpRequest.cpp.
References body, json(), and setHeader().
Here is the call graph for this function:
|
inline |
|
inline |
Definition at line 302 of file HttpRequest.h.
References path.
Referenced by receive(), and FrameworkManager::warmUp().
Here is the caller graph for this function:| HttpRequest & HttpRequest::setProtocol | ( | const std::string & | protocol | ) |
Definition at line 458 of file HttpRequest.cpp.
References protocol.
|
inlineprivate |
| HttpRequest & HttpRequest::setRootCACertificate | ( | const std::string & | certData | ) |
| certData | PEM-encoded certificate. |
Definition at line 490 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 423 of file HttpRequest.cpp.
References host, protocol, and uri.
Referenced by del(), get(), post(), and put().
Here is the caller graph for this function:| HttpRequest & HttpRequest::setUserAgent | ( | const std::string & | userAgent | ) |
Definition at line 479 of file HttpRequest.cpp.
References headers.
| HttpRequest & HttpRequest::toFile | ( | const std::string & | path | ) |
Definition at line 521 of file HttpRequest.cpp.
References outputFilePath, and path.
| bool HttpRequest::wantsToFile | ( | ) | const |
Definition at line 526 of file HttpRequest.cpp.
References outputFilePath.
|
private |
Definition at line 472 of file HttpRequest.h.
Referenced by appendRemainingBody(), getBody(), getFormParams(), post(), put(), receive(), setBody(), setJson(), and setJson().
|
private |
Definition at line 475 of file HttpRequest.h.
Referenced by isBodyTruncated(), and markBodyTruncated().
|
private |
Definition at line 464 of file HttpRequest.h.
|
private |
Definition at line 474 of file HttpRequest.h.
Referenced by getHeaderEnd(), receiveUntilHeadersComplete(), and setHeaderEnd().
|
private |
Definition at line 471 of file HttpRequest.h.
Referenced by getHeader(), getHeaders(), isFormUrlEncoded(), isJson(), parseHeaders(), printHeaders(), receive(), receiveUntilHeadersComplete(), setAcceptEncoding(), setHeader(), setHeaders(), and setUserAgent().
|
private |
Definition at line 469 of file HttpRequest.h.
|
private |
Definition at line 465 of file HttpRequest.h.
Referenced by getMethod(), getMethodAndPath(), receive(), and setMethod().
|
private |
Definition at line 476 of file HttpRequest.h.
Referenced by getOutputFilePath(), toFile(), and wantsToFile().
|
private |
Definition at line 467 of file HttpRequest.h.
Referenced by getMethodAndPath(), getPath(), HttpRequest(), receive(), setPath(), and toFile().
|
private |
Definition at line 470 of file HttpRequest.h.
Referenced by getProtocol(), setProtocol(), and setUri().
|
private |
Definition at line 468 of file HttpRequest.h.
Referenced by getQuery(), getQueryParams(), HttpRequest(), and setQueryString().
|
private |
Definition at line 473 of file HttpRequest.h.
Referenced by getRootCACertificate(), and setRootCACertificate().
|
private |
Definition at line 462 of file HttpRequest.h.
Referenced by appendRemainingBody(), getTcp(), and receive().
|
private |
Definition at line 466 of file HttpRequest.h.
Referenced by getUri(), HttpRequest(), and setUri().