-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathResourceResolver.cpp
250 lines (212 loc) · 8.73 KB
/
ResourceResolver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "ResourceResolver.hpp"
namespace httpsserver {
ResourceResolver::HTTPSMiddlewareFunctionCallback::HTTPSMiddlewareFunctionCallback(const HTTPSMiddlewareFunction callback, const HTTPSMiddlewareFunction* callback_std_function, const HTTPSMiddlewareFunctionType* callback_raw_pointer) : _callback(callback), _callback_std_function(callback_std_function), _callback_raw_pointer(callback_raw_pointer) {
};
HTTPSMiddlewareFunction ResourceResolver::HTTPSMiddlewareFunctionCallback::getCallback() {
return _callback;
};
const HTTPSMiddlewareFunction* ResourceResolver::HTTPSMiddlewareFunctionCallback::getStdFunctionPointer() {
return _callback_std_function;
};
const HTTPSMiddlewareFunctionType* ResourceResolver::HTTPSMiddlewareFunctionCallback::getRawFunctionPointer() {
return _callback_raw_pointer;
};
ResourceResolver::ResourceResolver() {
_nodes = new std::vector<HTTPNode *>();
_defaultNode = NULL;
}
ResourceResolver::~ResourceResolver() {
delete _nodes;
}
/**
* This method will register the HTTPSNode so it is reachable and its callback gets called for a request
*/
void ResourceResolver::registerNode(HTTPNode *node) {
_nodes->push_back(node);
}
/**
* This method can be used to deactivate a HTTPSNode that has been registered previously
*/
void ResourceResolver::unregisterNode(HTTPNode *node) {
}
void ResourceResolver::resolveNode(const std::string &method, const std::string &url, ResolvedResource &resolvedResource, HTTPNodeType nodeType) {
// Reset the resource
resolvedResource.setMatchingNode(NULL);
resolvedResource.setParams(NULL);
// Memory management of this object will be performed by the ResolvedResource instance
ResourceParameters * params = new ResourceParameters();
// Split URL in resource name and request params. Request params start after an optional '?'
size_t reqparamIdx = url.find('?');
// Store this index to stop path parsing there
size_t pathEnd = reqparamIdx != std::string::npos ? reqparamIdx : url.size();
// If no '?' is contained in url, 0:npos will return the string as it is
std::string resourceName = url.substr(0, reqparamIdx);
// Set request params in params object if a '?' exists
if (reqparamIdx != std::string::npos) {
do {
// Drop the '?' or '&'
reqparamIdx += 1;
// Parameters are separated by '&'
size_t nextparamIdx = url.find('&', reqparamIdx);
// Get the "name=value" string
std::string param = nextparamIdx == std::string::npos ?
url.substr(reqparamIdx) :
url.substr(reqparamIdx, nextparamIdx - reqparamIdx);
if (param.length() > 0) {
// Find the position where the string has to be split
size_t nvSplitIdx = param.find('=');
// Use empty string if only name is set. /foo?bar&baz=1 will return "" for bar
std::string name = urlDecode(param.substr(0, nvSplitIdx));
std::string value = "";
if (nvSplitIdx != std::string::npos) {
value = urlDecode(param.substr(nvSplitIdx+1));
}
// Now we finally have name and value.
params->setQueryParameter(name, value);
}
// Update reqparamIdx
reqparamIdx = nextparamIdx;
} while(reqparamIdx != std::string::npos);
}
// Check whether a resource matches
for(std::vector<HTTPNode*>::iterator itNode = _nodes->begin(); itNode != _nodes->end(); ++itNode) {
params->resetPathParameters();
HTTPNode *node = *itNode;
if (node->_nodeType==nodeType) {
if (
// For handler functions, check the method declared with the node
(node->_nodeType==HANDLER_CALLBACK && ((ResourceNode*)node)->_method == method) ||
// For websockets, the specification says that GET is the only choice
(node->_nodeType==WEBSOCKET && method=="GET")
) {
HTTPS_LOGD("Testing route %s", node->_path.c_str());
bool match = true;
size_t paramCount = node->getPathParamCount();
// indices in input and pattern
size_t inputIdx = 0, pathIdx = 0;
HTTPS_LOGD("(INIT) inputIdx: %d, pathIdx: %d, pathEnd: %d, path: %s, url: %s",
inputIdx, pathIdx, pathEnd, node->_path.c_str(), url.c_str());
for (size_t paramIdx = 0; match && paramIdx < paramCount; paramIdx += 1) {
HTTPS_LOGD("(LOOP) inputIdx: %d, pathIdx: %d, pathEnd: %d, path: %s, url: %s",
inputIdx, pathIdx, pathEnd, node->_path.c_str(), url.c_str());
// Test static path before the parameter
size_t paramPos = node->getParamIdx(paramIdx);
size_t staticLength = paramPos - pathIdx;
match &= url.substr(inputIdx, staticLength) == node->_path.substr(pathIdx, staticLength);
inputIdx += staticLength;
pathIdx += staticLength;
// Extract parameter value
if (match) {
size_t paramEnd = url.find('/', inputIdx);
if (paramEnd == std::string::npos && inputIdx <= pathEnd) {
// Consume the remaining input (might be "" for the last param)
paramEnd = pathEnd;
}
if (paramEnd != std::string::npos) {
size_t paramLength = paramEnd - inputIdx;
params->setPathParameter(paramIdx, urlDecode(url.substr(inputIdx, paramLength)));
pathIdx += 1;
inputIdx += paramLength;
} else {
match = false;
HTTPS_LOGD("(LOOP) No match on param part");
}
} else {
HTTPS_LOGD("(LOOP) No match on static part");
}
}
HTTPS_LOGD("(STTC) inputIdx: %d, pathIdx: %d, pathEnd: %d, path: %s, url: %s",
inputIdx, pathIdx, pathEnd, node->_path.c_str(), url.c_str());
// Test static path after the parameter (up to pathEnd)
if (match) {
match = url.substr(inputIdx, pathEnd - inputIdx)==node->_path.substr(pathIdx);
}
HTTPS_LOGD("(END ) inputIdx: %d, pathIdx: %d, pathEnd: %d, path: %s, url: %s",
inputIdx, pathIdx, pathEnd, node->_path.c_str(), url.c_str());
if (match) {
resolvedResource.setMatchingNode(node);
HTTPS_LOGD("It's a match!");
break;
}
} // method check
} // node type check
} // resource node for loop
// If the resource did not match, configure the default resource
if (!resolvedResource.didMatch() && _defaultNode != NULL) {
params->resetPathParameters();
resolvedResource.setMatchingNode(_defaultNode);
}
// If resolving did work, set the params, otherwise delete them
if (resolvedResource.didMatch()) {
// The resolvedResource now takes care of memory management for the params
resolvedResource.setParams(params);
} else {
delete params;
}
}
void ResourceResolver::updateMiddlewareList() {
_middleware.clear();
_middleware.reserve(_middleware_callback.size());
for (auto& callback : _middleware_callback) {
_middleware.push_back(callback.getCallback());
}
}
void ResourceResolver::addMiddleware(const HTTPSMiddlewareFunction &mwFunction) {
const HTTPSMiddlewareFunctionCallback callback{
mwFunction,
&mwFunction,
nullptr
};
_middleware.push_back(mwFunction);
_middleware_callback.push_back(callback);
}
void ResourceResolver::addMiddleware(void (*mwFunction)(HTTPRequest * req, HTTPResponse * res, std::function<void()> next)) {
auto mwFunction_callback = HTTPSMiddlewareFunction(mwFunction);
const HTTPSMiddlewareFunctionCallback callback{
mwFunction_callback,
&mwFunction_callback,
mwFunction
};
_middleware.push_back(mwFunction_callback);
_middleware_callback.push_back(callback);
}
void ResourceResolver::removeMiddleware(const HTTPSMiddlewareFunction &mwFunction) {
bool found = false;
for (auto it = _middleware_callback.begin(); it != _middleware_callback.end();) {
auto element = *it;
const auto callback = element.getStdFunctionPointer();
const auto callback_supplied = &mwFunction;
if (callback != nullptr && callback == callback_supplied) {
it = _middleware_callback.erase(it);
found = true;
} else {
++it;
}
}
if (found) {
updateMiddlewareList();
}
}
void ResourceResolver::removeMiddleware(void (*mwFunction)(HTTPRequest * req, HTTPResponse * res, std::function<void()> next)) {
bool found = false;
for (auto it = _middleware_callback.begin(); it != _middleware_callback.end();) {
auto element = *it;
auto callback = element.getRawFunctionPointer();
if (callback != nullptr && callback == mwFunction) {
it = _middleware_callback.erase(it);
found = true;
} else {
++it;
}
}
if (found) {
updateMiddlewareList();
}
}
const std::vector<HTTPSMiddlewareFunction> ResourceResolver::getMiddleware() {
return _middleware;
}
void ResourceResolver::setDefaultNode(HTTPNode * defaultNode) {
_defaultNode = defaultNode;
}
}