Skip to content

Commit

Permalink
Fix deep parameter routes
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 25, 2024
1 parent fa5f5e3 commit fc2b7be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/HttpRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ struct HttpRouter {
/* Iterate over all segments */
setUrl(pattern);
for (int i = 0; !getUrlSegment(i).second; i++) {
node = getNode(node, std::string(getUrlSegment(i).first), priority == HIGH_PRIORITY);
std::string strippedSegment(getUrlSegment(i).first);
if (strippedSegment.length() && strippedSegment[0] == ':') {
/* Parameter routes must be named only : */
strippedSegment = ":";
}
node = getNode(node, strippedSegment, priority == HIGH_PRIORITY);
}
/* Insert handler in order sorted by priority (most significant 1 byte) */
node->handlers.insert(std::upper_bound(node->handlers.begin(), node->handlers.end(), (uint32_t) (priority | handlers.size())), (uint32_t) (priority | handlers.size()));
Expand Down
29 changes: 29 additions & 0 deletions tests/HttpRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ void testMethodPriority() {
assert(result == "PSAS");
}

void testDeepParameterRoutes() {
std::cout << "TestDeepParameterRoutes" << std::endl;
uWS::HttpRouter<int> r;
std::string result;

r.add({"GET"}, "/something/:id/sync", [&result](auto *h) {
result += "ETT";
return false;
});

r.add({"GET"}, "/something/:somethingId/pin", [&result](auto *h) {
result += "TVÅ";
return false;
});

r.add({"GET"}, "/something/:id/:attribute", [&result](auto *h) {
result += "TRE";
return false;
});

assert(r.route("GET", "/something/1234/pin") == false);
assert(result == "TVÅTRE");

result.clear();
assert(r.route("GET", "/something/1234/sync") == false);
assert(result == "ETTTRE");
}

void testPatternPriority() {
std::cout << "TestPatternPriority" << std::endl;
uWS::HttpRouter<int> r;
Expand Down Expand Up @@ -402,6 +430,7 @@ void testPerformance() {
}

int main() {
testDeepParameterRoutes();
testPatternPriority();
testMethodPriority();
testUpgrade();
Expand Down

0 comments on commit fc2b7be

Please sign in to comment.