HTTP Server that listens for incoming connections and dispatches requests. More...
#include <HttpServer.h>
Public Member Functions | |
HttpServer (int port, Router &router) | |
Construct a new HttpServer instance. | |
void | enableTLS (const std::string &certPem, const std::string &keyPem) |
Enable TLS/HTTPS support with certificate and private key. | |
bool | isTLSEnabled () const |
Check if TLS is enabled for this server. | |
bool | start () |
Start the HTTP server as a FreeRTOS task. | |
bool | initNetwork () |
Initialize the network stack (wait for DHCP or static IP). | |
void | run () |
Main server loop: initializes, binds, and begins accepting connections. | |
Tcp * | initListener () |
Create, bind, and listen on the server. | |
void | handleClient (Tcp *conn) |
Accept a client connection and handle it directly (not task-based). | |
void | startHandlingClient (Tcp *conn) |
Spawn a task to handle the client connection. | |
HttpRequest | receiveRequest (Tcp *conn, char *method, char *path, std::string &body, size_t &contentLength, std::unordered_map< std::string, std::string > &headers) |
Receive an HTTP request and parse key components. | |
Router & | getRouter () |
Return the router associated with the server. | |
Static Public Member Functions | |
static void | startServerTask (void *pvParameters) |
Launch the HTTP server task (used by FreeRTOS). | |
static void | handleClientTask (void *pvParameters) |
Handle client logic inside a FreeRTOS task. | |
Private Attributes | |
int | port |
Accept client connections in a blocking loop and spawn handlers. | |
Router & | router |
Reference to router for dispatching requests. | |
Tcp | listener |
bool | tlsEnabled = false |
std::string | serverCert |
std::string | serverKey |
Static Private Attributes | |
static constexpr int | BUFFER_SIZE = 1460 |
static constexpr int | BOUNDARY_MAX_LEN = 128 |
static StackType_t | xStack [HTTP_STACK_SIZE] |
Stack for static FreeRTOS task. | |
static StaticTask_t | xTaskBuffer |
Task control block buffer. | |
Definition at line 28 of file HttpServer.h.
HttpServer::HttpServer | ( | int | port, |
Router & | router | ||
) |
Construct a new HttpServer instance.
port | Port number to listen on. |
router | Reference to the Router for handling requests. |
port | Port number to listen on. |
router | Reference to the Router for handling requests. |
Definition at line 78 of file HttpServer.cpp.
void HttpServer::enableTLS | ( | const std::string & | certPem, |
const std::string & | keyPem | ||
) |
Enable TLS/HTTPS support with certificate and private key.
certPem | Server certificate in PEM format |
keyPem | Private key in PEM format |
certPem | Server certificate in PEM format |
keyPem | Private key in PEM format |
Definition at line 84 of file HttpServer.cpp.
References serverCert, serverKey, and tlsEnabled.
|
inline |
void HttpServer::handleClient | ( | Tcp * | conn | ) |
Accept a client connection and handle it directly (not task-based).
Tcp* TCP connection instance
Tcp* TCP connection instance
Definition at line 245 of file HttpServer.cpp.
References Tcp::close(), HttpRequest::getBody(), HttpRequest::getBoundary(), HttpRequest::getContentLength(), HttpRequest::getContentType(), HttpRequest::getCookies(), HttpRequest::getFormParams(), HttpRequest::getHeader(), HttpRequest::getHeaderEnd(), HttpRequest::getHeaders(), HttpRequest::getMethod(), HttpRequest::getPath(), HttpRequest::getQuery(), HttpRequest::getQueryParams(), HttpRequest::getUri(), Router::handleRequest(), HTTP_IDLE_TIMEOUT, HttpRequest::isMultipart(), QUIET_PRINTF, HttpRequest::receive(), router, JsonResponse::sendError(), HttpResponse::setHeader(), start(), and TRACE.
Referenced by startHandlingClient().
|
static |
pvParameters | Pointer to TaskParams (including Tcp*) |
Referenced by startHandlingClient().
Tcp * HttpServer::initListener | ( | ) |
Definition at line 192 of file HttpServer.cpp.
References Tcp::bindAndListen(), AppContext::getTypeKey(), HttpServerStarted, listener, port, serverCert, serverKey, Tcp::setServerTlsConfig(), and tlsEnabled.
Referenced by run().
bool HttpServer::initNetwork | ( | ) |
Initialize the network stack (wait for DHCP or static IP).
Definition at line 170 of file HttpServer.cpp.
Referenced by run().
bool HttpServer::isTLSEnabled | ( | ) | const |
Check if TLS is enabled for this server.
Definition at line 93 of file HttpServer.cpp.
References tlsEnabled.
HttpRequest HttpServer::receiveRequest | ( | Tcp * | conn, |
char * | method, | ||
char * | path, | ||
std::string & | body, | ||
size_t & | contentLength, | ||
std::unordered_map< std::string, std::string > & | headers | ||
) |
Tcp* | TCP connection instance |
method | Output buffer for method. |
path | Output buffer for path. |
body | Output body content. |
contentLength | Output content length. |
headers | Output map of parsed headers. |
void HttpServer::run | ( | ) |
Main server loop: initializes, binds, and begins accepting connections.
Definition at line 118 of file HttpServer.cpp.
References Tcp::accept(), AppContext::getTypeKey(), HttpServerStarted, initListener(), initNetwork(), listener, port, QUIET_PRINTF, startHandlingClient(), tlsEnabled, TRACE, and warning().
Referenced by startServerTask().
bool HttpServer::start | ( | ) |
Start the HTTP server as a FreeRTOS task.
Definition at line 99 of file HttpServer.cpp.
References clientSemaphore, HTTP_STACK_SIZE, MAX_CONCURRENT_CLIENTS, startServerTask(), xStack, and xTaskBuffer.
Referenced by handleClient(), and App::onEvent().
void HttpServer::startHandlingClient | ( | Tcp * | conn | ) |
Tcp* | TCP connection instance. |
Definition at line 213 of file HttpServer.cpp.
References clientSemaphore, Tcp::close(), handleClient(), handleClientTask(), and TRACE.
Referenced by run().
|
static |
Launch the HTTP server task (used by FreeRTOS).
pvParameters | Pointer to HttpServer instance. |
pvParameters | Pointer to HttpServer instance. |
Definition at line 106 of file HttpServer.cpp.
References run().
Referenced by start().
|
staticconstexprprivate |
Definition at line 134 of file HttpServer.h.
|
staticconstexprprivate |
Definition at line 133 of file HttpServer.h.
|
private |
Definition at line 127 of file HttpServer.h.
Referenced by initListener(), and run().
|
private |
Port number to listen on.
Definition at line 124 of file HttpServer.h.
Referenced by initListener(), and run().
|
private |
Definition at line 125 of file HttpServer.h.
Referenced by getRouter(), and handleClient().
|
private |
Definition at line 131 of file HttpServer.h.
Referenced by enableTLS(), and initListener().
|
private |
Definition at line 132 of file HttpServer.h.
Referenced by enableTLS(), and initListener().
|
private |
Definition at line 130 of file HttpServer.h.
Referenced by enableTLS(), initListener(), isTLSEnabled(), and run().
|
staticprivate |
Definition at line 136 of file HttpServer.h.
Referenced by start().
|
staticprivate |
Definition at line 137 of file HttpServer.h.
Referenced by start().