高性能的 URL 路由C语言开发包:R3
jopen
10年前
R3是一个URL路由分发开发库,拥有较高的性能。采用C语言实现。可将你的路由规则编译成前缀树。 在启动时利用构造前缀树,你可以高效的分发路线。
Requirement
- autoconf
- automake
- check
- pcre
- jemalloc
- graphviz version 2.38.0 (20140413.2041)
- pkg-config
Pattern Syntax
/blog/post/{id} use [^/]+ regular expression by default. /blog/post/{id:\d+} use `\d+` regular expression instead of default.
C API
#include <r3.h> // create a router tree with 10 children capacity (this capacity can grow dynamically) n = r3_tree_create(10); int route_data = 3; // insert the route path into the router tree r3_tree_insert_pathl(n , "/zoo" , strlen("/zoo") , NULL, &route_data ); r3_tree_insert_pathl(n , "/foo/bar" , strlen("/foo/bar") , NULL, &route_data ); r3_tree_insert_pathl(n , "/bar" , strlen("/bar") , NULL, &route_data ); r3_tree_insert_pathl(n , "/post/{id}" , strlen("/post/{id}") , NULL, &route_data ); r3_tree_insert_pathl(n , "/user/{id:\\d+}" , strlen("/user/{id:\\d+}") , NULL, &route_data ); // let's compile the tree! r3_tree_compile(n); // dump the compiled tree r3_tree_dump(n, 0); // match a route node *matched_node = r3_tree_match(n, "/foo/bar", strlen("/foo/bar"), NULL); matched_node->endpoint; // make sure there is a route end at here. int ret = *( (*int) matched_node->route_ptr );