Permalink
Browse files

Multiple patterns with same prefix

  • Loading branch information...
1 parent 0138958 commit 42834e88abaceb596f9433cb90fb15ffe75b2eab @jasom jasom committed Dec 14, 2011
Showing with 44 additions and 3 deletions.
  1. +20 −3 src/routing.c
  2. +1 −0 src/routing.h
  3. +23 −0 tests/routing_tests.c
View
@@ -81,17 +81,26 @@ void RouteMap_destroy(RouteMap *map)
Route *RouteMap_insert_base(RouteMap *map, bstring prefix, bstring pattern)
{
Route *route = h_malloc(sizeof(Route));
+ Route *route2;
check_mem(route);
route->pattern = pattern;
check(route->pattern, "Pattern is required.");
route->prefix = prefix;
check(route->prefix, "Prefix is required.");
+ route->next = NULL;
debug("ADDING prefix: %s, pattern: %s", bdata(prefix), bdata(pattern));
-
- map->routes = tst_insert(map->routes, bdata(prefix), blength(prefix), route);
+ route2= tst_search(map->routes, bdata(prefix), blength(prefix));
+
+ if (route2 != NULL) {
+ route->next=route2->next;
+ route2->next=route;
+ }
+ else {
+ map->routes = tst_insert(map->routes, bdata(prefix), blength(prefix), route);
+ }
hattach(route, map);
@@ -225,12 +234,20 @@ Route *RouteMap_simple_prefix_match(RouteMap *map, bstring target)
{
debug("Searching for route: %s in map: %p", bdata(target), map);
Route *route = tst_search_prefix(map->routes, bdata(target), blength(target));
+ Route *matched;
if(route) {
debug("Found simple prefix: %s", bdata(route->pattern));
if(route->has_pattern) {
- return match_route_pattern(target, route, 0);
+ while(route != NULL)
+ {
+ Route *matched = match_route_pattern(target, route, 0);
+ if(matched != NULL) {
+ return matched;
+ }
+ route=route->next;
+ }
} else {
return route;
}
View
@@ -46,6 +46,7 @@ typedef struct Route {
void *data;
int has_pattern;
int first_paren;
+ struct Route *next;
} Route;
struct RouteMap;
View
@@ -85,7 +85,29 @@ char *test_simple_prefix_matching()
return NULL;
}
+char *test_simple_prefix_pattern_matching()
+{
+ RouteMap *routes = RouteMap_create(NULL);
+ mu_assert(routes != NULL, "Failed to make the route map.");
+ char *route_data0 = "route0";
+ char *route_data1 = "route1";
+ char *route_data2 = "route2";
+ bstring route0 = bfromcstr("/");
+ bstring route1 = bfromcstr("/img/(.*).jpg$");
+ bstring route2 = bfromcstr("/img/(.*).png$");
+
+ RouteMap_insert(routes, route0, route_data0);
+ RouteMap_insert(routes, route1, route_data1);
+ RouteMap_insert(routes, route2, route_data2);
+
+ mu_assert(check_simple_prefix(routes, "/img/foo.jpg", route_data1), "Failed 1.");
+ mu_assert(check_simple_prefix(routes, "/img/foo.png", route_data2), "Failed 2.");
+ mu_assert(check_simple_prefix(routes, "/img/foo.bmp", NULL), "Failed 3.");
+ RouteMap_destroy(routes);
+
+ return NULL;
+}
char *test_routing_match()
{
RouteMap *routes = RouteMap_create(NULL);
@@ -238,6 +260,7 @@ char * all_tests() {
mu_run_test(test_routing_match);
mu_run_test(test_simple_prefix_matching);
mu_run_test(test_routing_match_reversed);
+ mu_run_test(test_simple_prefix_pattern_matching);
return NULL;
}

0 comments on commit 42834e8

Please sign in to comment.