|
magpie
|
#include <Middleware.hpp>
Public Member Functions | |
| virtual void | onRequest (IMiddlewareProcessor< ContextType > *proc, ContextType *ctx, Request &req, Response &res)=0 |
| void | next (IMiddlewareProcessor< ContextType > *proc, ContextType *ctx, Request &req, Response &res) |
Base definition for middlewares used by magpie.
There's no technical distinction between global and scoped middlewares in their implementation. The call order for middlewares is:
The order within the middlewares is defined by their declaration order. Middlewares can therefore be arbitrarily chained by their declaration order. See App::registerGlobalMiddlewares and BaseRoute::registerMiddlewares
Chaining is controlled by the next function. To call the next step of the chain, invoke next. Every middleware must invoke next. See the documentation for the next function for more information about the chaining. Example middleware:
|
inline |
Forwards to the next middleware, or to the route of the current middleware is the last middleware in the chain. The call to next separates before and after handlers. Callers do not need to worry about the inner working of this function, only the semantics it implies.
As described in the documentation for this class, the middlewares are called in the same order they're declared. Because the next function directly invokes the next step of the chain, everything after the next is invoked in reverse order. The first middleware to be entered is the last to be exited.
|
pure virtual |
Called when a request is about to be handled.
The next function must be called somewhere in this function. The call to next is required for a route to be invoked, as well as for the next middleware in the chain to be invoked.
Conceptually, the onRequest call can be thought of as a function containing:
However, the next function being exposed enables the middlewares to also become error handlers. An error handler middleware can simply put a try-catch around the call to next to handle exceptions. This is only necessary if you want a special exception format, as there is a default exception handler built into magpie to keep the server alive through most standard exceptions.
| proc | The middleware processor. This is exclusively used to pass forward to the next call, to tell what to invoke next. |
| ctx | The context object. Can be used by the middleware. |
| req | The request object. Can be used by the middleware. |
| res | The response object. Can be used by the middleware. |