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

#include <ApplicationDispatcher.h>

Inheritance diagram for express::dispatcher::ApplicationDispatcher:
Collaboration diagram for express::dispatcher::ApplicationDispatcher:

Public Member Functions

 ApplicationDispatcher (const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &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 > &)> lambda
std::regex regex
std::vector< std::string > names

Additional Inherited Members

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

Detailed Description

Definition at line 64 of file ApplicationDispatcher.h.

Constructor & Destructor Documentation

◆ ApplicationDispatcher()

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

Definition at line 65 of file ApplicationDispatcher.cpp.

67 : lambda(lambda) {
68 }
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> lambda

References lambda.

Member Function Documentation

◆ dispatch()

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

Implements express::Dispatcher.

Definition at line 70 of file ApplicationDispatcher.cpp.

72 {
73 bool requestMatched = false;
74
75 if (controller.getRequest()->method == mountPoint.method || mountPoint.method == "use" || mountPoint.method == "all") {
76 const std::string absoluteMountPath = parentMountPath + mountPoint.relativeMountPath;
77
78 if ((controller.getFlags() & Controller::NEXT) == 0) {
79 // Split mount & request into path + query
80 std::string_view mountPath;
81 std::string_view mountQueryString;
82 splitPathAndQuery(absoluteMountPath, mountPath, mountQueryString);
83 auto requiredQueryPairs = parseQuery(mountQueryString);
84
85 std::string_view requestPath;
86 std::string_view requestQueryString;
87 splitPathAndQuery(controller.getRequest()->originalUrl, requestPath, requestQueryString);
88 auto requestQueryPairs = parseQuery(requestQueryString);
89
90 // End-anchored equality (verbs) with one trailing slash relaxed when !strict
91 std::string_view normalizedMountPath = mountPath;
92 std::string_view normalizedRequestPath = requestPath;
93 if (!controller.getStrictRouting()) {
94 normalizedMountPath = trimOneTrailingSlash(normalizedMountPath);
95 normalizedRequestPath = trimOneTrailingSlash(normalizedRequestPath);
96 }
97 if (normalizedMountPath.empty()) {
98 normalizedMountPath = "/";
99 }
100
101 bool pathMatches = false;
102 if (mountPath.find(':') != std::string::npos) {
103 if (regex.mark_count() == 0) {
104 LOG(TRACE) << "ApplicationDispatcher: precompiled regex";
105 std::tie(regex, names) = compileParamRegex(mountPath,
106 /*isPrefix*/ true,
107 controller.getStrictRouting(),
108 controller.getCaseInsensitiveRouting());
109 } else {
110 LOG(TRACE) << "ApplicationDispatcher: using precompiled regex";
111 }
112 pathMatches = matchAndFillParams(regex, names, requestPath, *controller.getRequest());
113 } else {
114 pathMatches = equalPath(normalizedRequestPath, normalizedMountPath, controller.getCaseInsensitiveRouting());
115 }
116
117 const bool queryMatches = querySupersetMatches(requestQueryPairs, requiredQueryPairs);
118 requestMatched = (pathMatches && queryMatches);
119
120 LOG(TRACE) << controller.getResponse()->getSocketContext()->getSocketConnection()->getConnectionName()
121 << " HTTP Express: application -> " << (requestMatched ? "MATCH" : "NO MATCH");
122 LOG(TRACE) << " RequestMethod: " << controller.getRequest()->method;
123 LOG(TRACE) << " RequestUrl: " << controller.getRequest()->url;
124 LOG(TRACE) << " RequestPath: " << controller.getRequest()->path;
125 LOG(TRACE) << " Mountpoint Method: " << mountPoint.method;
126 LOG(TRACE) << " Mountpoint RelativePath: " << mountPoint.relativeMountPath;
127 LOG(TRACE) << " Mountpoint AbsolutePath: " << absoluteMountPath;
128 LOG(TRACE) << " StrictRouting: " << controller.getStrictRouting();
129 LOG(TRACE) << " CaseInsensitiveRouting: " << controller.getCaseInsensitiveRouting();
130
131 if (requestMatched) {
132 controller.getRequest()->queries.insert(requestQueryPairs.begin(), requestQueryPairs.end());
133
134 if (hasResult(absoluteMountPath)) {
135 setParams(absoluteMountPath, *controller.getRequest());
136 }
137
138 lambda(controller.getRequest(), controller.getResponse());
139 }
140 } else {
141 LOG(TRACE) << controller.getResponse()->getSocketContext()->getSocketConnection()->getConnectionName()
142 << " HTTP Express: application -> next(...) called";
143 LOG(TRACE) << " RequestMethod: " << controller.getRequest()->method;
144 LOG(TRACE) << " RequestUrl: " << controller.getRequest()->url;
145 LOG(TRACE) << " RequestPath: " << controller.getRequest()->path;
146 LOG(TRACE) << " AbsoluteMountPath: " << absoluteMountPath;
147 }
148 }
149
150 return requestMatched;
151 }
const std::shared_ptr< Request > & getRequest()
bool getStrictRouting() const
bool getCaseInsensitiveRouting() const
const std::shared_ptr< Response > & getResponse()
bool matchAndFillParams(const std::regex &rx, const std::vector< std::string > &names, std::string_view reqPath, RequestLike &req)
bool querySupersetMatches(const std::unordered_map< std::string, std::string > &rq, const std::unordered_map< std::string, std::string > &need)
void setParams(const std::string &cpath, Request &req)
void splitPathAndQuery(std::string_view url, std::string_view &path, std::string_view &query)
std::pair< std::regex, std::vector< std::string > > compileParamRegex(std::string_view mountPath, bool isPrefix, bool strictRouting, bool caseInsensitive)
std::unordered_map< std::string, std::string > parseQuery(std::string_view qs)
bool hasResult(const std::string &cpath)
std::string_view trimOneTrailingSlash(std::string_view s)
bool equalPath(std::string_view a, std::string_view b, bool caseInsensitive)
std::string method
Definition MountPoint.h:56
std::string relativeMountPath
Definition MountPoint.h:57

