Router API Spec
Overview
The Router module maps HTTP request paths and methods to handler functions. It supports two routing strategies: array-based (for small route sets) and trie-based (for large route sets with prefix matching). Transition between the two modes is automatic.
Interfaces
uvhttp_router_new
- Signature:
uvhttp_error_t uvhttp_router_new(uvhttp_router_t** router) - Purpose: Create a new router instance
- Preconditions:
routermust be a non-NULL pointer touvhttp_router_t* - Postconditions: On success,
*routerpoints to a valid router withroute_count=0,use_trie=0,array_routes=NULL,node_pool=NULL. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM:routeris NULLUVHTTP_ERROR_OUT_OF_MEMORY: allocation failure
- Thread safety: Not thread-safe.
uvhttp_router_free
- Signature:
void uvhttp_router_free(uvhttp_router_t* router) - Purpose: Free all router resources
- Preconditions:
routermust be valid (fromuvhttp_router_new). Can be NULL (no-op). - Postconditions: All memory is freed. The router pointer is invalid after return.
- Thread safety: Not thread-safe.
uvhttp_router_add_route
- Signature:
uvhttp_error_t uvhttp_router_add_route(uvhttp_router_t* router, const char* path, uvhttp_request_handler_t handler) - Purpose: Add a route for all HTTP methods
- Preconditions:
routermust be valid.pathmust be non-NULL.handlermust be non-NULL. - Postconditions: The route is added to the router. If the route count exceeds the array threshold, automatic migration to trie mode occurs.
- Error conditions:
UVHTTP_ERROR_INVALID_PARAM:router,path, orhandleris NULLUVHTTP_ERROR_OUT_OF_MEMORY: allocation failureUVHTTP_ERROR_NOT_FOUND: route count exceeds MAX_ROUTES
- Thread safety: Not thread-safe.
uvhttp_router_add_route_method
- Signature:
uvhttp_error_t uvhttp_router_add_route_method(uvhttp_router_t* router, const char* path, uvhttp_method_t method, uvhttp_request_handler_t handler) - Purpose: Add a route for a specific HTTP method
- Preconditions: Same as
uvhttp_router_add_route, plusmethodmust be a validuvhttp_method_t. - Postconditions: Same as
uvhttp_router_add_route, but the route is only matched for the specified method. - Error conditions: Same as
uvhttp_router_add_route. - Thread safety: Not thread-safe.
uvhttp_router_find_handler
- Signature:
uvhttp_request_handler_t uvhttp_router_find_handler(const uvhttp_router_t* router, const char* path, const char* method) - Purpose: Find the handler for a given path and method
- Preconditions:
routermust be valid.pathmust be non-NULL.methodmust be a valid HTTP method string. - Postconditions: Returns the matching handler, or NULL if no match is found.
- Thread safety: Thread-safe for reads after all routes are added.
uvhttp_router_match
- Signature:
uvhttp_error_t uvhttp_router_match(const uvhttp_router_t* router, const char* path, const char* method, uvhttp_route_match_t* match) - Purpose: Match a path and extract path parameters
- Preconditions: Same as
uvhttp_router_find_handler, plusmatchmust be non-NULL. - Postconditions: On success,
match->handleris set,match->paramscontains extracted parameters,match->param_countis set. - Error conditions:
UVHTTP_ERROR_INVALID_PARAM: any argument is NULLUVHTTP_ERROR_NOT_FOUND: no matching route
- Thread safety: Thread-safe for reads.
Route Path Syntax
/users— exact match/users/:id— parameter match (extractsidfrom path segment)/static/*— prefix match (matches any path starting with/static/)/api/v1/users— static prefix with multiple segments
Behavior Rules
Array mode: Routes are stored in a flat array. Matching is O(n) linear scan. Used when route count is below the migration threshold.
Trie mode: Routes are stored in a compact prefix trie (128-byte nodes). Matching is O(k) where k is the path length. Used after automatic migration.
Automatic migration: When the route count exceeds the array threshold (default: 8), the array is migrated to a trie. Migration is transparent: all routes remain functional.
Parameter extraction: Path parameters (
:param) are extracted during matching. Parameters are stored inuvhttp_route_match_t::params.Wildcard routes: The
*wildcard matches any path prefix. Wildcard routes have the lowest priority.Method matching: When a route is added with a specific method, only requests with that method match. Routes added without a method match all methods.
Fallback handler: If set, the fallback handler is called when no route matches. The fallback handler is the last resort before returning 404.
Performance Requirements
- Array mode matching: O(n) where n = route count
- Trie mode matching: O(k) where k = path segment count
- Route addition: O(1) amortized (array mode), O(k) (trie mode)
- Node size: 128 bytes (2 cache lines)
- Memory: ~128 bytes per route (trie mode), ~512 bytes per route (array mode)
- Migration: automatic, transparent
Test Requirements
- Route addition (all methods, specific methods, wildcard, parameter)
- Route matching (exact, prefix, parameter, method-specific)
- Non-matching routes return NULL handler
- Array-to-trie migration
- NULL parameter handling for all public functions
- Maximum route count enforcement
- Parameter extraction correctness
- Fallback handler behavior
- Static file route registration
- Memory cleanup (no leaks on free)