Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Middleware.h File Reference

Middleware definitions for HTTP routing (e.g., logging, auth). Part of the PicoFramework HTTP server. This module defines middleware functions that can be used in the HTTP request processing pipeline. Middleware can perform tasks such as authentication, logging, and modifying requests or responses before passing them to the next handler in the chain. Middleware functions are designed to be composable, allowing multiple middleware components to be applied in sequence. Each middleware function can either allow the request to continue to the next handler or stop further processing based on certain conditions. More...

#include "HttpRequest.h"
#include "HttpResponse.h"
#include "JwtAuthenticator.h"
#include <functional>
#include <vector>
#include "router.h"
+ Include dependency graph for Middleware.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

using Middleware = std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)>
 Function signature for middleware components.
 

Variables

Middleware authMiddleware
 
Middleware loggingMiddleware
 Logging middleware that prints the HTTP method and path to the console.
 

Detailed Description

Author
Ian Archbell

To create your own middleware:

Middleware myMiddleware = [](HttpRequest& req, HttpResponse& res, const auto& params) {
Check something
return true;
};
std::function< bool(HttpRequest &, HttpResponse &, const RouteMatch &)> Middleware
Function signature for middleware components.
Definition Middleware.h:50
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
Represents an HTTP response object.
Version
0.1
Date
2025-03-26
License:\n MIT License

Definition in file Middleware.h.

Typedef Documentation

◆ Middleware

using Middleware = std::function<bool(HttpRequest &, HttpResponse &, const RouteMatch &)>

Middleware functions operate on the request and response objects. They return true to allow processing to continue, or false to stop further middleware and route execution.

Parameters
reqThe incoming HTTP request.
resThe HTTP response being constructed.
paramsPath parameters captured by the router.
Returns
true to continue to the next middleware or handler; false to halt processing.

Definition at line 50 of file Middleware.h.

Variable Documentation

◆ authMiddleware

◆ loggingMiddleware

Middleware loggingMiddleware
extern

This middleware always allows processing to continue.

Logging middleware that prints the HTTP method and path to the console.

Logging middleware that prints the HTTP method and path to the console.

Definition at line 57 of file Middleware.cpp.

58{
59 std::cout << "Received request: " << req.getMethod() << " " << req.getPath() << std::endl;
60 return true;
61};