std :: http::router
Struct std::Router
struct Router {
struct Vec<struct HttpRoute_> routes;
struct Vec<Middleware> middleware;
RouteHandler not_found;
int has_not_found;
struct StaticOptions static_opts;
struct String cors_origin;
struct String cors_methods;
struct String cors_headers;
unsigned long body_limit;
unsigned long gzip_min;
?&Erased state;
}Fields
set_not_found; plain 404 when unset
for static_dir routes
Built-in middleware settings (use_cors / use_body_limit / use_gzip).
set_state / router_state (erased, see std/erased.h)
Methods
Registers 'handler' for 'method' ("GET", ...; "*" = any) and 'pattern'. Later registrations of the same method+pattern replace earlier ones.
DELETE
Serves files under 'dir' for GET/HEAD below URL 'prefix' ("/assets" -> "/assets/*"), through http_serve_dir with static_opts.
Appends a middleware to the chain.
Built-ins: one log line per request through std/log.h (LOG_LEVEL_INFO, tag "http"): "GET /path -> 200 (1.2 ms, 512 B)".
CORS: answers preflights (std/http/cors.h) and adds Access-Control-Allow-Origin & co. to every response.
413 for request bodies over 'max_bytes' (on top of the server's own max_body_bytes).
gzip-compresses compressible responses of at least 'min_bytes' when the client sends Accept-Encoding: gzip (adds Vary: Accept-Encoding).
Recovers nothing (SafeC has no exceptions) but turns a handler's status 0 response into 500 — a cheap guard for "forgot to set status".
Runs the chain for one request (what the server calls; also handy in tests: build an HttpRequest::new and an HttpConn by hand).
Serves this router on a bound server (blocking / background). The Router must outlive the server.
HttpServer::bind + serve; -1 if the bind fails.
= drop()