Fix deep parameter routes

This commit is contained in:
Alex Hultman
2024-01-25 01:37:45 +01:00
parent fa5f5e3d63
commit fc2b7be765
2 changed files with 35 additions and 1 deletions
+6 -1
View File
@@ -286,7 +286,12 @@ public:
/* Iterate over all segments */ /* Iterate over all segments */
setUrl(pattern); setUrl(pattern);
for (int i = 0; !getUrlSegment(i).second; i++) { 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) */ /* 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())); node->handlers.insert(std::upper_bound(node->handlers.begin(), node->handlers.end(), (uint32_t) (priority | handlers.size())), (uint32_t) (priority | handlers.size()));
+29
View File
@@ -46,6 +46,34 @@ void testMethodPriority() {
assert(result == "PSAS"); 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() { void testPatternPriority() {
std::cout << "TestPatternPriority" << std::endl; std::cout << "TestPatternPriority" << std::endl;
uWS::HttpRouter<int> r; uWS::HttpRouter<int> r;
@@ -402,6 +430,7 @@ void testPerformance() {
} }
int main() { int main() {
testDeepParameterRoutes();
testPatternPriority(); testPatternPriority();
testMethodPriority(); testMethodPriority();
testUpgrade(); testUpgrade();