40#ifndef DETERMINISTICESPASYNCWEBSERVER_H
41#define DETERMINISTICESPASYNCWEBSERVER_H
45#if DETWS_ENABLE_WEBSOCKET
51#if DETWS_ENABLE_MULTIPART
55#if DETWS_ENABLE_FILE_SERVING
105#if DETWS_ENABLE_WEBSOCKET
111typedef void (*WsConnectHandler)(uint8_t ws_id);
121typedef void (*WsMessageHandler)(uint8_t ws_id);
128typedef void (*WsCloseHandler)(uint8_t ws_id);
139typedef void (*SseConnectHandler)(uint8_t sse_id);
150#if DETWS_ENABLE_WEBSOCKET
171#if DETWS_ENABLE_WEBSOCKET
172 WsConnectHandler ws_connect;
173 WsMessageHandler ws_message;
178 SseConnectHandler sse_connect;
231 uint8_t _route_count;
255 static bool path_matches(
const char *route,
bool is_wildcard,
const char *req_path);
258 static bool check_basic_auth(uint8_t slot_id,
HttpReq *req,
const Route *r);
259 void send_unauth(uint8_t slot_id,
const char *realm);
271 void match_and_execute(uint8_t slot_id);
380 const char *realm,
const char *user,
const char *pass);
383#if DETWS_ENABLE_FILE_SERVING
397 void serve_file(uint8_t slot_id, fs::FS &file_sys,
398 const char *fs_path,
const char *content_type);
454 void send(uint8_t slot_id,
int code,
const char *content_type,
const char *payload);
478 void diag(uint8_t slot_id);
481#if DETWS_ENABLE_WEBSOCKET
501 void on_ws(
const char *path,
502 WsConnectHandler on_connect,
503 WsMessageHandler on_message,
504 WsCloseHandler on_close);
512 void ws_send_text(uint8_t ws_id,
const char *text);
521 void ws_send_binary(uint8_t ws_id,
const uint8_t *data, uint16_t len);
531 void ws_disconnect(uint8_t ws_id);
549 void on_sse(
const char *path, SseConnectHandler on_connect);
563 void sse_send(uint8_t sse_id,
const char *data,
564 const char *event =
nullptr,
const char *
id =
nullptr);
577 void sse_broadcast(
const char *path,
const char *data,
578 const char *event =
nullptr,
const char *
id =
nullptr);
#define MAX_PATH_LEN
Maximum URL path length (including leading /).
#define MAX_AUTH_LEN
Maximum username or password length for HTTP Basic Authentication.
#define CORS_HDR_BUF_SIZE
Size of the pre-built CORS header block stored in DetWebServer.
#define MAX_ROUTES
Maximum simultaneously registered routes.
RouteType
Discriminates between HTTP, WebSocket, and SSE route entries.
@ ROUTE_HTTP
Standard HTTP request/response.
HttpMethod
HTTP request methods supported by the router.
@ HTTP_PUT
Idempotent replace.
@ HTTP_DELETE
Idempotent delete.
@ HTTP_GET
Safe, idempotent read.
@ HTTP_OPTIONS
Capability query / CORS preflight.
@ HTTP_POST
Non-idempotent create / action.
@ HTTP_HEAD
Same as GET but no response body.
@ HTTP_PATCH
Partial update.
void(* Handler)(uint8_t slot_id, HttpReq *request)
Callback signature for HTTP request handlers.
Single-port HTTP server with deterministic, zero-allocation execution.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server — call every Arduino loop() iteration.
int32_t restart(const WebServerConfig *cfg=nullptr)
Hard-reset all connections and reinitialise the server on the same port that was passed to begin().
int32_t begin(uint16_t port, const WebServerConfig *cfg=nullptr)
Initialise all connection slots and open the TCP listener.
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
void on_not_found(Handler callback)
Register a fallback handler for unmatched requests.
void send_empty(uint8_t slot_id, int code)
Send a headers-only HTTP response and close the connection.
void set_cors(const char *origin)
Enable CORS by pre-building the Access-Control headers.
static bool heap_available()
True if the largest contiguous free heap block >= heap_needed().
static size_t heap_needed()
Bytes of contiguous heap that begin() will allocate.
DetWebServer()
Construct a DetWebServer with an empty routing table.
void stop()
Gracefully stop the server.
In-place multipart/form-data parser (RFC 7578).
Layer 6 (Presentation) — wires the transport ring buffer to the HTTP parser.
Layer 5 (Session) — event queue dispatcher and session lifecycle.
Layer 6 (Presentation) – Server-Sent Events connection pool.
Fully-parsed HTTP/1.1 request.
Internal route entry stored in the routing table.
RouteType type
HTTP, WS, or SSE.
Handler callback
HTTP handler (ROUTE_HTTP only).
HttpMethod method
HTTP method (ROUTE_HTTP only).
bool is_active
false for unused table slots.
bool is_wildcard
true when path ends with *.
char path[MAX_PATH_LEN]
Null-terminated path pattern.
Runtime-tunable server parameters.
void ws_close(WsConn *ws, WsCloseCode code)
Send a Close frame and mark the slot WS_CLOSED.
Layer 6 (Presentation) – WebSocket frame parser and connection pool.