Backend *Host_match_backend(Host *host, bstring target, Route **out_route) { Route *found = NULL; debug("MATCHING BACKEND IN HOST %p AGAINST %s in ROUTES: %p", host, bdata(target), host->routes); found = RouteMap_simple_prefix_match(host->routes, target); if(found) { debug("Found backend at %s", bdata(found->pattern)); assert(found->data && "Invalid value for stored route."); if(out_route) *out_route = found; return found->data; } else { if(out_route) *out_route = found; 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; }
int check_simple_prefix(RouteMap *routes, const char* path, const char *expected) { bstring path_str = bfromcstr(path); Route *found = RouteMap_simple_prefix_match(routes, path_str); debug("Testing simple prefix: %s to match %s and found %s", path, expected, found == NULL ? "NULL" : (const char *)found->data); if(expected != NULL) { check(found != NULL, "Should find a match."); check(found->data == expected, "Didn't match."); } else { check(found == NULL, "Should not find a match."); } bdestroy(path_str); return 1; error: bdestroy(path_str); return 0; }