2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "express/dispatcher/MiddlewareDispatcher.h"
44#include "core/socket/stream/SocketConnection.h"
45#include "express/Next.h"
46#include "express/Request.h"
47#include "express/Response.h"
48#include "express/Route.h"
49#include "express/dispatcher/regex_utils.h"
50#include "web/http/server/SocketContext.h"
52#ifndef DOXYGEN_SHOULD_SKIP_THIS
54#include "log/Logger.h"
62#include <unordered_map>
69 const std::function<
void(
const std::shared_ptr<
Request>&,
const std::shared_ptr<
Response>&, express::
Next&)>& lambda)
74 bool requestMatched =
false;
81 std::string_view mountPath;
82 std::string_view mountQueryString;
86 std::string_view requestPath;
87 std::string_view requestQueryString;
96 if (mountPath.empty()) {
101 bool pathMatches =
false;
102 std::size_t consumedLength = 0;
103 if (absoluteMountPath.find(
':') != std::string::npos) {
105 if (regex.mark_count() == 0) {
106 if (regex.mark_count() == 0) {
107 LOG(TRACE) <<
"MiddlewarDispatchere: precompiled regex";
113 LOG(TRACE) <<
"MiddlewareDispatcher: using precompiled regex";
116 std::cmatch regexMatches;
117 pathMatches = std::regex_search(requestPath.begin(), requestPath.end(), regexMatches, regex);
120 consumedLength =
static_cast<std::size_t>(regexMatches.length(0));
123 const std::size_t groups = !regexMatches.empty() ? (regexMatches.size() - 1) : 0;
124 const std::size_t n = std::min(
names.size(), groups);
125 for (std::size_t i = 0; i < n; ++i) {
133 consumedLength = mountPath.size();
138 requestMatched = (pathMatches && queryMatches);
141 <<
" HTTP Express: middleware -> " << (requestMatched ?
"MATCH" :
"NO MATCH");
145 LOG(TRACE) <<
" Mountpoint Method: " << mountPoint
.method;
147 LOG(TRACE) <<
" Mountpoint AbsolutePath: " << absoluteMountPath;
151 if (requestMatched) {
153 std::string_view remainderPath{};
154 if (requestPath.size() > consumedLength) {
155 remainderPath = requestPath.substr(consumedLength);
156 if (!remainderPath.empty() && remainderPath.front() ==
'/') {
157 remainderPath.remove_prefix(1);
161 const std::string previousPathBackup = req
.path;
162 req
.path = remainderPath.empty() ?
"/" : (
"/" + std::string(remainderPath));
164 req
.queries.insert(requestQueryPairs.begin(), requestQueryPairs.end());
175 LOG(TRACE) <<
"Express: M - Next called - set to NO MATCH";
176 requestMatched =
false;
181 req
.path = previousPathBackup;
185 <<
" HTTP Express: middleware -> next(...) called";
189 LOG(TRACE) <<
" AbsoluteMountPath: " << absoluteMountPath;
193 return requestMatched;
196 std::list<std::string>
198 std::list<std::string> routes{
"M " + parentMountPath + mountPoint
.relativeMountPath + (!strictRouting ?
"*" :
"")};
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
const std::shared_ptr< Request > & getRequest()
bool getStrictRouting() const
bool getCaseInsensitiveRouting() const
Controller & operator=(const Controller &controller) noexcept
const std::shared_ptr< Response > & getResponse()
std::shared_ptr< Route > nextRoute
Next(Controller &controller)
web::http::CiStringMap< std::string > queries
web::http::server::SocketContext * getSocketContext() const
std::list< std::string > getRoute(const std::string &parentMountPath, bool strictRouting) const
bool dispatch(express::Controller &controller, const std::string &parentMountPath, const express::MountPoint &mountPoint) override
MiddlewareDispatcher(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> &lambda)
std::list< std::string > getRoutes(const std::string &parentMountPath, const MountPoint &mountPoint, bool strictRouting) const override
const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &, express::Next &)> lambda
std::vector< std::string > names
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)
bool boundaryPrefix(std::string_view path, std::string_view base, bool caseInsensitive)
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)
std::string relativeMountPath