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. | |
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 |
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.
|
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 219 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 176 of file HttpServer.cpp.
References Tcp::bindAndListen(), AppContext::getTypeKey(), HttpServerStarted, listener, and port.
Referenced by run().
bool HttpServer::initNetwork | ( | ) |
Initialize the network stack (wait for DHCP or static IP).
Definition at line 154 of file HttpServer.cpp.
Referenced by run().
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 103 of file HttpServer.cpp.
References Tcp::accept(), AppContext::getTypeKey(), HttpServerStarted, initListener(), initNetwork(), listener, port, QUIET_PRINTF, startHandlingClient(), TRACE, and warning().
Referenced by startServerTask().
bool HttpServer::start | ( | ) |
Start the HTTP server as a FreeRTOS task.
Definition at line 84 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 187 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 91 of file HttpServer.cpp.
References run().
Referenced by start().
|
staticconstexprprivate |
Definition at line 115 of file HttpServer.h.
|
staticconstexprprivate |
Definition at line 114 of file HttpServer.h.
|
private |
Definition at line 113 of file HttpServer.h.
Referenced by initListener(), and run().
|
private |
Port number to listen on.
Definition at line 110 of file HttpServer.h.
Referenced by initListener(), and run().
|
private |
Definition at line 111 of file HttpServer.h.
Referenced by getRouter(), and handleClient().
|
staticprivate |
Definition at line 117 of file HttpServer.h.
Referenced by start().
|
staticprivate |
Definition at line 118 of file HttpServer.h.
Referenced by start().