FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
esp32_https_server/src/ResourceResolver.cpp at master · marklove5102/esp32_https_server · GitHub
marklove5102
esp32_https_server
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
esp32_https_server
/
src
/
ResourceResolver.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
179 lines (148 loc) · 6.39 KB
Breadcrumbs
esp32_https_server
/
src
/
ResourceResolver.cpp
Copy path
File metadata and controls
179 lines (148 loc) · 6.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
ResourceResolver.hpp
"
namespace
httpsserver
{
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::addMiddleware
(
const
HTTPSMiddlewareFunction * mwFunction) {
_middleware.
push_back
(mwFunction);
}
void
ResourceResolver::removeMiddleware
(
const
HTTPSMiddlewareFunction * mwFunction) {
_middleware.
erase
(
std::remove
(_middleware.
begin
(), _middleware.
end
(), mwFunction), _middleware.
end
());
}
const
std::vector<HTTPSMiddlewareFunction*>
ResourceResolver::getMiddleware
() {
return
_middleware;
}
void
ResourceResolver::setDefaultNode
(HTTPNode * defaultNode) {
_defaultNode = defaultNode;
}
}
Back
|
FazBrowse Home
|
New Git URL