The central router for handling HTTP requests and middleware. More...
#include <Router.h>
Collaboration diagram for Router:Public Member Functions | |
| Router () | |
| Construct the router instance. | |
| Router (const Router &)=delete | |
| Router & | operator= (const Router &)=delete |
| void | use (Middleware middleware) |
| Register a global middleware function. | |
| void | addRoute (const std::string &method, const std::string &path, RouteHandler handler, std::vector< Middleware > middleware={}) |
| Register a route with optional middleware. | |
| void | addCatchAllGetRoute (RouteHandler handler, std::vector< Middleware > middleware={}) |
| Register a catch-all route with optional middleware. | |
| void | addRoute (Router &router, const std::string &method, const std::string &path, std::function< void(HttpRequest &, HttpResponse &)> simpleHandler) |
| bool | handleRequest (HttpRequest &req, HttpResponse &res) |
| Handle an incoming HTTP request. | |
| bool | extractAuthorizationToken (const std::string &auth_header) |
| Built-in route handler for /auth token testing. | |
| std::string | getAuthorizationToken (const HttpRequest &req) |
| Returns the cached Authorization token, or extracts it from the request. | |
| bool | isAuthorizedForRoute (const Route &route, HttpRequest &req, HttpResponse &res) |
| Check if a route requires and is granted JWT authorization. | |
| void | printRoutes () |
| Print all registered routes to stdout. | |
| HttpFileserver & | getFileHandler () |
| Get the file server instance. | |
| void | serveStatic (HttpRequest &req, HttpResponse &res, const RouteMatch &match) |
| Serve static files from the internal HttpFileserver. | |
| void | listDirectory (HttpRequest &req, HttpResponse &res, const RouteMatch &match) |
| Convenience method to list directory from the internal HttpFileserver. | |
Private Member Functions | |
| void | withRoutes (const std::function< void(std::unordered_map< std::string, std::vector< Route > > &)> &fn) |
Private Attributes | |
| HttpFileserver | fileServer |
| Internal file server instance. | |
| std::unordered_map< std::string, std::vector< Route > > | routes |
| Route | catchallGetRoute |
| Catch-all route for unmatched requests. | |
| bool | hasCatchallGetRoute = false |
| Flag to indicate if a catch-all route exists. | |
| std::string | cached_token |
| Cached Bearer token. | |
| std::vector< Middleware > | globalMiddleware |
| SemaphoreHandle_t | lock_ = xSemaphoreCreateRecursiveMutexStatic(&lockBuffer_) |
Static Private Attributes | |
| static StaticSemaphore_t | lockBuffer_ |
Supports:
| Router::Router | ( | ) |
Construct the router instance.
Definition at line 61 of file Router.cpp.
References configASSERT, and lock_.
|
delete |
| void Router::addCatchAllGetRoute | ( | RouteHandler | handler, |
| std::vector< Middleware > | middleware = {} |
||
| ) |
| method | HTTP method |
| path | URL path (can include {params}) |
| handler | Handler to invoke on match |
| middleware | Optional list of middleware specific to this route |
Definition at line 203 of file Router.cpp.
References catchallGetRoute, globalMiddleware, hasCatchallGetRoute, TRACE, and withRoutes().
Referenced by DashboardController::initRoutes().
Here is the call graph for this function:
Here is the caller graph for this function:| void Router::addRoute | ( | const std::string & | method, |
| const std::string & | path, | ||
| RouteHandler | handler, | ||
| std::vector< Middleware > | middleware = {} |
||
| ) |
Check if a route requires and is granted JWT authorization.
| method | HTTP method |
| path | URL path (can include {params}) |
| handler | Handler to invoke on match |
| middleware | Optional list of middleware specific to this route |
| route | The matched route |
| req | HTTP request |
| res | HTTP response |
Register a route with optional middleware.
Check if a route requires and is granted JWT authorization.
| method | HTTP method |
| path | URL path (can include {params}) |
| handler | Handler to invoke on match |
| middleware | Optional list of middleware specific to this route |
| route | The matched route |
| req | HTTP request |
| res | HTTP response |
Definition at line 144 of file Router.cpp.
References globalMiddleware, TRACE, and withRoutes().
Referenced by addRoute(), App::initRoutes(), DashboardController::initRoutes(), and GpioController::initRoutes().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
Definition at line 97 of file Router.h.
References addRoute().
Here is the call graph for this function:| bool Router::extractAuthorizationToken | ( | const std::string & | auth_header | ) |
| req | Incoming request |
| res | HttpResponse to send |
| params | Unused in this handler |
Extracts and caches a Bearer token from an Authorization header.
| auth_header | Raw header string |
| std::string Router::getAuthorizationToken | ( | const HttpRequest & | req | ) |
Returns the cached Authorization token, or extracts it from the request.
| req | The incoming request |
| req | The incoming request |
Definition at line 69 of file Router.cpp.
References extractBearerToken(), HttpRequest::getHeader(), and TRACE.
Here is the call graph for this function:
|
inline |
Definition at line 162 of file Router.h.
References fileServer.
| bool Router::handleRequest | ( | HttpRequest & | req, |
| HttpResponse & | res | ||
| ) |
| req | Fully parsed HttpRequest object |
| res | HttpResponse to send |
Definition at line 228 of file Router.cpp.
References catchallGetRoute, Route::compiledRegex, HttpRequest::getMethod(), HttpRequest::getPath(), Route::handler, hasCatchallGetRoute, RouteMatch::named, RouteMatch::ordered, Route::paramNames, Route::path, printRoutes(), TRACE, urlDecode(), and withRoutes().
Referenced by HttpServer::handleClient().
Here is the call graph for this function:
Here is the caller graph for this function:
|
inline |
| void Router::listDirectory | ( | HttpRequest & | req, |
| HttpResponse & | res, | ||
| const RouteMatch & | match | ||
| ) |
Convenience method to list directory from the internal HttpFileserver.
Definition at line 321 of file Router.cpp.
References fileServer, and HttpFileserver::handle_list_directory().
Referenced by DashboardController::initRoutes().
Here is the call graph for this function:
Here is the caller graph for this function:| void Router::printRoutes | ( | ) |
Definition at line 294 of file Router.cpp.
References Route::isDynamic, Route::path, Route::requiresAuth, routes, TRACE, and withRoutes().
Referenced by handleRequest().
Here is the call graph for this function:
Here is the caller graph for this function:| void Router::serveStatic | ( | HttpRequest & | req, |
| HttpResponse & | res, | ||
| const RouteMatch & | match | ||
| ) |
Serve static files from the internal HttpFileserver.
Definition at line 314 of file Router.cpp.
References fileServer, and HttpFileserver::handle_static_request().
Referenced by DashboardController::initRoutes().
Here is the call graph for this function:
Here is the caller graph for this function:| void Router::use | ( | Middleware | middleware | ) |
| middleware | Middleware to apply to all routes. |
Register a global middleware function.
| middleware | Middleware to apply to all routes. |
Definition at line 110 of file Router.cpp.
References globalMiddleware.
|
private |
Definition at line 328 of file Router.cpp.
Referenced by addCatchAllGetRoute(), addRoute(), handleRequest(), and printRoutes().
Here is the caller graph for this function:
|
private |
Definition at line 177 of file Router.h.
Referenced by addCatchAllGetRoute(), and handleRequest().
|
private |
Definition at line 175 of file Router.h.
Referenced by getFileHandler(), listDirectory(), and serveStatic().
|
private |
Definition at line 180 of file Router.h.
Referenced by addCatchAllGetRoute(), addRoute(), and use().
|
private |
Definition at line 178 of file Router.h.
Referenced by addCatchAllGetRoute(), and handleRequest().
|
private |
Definition at line 183 of file Router.h.
Referenced by Router(), and withRoutes().
|
private |
Definition at line 176 of file Router.h.
Referenced by printRoutes(), and withRoutes().