Skip to content

Refactor HttpService routing to trie-based matching with deterministic wildcard fallback#834

Open
Copilot wants to merge 2 commits into
masterfrom
copilot/refactor-http-router-with-trie
Open

Refactor HttpService routing to trie-based matching with deterministic wildcard fallback#834
Copilot wants to merge 2 commits into
masterfrom
copilot/refactor-http-router-with-trie

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 18, 2026

HttpService::GetRoute(HttpRequest*, ...) depended on unordered_map iteration order, so a catch-all ANY("*") could shadow specific routes unpredictably. This refactor makes route resolution deterministic by splitting exact/param matching from wildcard fallback.

  • Router data model

    • Added a trie structure for non-wildcard routes (/a/b, /:id, /{id}).
    • Added ordered wildcard route storage to preserve registration order for * patterns.
    • Kept pathHandlers intact for existing APIs/introspection.
  • Route registration (AddRoute)

    • New paths are inserted into:
      • trie (non-wildcard), or
      • wildcard list (contains *).
    • Existing method handler update behavior is unchanged.
  • Route lookup (GetRoute(HttpRequest*, ...))

    • Match order is now:
      1. trie (exact/static + REST params),
      2. wildcard fallback.
    • Parameter extraction remains mapped into req->query_params.
    • METHOD_NOT_ALLOWED semantics are preserved when path matches but method does not.
  • Coverage

    • Added focused unit test for:
      • exact route precedence over wildcard catch-all,
      • REST param extraction,
      • wildcard fallback behavior,
      • method-not-allowed on matched path.
HttpService router;
router.GET("/status", status_handler);
router.GET("/user/:id", user_handler);
router.Any("*", fallback_handler);

// "/status" resolves to status_handler (not fallback),
// "/user/123" extracts id=123,
// unknown paths resolve via fallback.

Copilot AI linked an issue May 18, 2026 that may be closed by this pull request
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
Copilot AI requested a review from ithewei May 18, 2026 07:34
@ithewei
Copy link
Copy Markdown
Owner

ithewei commented May 19, 2026

@copilot 不用Kept pathHandlers intact for existing APIs/introspection,我希望你先使用c++实现一个Trie,然后基于Trie实现一个HttpRouter,然后HttpService使用这个HttpRouter

@ithewei ithewei marked this pull request as ready for review May 19, 2026 11:37
Copilot AI review requested due to automatic review settings May 19, 2026 11:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_node and wildcardHandlers; AddRoute now inserts non-wildcard paths into the trie and wildcard paths into an ordered list (while still populating pathHandlers).
  • Rewrites GetRoute(HttpRequest*, ...) to (1) match via the trie with REST param extraction, then (2) fall back to wildcard patterns in registration order, preserving METHOD_NOT_ALLOWED semantics.
  • Adds unittest/http_service_route_test.cpp (and wires it into unittest/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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use trie to refactor http router

3 participants