Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Middleware.cpp
Go to the documentation of this file.
1
21#include "http/Middleware.h"
22
23#include "framework_config.h" // Must be included before DebugTrace.h to ensure framework_config.h is processed first
24#include "DebugTrace.h"
26
27#include <iostream>
28#include "http/HttpResponse.h"
29#include "http/JsonResponse.h"
31
33#ifdef PICO_HTTP_ENABLE_JWT
36{
37 std::string token = req.getHeader("Authorization");
38 if (token.empty() || token.find("Bearer ") != 0)
39 {
40 res.setStatus(401).send("{\"error\":\"Unauthorized\"}");
41 return false;
42 }
43
44 token = token.substr(7); // Remove "Bearer " prefix
45 if (!AppContext::get<JwtAuthenticator>()->validateJWT(token))
46 {
47 JsonResponse::sendError(res, 401, "INVALID_TOKEN", "Invalid token");
48 return false;
49 }
50
51 return true; // Authorized
52};
53
54#endif
55
58{
59 std::cout << "Received request: " << req.getMethod() << " " << req.getPath() << std::endl;
60 return true;
61};
62
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Definition DebugTrace.h:170
HTTP HttpResponse class for managing status, headers, body, and streaming support.
Utility functions to send standard JSON responses using nlohmann::json.
Stateless singleton class for creating and validating JWTs using HMAC-SHA256.
Middleware loggingMiddleware
Middleware definitions for HTTP routing (e.g., logging, auth). Part of the PicoFramework HTTP server....
Middleware authMiddleware
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware components.
Definition Middleware.h:50
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
const std::string & getMethod() const
Get the HTTP method.
const std::string & getPath() const
Get the parsed request path (without query string).
std::string getHeader(const std::string &field) const
Get a specific header field (case-insensitive).
Represents an HTTP response object.
Delegates to user or system configuration.
void sendError(HttpResponse &res, int statusCode, const std::string &code, const std::string &message)
Represents a match of a route against an incoming HTTP request.
Definition RouteTypes.h:18