Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
HttpServer.h
Go to the documentation of this file.
1
13#ifndef HTTP_SERVER_H
14#define HTTP_SERVER_H
15#pragma once
16
17#include <string>
18#include <unordered_map>
19#include "Router.h"
20#include "FreeRTOS.h"
21#include "task.h"
22
23#define HTTP_STACK_SIZE 10 * 1024 / sizeof(StackType_t) // Stack size for FreeRTOS task
24
29{
30public:
37
44 void enableTLS(const std::string& certPem, const std::string& keyPem);
45
50 bool isTLSEnabled() const;
51
56 bool start();
57
62 bool initNetwork();
63
67 void run();
68
73 static void startServerTask(void *pvParameters);
74
80
85 void handleClient(Tcp *conn);
86
91 void startHandlingClient(Tcp* conn);
92
103 HttpRequest receiveRequest(Tcp* conn, char* method, char* path, std::string& body,
104 size_t& contentLength, std::unordered_map<std::string, std::string>& headers);
105
110 Router &getRouter() { return router; }
111
116 static void handleClientTask(void *pvParameters);
117
121 //void acceptClientConnections(); // Handled in run()
122
123private:
124 int port;
126
127 Tcp listener; // Listening insance of Tcp class
128
129 // TLS configuration
130 bool tlsEnabled = false;
131 std::string serverCert;
132 std::string serverKey;
133 static constexpr int BUFFER_SIZE = 1460;
134 static constexpr int BOUNDARY_MAX_LEN = 128;
135
136 static StackType_t xStack[HTTP_STACK_SIZE];
137 static StaticTask_t xTaskBuffer;
138};
139
140#endif // HTTP_SERVER_H
#define HTTP_STACK_SIZE
Definition HttpServer.h:23
HTTP routing with middleware and optional JWT-based authorization. Part of the PicoFramework HTTP ser...
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
HTTP Server that listens for incoming connections and dispatches requests.
Definition HttpServer.h:29
bool initNetwork()
Initialize the network stack (wait for DHCP or static IP).
static void startServerTask(void *pvParameters)
Launch the HTTP server task (used by FreeRTOS).
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.
static constexpr int BUFFER_SIZE
Definition HttpServer.h:133
void enableTLS(const std::string &certPem, const std::string &keyPem)
Enable TLS/HTTPS support with certificate and private key.
void handleClient(Tcp *conn)
Accept a client connection and handle it directly (not task-based).
bool tlsEnabled
Definition HttpServer.h:130
static StackType_t xStack[HTTP_STACK_SIZE]
Stack for static FreeRTOS task.
Definition HttpServer.h:136
static constexpr int BOUNDARY_MAX_LEN
Definition HttpServer.h:134
Router & getRouter()
Return the router associated with the server.
Definition HttpServer.h:110
Tcp * initListener()
Create, bind, and listen on the server.
bool isTLSEnabled() const
Check if TLS is enabled for this server.
int port
Accept client connections in a blocking loop and spawn handlers.
Definition HttpServer.h:124
void startHandlingClient(Tcp *conn)
Spawn a task to handle the client connection.
std::string serverCert
Definition HttpServer.h:131
Router & router
Reference to router for dispatching requests.
Definition HttpServer.h:125
static void handleClientTask(void *pvParameters)
Handle client logic inside a FreeRTOS task.
std::string serverKey
Definition HttpServer.h:132
static StaticTask_t xTaskBuffer
Task control block buffer.
Definition HttpServer.h:137
bool start()
Start the HTTP server as a FreeRTOS task.
void run()
Main server loop: initializes, binds, and begins accepting connections.
The central router for handling HTTP requests and middleware.
Definition Router.h:60
General-purpose TCP socket wrapper with optional TLS support via mbedTLS (altcp).
Definition Tcp.h:39