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

150 {
151 std::list<std::string> routes{"A " + parentMountPath + mountPoint.relativeMountPath + (!strictRouting ? "*" : "")};
152 routes.push_back(" " + mountPoint.method + " " + mountPoint.relativeMountPath);
153
154 if (nextRoute) {
155 routes.splice(routes.end(), nextRoute->getRoute(parentMountPath, strictRouting));
156 }
157
158 return routes;
159 }
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: