branch at the correct position (avoid broken slug)

This commit is contained in:
c9s 2014-05-15 23:03:23 +08:00
parent cd3e98465b
commit 9381ab943c
2 changed files with 21 additions and 1 deletions

View file

@ -217,6 +217,7 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
n = child; n = child;
return n; return n;
} else if ( dl == e->pattern_len ) { // fully-equal to the pattern of the edge } else if ( dl == e->pattern_len ) { // fully-equal to the pattern of the edge
char * subroute = route + dl; char * subroute = route + dl;
int subroute_len = route_len - dl; int subroute_len = route_len - dl;
@ -230,7 +231,18 @@ rnode * rnode_insert_routel(rnode *tree, char *route, int route_len)
} }
} else if ( dl < e->pattern_len ) { } else if ( dl < e->pattern_len ) {
printf("branch the edge dl: %d\n", dl); // printf("branch the edge dl: %d\n", dl);
// branch the edge at correct position (avoid broken slugs)
char *slug_s = strchr(route, '{');
char *slug_e = strchr(route, '}');
if ( dl > (slug_s - route) && dl < (slug_e - route) ) {
// break before '{'
dl = slug_s - route;
}
/* it's partically matched with the pattern, /* it's partically matched with the pattern,
* we should split the end point and make a branch here... * we should split the end point and make a branch here...
*/ */

View file

@ -106,6 +106,14 @@ START_TEST (test_rnode_insert_routel)
rnode_insert_routel(n, "/f/id", strlen("/f/id") ); rnode_insert_routel(n, "/f/id", strlen("/f/id") );
rnode_dump(n, 0); rnode_dump(n, 0);
printf("Inserting /post/{id}\n");
rnode_insert_routel(n, "/post/{id}", strlen("/post/{id}") );
rnode_dump(n, 0);
printf("Inserting /post/{handle}\n");
rnode_insert_routel(n, "/post/{handle}", strlen("/post/{handle}") );
rnode_dump(n, 0);
/* /*
fail_if(n == NULL, "rnode tree"); fail_if(n == NULL, "rnode tree");