24#include <unordered_map>
82 void addRoute(
const std::string &method,
83 const std::string &path,
85 std::vector<Middleware> middleware = {});
98 const std::string& method,
99 const std::string& path,
103 simpleHandler(req, res);
123#ifdef PICO_HTTP_ENABLE_JWT
148#ifdef PICO_HTTP_ENABLE_JWT
176 std::unordered_map<std::string, std::vector<Route>>
routes;
185 void withRoutes(
const std::function<
void(std::unordered_map<std::string, std::vector<Route>> &)> &fn);
HTTP file server and file handling helpers for static content.
Defines the HttpRequest class for handling HTTP requests: headers, method, path, query string,...
HTTP HttpResponse class for managing status, headers, body, and streaming support.
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware components.
std::function< void(HttpRequest &, HttpResponse &, const RouteMatch &)> RouteHandler
Function signature for HTTP route handlers.
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware.
HTTP-level controller for serving static files and directory listings.
Forward declaration for potential routing needs.
Represents an HTTP response object.
The central router for handling HTTP requests and middleware.
std::unordered_map< std::string, std::vector< Route > > routes
void withRoutes(const std::function< void(std::unordered_map< std::string, std::vector< Route > > &)> &fn)
bool isAuthorizedForRoute(const Route &route, HttpRequest &req, HttpResponse &res)
Check if a route requires and is granted JWT authorization.
void addCatchAllGetRoute(RouteHandler handler, std::vector< Middleware > middleware={})
Register a catch-all route with optional middleware.
bool extractAuthorizationToken(const std::string &auth_header)
Built-in route handler for /auth token testing.
static StaticSemaphore_t lockBuffer_
HttpFileserver fileServer
Internal file server instance.
void serveStatic(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Serve static files from the internal HttpFileserver.
Route catchallGetRoute
Catch-all route for unmatched requests.
Router()
Construct the router instance.
void printRoutes()
Print all registered routes to stdout.
HttpFileserver & getFileHandler()
Get the file server instance.
Router & operator=(const Router &)=delete
std::string getAuthorizationToken(const HttpRequest &req)
Returns the cached Authorization token, or extracts it from the request.
bool handleRequest(HttpRequest &req, HttpResponse &res)
Handle an incoming HTTP request.
bool hasCatchallGetRoute
Flag to indicate if a catch-all route exists.
void addRoute(Router &router, const std::string &method, const std::string &path, std::function< void(HttpRequest &, HttpResponse &)> simpleHandler)
void listDirectory(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Convenience method to list directory from the internal HttpFileserver.
std::vector< Middleware > globalMiddleware
void addRoute(const std::string &method, const std::string &path, RouteHandler handler, std::vector< Middleware > middleware={})
Register a route with optional middleware.
std::string cached_token
Cached Bearer token.
void use(Middleware middleware)
Register a global middleware function.
Router(const Router &)=delete
Represents a match of a route against an incoming HTTP request.
Represents a single HTTP route.