Skip to content

Commit fc2b7be

Browse files
committed
Fix deep parameter routes
1 parent fa5f5e3 commit fc2b7be

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/HttpRouter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,12 @@ struct HttpRouter {
286286
/* Iterate over all segments */
287287
setUrl(pattern);
288288
for (int i = 0; !getUrlSegment(i).second; i++) {
289-
node = getNode(node, std::string(getUrlSegment(i).first), priority == HIGH_PRIORITY);
289+
std::string strippedSegment(getUrlSegment(i).first);
290+
if (strippedSegment.length() && strippedSegment[0] == ':') {
291+
/* Parameter routes must be named only : */
292+
strippedSegment = ":";
293+
}
294+
node = getNode(node, strippedSegment, priority == HIGH_PRIORITY);
290295
}
291296
/* Insert handler in order sorted by priority (most significant 1 byte) */
292297
node->handlers.insert(std::upper_bound(node->handlers.begin(), node->handlers.end(), (uint32_t) (priority | handlers.size())), (uint32_t) (priority | handlers.size()));

tests/HttpRouter.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ void testMethodPriority() {
4646
assert(result == "PSAS");
4747
}
4848

49+
void testDeepParameterRoutes() {
50+
std::cout << "TestDeepParameterRoutes" << std::endl;
51+
uWS::HttpRouter<int> r;
52+
std::string result;
53+
54+
r.add({"GET"}, "/something/:id/sync", [&result](auto *h) {
55+
result += "ETT";
56+
return false;
57+
});
58+
59+
r.add({"GET"}, "/something/:somethingId/pin", [&result](auto *h) {
60+
result += "TVÅ";
61+
return false;
62+
});
63+
64+
r.add({"GET"}, "/something/:id/:attribute", [&result](auto *h) {
65+
result += "TRE";
66+
return false;
67+
});
68+
69+
assert(r.route("GET", "/something/1234/pin") == false);
70+
assert(result == "TVÅTRE");
71+
72+
result.clear();
73+
assert(r.route("GET", "/something/1234/sync") == false);
74+
assert(result == "ETTTRE");
75+
}
76+
4977
void testPatternPriority() {
5078
std::cout << "TestPatternPriority" << std::endl;
5179
uWS::HttpRouter<int> r;
@@ -402,6 +430,7 @@ void testPerformance() {
402430
}
403431

404432
int main() {
433+
testDeepParameterRoutes();
405434
testPatternPriority();
406435
testMethodPriority();
407436
testUpgrade();

0 commit comments

Comments
 (0)