26 _middleware[_middleware_count++] = mw;
31bool DetWebServer::run_middleware(uint8_t slot_id,
HttpReq *req)
33 for (uint8_t i = 0; i < _middleware_count; i++)
43 _rl_max = max_requests;
44 _rl_window_ms = window_ms;
45 _rl_window_start = millis();
51bool DetWebServer::rate_limit_check(uint8_t slot_id)
53 if (_rl_max == 0 || _rl_window_ms == 0)
56 uint32_t now = millis();
57 if ((uint32_t)(now - _rl_window_start) >= _rl_window_ms)
59 _rl_window_start = now;
64 if (_rl_count <= _rl_max)
68 uint32_t elapsed = (uint32_t)(now - _rl_window_start);
69 uint32_t remain_ms = (_rl_window_ms > elapsed) ? (_rl_window_ms - elapsed) : 0;
71 snprintf(secs,
sizeof(secs),
"%lu", (
unsigned long)((remain_ms + 999) / 1000));
73 send(slot_id, 429, DET_MIME_TEXT_PLAIN,
"Too Many Requests");
#define MAX_MIDDLEWARE
Maximum globally-registered middleware functions.
void add_response_header(uint8_t slot_id, const char *name, const char *value)
Queue a custom response header for the next send on this slot.
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 use(Middleware mw)
Register a middleware to run before every request is dispatched.
void enable_rate_limit(uint16_t max_requests, uint32_t window_ms)
Enable a built-in fixed-window request rate limiter.
Layer 7 (Application) - public HTTP routing API.
MwResult(* Middleware)(uint8_t slot_id, HttpReq *request)
Composable pre-dispatch middleware (see DetWebServer::use()).
@ MW_HALT
Stop dispatch; the middleware already sent a response.
Shared HTTP Content-Type ("MIME") string constants (one source of truth).
Fully-parsed HTTP/1.1 request.