SNode.C
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
express::dispatcher Namespace Reference

Classes

class  ApplicationDispatcher
 
class  MiddlewareDispatcher
 
class  RouterDispatcher
 

Functions

std::string path_concat (const std::vector< std::string > &stringvec)
 
std::vector< std::string > explode (const std::string &s, char delim)
 
const std::regex & pathRegex ()
 
std::smatch matchResult (const std::string &cpath)
 
bool hasResult (const std::string &cpath)
 
bool matchFunction (const std::string &cpath, const std::string &reqpath)
 
void setParams (const std::string &cpath, express::Request &req)
 
bool checkForUrlMatch (const std::string &cpath, const std::string &reqpath)
 

Function Documentation

◆ checkForUrlMatch()

bool express::dispatcher::checkForUrlMatch ( const std::string & cpath,
const std::string & reqpath )

Definition at line 166 of file regex_utils.cpp.

166 {
167 return hasResult(cpath) && matchFunction(cpath, reqpath);
168 }
bool hasResult(const std::string &cpath)
bool matchFunction(const std::string &cpath, const std::string &reqpath)

◆ explode()

std::vector< std::string > express::dispatcher::explode ( const std::string & s,
char delim )

Definition at line 66 of file regex_utils.cpp.

66 {
67 std::vector<std::string> result;
68 std::string current;
69 int parenDepth = 0;
70
71 for (const char ch : input) {
72 if (ch == '(') {
73 parenDepth++;
74 current += ch;
75 } else if (ch == ')') {
76 parenDepth--;
77 current += ch;
78 } else if (ch == delim && parenDepth == 0) {
79 result.push_back(current);
80 current.clear();
81 } else {
82 current += ch;
83 }
84 }
85
86 if (!current.empty()) {
87 result.push_back(current);
88 }
89
90 return result;
91 }

◆ hasResult()

bool express::dispatcher::hasResult ( const std::string & cpath)

Definition at line 108 of file regex_utils.cpp.

108 {
109 std::smatch smatch;
110
111 return std::regex_search(cpath, smatch, pathRegex());
112 }
const std::regex & pathRegex()

◆ matchFunction()

bool express::dispatcher::matchFunction ( const std::string & cpath,
const std::string & reqpath )

Definition at line 114 of file regex_utils.cpp.

114 {
115 std::vector<std::string> explodedString = explode(cpath, '/');
116
117 for (std::vector<std::string>::size_type i = 0; i < explodedString.size(); i++) {
118 if (explodedString[i].front() == ':') {
119 const std::smatch smatch = matchResult(explodedString[i]);
120 std::string regex = "(.*)";
121
122 if (smatch.size() > 1) {
123 if (smatch[1] != "") {
124 regex = smatch[1];
125 } else if (i == explodedString.size() - 1) {
126 regex = "([^/]+)(/)?$";
127 }
128 }
129
130 explodedString[i] = regex;
131 }
132 }
133
134 const std::string regexPath = path_concat(explodedString);
135
136 return std::regex_match(reqpath, std::regex(regexPath));
137 }
std::smatch matchResult(const std::string &cpath)
std::string path_concat(const std::vector< std::string > &stringvec)
std::vector< std::string > explode(const std::string &s, char delim)

◆ matchResult()

std::smatch express::dispatcher::matchResult ( const std::string & cpath)

Definition at line 100 of file regex_utils.cpp.

100 {
101 std::smatch smatch;
102
103 std::regex_search(cpath, smatch, pathRegex());
104
105 return smatch;
106 }

◆ path_concat()

std::string express::dispatcher::path_concat ( const std::vector< std::string > & stringvec)

Definition at line 54 of file regex_utils.cpp.

54 {
55 std::string s;
56
57 for (std::vector<std::string>::size_type i = 0; i < stringvec.size(); i++) {
58 if (!stringvec[i].empty() && stringvec[i].front() != ' ') {
59 s += "\\/" + stringvec[i];
60 }
61 }
62
63 return s;
64 }

◆ pathRegex()

const std::regex & express::dispatcher::pathRegex ( )

Definition at line 94 of file regex_utils.cpp.

94 {
95 static const std::regex pathregex(PATH_REGEX);
96
97 return pathregex;
98 }
#define PATH_REGEX

◆ setParams()

void express::dispatcher::setParams ( const std::string & cpath,
express::Request & req )

Definition at line 139 of file regex_utils.cpp.

139 {
140 std::vector<std::string> explodedString = explode(cpath, '/');
141 std::vector<std::string> explodedReqString = explode(req.url, '/');
142
143 for (std::vector<std::string>::size_type i = 0; i < explodedString.size() && i < explodedReqString.size(); i++) {
144 if (explodedString[i].front() == ':') {
145 const std::smatch smatch = matchResult(explodedString[i]);
146 std::string regex = "(.*)";
147
148 if (smatch.size() > 1) {
149 if (smatch[1] != "") {
150 regex = smatch[1];
151 }
152 }
153
154 if (std::regex_match(explodedReqString[i], std::regex(regex))) {
155 std::string attributeName = smatch[0];
156 attributeName.erase(0, 1);
157 attributeName.erase((attributeName.length() - static_cast<std::size_t>(smatch[1].length())),
158 static_cast<std::size_t>(smatch[1].length()));
159
160 req.params[attributeName] = explodedReqString[i];
161 }
162 }
163 }
164 }
std::string url
Definition Request.h:96
std::map< std::string, std::string > params
Definition Request.h:79