Skip to content

Commit fa9a76d

Browse files
committed
Fix removal of param routes
1 parent a634261 commit fa9a76d

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/HttpRouter.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ struct HttpRouter {
217217
std::string segment = std::string(getUrlSegment(i).first);
218218
Node *next = nullptr;
219219
for (std::unique_ptr<Node> &child : n->children) {
220-
if (child->name == segment && child->isHighPriority == (priority == HIGH_PRIORITY)) {
220+
if (((segment.length() && child->name.length() && segment[0] == ':' && child->name[0] == ':') || child->name == segment) && child->isHighPriority == (priority == HIGH_PRIORITY)) {
221221
next = child.get();
222222
break;
223223
}
@@ -271,7 +271,10 @@ struct HttpRouter {
271271
}
272272
}
273273

274-
/* Always test any route last */
274+
/* Always test any route last (this check should not be necessary if we always have at least one handler) */
275+
if (root.children.empty()) [[unlikely]] {
276+
return false;
277+
}
275278
return executeHandlers(root.children.back().get(), 0, userData);
276279
}
277280

@@ -356,11 +359,11 @@ struct HttpRouter {
356359
/* Removes ALL routes with the same handler as can be found with the given parameters.
357360
* Removing a wildcard is done by removing ONE OF the methods the wildcard would match with.
358361
* Example: If wildcard includes POST, GET, PUT, you can remove ALL THREE by removing GET. */
359-
void remove(std::string method, std::string pattern, uint32_t priority) {
362+
bool remove(std::string method, std::string pattern, uint32_t priority) {
360363
uint32_t handler = findHandler(method, pattern, priority);
361364
if (handler == UINT32_MAX) {
362365
/* Not found or already removed, do nothing */
363-
return;
366+
return false;
364367
}
365368

366369
/* Cull the entire tree */
@@ -371,6 +374,8 @@ struct HttpRouter {
371374

372375
/* Now remove the actual handler */
373376
handlers.erase(handlers.begin() + (handler & HANDLER_MASK));
377+
378+
return true;
374379
}
375380
};
376381

tests/HttpRouter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ void testBugReports() {
182182
r.route("GET", "/route");
183183
assert(result == "");
184184

185-
r.remove("GET", "/route/:id", r.MEDIUM_PRIORITY);
185+
std::cout << "TRYING TO DELETE /route/:id" << std::endl;
186+
bool found = r.remove("GET", "/route/:id", r.MEDIUM_PRIORITY);
187+
std::cout << "Found:" << found << std::endl;
186188

187189
/* The bug is really only this line, it does not remove parameter routes */
188190
r.route("GET", "/route/21");
189-
std::cout << result << std::endl;
191+
std::cout << "Found:" << found << ", " << result << std::endl;
190192
assert(result == "");
191193
}
192194
{

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ default:
55
./ChunkedEncoding
66
$(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree
77
./TopicTree
8-
$(CXX) -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -std=c++17 -fsanitize=address HttpRouter.cpp -o HttpRouter
8+
$(CXX) -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG -std=c++17 -g -fsanitize=address HttpRouter.cpp -o HttpRouter
99
./HttpRouter
1010
$(CXX) -std=c++17 -fsanitize=address BloomFilter.cpp -o BloomFilter
1111
./BloomFilter

0 commit comments

Comments
 (0)