Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Router.h
Go to the documentation of this file.
1
18#ifndef ROUTER_HPP
19#define ROUTER_HPP
20#pragma once
21
22#include <vector>
23#include <string>
24#include <unordered_map>
25#include <functional>
26#include <FreeRTOS.h>
27#include <semphr.h>
28#include "http/RouteTypes.h" // for RouteMatch
29#include "http/HttpRequest.h"
30#include "http/HttpResponse.h"
31#include "http/HttpFileserver.h" // for HttpFileserver
32
33
41using RouteHandler = std::function<void(HttpRequest &, HttpResponse &, const RouteMatch &)>;
42
48using Middleware = std::function<bool(HttpRequest &, HttpResponse &, const RouteMatch &)>;
49
59class Router
60{
61public:
65 Router();
66 Router(const Router&) = delete;
67 Router& operator=(const Router&) = delete;
68
73 void use(Middleware middleware);
74
82 void addRoute(const std::string &method,
83 const std::string &path,
84 RouteHandler handler,
85 std::vector<Middleware> middleware = {});
86
94 void addCatchAllGetRoute(RouteHandler handler, std::vector<Middleware> middleware = {});
95
96 // Optional 2-argument route handler overload for convenience
97 inline void addRoute(Router& router,
98 const std::string& method,
99 const std::string& path,
100 std::function<void(HttpRequest&, HttpResponse&)> simpleHandler) {
101 router.addRoute(method, path,
102 [=](HttpRequest& req, HttpResponse& res, const RouteMatch&) {
103 simpleHandler(req, res);
104 });
105 }
106
114 bool handleRequest(HttpRequest &req, HttpResponse &res);
115
123#ifdef PICO_HTTP_ENABLE_JWT
124 bool handle_auth_req(HttpRequest &req, HttpResponse &res, const std::vector<std::string> &params);
125#endif
126
132 bool extractAuthorizationToken(const std::string &auth_header);
133
139 std::string getAuthorizationToken(const HttpRequest &req);
140
148#ifdef PICO_HTTP_ENABLE_JWT
149 bool isAuthorizedForRoute(const Route &route, HttpRequest &req, HttpResponse &res);
150#else
151 bool isAuthorizedForRoute(const Route &route, HttpRequest &req, HttpResponse &res) { return true; }
152#endif
156 void printRoutes();
157
163
167 void serveStatic(HttpRequest &req, HttpResponse &res, const RouteMatch &match);
168
172 void listDirectory(HttpRequest &req, HttpResponse &res, const RouteMatch &match);
173
174private:
176 std::unordered_map<std::string, std::vector<Route>> routes;
178 bool hasCatchallGetRoute = false;
179 std::string cached_token;
180 std::vector<Middleware> globalMiddleware;
181
182 static StaticSemaphore_t lockBuffer_;
183 SemaphoreHandle_t lock_ = xSemaphoreCreateRecursiveMutexStatic(&lockBuffer_);
184
185 void withRoutes(const std::function<void(std::unordered_map<std::string, std::vector<Route>> &)> &fn);
186};
187
188#endif // ROUTER_HPP
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.
Definition Middleware.h:50
std::function< void(HttpRequest &, HttpResponse &, const RouteMatch &)> RouteHandler
Function signature for HTTP route handlers.
Definition Router.h:41
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware.
Definition Router.h:48
HTTP-level controller for serving static files and directory listings.
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
Represents an HTTP response object.
The central router for handling HTTP requests and middleware.
Definition Router.h:60
std::unordered_map< std::string, std::vector< Route > > routes
Definition Router.h:176
void withRoutes(const std::function< void(std::unordered_map< std::string, std::vector< Route > > &)> &fn)
Definition Router.cpp:328
bool isAuthorizedForRoute(const Route &route, HttpRequest &req, HttpResponse &res)
Check if a route requires and is granted JWT authorization.
Definition Router.h:151
void addCatchAllGetRoute(RouteHandler handler, std::vector< Middleware > middleware={})
Register a catch-all route with optional middleware.
Definition Router.cpp:203
bool extractAuthorizationToken(const std::string &auth_header)
Built-in route handler for /auth token testing.
SemaphoreHandle_t lock_
Definition Router.h:183
static StaticSemaphore_t lockBuffer_
Definition Router.h:182
HttpFileserver fileServer
Internal file server instance.
Definition Router.h:175
void serveStatic(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Serve static files from the internal HttpFileserver.
Definition Router.cpp:314
Route catchallGetRoute
Catch-all route for unmatched requests.
Definition Router.h:177
Router()
Construct the router instance.
Definition Router.cpp:61
void printRoutes()
Print all registered routes to stdout.
Definition Router.cpp:294
HttpFileserver & getFileHandler()
Get the file server instance.
Definition Router.h:162
Router & operator=(const Router &)=delete
std::string getAuthorizationToken(const HttpRequest &req)
Returns the cached Authorization token, or extracts it from the request.
Definition Router.cpp:69
bool handleRequest(HttpRequest &req, HttpResponse &res)
Handle an incoming HTTP request.
Definition Router.cpp:228
bool hasCatchallGetRoute
Flag to indicate if a catch-all route exists.
Definition Router.h:178
void addRoute(Router &router, const std::string &method, const std::string &path, std::function< void(HttpRequest &, HttpResponse &)> simpleHandler)
Definition Router.h:97
void listDirectory(HttpRequest &req, HttpResponse &res, const RouteMatch &match)
Convenience method to list directory from the internal HttpFileserver.
Definition Router.cpp:321
std::vector< Middleware > globalMiddleware
Definition Router.h:180
void addRoute(const std::string &method, const std::string &path, RouteHandler handler, std::vector< Middleware > middleware={})
Register a route with optional middleware.
Definition Router.cpp:144
std::string cached_token
Cached Bearer token.
Definition Router.h:179
void use(Middleware middleware)
Register a global middleware function.
Definition Router.cpp:110
Router(const Router &)=delete
Represents a match of a route against an incoming HTTP request.
Definition RouteTypes.h:18
Represents a single HTTP route.
Definition RouteTypes.h:42