diff --git a/include/r3.h b/include/r3.h index 9cafb69..8b47467 100644 --- a/include/r3.h +++ b/include/r3.h @@ -156,6 +156,8 @@ void r3_node_append_condition(node * n, condition * condition); void condition_free(condition * condition); +condition * r3_node_match_condition(node *n, match_entry * entry); + #define METHOD_GET 2 #define METHOD_POST 2<<1 #define METHOD_PUT 2<<1 diff --git a/src/node.c b/src/node.c index d975576..f0be357 100644 --- a/src/node.c +++ b/src/node.c @@ -304,9 +304,12 @@ condition * r3_node_match_condition(node *n, match_entry * entry) { if (n->conditions && n->condition_len > 0) { int i; for (i = 0; i < n->condition_len ; i++ ) { - + if ( condition_cmp(n->conditions[i], entry) == 0 ) { + return n->conditions[i]; + } } } + return NULL; } inline edge * r3_node_find_edge_str(node * n, char * str, int str_len) { @@ -541,25 +544,20 @@ int condition_cmp(condition *r1, match_entry *r2) { } } - int ret; - if ( r1->path && r2->path ) { - ret = strcmp(r1->path, r2->path); - if (ret != 0) { + if ( strcmp(r1->path, r2->path) != 0 ) { return -1; } } if ( r1->host && r2->host ) { - ret = strcmp(r1->host, r2->host); - if (ret != 0) { + if (strcmp(r1->host, r2->host) != 0 ) { return -1; } } if (r1->remote_addr_pattern) { - ret = strcmp(r1->remote_addr_pattern, r2->remote_addr); - if (ret != 0) { + if ( strcmp(r1->remote_addr_pattern, r2->remote_addr) != 0 ) { return -1; } } diff --git a/tests/bench_str.csv b/tests/bench_str.csv index acdbc55..d4e7035 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -134,3 +134,13 @@ 1400388529,10815527.23 1400388539,10930897.10 1400388591,10720695.10 +1400388676,10571166.21 +1400388683,10882166.49 +1400388712,11008826.02 +1400388799,10728815.96 +1400388848,10409083.22 +1400388867,10470546.30 +1400388931,10554179.23 +1400388944,10969249.80 +1400388966,10116208.17 +1400389005,6688733.32 diff --git a/tests/check_tree.c b/tests/check_tree.c index 8abd633..cde9d77 100644 --- a/tests/check_tree.c +++ b/tests/check_tree.c @@ -225,21 +225,24 @@ END_TEST START_TEST(test_route_cmp) { condition *r1 = condition_create("/blog/post"); - match_entry * r2 = match_entry_create("/blog/post"); + match_entry * m = match_entry_create("/blog/post"); - fail_if( condition_cmp(r1, r2) == -1, "should be the same"); + fail_if( condition_cmp(r1, m) == -1, "should match"); r1->request_method = METHOD_GET; - r2->request_method = METHOD_GET; - fail_if( condition_cmp(r1, r2) == -1, "should be the same"); + m->request_method = METHOD_GET; + fail_if( condition_cmp(r1, m) == -1, "should match"); r1->request_method = METHOD_GET; - r2->request_method = METHOD_POST; - fail_if( condition_cmp(r1, r2) == 0, "should be different"); + m->request_method = METHOD_POST; + fail_if( condition_cmp(r1, m) == 0, "should be different"); + r1->request_method = METHOD_GET; + m->request_method = METHOD_POST | METHOD_GET; + fail_if( condition_cmp(r1, m) == -1, "should match"); condition_free(r1); - match_entry_free(r2); + match_entry_free(m); } END_TEST @@ -263,6 +266,11 @@ START_TEST(test_insert_route) node *m; m = r3_tree_match(n , "/qux/bar/corge", strlen("/qux/bar/corge"), NULL); + fail_if(m == NULL); + condition *c = r3_node_match_condition(m, entry); + fail_if(c == NULL); + + match_entry_free(entry); condition_free(r1); condition_free(r2);