Refactor HttpService routing to trie-based matching with deterministic wildcard fallback#834
Open
Copilot wants to merge 2 commits into
Open
Refactor HttpService routing to trie-based matching with deterministic wildcard fallback#834Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
…order Agent-Logs-Url: https://github.com/ithewei/libhv/sessions/a6daa3d4-3ada-463b-ab36-863bd45f48f3 Co-authored-by: ithewei <26049660+ithewei@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor HTTP router using trie data structure
Refactor HttpService routing to trie-based matching with deterministic wildcard fallback
May 18, 2026
Owner
|
@copilot 不用Kept pathHandlers intact for existing APIs/introspection,我希望你先使用c++实现一个Trie,然后基于Trie实现一个HttpRouter,然后HttpService使用这个HttpRouter |
There was a problem hiding this comment.
Pull request overview
Refactors HttpService route resolution from an order-dependent iteration over an unordered_map to a deterministic trie-based match for static/parameterized routes, with a separate ordered wildcard list as a fallback. This prevents catch-all * handlers from unpredictably shadowing more specific routes.
Changes:
- Introduces
route_trie_nodeandwildcardHandlers;AddRoutenow inserts non-wildcard paths into the trie and wildcard paths into an ordered list (while still populatingpathHandlers). - Rewrites
GetRoute(HttpRequest*, ...)to (1) match via the trie with REST param extraction, then (2) fall back to wildcard patterns in registration order, preservingMETHOD_NOT_ALLOWEDsemantics. - Adds
unittest/http_service_route_test.cpp(and wires it intounittest/CMakeLists.txt) covering exact-vs-wildcard precedence, REST param extraction, wildcard fallback, and method-not-allowed.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| http/server/HttpService.h | Adds route_trie_node, http_wildcard_handlers, new members, and initializes routeTrie in the constructor. |
| http/server/HttpService.cpp | Adds trie/wildcard helpers, populates trie/wildcard structures in AddRoute, and rewrites GetRoute lookup. |
| unittest/http_service_route_test.cpp | New focused unit test for exact, REST-param, wildcard fallback, and method-not-allowed routing. |
| unittest/CMakeLists.txt | Builds the new http_service_route_test and adds it to the unittest aggregate target. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+135
to
+140
| if (!param_name.empty()) { | ||
| if (!node->param_child) { | ||
| node->param_child = std::make_shared<route_trie_node>(); | ||
| } | ||
| node = node->param_child; | ||
| node->param_name = param_name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
HttpService::GetRoute(HttpRequest*, ...)depended onunordered_mapiteration order, so a catch-allANY("*")could shadow specific routes unpredictably. This refactor makes route resolution deterministic by splitting exact/param matching from wildcard fallback.Router data model
/a/b,/:id,/{id}).*patterns.pathHandlersintact for existing APIs/introspection.Route registration (
AddRoute)*).Route lookup (
GetRoute(HttpRequest*, ...))req->query_params.METHOD_NOT_ALLOWEDsemantics are preserved when path matches but method does not.Coverage