SNode.C
Loading...
Searching...
No Matches
express::dispatcher::MiddlewareDispatcher Class Reference

#include <MiddlewareDispatcher.h>

Inheritance diagram for express::dispatcher::MiddlewareDispatcher:
Collaboration diagram for express::dispatcher::MiddlewareDispatcher:

Public Member Functions

 MiddlewareDispatcher (const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> &lambda)
 
- Public Member Functions inherited from express::Dispatcher
 Dispatcher (const Dispatcher &)=delete
 
Dispatcheroperator= (const Dispatcher &)=delete
 
 Dispatcher ()=default
 
virtual ~Dispatcher ()
 
bool dispatchNext (Controller &controller, const std::string &parentMountPath)
 

Private Member Functions

bool dispatch (express::Controller &controller, const std::string &parentMountPath, const express::MountPoint &mountPoint) override
 
std::list< std::string > getRoutes (const std::string &parentMountPath, const MountPoint &mountPoint, bool strictRouting) const override
 

Private Attributes

const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> lambda
 

Additional Inherited Members

- Protected Attributes inherited from express::Dispatcher
std::shared_ptr< RoutenextRoute = nullptr
 

Detailed Description

Definition at line 63 of file MiddlewareDispatcher.h.

Constructor & Destructor Documentation

◆ MiddlewareDispatcher()

express::dispatcher::MiddlewareDispatcher::MiddlewareDispatcher ( const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> &  lambda)
explicit

Definition at line 62 of file MiddlewareDispatcher.cpp.

64 : lambda(lambda) {
65 }
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> lambda

References lambda.

Member Function Documentation

◆ dispatch()

bool express::dispatcher::MiddlewareDispatcher::dispatch ( express::Controller controller,
const std::string &  parentMountPath,
const express::MountPoint mountPoint 
)
overrideprivatevirtual

Implements express::Dispatcher.

Definition at line 67 of file MiddlewareDispatcher.cpp.

67 {
68 bool requestMatched = false;
69
70 if ((controller.getFlags() & Controller::NEXT) == 0) {
71 const std::string absoluteMountPath = parentMountPath + mountPoint.relativeMountPath;
72
73 const std::string requestMethod = controller.getRequest()->method;
74 const std::string requestUrl = controller.getRequest()->url;
75 const std::string requestPath = controller.getRequest()->path;
76
77 // clang-format off
78 requestMatched =
79 (
80 (
81 (
82 mountPoint.method == requestMethod
83 ) || (
84 mountPoint.method == "all"
85 )
86 ) && (
87 (
88 requestUrl == absoluteMountPath
89 ) || (
90 (
91 !controller.getStrictRouting()
92 ) && (
93 requestPath.starts_with(absoluteMountPath)
94 )
95 ) || (
96 checkForUrlMatch(absoluteMountPath, requestUrl)
97 )
98 )
99 ) || (
100 (
101 mountPoint.method == "use"
102 ) && (
103 (
104 requestUrl.starts_with(absoluteMountPath)
105 )
106 )
107 );
108 // clang-format on
109
110 LOG(TRACE) << controller.getResponse()->getSocketContext()->getSocketConnection()->getConnectionName()
111 << " HTTP Express: middleware -> " << (requestMatched ? "MATCH" : "NO MATCH");
112 LOG(TRACE) << " RequestMethod: " << controller.getRequest()->method;
113 LOG(TRACE) << " RequestUrl: " << controller.getRequest()->url;
114 LOG(TRACE) << " RequestPath: " << controller.getRequest()->path;
115 LOG(TRACE) << " AbsoluteMountPath: " << absoluteMountPath;
116 LOG(TRACE) << " StrictRouting: " << controller.getStrictRouting();
117 LOG(TRACE) << " StrictRouting: " << controller.getStrictRouting();
118
119 if (requestMatched) {
120 if (hasResult(absoluteMountPath)) {
121 setParams(absoluteMountPath, *controller.getRequest());
122 }
123
124 Next next(controller);
125 lambda(controller.getRequest(), controller.getResponse(), next);
126
127 // If next() was called synchronously continue current route-tree traversal
128 if ((next.controller.getFlags() & express::Controller::NEXT) != 0) {
129 LOG(TRACE) << "Express: M - Next called - set to NO MATCH";
130 requestMatched = false;
131 controller = next.controller;
132 }
133 }
134 }
135
136 return requestMatched;
137 }
const std::shared_ptr< Request > & getRequest()
bool getStrictRouting() const
const std::shared_ptr< Response > & getResponse()
bool checkForUrlMatch(const std::string &cpath, const std::string &reqpath)
void setParams(const std::string &cpath, Request &req)
bool hasResult(const std::string &cpath)
std::string method
Definition MountPoint.h:56
std::string relativeMountPath
Definition MountPoint.h:57

References express::dispatcher::checkForUrlMatch(), express::Next::controller, core::socket::stream::SocketConnection::getConnectionName(), express::Controller::getFlags(), express::Controller::getRequest(), express::Controller::getResponse(), core::socket::stream::SocketContext::getSocketConnection(), express::Response::getSocketContext(), express::Controller::getStrictRouting(), express::dispatcher::hasResult(), lambda, express::MountPoint::method, express::Request::method, express::Controller::NEXT, express::Next::Next(), express::Controller::operator=(), express::Request::path, express::MountPoint::relativeMountPath, express::dispatcher::setParams(), and express::Request::url.

Here is the call graph for this function:

◆ getRoutes()

std::list< std::string > express::dispatcher::MiddlewareDispatcher::getRoutes ( const std::string &  parentMountPath,
const MountPoint mountPoint,
bool  strictRouting 
) const
overrideprivatevirtual

Implements express::Dispatcher.

Definition at line 140 of file MiddlewareDispatcher.cpp.

140 {
141 std::list<std::string> routes{"M " + parentMountPath + mountPoint.relativeMountPath + (!strictRouting ? "*" : "")};
142 routes.push_back(" " + mountPoint.method + " " + mountPoint.relativeMountPath);
143
144 if (nextRoute) {
145 routes.splice(routes.end(), nextRoute->getRoute(parentMountPath, strictRouting));
146 }
147
148 return routes;
149 }
std::shared_ptr< Route > nextRoute
Definition Dispatcher.h:78

References express::Route::getRoute(), express::MountPoint::method, express::Dispatcher::nextRoute, and express::MountPoint::relativeMountPath.

Here is the call graph for this function:

Member Data Documentation

◆ lambda

const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&, express::Next&)> express::dispatcher::MiddlewareDispatcher::lambda
private

Definition at line 73 of file MiddlewareDispatcher.h.

Referenced by dispatch(), and MiddlewareDispatcher().


The documentation for this class was generated from the following files: