Skip to content

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

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

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
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

2 participants