2014-05-23 05:36:47 -04:00
|
|
|
#include "config.h"
|
2014-05-14 22:08:42 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-05-23 03:48:26 -04:00
|
|
|
#include <ctype.h>
|
2014-05-14 22:08:42 -04:00
|
|
|
|
2014-05-15 01:39:50 -04:00
|
|
|
// PCRE
|
|
|
|
#include <pcre.h>
|
|
|
|
|
2014-05-16 08:22:25 -04:00
|
|
|
#include "r3.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "r3_slug.h"
|
2014-06-01 18:27:35 -04:00
|
|
|
#include "slug.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "str.h"
|
2015-11-17 08:24:36 -05:00
|
|
|
#include "r3_debug.h"
|
2014-05-20 13:50:15 -04:00
|
|
|
#include "zmalloc.h"
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2015-11-10 07:41:52 -05:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
# define unlikely(x) __builtin_expect(!!(x), 0)
|
|
|
|
#else
|
|
|
|
# define likely(x) !!(x)
|
|
|
|
# define unlikely(x) !!(x)
|
|
|
|
#endif
|
2014-06-01 09:58:29 -04:00
|
|
|
|
|
|
|
#define CHECK_PTR(ptr) if (ptr == NULL) return NULL;
|
|
|
|
|
2014-05-14 22:08:42 -04:00
|
|
|
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-20 11:25:12 -04:00
|
|
|
|
|
|
|
static int strndiff(char * d1, char * d2, unsigned int n) {
|
|
|
|
char * o = d1;
|
2014-05-20 13:50:15 -04:00
|
|
|
while ( *d1 == *d2 && n-- > 0 ) {
|
2014-05-20 11:25:12 -04:00
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
|
|
|
return d1 - o;
|
|
|
|
}
|
|
|
|
|
2014-06-03 10:15:59 -04:00
|
|
|
/*
|
2014-05-20 11:25:12 -04:00
|
|
|
static int strdiff(char * d1, char * d2) {
|
|
|
|
char * o = d1;
|
2014-05-20 13:50:15 -04:00
|
|
|
while( *d1 == *d2 ) {
|
2014-05-20 11:25:12 -04:00
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
|
|
|
return d1 - o;
|
|
|
|
}
|
2014-06-03 10:15:59 -04:00
|
|
|
*/
|
2014-05-20 11:25:12 -04:00
|
|
|
|
|
|
|
|
2014-05-15 06:26:41 -04:00
|
|
|
/**
|
2014-05-16 03:29:25 -04:00
|
|
|
* Create a node object
|
2014-05-15 06:26:41 -04:00
|
|
|
*/
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node * r3_tree_create(int cap) {
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Node * n = r3_mem_alloc( sizeof(R3Node) );
|
|
|
|
memset(n, 0, sizeof(*n));
|
|
|
|
|
|
|
|
r3_vector_reserve(NULL, &n->edges, n->edges.size + cap);
|
|
|
|
|
|
|
|
r3_vector_reserve(NULL, &n->routes, n->routes.size + 1);
|
|
|
|
|
|
|
|
n->compare_type = NODE_COMPARE_PCRE;
|
2014-05-15 01:39:50 -04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
void r3_tree_free(R3Node * tree) {
|
2016-03-21 22:23:37 -04:00
|
|
|
for (int j=0;j<tree->edges.size;j++) {
|
|
|
|
r3_edge_free(tree->edges.entries + j);
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
free(tree->edges.entries);
|
|
|
|
for (int k=0;k<tree->routes.size;k++) {
|
|
|
|
r3_route_free(tree->routes.entries + k);
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
free(tree->routes.entries);
|
2014-05-21 06:16:48 -04:00
|
|
|
if (tree->pcre_pattern) {
|
|
|
|
pcre_free(tree->pcre_pattern);
|
|
|
|
}
|
2014-05-21 06:28:21 -04:00
|
|
|
#ifdef PCRE_STUDY_JIT_COMPILE
|
2014-05-21 06:21:15 -04:00
|
|
|
if (tree->pcre_extra) {
|
|
|
|
pcre_free_study(tree->pcre_extra);
|
2014-05-21 06:16:48 -04:00
|
|
|
}
|
2014-05-21 06:28:21 -04:00
|
|
|
#endif
|
2014-05-22 23:15:57 -04:00
|
|
|
zfree(tree->combined_pattern);
|
2016-03-21 22:23:37 -04:00
|
|
|
free(tree);
|
2014-05-21 06:12:14 -04:00
|
|
|
tree = NULL;
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 20:19:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect two node objects, and create an edge object between them.
|
|
|
|
*/
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge * r3_node_connectl(R3Node * n, const char * pat, int len, int dupl, R3Node *child) {
|
2014-05-15 01:39:50 -04:00
|
|
|
// find the same sub-pattern, if it does not exist, create one
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge * e;
|
2014-06-01 20:04:57 -04:00
|
|
|
e = r3_node_find_edge(n, pat, len);
|
2014-05-15 06:02:10 -04:00
|
|
|
if (e) {
|
2014-05-15 10:57:13 -04:00
|
|
|
return e;
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
2014-05-23 01:49:18 -04:00
|
|
|
if (dupl) {
|
|
|
|
pat = zstrndup(pat, len);
|
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
// e = r3_edge_createl(pat, len, child);
|
|
|
|
e = r3_node_append_edge(n);
|
|
|
|
r3_edge_initl(e, pat, len, child);
|
|
|
|
// CHECK_PTR(e);
|
|
|
|
return e;
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Edge * r3_node_append_edge(R3Node *n)
|
2015-11-10 07:29:07 -05:00
|
|
|
{
|
2016-03-21 22:23:37 -04:00
|
|
|
r3_vector_reserve(NULL, &n->edges, n->edges.size + 1);
|
|
|
|
R3Edge *new_e = n->edges.entries + n->edges.size++;
|
|
|
|
memset(new_e, 0, sizeof(*new_e));
|
|
|
|
return new_e;
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 20:04:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the existing edge with specified pattern (include slug)
|
2014-06-01 20:19:44 -04:00
|
|
|
*
|
|
|
|
* if "pat" is a slug, we should compare with the specified pattern.
|
2014-06-01 20:04:57 -04:00
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Edge * r3_node_find_edge(const R3Node * n, const char * pat, unsigned int pat_len) {
|
|
|
|
R3Edge *edge_entries = n->edges.entries;
|
|
|
|
R3Edge *e;
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0 ; i < n->edges.size ; i++ ) {
|
|
|
|
e = edge_entries + i;
|
2014-06-01 20:04:57 -04:00
|
|
|
// there is a case: "{foo}" vs "{foo:xxx}",
|
|
|
|
// we should return the match result: full-match or partial-match
|
2016-03-21 22:23:37 -04:00
|
|
|
if (e->pattern.len == pat_len &&
|
|
|
|
!strncmp(e->pattern.base, pat, e->pattern.len)) {
|
2015-11-10 07:31:36 -05:00
|
|
|
return e;
|
2014-05-15 01:39:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
int r3_tree_compile(R3Node *n, char **errstr)
|
2014-05-16 00:33:59 -04:00
|
|
|
{
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int i;
|
2014-05-31 13:51:42 -04:00
|
|
|
int ret = 0;
|
2016-03-21 22:23:37 -04:00
|
|
|
// bool use_slug = r3_node_has_slug_edges(n);
|
|
|
|
if ( r3_node_has_slug_edges(n) ) {
|
|
|
|
if ( ret = r3_tree_compile_patterns(n, errstr) ) {
|
2014-05-31 13:51:42 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2014-05-16 00:33:59 -04:00
|
|
|
} else {
|
|
|
|
// use normal text matching...
|
|
|
|
n->combined_pattern = NULL;
|
|
|
|
}
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
for (i = 0 ; i < n->edges.size ; i++ ) {
|
|
|
|
if ((ret = r3_tree_compile(n->edges.entries[i].child, errstr))) {
|
2014-05-31 13:51:42 -04:00
|
|
|
return ret; // stop here if error occurs
|
|
|
|
}
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
2014-05-31 13:51:42 -04:00
|
|
|
return 0;
|
2014-05-16 00:33:59 -04:00
|
|
|
}
|
2014-05-15 09:17:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function combines ['/foo', '/bar', '/{slug}'] into (/foo)|(/bar)|/([^/]+)}
|
|
|
|
*
|
2014-06-02 04:24:32 -04:00
|
|
|
* Return -1 if error occurs
|
|
|
|
* Return 0 if success
|
2014-05-15 09:17:30 -04:00
|
|
|
*/
|
2015-11-17 23:52:06 -05:00
|
|
|
int r3_tree_compile_patterns(R3Node * n, char **errstr) {
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Edge *e;
|
2014-05-15 09:17:30 -04:00
|
|
|
char * p;
|
2015-11-10 06:56:07 -05:00
|
|
|
char * cpat = zcalloc(sizeof(char) * 64 * 3); // XXX
|
2014-05-31 13:51:42 -04:00
|
|
|
if (!cpat) {
|
|
|
|
asprintf(errstr, "Can not allocate memory");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-15 09:17:30 -04:00
|
|
|
|
|
|
|
p = cpat;
|
2015-11-10 06:56:07 -05:00
|
|
|
int opcode_cnt = 0;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int i = 0;
|
|
|
|
for (; i < n->edges.size ; i++) {
|
|
|
|
e = n->edges.entries + i;
|
2015-11-10 06:56:07 -05:00
|
|
|
if (e->opcode) {
|
2014-05-23 03:48:26 -04:00
|
|
|
opcode_cnt++;
|
2015-11-10 06:56:07 -05:00
|
|
|
}
|
2014-05-23 03:48:26 -04:00
|
|
|
|
2015-11-10 06:56:07 -05:00
|
|
|
if (e->has_slug) {
|
2014-05-23 00:08:06 -04:00
|
|
|
// compile "foo/{slug}" to "foo/[^/]+"
|
2016-03-21 22:23:37 -04:00
|
|
|
char * slug_pat = r3_slug_compile(e->pattern.base, e->pattern.len);
|
|
|
|
info("slug_pat for pattern: %s\n",slug_pat);
|
2014-05-16 00:33:59 -04:00
|
|
|
strcat(p, slug_pat);
|
2016-03-08 01:19:54 -05:00
|
|
|
zfree(slug_pat);
|
2016-03-21 22:23:37 -04:00
|
|
|
info("temp pattern: %s\n",cpat);
|
2014-05-16 00:33:59 -04:00
|
|
|
} else {
|
2014-06-01 13:05:57 -04:00
|
|
|
strncat(p,"^(", 2);
|
|
|
|
p += 2;
|
2014-05-15 09:17:30 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
strncat(p, e->pattern.base, e->pattern.len);
|
|
|
|
p += e->pattern.len;
|
2014-05-15 09:17:30 -04:00
|
|
|
|
2014-05-16 00:33:59 -04:00
|
|
|
strncat(p++,")", 1);
|
|
|
|
}
|
2014-05-15 09:17:30 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( i + 1 < n->edges.size && n->edges.size > 1 ) {
|
2014-05-15 09:17:30 -04:00
|
|
|
strncat(p++,"|",1);
|
|
|
|
}
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
2014-05-16 08:51:30 -04:00
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
info("pattern: %s\n",cpat);
|
|
|
|
|
2014-05-23 00:08:06 -04:00
|
|
|
// if all edges use opcode, we should skip the combined_pattern.
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( opcode_cnt == n->edges.size ) {
|
2014-05-23 03:48:26 -04:00
|
|
|
// zfree(cpat);
|
|
|
|
n->compare_type = NODE_COMPARE_OPCODE;
|
|
|
|
} else {
|
|
|
|
n->compare_type = NODE_COMPARE_PCRE;
|
2014-05-23 00:08:06 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
info("COMPARE_TYPE: %d\n",n->compare_type);
|
2014-05-16 08:58:30 -04:00
|
|
|
|
2014-05-15 09:17:30 -04:00
|
|
|
n->combined_pattern = cpat;
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-31 13:51:42 -04:00
|
|
|
const char *pcre_error;
|
|
|
|
int pcre_erroffset;
|
2014-05-17 21:05:55 -04:00
|
|
|
unsigned int option_bits = 0;
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
n->ov_cnt = (1 + n->edges.size) * 3;
|
2014-05-24 06:45:43 -04:00
|
|
|
|
2014-05-21 06:20:48 -04:00
|
|
|
if (n->pcre_pattern) {
|
|
|
|
pcre_free(n->pcre_pattern);
|
2014-05-21 06:16:48 -04:00
|
|
|
}
|
2014-05-16 02:05:51 -04:00
|
|
|
n->pcre_pattern = pcre_compile(
|
|
|
|
n->combined_pattern, /* the pattern */
|
2014-05-17 21:05:55 -04:00
|
|
|
option_bits, /* default options */
|
2014-05-31 13:51:42 -04:00
|
|
|
&pcre_error, /* for error message */
|
|
|
|
&pcre_erroffset, /* for error offset */
|
2014-05-16 02:05:51 -04:00
|
|
|
NULL); /* use default character tables */
|
2014-05-16 03:29:25 -04:00
|
|
|
if (n->pcre_pattern == NULL) {
|
2014-05-31 13:51:42 -04:00
|
|
|
if (errstr) {
|
2014-06-01 08:15:25 -04:00
|
|
|
asprintf(errstr, "PCRE compilation failed at offset %d: %s, pattern: %s", pcre_erroffset, pcre_error, n->combined_pattern);
|
2014-05-31 13:51:42 -04:00
|
|
|
}
|
|
|
|
return -1;
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
2014-05-21 06:28:21 -04:00
|
|
|
#ifdef PCRE_STUDY_JIT_COMPILE
|
|
|
|
if (n->pcre_extra) {
|
|
|
|
pcre_free_study(n->pcre_extra);
|
|
|
|
}
|
2014-05-31 13:51:42 -04:00
|
|
|
n->pcre_extra = pcre_study(n->pcre_pattern, 0, &pcre_error);
|
2016-03-21 22:23:37 -04:00
|
|
|
if (!n->pcre_extra) {
|
2014-05-31 13:51:42 -04:00
|
|
|
if (errstr) {
|
2014-06-01 08:15:25 -04:00
|
|
|
asprintf(errstr, "PCRE study failed at offset %s, pattern: %s", pcre_error, n->combined_pattern);
|
2014-05-31 13:51:42 -04:00
|
|
|
}
|
|
|
|
return -1;
|
2014-05-16 03:29:25 -04:00
|
|
|
}
|
2014-05-21 06:28:21 -04:00
|
|
|
#endif
|
2014-05-31 13:51:42 -04:00
|
|
|
return 0;
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-16 02:24:00 -04:00
|
|
|
|
2014-05-18 00:32:20 -04:00
|
|
|
|
2014-05-16 08:51:30 -04:00
|
|
|
/**
|
|
|
|
* This function matches the URL path and return the left node
|
|
|
|
*
|
2014-05-18 22:34:48 -04:00
|
|
|
* r3_tree_matchl returns NULL when the path does not match. returns *node when the path matches.
|
2014-05-16 08:51:30 -04:00
|
|
|
*
|
|
|
|
* @param node n the root of the tree
|
|
|
|
* @param char* path the URL path to dispatch
|
|
|
|
* @param int path_len the length of the URL path.
|
|
|
|
* @param match_entry* entry match_entry is used for saving the captured dynamic strings from pcre result.
|
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Node * r3_tree_matchl(const R3Node * n, const char * path, unsigned int path_len, match_entry * entry) {
|
2014-05-18 08:08:43 -04:00
|
|
|
info("try matching: %s\n", path);
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge *e;
|
2015-11-10 06:29:18 -05:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int restlen;
|
2014-05-24 05:13:55 -04:00
|
|
|
|
2014-06-04 11:03:58 -04:00
|
|
|
const char *pp;
|
|
|
|
const char *pp_end;
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
info("n->compare_type: %d\n",n->compare_type);
|
|
|
|
info("n->pcre_pattern: %s\n",n->pcre_pattern);
|
|
|
|
|
2014-05-23 04:17:35 -04:00
|
|
|
if (n->compare_type == NODE_COMPARE_OPCODE) {
|
2016-03-21 22:23:37 -04:00
|
|
|
info("NODE_COMPARE_OPCODE\n");
|
2014-06-04 11:03:58 -04:00
|
|
|
pp_end = path + path_len;
|
2014-06-04 07:54:41 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
e = n->edges.entries;
|
|
|
|
unsigned int cies = n->edges.size;
|
|
|
|
for (i = 0; i < cies; i++) {
|
2014-06-04 07:54:41 -04:00
|
|
|
pp = path;
|
2014-05-23 03:48:26 -04:00
|
|
|
switch(e->opcode) {
|
|
|
|
case OP_EXPECT_NOSLASH:
|
2014-05-23 10:58:20 -04:00
|
|
|
while (*pp != '/' && pp < pp_end) pp++;
|
|
|
|
break;
|
|
|
|
case OP_EXPECT_MORE_ALPHA:
|
|
|
|
while ( isalpha(*pp) && pp < pp_end) pp++;
|
2014-05-23 03:48:26 -04:00
|
|
|
break;
|
2014-05-23 10:58:20 -04:00
|
|
|
case OP_EXPECT_MORE_DIGITS:
|
|
|
|
while ( isdigit(*pp) && pp < pp_end) pp++;
|
2014-05-23 03:48:26 -04:00
|
|
|
break;
|
2014-05-23 10:58:20 -04:00
|
|
|
case OP_EXPECT_MORE_WORDS:
|
|
|
|
while ( (isdigit(*pp) || isalpha(*pp)) && pp < pp_end) pp++;
|
2014-05-23 03:48:26 -04:00
|
|
|
break;
|
|
|
|
case OP_EXPECT_NODASH:
|
2014-05-23 10:58:20 -04:00
|
|
|
while (*pp != '-' && pp < pp_end) pp++;
|
2014-05-23 03:48:26 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-23 04:17:35 -04:00
|
|
|
// check match
|
2015-11-10 07:42:41 -05:00
|
|
|
if ((pp - path) > 0) {
|
2014-05-23 03:48:26 -04:00
|
|
|
if (entry) {
|
2016-03-21 22:23:37 -04:00
|
|
|
str_array_append(&entry->vars , path, pp - path);
|
2014-05-23 03:48:26 -04:00
|
|
|
}
|
2015-11-10 06:48:22 -05:00
|
|
|
restlen = pp_end - pp;
|
2016-03-21 22:23:37 -04:00
|
|
|
if (!restlen) {
|
|
|
|
return e->child && e->child->endpoint ? e->child : NULL;
|
2014-05-23 03:48:26 -04:00
|
|
|
}
|
2015-11-10 06:48:22 -05:00
|
|
|
return r3_tree_matchl(e->child, pp, restlen, entry);
|
2014-05-23 03:48:26 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
e++;
|
2014-05-23 03:48:26 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 08:51:30 -04:00
|
|
|
|
|
|
|
// if the pcre_pattern is found, and the pointer is not NULL, then it's
|
|
|
|
// pcre pattern node, we use pcre_exec to match the nodes
|
|
|
|
if (n->pcre_pattern) {
|
2016-03-21 22:23:37 -04:00
|
|
|
info("COMPARE PCRE_PATTERN\n");
|
|
|
|
const char *substring_start = 0;
|
2014-05-24 06:45:43 -04:00
|
|
|
int substring_length = 0;
|
|
|
|
int ov[ n->ov_cnt ];
|
2014-08-03 13:07:37 -04:00
|
|
|
int rc;
|
2014-05-24 06:45:43 -04:00
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
info("pcre matching %s on %s\n", n->combined_pattern, path);
|
2014-05-16 06:57:36 -04:00
|
|
|
|
2014-05-16 02:05:51 -04:00
|
|
|
rc = pcre_exec(
|
2014-05-22 23:47:10 -04:00
|
|
|
n->pcre_pattern, /* the compiled pattern */
|
2014-05-22 23:31:19 -04:00
|
|
|
n->pcre_extra,
|
2014-05-22 23:47:10 -04:00
|
|
|
path, /* the subject string */
|
|
|
|
path_len, /* the length of the subject */
|
|
|
|
0, /* start at offset 0 in the subject */
|
|
|
|
0, /* default options */
|
2014-05-22 23:42:19 -04:00
|
|
|
ov, /* output vector for substring information */
|
2014-05-24 06:45:43 -04:00
|
|
|
n->ov_cnt); /* number of elements in the output vector */
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-23 11:00:49 -04:00
|
|
|
// does not match all edges, return NULL;
|
2014-05-16 02:05:51 -04:00
|
|
|
if (rc < 0) {
|
2014-05-23 11:00:49 -04:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("pcre rc: %d\n", rc );
|
2014-05-16 02:05:51 -04:00
|
|
|
switch(rc)
|
|
|
|
{
|
2014-05-22 23:47:10 -04:00
|
|
|
case PCRE_ERROR_NOMATCH:
|
|
|
|
printf("pcre: no match '%s' on pattern '%s'\n", path, n->combined_pattern);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Handle other special cases if you like
|
|
|
|
default:
|
|
|
|
printf("pcre matching error '%d' '%s' on pattern '%s'\n", rc, path, n->combined_pattern);
|
|
|
|
break;
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
2014-05-23 11:00:49 -04:00
|
|
|
#endif
|
2014-05-16 02:05:51 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-18 08:08:43 -04:00
|
|
|
|
2014-06-04 19:43:51 -04:00
|
|
|
|
|
|
|
restlen = path_len - ov[1]; // if it's fully matched to the end (rest string length)
|
2016-03-21 22:23:37 -04:00
|
|
|
int *inv = ov + 2;
|
|
|
|
if (!restlen) {
|
2014-06-04 19:43:51 -04:00
|
|
|
// Check the substring to decide we should go deeper on which edge
|
|
|
|
for (i = 1; i < rc; i++)
|
|
|
|
{
|
2016-03-21 22:23:37 -04:00
|
|
|
substring_length = *(inv+1) - *inv;
|
2014-06-04 19:43:51 -04:00
|
|
|
|
|
|
|
// if it's not matched for this edge, just skip them quickly
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( !substring_length ) {
|
|
|
|
inv += 2;
|
2014-06-04 19:43:51 -04:00
|
|
|
continue;
|
2016-03-21 22:23:37 -04:00
|
|
|
}
|
2014-06-04 19:43:51 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
substring_start = path + *inv;
|
|
|
|
e = n->edges.entries + i - 1;
|
2014-06-04 19:43:51 -04:00
|
|
|
|
|
|
|
if (entry && e->has_slug) {
|
|
|
|
// append captured token to entry
|
2016-03-21 22:23:37 -04:00
|
|
|
str_array_append(&entry->vars, substring_start, substring_length);
|
2014-06-04 19:43:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// since restlen == 0 return the edge quickly.
|
2016-03-21 22:23:37 -04:00
|
|
|
return e->child && e->child->endpoint ? e->child : NULL;
|
2014-06-04 19:43:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check the substring to decide we should go deeper on which edge
|
2016-03-21 22:23:37 -04:00
|
|
|
inv = ov + 2;
|
2014-05-16 02:05:51 -04:00
|
|
|
for (i = 1; i < rc; i++)
|
|
|
|
{
|
2016-03-21 22:23:37 -04:00
|
|
|
substring_length = *(inv+1) - *inv;
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2014-06-04 19:43:51 -04:00
|
|
|
// if it's not matched for this edge, just skip them quickly
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( !substring_length ) {
|
|
|
|
inv += 2;
|
2014-06-04 11:03:58 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
substring_start = path + *inv;
|
|
|
|
e = n->edges.entries + i - 1;
|
2014-06-04 11:03:58 -04:00
|
|
|
|
|
|
|
if (entry && e->has_slug) {
|
|
|
|
// append captured token to entry
|
2016-03-21 22:23:37 -04:00
|
|
|
str_array_append(&entry->vars , substring_start, substring_length);
|
2014-06-04 11:03:58 -04:00
|
|
|
}
|
2014-06-04 19:43:51 -04:00
|
|
|
|
2014-06-04 11:03:58 -04:00
|
|
|
// get the length of orginal string: $0
|
|
|
|
return r3_tree_matchl( e->child, path + (ov[1] - ov[0]), restlen, entry);
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
2014-05-16 02:24:00 -04:00
|
|
|
// does not match
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
info("COMPARE COMPARE_STR\n");
|
|
|
|
|
|
|
|
if (e = r3_node_find_edge_str(n, path, path_len)) {
|
|
|
|
restlen = path_len - e->pattern.len;
|
|
|
|
if (!restlen) {
|
|
|
|
return e->child && e->child->endpoint ? e->child : NULL;
|
2014-05-16 02:24:00 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
return r3_tree_matchl(e->child, path + e->pattern.len, restlen, entry);
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-24 05:13:55 -04:00
|
|
|
|
|
|
|
|
2015-11-18 01:16:26 -05:00
|
|
|
R3Route * r3_tree_match_route(const R3Node *tree, match_entry * entry) {
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node *n;
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Route *r;
|
2014-05-18 22:34:48 -04:00
|
|
|
n = r3_tree_match_entry(tree, entry);
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int i, irs = n->routes.size;
|
|
|
|
if (n && irs) {
|
|
|
|
r = n->routes.entries;
|
|
|
|
for (i = 0; irs - i; i++) {
|
|
|
|
if ( r3_route_cmp(r, entry) == 0 ) {
|
2016-03-08 01:19:54 -05:00
|
|
|
// Add slugs from found route to match_entry
|
2016-03-21 22:23:37 -04:00
|
|
|
entry->vars.slugs.entries = r->slugs.entries;
|
|
|
|
entry->vars.slugs.size = r->slugs.size;
|
|
|
|
return r;
|
2014-05-18 00:56:53 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
r++;
|
2014-05-18 00:49:58 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-18 00:56:53 -04:00
|
|
|
return NULL;
|
2014-05-18 00:49:58 -04:00
|
|
|
}
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
inline R3Edge * r3_node_find_edge_str(const R3Node * n, const char * str, int str_len) {
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Edge *e;
|
|
|
|
unsigned int i, cst = *str;
|
|
|
|
e = n->edges.entries;
|
|
|
|
unsigned int ies = n->edges.size;
|
|
|
|
for (i = 0; ies - i; i++ ) {
|
|
|
|
if (cst == *e->pattern.base) {
|
|
|
|
if (!strncmp(e->pattern.base, str, e->pattern.len)) {
|
|
|
|
return e;
|
2014-05-23 01:49:18 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
e++;
|
2014-05-16 02:05:51 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
2014-05-15 08:38:07 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
// R3Node * r3_node_create() {
|
|
|
|
// R3Node * n = (R3Node*) zmalloc( sizeof(R3Node) );
|
|
|
|
// CHECK_PTR(n);
|
|
|
|
// n->edges = NULL;
|
|
|
|
// n->edge_len = 0;
|
|
|
|
// n->edge_cap = 0;
|
|
|
|
|
|
|
|
// n->routes = NULL;
|
|
|
|
// n->route_len = 0;
|
|
|
|
// n->route_cap = 0;
|
|
|
|
|
|
|
|
// n->endpoint = 0;
|
|
|
|
// n->combined_pattern = NULL;
|
|
|
|
// n->pcre_pattern = NULL;
|
|
|
|
// n->pcre_extra = NULL;
|
|
|
|
// n->data = NULL;
|
|
|
|
// return n;
|
|
|
|
// }
|
2014-05-15 06:26:41 -04:00
|
|
|
|
2015-11-18 01:16:26 -05:00
|
|
|
void r3_route_free(R3Route * route) {
|
2016-03-08 01:19:54 -05:00
|
|
|
assert(route);
|
2016-03-21 22:23:37 -04:00
|
|
|
free(route->slugs.entries);
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
// static bool router_slugs_full(const R3Route * route) {
|
|
|
|
// return route->slugs_len >= route->slugs_cap;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// static bool router_slugs_resize(R3Route * route, int new_cap) {
|
|
|
|
// route->slugs = zrealloc(route->slugs, sizeof(char**) * new_cap);
|
|
|
|
// route->slugs_cap = new_cap;
|
|
|
|
// return route->slugs != NULL;
|
|
|
|
// }
|
|
|
|
|
|
|
|
static r3_iovec_t* router_append_slug(R3Route * route, char * slug, unsigned int len) {
|
|
|
|
r3_iovec_t *temp;
|
|
|
|
r3_vector_reserve(NULL, &route->slugs, route->slugs.size + 1);
|
|
|
|
temp = route->slugs.entries + route->slugs.size++;
|
|
|
|
temp->base = slug;
|
|
|
|
temp->len = len;
|
|
|
|
return temp;
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
static void get_slugs(R3Route * route, const char * path, int path_len) {
|
|
|
|
char *plh = (char*)path;
|
|
|
|
unsigned int l, namel;
|
|
|
|
l = 0;
|
2016-03-08 01:19:54 -05:00
|
|
|
char *name;
|
2016-03-21 22:23:37 -04:00
|
|
|
while (plh < (path + path_len)) {
|
|
|
|
plh = r3_slug_find_placeholder(plh+l, path_len, &l);
|
|
|
|
if (!plh) break;
|
2016-03-08 01:19:54 -05:00
|
|
|
namel = 0;
|
2016-03-21 22:23:37 -04:00
|
|
|
name = r3_slug_find_name(plh, l, &namel);
|
|
|
|
if (name) {
|
|
|
|
router_append_slug(route, name, namel);
|
|
|
|
}
|
2016-03-08 01:19:54 -05:00
|
|
|
if ((plh + l) >= (path + path_len)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Route * r3_node_append_route(R3Node *tree, const char * path, int path_len, int method, void *data) {
|
|
|
|
r3_vector_reserve(NULL, &tree->routes, tree->routes.size + 1);
|
|
|
|
R3Route *info = tree->routes.entries + tree->routes.size++;
|
|
|
|
memset(info, 0, sizeof(*info));
|
2014-05-18 00:24:07 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
r3_vector_reserve(NULL, &info->slugs, info->slugs.size + 3);
|
|
|
|
info->path.base = (char*) path;
|
|
|
|
info->path.len = path_len;
|
|
|
|
info->request_method = method; // ALLOW GET OR POST METHOD
|
|
|
|
info("\tinfo router path is: %s, with len: %d\n", path, path_len);
|
|
|
|
info("\troutes size is: %d\n", tree->routes.size);
|
2014-05-18 00:40:06 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
info->data = data;
|
2014-05-18 00:24:07 -04:00
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:56:46 -04:00
|
|
|
|
2014-06-03 11:50:02 -04:00
|
|
|
/**
|
|
|
|
* Helper function for creating routes from request URI path and request method
|
|
|
|
*
|
|
|
|
* method (int): METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_DELETE ...
|
|
|
|
*/
|
2015-11-18 01:16:26 -05:00
|
|
|
R3Route * r3_tree_insert_routel_ex(R3Node *tree, int method, const char *path, int path_len, void *data, char **errstr) {
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Node * ret = r3_tree_insert_pathl_ex(tree, path, path_len, method, 1, data, errstr);
|
|
|
|
R3Route *router = ret->routes.entries + (ret->routes.size - 1);
|
|
|
|
get_slugs(router, path, path_len);
|
|
|
|
|
|
|
|
return router;
|
2014-05-31 07:56:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-01 21:09:41 -04:00
|
|
|
/**
|
|
|
|
* Find common prefix from the edges of the node.
|
|
|
|
*
|
|
|
|
* Some cases of the common prefix:
|
|
|
|
*
|
|
|
|
* 1. "/foo/{slug}" vs "/foo/bar" => common prefix = "/foo/"
|
|
|
|
* 2. "{slug}/hate" vs "{slug}/bar" => common prefix = "{slug}/"
|
|
|
|
* 2. "/z/{slug}/hate" vs "/z/{slog}/bar" => common prefix = "/z/"
|
|
|
|
* 3. "{slug:xxx}/hate" vs "{slug:yyy}/bar" => common prefix = ""
|
|
|
|
* 4. "aaa{slug:xxx}/hate" vs "aab{slug:yyy}/bar" => common prefix = "aa"
|
|
|
|
* 5. "/foo/{slug}/hate" vs "/fo{slug}/bar" => common prefix = "/fo"
|
|
|
|
*/
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge * r3_node_find_common_prefix(R3Node *n, const char *path, int path_len, int *prefix_len, char **errstr) {
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int i = 0;
|
2014-06-01 21:09:41 -04:00
|
|
|
int prefix = 0;
|
2014-06-02 04:08:46 -04:00
|
|
|
*prefix_len = 0;
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge *e = NULL;
|
2016-03-21 22:23:37 -04:00
|
|
|
for(i = 0 ; i < n->edges.size ; i++ ) {
|
2014-06-01 21:09:41 -04:00
|
|
|
// ignore all edges with slug
|
2016-03-21 22:23:37 -04:00
|
|
|
prefix = strndiff( (char*) path, n->edges.entries[i].pattern.base, n->edges.entries[i].pattern.len);
|
2014-06-01 21:09:41 -04:00
|
|
|
|
|
|
|
// no common, consider insert a new edge
|
|
|
|
if ( prefix > 0 ) {
|
2016-03-21 22:23:37 -04:00
|
|
|
e = n->edges.entries + i;
|
2014-06-01 21:09:41 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// found common prefix edge
|
|
|
|
if (prefix > 0) {
|
|
|
|
r3_slug_t *slug;
|
|
|
|
int ret = 0;
|
2014-06-03 10:21:40 -04:00
|
|
|
const char *offset = path;
|
|
|
|
const char *p = path + prefix;
|
2014-06-01 21:09:41 -04:00
|
|
|
|
|
|
|
slug = r3_slug_new(path, path_len);
|
|
|
|
|
|
|
|
do {
|
2014-06-02 03:52:40 -04:00
|
|
|
ret = r3_slug_parse(slug, path, path_len, offset, errstr);
|
2014-06-01 21:09:41 -04:00
|
|
|
// found slug
|
|
|
|
if (ret == 1) {
|
|
|
|
// inside slug, backtrace to the begin of the slug
|
|
|
|
if ( p >= slug->begin && p <= slug->end ) {
|
|
|
|
prefix = slug->begin - path - 1;
|
|
|
|
break;
|
|
|
|
} else if ( p < slug->begin ) {
|
|
|
|
break;
|
2014-06-01 21:48:00 -04:00
|
|
|
} else if ( p >= slug->end && p < (path + path_len) ) {
|
2014-06-03 10:20:54 -04:00
|
|
|
offset = slug->end + 1;
|
2014-06-01 21:48:00 -04:00
|
|
|
prefix = p - path;
|
2014-06-01 21:09:41 -04:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2014-06-02 03:52:40 -04:00
|
|
|
} else if (ret == -1) {
|
2014-08-12 06:02:48 -04:00
|
|
|
r3_slug_free(slug);
|
2014-06-02 14:09:11 -04:00
|
|
|
return NULL;
|
2014-06-01 21:48:00 -04:00
|
|
|
} else {
|
|
|
|
break;
|
2014-06-01 21:09:41 -04:00
|
|
|
}
|
|
|
|
} while(ret == 1);
|
2014-08-12 06:02:48 -04:00
|
|
|
|
|
|
|
// free the slug
|
|
|
|
r3_slug_free(slug);
|
2014-06-01 21:09:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*prefix_len = prefix;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-18 00:24:07 -04:00
|
|
|
/**
|
|
|
|
* Return the last inserted node.
|
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int path_len, int method, unsigned int router, void * data, char **errstr)
|
2014-05-15 06:26:41 -04:00
|
|
|
{
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node * n = tree;
|
2014-06-01 21:09:41 -04:00
|
|
|
|
|
|
|
// common edge
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge * e = NULL;
|
2014-05-15 10:57:13 -04:00
|
|
|
|
2015-11-17 03:10:26 -05:00
|
|
|
// If there is no path to insert at the node, we just increase the mount
|
|
|
|
// point on the node and append the route.
|
|
|
|
if (path_len == 0) {
|
|
|
|
tree->endpoint++;
|
2016-03-21 22:23:37 -04:00
|
|
|
if (router) {
|
|
|
|
r3_node_append_route(tree, path, path_len, method, data);
|
|
|
|
info("tree router path is: %s, with len: %d\n", path, path_len);
|
2015-11-17 03:10:26 -05:00
|
|
|
}
|
|
|
|
return tree;
|
|
|
|
}
|
2014-06-01 21:48:00 -04:00
|
|
|
|
2014-05-15 10:57:13 -04:00
|
|
|
/* length of common prefix */
|
2014-05-18 06:58:31 -04:00
|
|
|
int prefix_len = 0;
|
2014-06-04 01:45:28 -04:00
|
|
|
char *err = NULL;
|
2014-06-02 14:09:11 -04:00
|
|
|
e = r3_node_find_common_prefix(tree, path, path_len, &prefix_len, &err);
|
|
|
|
if (err) {
|
|
|
|
// copy the error message pointer
|
|
|
|
if (errstr) *errstr = err;
|
2014-06-02 03:52:40 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-15 10:57:13 -04:00
|
|
|
|
2014-06-01 02:33:56 -04:00
|
|
|
const char * subpath = path + prefix_len;
|
|
|
|
const int subpath_len = path_len - prefix_len;
|
|
|
|
|
2014-05-18 06:58:31 -04:00
|
|
|
// common prefix not found, insert a new edge for this pattern
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( !prefix_len ) {
|
2014-05-18 06:58:31 -04:00
|
|
|
// there are two more slugs, we should break them into several parts
|
2015-08-26 10:00:23 -04:00
|
|
|
int slug_cnt = r3_slug_count(path, path_len, errstr);
|
2014-06-02 03:47:02 -04:00
|
|
|
if (slug_cnt == -1) {
|
2014-06-01 18:38:50 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
info("slug_cnt: %d\n",slug_cnt);
|
2014-05-23 01:49:18 -04:00
|
|
|
if ( slug_cnt > 1 ) {
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int slug_len;
|
|
|
|
char *p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
2014-05-18 00:28:12 -04:00
|
|
|
|
2014-05-18 06:58:31 -04:00
|
|
|
#ifdef DEBUG
|
|
|
|
assert(p);
|
|
|
|
#endif
|
|
|
|
|
2014-05-23 01:49:18 -04:00
|
|
|
// find the next one '{', then break there
|
2014-05-20 09:38:37 -04:00
|
|
|
if(p) {
|
2016-03-21 22:23:37 -04:00
|
|
|
p = r3_slug_find_placeholder(p + slug_len + 1, path_len - slug_len - 1, NULL);
|
2014-05-20 09:38:37 -04:00
|
|
|
}
|
2014-05-18 06:58:31 -04:00
|
|
|
#ifdef DEBUG
|
|
|
|
assert(p);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// insert the first one edge, and break at "p"
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node * child = r3_tree_create(3);
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int paln = p - path;
|
|
|
|
r3_node_connectl(n, path, p - path, 0, child); // no duplicate
|
2014-05-18 06:58:31 -04:00
|
|
|
|
|
|
|
// and insert the rest part to the child
|
2016-03-21 22:23:37 -04:00
|
|
|
return r3_tree_insert_pathl_ex(child, p, path_len - (int)(p - path), method, 1, data, errstr);
|
2014-05-23 01:49:18 -04:00
|
|
|
|
2014-05-18 06:58:31 -04:00
|
|
|
} else {
|
2014-05-23 01:49:18 -04:00
|
|
|
if (slug_cnt == 1) {
|
|
|
|
// there is one slug, let's see if it's optimiz-able by opcode
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int slug_len = 0;
|
|
|
|
char *slug_p = r3_slug_find_placeholder(path, path_len, &slug_len);
|
|
|
|
unsigned int slug_pattern_len = 0;
|
|
|
|
char *slug_pattern = r3_slug_find_pattern(slug_p, slug_len, &slug_pattern_len);
|
2014-06-01 20:04:57 -04:00
|
|
|
|
2014-05-23 01:49:18 -04:00
|
|
|
int opcode = 0;
|
|
|
|
// if there is a pattern defined.
|
2014-06-01 20:04:57 -04:00
|
|
|
if (slug_pattern_len) {
|
2014-06-03 08:50:19 -04:00
|
|
|
char *cpattern = r3_slug_compile(slug_pattern, slug_pattern_len);
|
2016-03-21 22:23:37 -04:00
|
|
|
info("cpattern: %s\n", cpattern);
|
2014-05-23 01:49:18 -04:00
|
|
|
opcode = r3_pattern_to_opcode(cpattern, strlen(cpattern));
|
2016-03-21 22:23:37 -04:00
|
|
|
info("opcode: %d\n", opcode);
|
2014-05-23 01:49:18 -04:00
|
|
|
zfree(cpattern);
|
|
|
|
} else {
|
|
|
|
opcode = OP_EXPECT_NOSLASH;
|
|
|
|
}
|
2014-06-01 20:04:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
// if the slug starts after one+ charactor, for example foo{slug}
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node *c1;
|
2014-06-01 20:04:57 -04:00
|
|
|
if (slug_p > path) {
|
|
|
|
c1 = r3_tree_create(3);
|
2016-03-21 22:23:37 -04:00
|
|
|
r3_node_connectl(n, path, slug_p - path, 0, c1); // no duplicate
|
2014-06-01 20:04:57 -04:00
|
|
|
} else {
|
|
|
|
c1 = n;
|
|
|
|
}
|
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node * c2 = r3_tree_create(3);
|
2016-03-21 22:23:37 -04:00
|
|
|
|
|
|
|
R3Edge * op_edge = r3_node_connectl(c1, slug_p, slug_len , 0, c2);
|
2014-06-01 20:04:57 -04:00
|
|
|
if(opcode) {
|
2014-05-23 01:49:18 -04:00
|
|
|
op_edge->opcode = opcode;
|
2014-06-01 20:04:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int restlen = path_len - ((slug_p - path) + slug_len);
|
2014-05-23 01:49:18 -04:00
|
|
|
|
2014-06-01 20:04:57 -04:00
|
|
|
if (restlen) {
|
2016-03-21 22:23:37 -04:00
|
|
|
return r3_tree_insert_pathl_ex(c2, slug_p + slug_len, restlen, method, 1, data, errstr);
|
2014-05-23 01:49:18 -04:00
|
|
|
}
|
2014-06-01 20:04:57 -04:00
|
|
|
|
|
|
|
c2->data = data;
|
|
|
|
c2->endpoint++;
|
2016-03-21 22:23:37 -04:00
|
|
|
if (router) {
|
|
|
|
// route->data = data;
|
|
|
|
r3_node_append_route(c2, path, path_len, method, data);
|
|
|
|
info("c2 router path is: %s, with len: %d\n", path, path_len);
|
2014-06-01 20:04:57 -04:00
|
|
|
}
|
|
|
|
return c2;
|
2014-05-23 01:49:18 -04:00
|
|
|
}
|
|
|
|
// only one slug
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Node * child = r3_tree_create(3);
|
2014-05-18 06:58:31 -04:00
|
|
|
child->endpoint++;
|
2014-06-01 20:04:57 -04:00
|
|
|
if (data)
|
|
|
|
child->data = data;
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
r3_node_connectl(n, path, path_len, 0, child);
|
|
|
|
if (router) {
|
|
|
|
r3_node_append_route(child, path, path_len, method, data);
|
|
|
|
info("child router path is: %s, with len: %d\n", path, path_len);
|
2014-05-18 06:58:31 -04:00
|
|
|
}
|
|
|
|
return child;
|
2014-05-18 00:28:12 -04:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
} else if ( prefix_len == e->pattern.len ) { // fully-equal to the pattern of the edge
|
2014-05-15 11:03:23 -04:00
|
|
|
|
2014-05-15 10:57:13 -04:00
|
|
|
// there are something more we can insert
|
2014-05-18 00:24:07 -04:00
|
|
|
if ( subpath_len > 0 ) {
|
2016-03-21 22:23:37 -04:00
|
|
|
return r3_tree_insert_pathl_ex(e->child, subpath, subpath_len, method, 1, data, errstr);
|
2014-05-15 10:57:13 -04:00
|
|
|
} else {
|
2014-05-18 22:49:47 -04:00
|
|
|
// there are no more path to insert
|
|
|
|
|
2014-06-12 05:50:47 -04:00
|
|
|
// see if there is an endpoint already, we should n't overwrite the data on child.
|
|
|
|
// but we still need to append the route.
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
if (router) {
|
|
|
|
// route->data = data;
|
|
|
|
r3_node_append_route(e->child, path, path_len, method, data);
|
|
|
|
info("e->child router path is: %s, with len: %d\n", path, path_len);
|
2014-06-12 05:50:47 -04:00
|
|
|
e->child->endpoint++; // make it as an endpoint
|
|
|
|
return e->child;
|
2014-05-18 00:28:12 -04:00
|
|
|
}
|
2014-06-12 05:50:47 -04:00
|
|
|
|
|
|
|
// insertion without route
|
|
|
|
if (e->child->endpoint > 0) {
|
|
|
|
// TODO: return an error code instead of NULL
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
e->child->endpoint++; // make it as an endpoint
|
|
|
|
e->child->data = data; // set data
|
2014-05-15 10:57:13 -04:00
|
|
|
return e->child;
|
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
} else if ( prefix_len < e->pattern.len ) {
|
2014-05-17 03:20:59 -04:00
|
|
|
/* it's partially matched with the pattern,
|
2014-05-15 10:57:13 -04:00
|
|
|
* we should split the end point and make a branch here...
|
|
|
|
*/
|
2014-05-18 06:58:31 -04:00
|
|
|
r3_edge_branch(e, prefix_len);
|
2016-03-21 22:23:37 -04:00
|
|
|
return r3_tree_insert_pathl_ex(e->child, subpath, subpath_len, method, 1, data, errstr);
|
2014-05-15 10:57:13 -04:00
|
|
|
} else {
|
2014-06-01 19:03:32 -04:00
|
|
|
fprintf(stderr, "unexpected route.");
|
2014-05-15 10:57:13 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return n;
|
2014-05-15 06:26:41 -04:00
|
|
|
}
|
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
bool r3_node_has_slug_edges(const R3Node *n) {
|
2015-11-17 08:40:21 -05:00
|
|
|
bool found = false;
|
2016-03-21 22:23:37 -04:00
|
|
|
R3Edge *edge_entries = n->edges.entries;
|
2015-11-17 23:52:06 -05:00
|
|
|
R3Edge *e;
|
2016-03-21 22:23:37 -04:00
|
|
|
unsigned int i;
|
|
|
|
for ( i = 0 ; i < n->edges.size ; i++ ) {
|
|
|
|
e = edge_entries + i;
|
|
|
|
e->has_slug = r3_path_contains_slug_char(e->pattern.base, e->pattern.len);
|
2014-05-20 13:50:15 -04:00
|
|
|
if (e->has_slug)
|
2015-11-17 08:40:21 -05:00
|
|
|
found = true;
|
2014-05-16 00:33:59 -04:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
|
2014-05-15 06:02:10 -04:00
|
|
|
|
2015-11-17 23:52:06 -05:00
|
|
|
void r3_tree_dump(const R3Node * n, int level) {
|
2014-05-18 08:08:43 -04:00
|
|
|
print_indent(level);
|
2014-05-20 13:15:54 -04:00
|
|
|
|
|
|
|
printf("(o)");
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
printf(" compare_type:%d", n->compare_type);
|
|
|
|
|
2014-05-18 03:06:11 -04:00
|
|
|
if ( n->combined_pattern ) {
|
|
|
|
printf(" regexp:%s", n->combined_pattern);
|
|
|
|
}
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2014-05-18 03:06:11 -04:00
|
|
|
printf(" endpoint:%d", n->endpoint);
|
2014-05-18 03:00:11 -04:00
|
|
|
|
2014-05-18 03:06:11 -04:00
|
|
|
if (n->data) {
|
|
|
|
printf(" data:%p", n->data);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
for ( int i = 0 ; i < n->edges.size ; i++ ) {
|
|
|
|
R3Edge * e = n->edges.entries + i;
|
2014-05-18 08:08:43 -04:00
|
|
|
print_indent(level + 1);
|
2016-03-21 22:23:37 -04:00
|
|
|
printf("|-\"%*.*s\"", e->pattern.len, e->pattern.len, e->pattern.base);
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-23 01:49:18 -04:00
|
|
|
if (e->opcode ) {
|
|
|
|
printf(" opcode:%d", e->opcode);
|
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
print_indent(level + 1);
|
|
|
|
printf("||-routes num: |%d|", n->routes.size);
|
|
|
|
|
|
|
|
for ( int j = 0 ; j < n->routes.size ; j++ ) {
|
|
|
|
R3Route * rr = n->routes.entries + j;
|
|
|
|
printf(" route path: |%*.*s|", rr->path.len,rr->path.len,rr->path.base);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2014-05-23 01:49:18 -04:00
|
|
|
|
2014-05-18 03:06:11 -04:00
|
|
|
if ( e->child ) {
|
2014-05-18 08:08:43 -04:00
|
|
|
printf("\n");
|
2014-05-18 03:06:11 -04:00
|
|
|
r3_tree_dump( e->child, level + 1);
|
2014-05-16 00:33:59 -04:00
|
|
|
}
|
2014-05-18 03:06:11 -04:00
|
|
|
printf("\n");
|
2014-05-16 00:33:59 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-16 06:03:52 -04:00
|
|
|
|
2014-05-17 23:59:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* return 0 == equal
|
|
|
|
*
|
2014-05-18 01:06:36 -04:00
|
|
|
* -1 == different route
|
2014-05-17 23:59:30 -04:00
|
|
|
*/
|
2015-11-18 01:16:26 -05:00
|
|
|
inline int r3_route_cmp(const R3Route *r1, const match_entry *r2) {
|
2016-03-21 22:23:37 -04:00
|
|
|
if (r1->request_method && r2->request_method) {
|
2014-05-18 00:49:58 -04:00
|
|
|
if (0 == (r1->request_method & r2->request_method) ) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-17 23:59:30 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( r1->host.len && r2->host.len ) {
|
|
|
|
if (strncmp(r1->host.base, r2->host.base, r2->host.len)) {
|
2014-05-17 23:59:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
if (r1->remote_addr_pattern.len && r2->remote_addr.len) {
|
2014-05-18 01:38:30 -04:00
|
|
|
/*
|
|
|
|
* XXX: consider "netinet/in.h"
|
|
|
|
if (r2->remote_addr) {
|
|
|
|
inet_addr(r2->remote_addr);
|
|
|
|
}
|
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
if ( strncmp(r1->remote_addr_pattern.base, r2->remote_addr.base, r2->remote_addr.len) ) {
|
2014-05-17 23:59:30 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-18 00:24:07 -04:00
|
|
|
|
|
|
|
/**
|
2014-05-20 13:50:15 -04:00
|
|
|
*
|
2014-05-18 00:24:07 -04:00
|
|
|
*/
|
2016-03-21 22:23:37 -04:00
|
|
|
// void r3_node_append_route(R3Node * n, R3Route * r)
|
|
|
|
// {
|
|
|
|
// r3_vector_reserve(NULL, &n->routes, n->routes.size + 1);
|
|
|
|
// memset(n->routes.entries + 1, 0, sizeof(*n->routes.entries));
|
|
|
|
|
|
|
|
// if (n->routes == NULL) {
|
|
|
|
// n->route_cap = 3;
|
|
|
|
// n->routes = zmalloc(sizeof(R3Route) * n->route_cap);
|
|
|
|
// }
|
|
|
|
// if (n->route_len >= n->route_cap) {
|
|
|
|
// n->route_cap *= 2;
|
|
|
|
// n->routes = zrealloc(n->routes, sizeof(R3Route) * n->route_cap);
|
|
|
|
// }
|
|
|
|
// n->routes[ n->route_len++ ] = r;
|
|
|
|
// }
|
2014-05-18 00:24:07 -04:00
|
|
|
|
|
|
|
|