SNode.C
Loading...
Searching...
No Matches
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)
 
std::string path_concat (const std::string &first, const std::string &second)
 

Function Documentation

◆ checkForUrlMatch()

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

Definition at line 146 of file regex_utils.cpp.

146 {
147 return hasResult(cpath) && matchFunction(cpath, reqpath);
148 }
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 46 of file regex_utils.cpp.

46 {
47 std::vector<std::string> result;
48 std::istringstream iss(s);
49
50 for (std::string token; std::getline(iss, token, delim);) {
51 result.push_back(std::move(token));
52 }
53
54 return result;
55 }

◆ hasResult()

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

Definition at line 72 of file regex_utils.cpp.

72 {
73 std::smatch smatch;
74
75 return std::regex_search(cpath, smatch, pathRegex());
76 }
const std::regex & pathRegex()

◆ matchFunction()

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

Definition at line 78 of file regex_utils.cpp.

78 {
79 std::vector<std::string> explodedString = explode(cpath, '/');
80
81 for (std::vector<std::string>::size_type i = 0; i < explodedString.size(); i++) {
82 if (explodedString[i].front() == ':') {
83 const std::smatch smatch = matchResult(explodedString[i]);
84 std::string regex = "(.*)";
85
86 if (smatch.size() > 1) {
87 if (smatch[1] != "") {
88 regex = smatch[1];
89 }
90 }
91
92 explodedString[i] = regex;
93 }
94 }
95
96 const std::string regexPath = path_concat(explodedString);
97
98 return std::regex_match(reqpath, std::regex(regexPath));
99 }
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 64 of file regex_utils.cpp.

64 {
65 std::smatch smatch;
66
67 std::regex_search(cpath, smatch, pathRegex());
68
69 return smatch;
70 }

◆ path_concat() [1/2]

std::string express::dispatcher::path_concat ( const std::string & first,
const std::string & second )

Definition at line 128 of file regex_utils.cpp.

128 {
129 std::string result;
130
131 if (first.back() == '/' && second.front() == '/') {
132 result = first + second.substr(1);
133 } else if (first.back() != '/' && second.front() != '/') {
134 result = first + '/' + second;
135 } else {
136 result = first + second;
137 }
138
139 if (result.length() > 1 && result.back() == '/') {
140 result.pop_back();
141 }
142
143 return result;
144 }

◆ path_concat() [2/2]

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

Definition at line 34 of file regex_utils.cpp.

34 {
35 std::string s;
36
37 for (std::vector<std::string>::size_type i = 0; i < stringvec.size(); i++) {
38 if (!stringvec[i].empty() && stringvec[i].front() != ' ') {
39 s += "\\/" + stringvec[i];
40 }
41 }
42
43 return s;
44 }

◆ pathRegex()

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

Definition at line 58 of file regex_utils.cpp.

58 {
59 static const std::regex pathregex(PATH_REGEX);
60
61 return pathregex;
62 }
#define PATH_REGEX

◆ setParams()

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

Definition at line 101 of file regex_utils.cpp.

101 {
102 std::vector<std::string> explodedString = explode(cpath, '/');
103 std::vector<std::string> explodedReqString = explode(req.url, '/');
104
105 for (std::vector<std::string>::size_type i = 0; i < explodedString.size() && i < explodedReqString.size(); i++) {
106 if (explodedString[i].front() == ':') {
107 const std::smatch smatch = matchResult(explodedString[i]);
108 std::string regex = "(.*)";
109
110 if (smatch.size() > 1) {
111 if (smatch[1] != "") {
112 regex = smatch[1];
113 }
114 }
115
116 if (std::regex_match(explodedReqString[i], std::regex(regex))) {
117 std::string attributeName = smatch[0];
118 attributeName.erase(0, 1);
119 attributeName.erase((attributeName.length() - static_cast<std::size_t>(smatch[1].length())),
120 static_cast<std::size_t>(smatch[1].length()));
121
122 req.params[attributeName] = explodedReqString[i];
123 }
124 }
125 }
126 }
std::string url
Definition Request.h:73
std::map< std::string, std::string > params
Definition Request.h:56