From e7d8355c37b06e424c7e9ffb1b1a20820764ac8e Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 29 Nov 2021 05:14:46 +0100 Subject: [PATCH] Fix router order bugs --- src/HttpRouter.h | 16 +++++++++++++++- tests/HttpRouter.cpp | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/HttpRouter.h b/src/HttpRouter.h index ef83654b4..d9b0dffe7 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -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 : parent->children) { @@ -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(); } diff --git a/tests/HttpRouter.cpp b/tests/HttpRouter.cpp index 3c00f5f70..1d0bd2d90 100644 --- a/tests/HttpRouter.cpp +++ b/tests/HttpRouter.cpp @@ -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"); } {