std1.0.0-beta-7 SafeC docs

std :: http::router

Struct std::Router

#include <std/http/router.h> source

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

§struct Vec<struct HttpRoute_> routessource
§struct Vec<Middleware> middlewaresource

set_not_found; plain 404 when unset

§int has_not_foundsource
§struct StaticOptions static_optssource

for static_dir routes

§struct String cors_originsource

Built-in middleware settings (use_cors / use_body_limit / use_gzip).

§struct String cors_methodssource
§struct String cors_headerssource
§unsigned long body_limitsource
§unsigned long gzip_minsource

set_state / router_state (erased, see std/erased.h)

Methods

§static struct Router new()source · definition
§void route(const &char method, const &char pattern, RouteHandler handler)source · definition

Registers 'handler' for 'method' ("GET", ...; "*" = any) and 'pattern'. Later registrations of the same method+pattern replace earlier ones.

§void get(const &char pattern, RouteHandler handler)source · definition
§void post(const &char pattern, RouteHandler handler)source · definition
§void put(const &char pattern, RouteHandler handler)source · definition
§void patch(const &char pattern, RouteHandler handler)source · definition
§void del(const &char pattern, RouteHandler handler)source · definition

DELETE

§void options(const &char pattern, RouteHandler handler)source · definition
§void any(const &char pattern, RouteHandler handler)source · definition
§void static_dir(const &char prefix, const &char dir)source · definition

Serves files under 'dir' for GET/HEAD below URL 'prefix' ("/assets" -> "/assets/*"), through http_serve_dir with static_opts.

§void set_not_found(RouteHandler handler)source · definition

Appends a middleware to the chain.

§void use_logger()source · definition

Built-ins: one log line per request through std/log.h (LOG_LEVEL_INFO, tag "http"): "GET /path -> 200 (1.2 ms, 512 B)".

§void use_cors(const &char origin, const &char methods, const &char headers)source · definition

CORS: answers preflights (std/http/cors.h) and adds Access-Control-Allow-Origin & co. to every response.

§void use_body_limit(unsigned long max_bytes)source · definition

413 for request bodies over 'max_bytes' (on top of the server's own max_body_bytes).

§void use_gzip(unsigned long min_bytes)source · definition

gzip-compresses compressible responses of at least 'min_bytes' when the client sends Accept-Encoding: gzip (adds Vary: Accept-Encoding).

§void use_status_guard()source · definition

Recovers nothing (SafeC has no exceptions) but turns a handler's status 0 response into 500 — a cheap guard for "forgot to set status".

§struct HttpResponse dispatch(&HttpRequest req, &HttpConn conn)source · definition

Runs the chain for one request (what the server calls; also handy in tests: build an HttpRequest::new and an HttpConn by hand).

§int serve(&HttpServer srv)source · definition

Serves this router on a bound server (blocking / background). The Router must outlive the server.

§int start(&HttpServer srv)source · definition
§int listen(const &HttpServerConfig cfg)source · definition

HttpServer::bind + serve; -1 if the bind fails.

§void drop()source · definition
§void free() consumesource · definition

= drop()