Skip to content

Commit

Permalink
Fix router order bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Nov 29, 2021
1 parent 2017892 commit e7d8355
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/HttpRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ struct HttpRouter {
Node(std::string name) : name(name) {}
} root = {"rootNode"};

/* Sort wildcards after alphanum */
int lexicalOrder(std::string &name) {
if (!name.length()) {
return 2;
}
if (name[0] == ':') {
return 1;
}
if (name[0] == '*') {
return 0;
}
return 2;
}

/* Advance from parent to child, adding child if necessary */
Node *getNode(Node *parent, std::string child, bool isHighPriority) {
for (std::unique_ptr<Node> &node : parent->children) {
Expand All @@ -82,7 +96,7 @@ struct HttpRouter {
return a->isHighPriority;
}

return b->name.length() && (parent != &root) && (b->name < a->name);
return b->name.length() && (parent != &root) && (lexicalOrder(b->name) < lexicalOrder(a->name));
}), std::move(newNode))->get();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/HttpRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ void testBugReports() {
r.route("get", "/1ab");
// this one fails with IDONEAB
std::cout << result << std::endl;
assert(result == "ONEAB");
assert(result == "ONEABID");
}

{
Expand Down

0 comments on commit e7d8355

Please sign in to comment.