References express::dispatcher::compileParamRegex(), express::dispatcher::equalPath(), express::Controller::getCaseInsensitiveRouting(), 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::dispatcher::matchAndFillParams(), express::MountPoint::method, express::Request::method, names, express::Controller::NEXT, express::Request::originalUrl, express::dispatcher::parseQuery(), express::Request::path, express::Request::queries, express::dispatcher::querySupersetMatches(), express::MountPoint::relativeMountPath, express::dispatcher::setParams(), express::dispatcher::splitPathAndQuery(), express::dispatcher::trimOneTrailingSlash(), and express::Request::url.

Here is the call graph for this function:

◆ getRoutes()

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

Implements express::Dispatcher.

Definition at line 154 of file ApplicationDispatcher.cpp.

154 {
155 std::list<std::string> routes{"A " + parentMountPath + mountPoint.relativeMountPath + (!strictRouting ? "*" : "")};
156 routes.push_back(" " + mountPoint.method + " " + mountPoint.relativeMountPath);
157
158 if (nextRoute) {
159 routes.splice(routes.end(), nextRoute->getRoute(parentMountPath, strictRouting));
160 }
161
162 return routes;
163 }
std::shared_ptr< Route > nextRoute
Definition Dispatcher.h:80

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::dispatcher::ApplicationDispatcher::lambda
private

Definition at line 74 of file ApplicationDispatcher.h.

Referenced by ApplicationDispatcher(), and dispatch().

◆ names

std::vector<std::string> express::dispatcher::ApplicationDispatcher::names
private

Definition at line 77 of file ApplicationDispatcher.h.

Referenced by dispatch().

◆ regex

std::regex express::dispatcher::ApplicationDispatcher::regex
private

Definition at line 76 of file ApplicationDispatcher.h.


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