int Server_add_host(Server *srv, Host *host) { check(host->matching != NULL, "Host's matching can't be NULL."); return RouteMap_insert_reversed(srv->hosts, host->matching, host); error: return -1; }
char *test_routing_match_reversed() { RouteMap *routes = RouteMap_create(NULL); mu_assert(routes != NULL, "Failed to make the route map."); char *route_data1 = "route1"; char *route_data2 = "route2"; char *route_data3 = "route3"; bstring route1 = bfromcstr("foo"); bstring route2 = bfromcstr("moo"); bstring route3 = bfromcstr("fio"); Route *route = NULL; RouteMap_insert_reversed(routes, route1, route_data1); RouteMap_insert_reversed(routes, route2, route_data2); RouteMap_insert_reversed(routes, route3, route_data3); bstring path1 = bfromcstr("foo"); bstring path2 = bfromcstr("moo"); bstring path3 = bfromcstr("fio"); route = RouteMap_match_suffix(routes, path1); mu_assert(route != NULL, "Pattern match failed."); mu_assert(route->data == route_data1, "Pattern matched wrong route."); route = RouteMap_match_suffix(routes, path2); mu_assert(route != NULL, "Pattern match failed."); mu_assert(route->data == route_data2, "Pattern matched wrong route."); route = RouteMap_match_suffix(routes, path3); mu_assert(route != NULL, "Pattern match failed."); mu_assert(route->data == route_data3, "Pattern matched wrong route."); bdestroy(path1); bdestroy(path2); bdestroy(path3); RouteMap_destroy(routes); return NULL; }
int Command_route(Command *cmd) { int rc = 0; RouteMap *map = RouteMap_create(null_destroy); check(map != NULL, "Error, can't create the RouteMap."); bstring match = option(cmd, "match", NULL); bstring pattern = option(cmd, "pattern", NULL); bstring reversed = option(cmd, "reversed", NULL); bstring sfxmatch = option(cmd, "suffix", NULL); check(match != NULL, "You have to give something to match against with --match."); check(pattern != NULL, "You have to give a pattern to use with --pattern."); if(reversed == NULL) { rc = RouteMap_insert(map, bstrcpy(pattern), NULL); check(rc == 0, "Failed to insert pattern into routing table."); } else { rc = RouteMap_insert_reversed(map, bstrcpy(pattern), NULL); check(rc == 0, "Failed to insert REVERSED pattern into routing table."); } Route *route = NULL; if(sfxmatch != NULL) { route = RouteMap_match_suffix(map, match); } else { route = RouteMap_simple_prefix_match(map, match); } if(route != NULL) { printf("Match passed on '%s' against '%s'.\n", bdata(match), bdata(pattern)); printf("ROUTE: pattern='%s', prefix='%s'\n", bdata(route->pattern), bdata(route->prefix)); } else { printf("Match FAILED on '%s' against '%s'.\n", bdata(match), bdata(pattern)); } RouteMap_destroy(map); return 0; error: RouteMap_destroy(map); return -1; }