r3_node_match_condition

This commit is contained in:
c9s 2014-05-18 12:56:53 +08:00
parent 641d1e53d8
commit 1632f9807e
4 changed files with 34 additions and 16 deletions

View file

@ -156,6 +156,8 @@ void r3_node_append_condition(node * n, condition * condition);
void condition_free(condition * condition); void condition_free(condition * condition);
condition * r3_node_match_condition(node *n, match_entry * entry);
#define METHOD_GET 2 #define METHOD_GET 2
#define METHOD_POST 2<<1 #define METHOD_POST 2<<1
#define METHOD_PUT 2<<1 #define METHOD_PUT 2<<1

View file

@ -304,9 +304,12 @@ condition * r3_node_match_condition(node *n, match_entry * entry) {
if (n->conditions && n->condition_len > 0) { if (n->conditions && n->condition_len > 0) {
int i; int i;
for (i = 0; i < n->condition_len ; 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) { 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 ) { if ( r1->path && r2->path ) {
ret = strcmp(r1->path, r2->path); if ( strcmp(r1->path, r2->path) != 0 ) {
if (ret != 0) {
return -1; return -1;
} }
} }
if ( r1->host && r2->host ) { if ( r1->host && r2->host ) {
ret = strcmp(r1->host, r2->host); if (strcmp(r1->host, r2->host) != 0 ) {
if (ret != 0) {
return -1; return -1;
} }
} }
if (r1->remote_addr_pattern) { if (r1->remote_addr_pattern) {
ret = strcmp(r1->remote_addr_pattern, r2->remote_addr); if ( strcmp(r1->remote_addr_pattern, r2->remote_addr) != 0 ) {
if (ret != 0) {
return -1; return -1;
} }
} }

View file

@ -134,3 +134,13 @@
1400388529,10815527.23 1400388529,10815527.23
1400388539,10930897.10 1400388539,10930897.10
1400388591,10720695.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

1 1400242718 5649455.80
134 1400388529 10815527.23
135 1400388539 10930897.10
136 1400388591 10720695.10
137 1400388676 10571166.21
138 1400388683 10882166.49
139 1400388712 11008826.02
140 1400388799 10728815.96
141 1400388848 10409083.22
142 1400388867 10470546.30
143 1400388931 10554179.23
144 1400388944 10969249.80
145 1400388966 10116208.17
146 1400389005 6688733.32

View file

@ -225,21 +225,24 @@ END_TEST
START_TEST(test_route_cmp) START_TEST(test_route_cmp)
{ {
condition *r1 = condition_create("/blog/post"); 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; r1->request_method = METHOD_GET;
r2->request_method = METHOD_GET; m->request_method = METHOD_GET;
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; r1->request_method = METHOD_GET;
r2->request_method = METHOD_POST; m->request_method = METHOD_POST;
fail_if( condition_cmp(r1, r2) == 0, "should be different"); 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); condition_free(r1);
match_entry_free(r2); match_entry_free(m);
} }
END_TEST END_TEST
@ -263,6 +266,11 @@ START_TEST(test_insert_route)
node *m; node *m;
m = r3_tree_match(n , "/qux/bar/corge", strlen("/qux/bar/corge"), NULL); 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); match_entry_free(entry);
condition_free(r1); condition_free(r1);
condition_free(r2); condition_free(r2);