2014-05-14 22:10:34 -04:00
|
|
|
/*
|
2014-05-16 08:22:25 -04:00
|
|
|
* r3.h
|
2014-05-14 22:10:34 -04:00
|
|
|
* Copyright (C) 2014 c9s <c9s@c9smba.local>
|
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
2014-05-22 10:21:29 -04:00
|
|
|
#ifndef R3_NODE_H
|
|
|
|
#define R3_NODE_H
|
2014-05-14 22:10:34 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-05-16 02:05:51 -04:00
|
|
|
#include <pcre.h>
|
2014-05-26 12:47:59 -04:00
|
|
|
#include <stdbool.h>
|
2014-06-01 07:49:56 -04:00
|
|
|
#include "config.h"
|
2014-05-20 12:37:27 -04:00
|
|
|
#include "r3_define.h"
|
2014-05-28 09:08:06 -04:00
|
|
|
#include "str_array.h"
|
|
|
|
#include "match_entry.h"
|
2014-05-16 07:12:01 -04:00
|
|
|
|
2014-06-01 06:06:12 -04:00
|
|
|
#ifdef ENABLE_JSON
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-06-01 04:48:54 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
struct _edge;
|
|
|
|
struct _node;
|
2014-05-18 01:06:36 -04:00
|
|
|
struct _route;
|
2014-05-16 03:29:25 -04:00
|
|
|
typedef struct _edge edge;
|
|
|
|
typedef struct _node node;
|
2014-05-18 01:06:36 -04:00
|
|
|
typedef struct _route route;
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
struct _node {
|
2014-05-18 00:24:07 -04:00
|
|
|
edge ** edges;
|
2014-05-27 02:16:42 -04:00
|
|
|
// edge ** edge_table;
|
2014-05-24 05:13:55 -04:00
|
|
|
|
|
|
|
// almost less than 255
|
2014-05-24 07:04:34 -04:00
|
|
|
unsigned char edge_len;
|
2014-05-24 05:22:29 -04:00
|
|
|
unsigned char compare_type;
|
|
|
|
unsigned char endpoint;
|
2014-05-24 06:45:43 -04:00
|
|
|
unsigned char ov_cnt;
|
|
|
|
|
|
|
|
// <-- here comes a char[3] struct padding for alignment since we have 5 char above.
|
2014-05-24 05:22:29 -04:00
|
|
|
|
2014-05-24 05:13:55 -04:00
|
|
|
|
2014-05-16 08:51:30 -04:00
|
|
|
/** compile-time variables here.... **/
|
|
|
|
|
2014-05-16 02:05:51 -04:00
|
|
|
/* the combined regexp pattern string from pattern_tokens */
|
2014-05-18 08:32:23 -04:00
|
|
|
pcre * pcre_pattern;
|
|
|
|
pcre_extra * pcre_extra;
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-24 07:04:34 -04:00
|
|
|
route ** routes;
|
|
|
|
|
2014-05-24 05:14:17 -04:00
|
|
|
char * combined_pattern;
|
|
|
|
|
2014-05-24 06:45:43 -04:00
|
|
|
// almost less than 255
|
2014-05-24 07:04:34 -04:00
|
|
|
unsigned char edge_cap;
|
2014-05-24 06:45:43 -04:00
|
|
|
unsigned char route_len;
|
|
|
|
unsigned char route_cap;
|
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
/**
|
2014-05-18 01:06:36 -04:00
|
|
|
* the pointer of route data
|
2014-05-16 03:29:25 -04:00
|
|
|
*/
|
2014-05-17 23:06:24 -04:00
|
|
|
void * data;
|
2014-05-16 02:05:51 -04:00
|
|
|
};
|
|
|
|
|
2014-05-28 09:08:06 -04:00
|
|
|
#define node_edge_pattern(node,i) node->edges[i]->pattern
|
|
|
|
#define node_edge_pattern_len(node,i) node->edges[i]->pattern_len
|
|
|
|
|
2014-05-16 03:29:25 -04:00
|
|
|
struct _edge {
|
2014-05-16 02:05:51 -04:00
|
|
|
char * pattern;
|
2014-05-16 03:29:25 -04:00
|
|
|
node * child;
|
2014-05-24 06:45:43 -04:00
|
|
|
unsigned short pattern_len; // 2 byte
|
|
|
|
unsigned char opcode; // 1 byte
|
|
|
|
unsigned char has_slug; // 1 bit
|
2014-05-16 02:05:51 -04:00
|
|
|
};
|
|
|
|
|
2014-05-18 01:06:36 -04:00
|
|
|
struct _route {
|
2014-05-17 06:57:36 -04:00
|
|
|
char * path;
|
|
|
|
int path_len;
|
|
|
|
|
2014-05-17 23:04:13 -04:00
|
|
|
int request_method; // can be (GET || POST)
|
2014-05-17 06:57:36 -04:00
|
|
|
|
|
|
|
char * host; // required host name
|
|
|
|
int host_len;
|
|
|
|
|
2014-05-18 00:40:06 -04:00
|
|
|
void * data;
|
|
|
|
|
2014-05-17 06:57:36 -04:00
|
|
|
char * remote_addr_pattern;
|
|
|
|
int remote_addr_pattern_len;
|
2014-05-17 23:59:30 -04:00
|
|
|
};
|
2014-05-17 06:57:36 -04:00
|
|
|
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-16 06:57:36 -04:00
|
|
|
node * r3_tree_create(int cap);
|
2014-05-16 03:29:25 -04:00
|
|
|
|
2014-05-16 06:57:36 -04:00
|
|
|
node * r3_node_create();
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-16 06:57:36 -04:00
|
|
|
void r3_tree_free(node * tree);
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-28 09:03:59 -04:00
|
|
|
edge * r3_node_connectl(node * n, const char * pat, int len, int strdup, node *child);
|
2014-05-23 01:49:18 -04:00
|
|
|
|
|
|
|
#define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child)
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-28 09:03:59 -04:00
|
|
|
edge * r3_node_find_edge(const node * n, const char * pat);
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-18 00:24:07 -04:00
|
|
|
void r3_node_append_edge(node *n, edge *child);
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-15 06:26:41 -04:00
|
|
|
|
2014-06-01 02:35:29 -04:00
|
|
|
node * r3_tree_insert_pathl(node *tree, const char *path, int path_len, void * data);
|
2014-05-18 22:12:41 -04:00
|
|
|
|
2014-06-01 02:35:29 -04:00
|
|
|
route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data);
|
2014-05-31 07:56:46 -04:00
|
|
|
|
2014-06-01 18:48:00 -04:00
|
|
|
#define r3_tree_insert_path(n,p,d) r3_tree_insert_pathl_(n,p,strlen(p), NULL, d, NULL)
|
2014-05-31 07:56:46 -04:00
|
|
|
|
|
|
|
#define r3_tree_insert_route(n,method,path,data) r3_tree_insert_routel(n, method, path, strlen(path), data)
|
|
|
|
|
2014-05-18 22:12:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The private API to insert a path
|
|
|
|
*/
|
2014-06-01 18:48:00 -04:00
|
|
|
node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route * route, void * data, char ** errstr);
|
2014-05-15 06:26:41 -04:00
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
void r3_tree_dump(const node * n, int level);
|
2014-05-15 01:39:50 -04:00
|
|
|
|
2014-05-17 20:13:58 -04:00
|
|
|
|
2014-05-27 00:34:28 -04:00
|
|
|
edge * r3_node_find_edge_str(const node * n, const char * str, int str_len);
|
2014-05-16 02:05:51 -04:00
|
|
|
|
|
|
|
|
2014-05-31 13:51:42 -04:00
|
|
|
int r3_tree_compile(node *n, char** errstr);
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-31 13:51:42 -04:00
|
|
|
int r3_tree_compile_patterns(node * n, char** errstr);
|
2014-05-16 02:05:51 -04:00
|
|
|
|
2014-05-31 14:38:12 -04:00
|
|
|
node * r3_tree_matchl(const node * n, const char * path, int path_len, match_entry * entry);
|
2014-05-18 22:34:48 -04:00
|
|
|
|
|
|
|
#define r3_tree_match(n,p,e) r3_tree_matchl(n,p, strlen(p), e)
|
2014-05-15 09:17:30 -04:00
|
|
|
|
2014-05-18 22:34:48 -04:00
|
|
|
// node * r3_tree_match_entry(node * n, match_entry * entry);
|
|
|
|
#define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry)
|
2014-05-18 00:32:20 -04:00
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
bool r3_node_has_slug_edges(const node *n);
|
2014-05-16 00:33:59 -04:00
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
edge * r3_edge_create(const char * pattern, int pattern_len, node * child);
|
2014-05-14 22:10:34 -04:00
|
|
|
|
2014-05-18 07:09:47 -04:00
|
|
|
node * r3_edge_branch(edge *e, int dl);
|
2014-05-15 11:46:49 -04:00
|
|
|
|
2014-05-16 06:57:36 -04:00
|
|
|
void r3_edge_free(edge * edge);
|
2014-05-14 22:10:34 -04:00
|
|
|
|
2014-05-16 06:03:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-17 07:25:25 -04:00
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
route * r3_route_create(const char * path);
|
2014-05-17 07:25:25 -04:00
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
route * r3_route_createl(const char * path, int path_len);
|
2014-05-17 07:25:25 -04:00
|
|
|
|
2014-05-28 09:09:37 -04:00
|
|
|
int r3_route_cmp(const route *r1, const match_entry *r2);
|
2014-05-17 23:59:30 -04:00
|
|
|
|
2014-05-18 01:06:36 -04:00
|
|
|
void r3_node_append_route(node * n, route * route);
|
2014-05-18 00:24:07 -04:00
|
|
|
|
2014-05-18 22:39:03 -04:00
|
|
|
void r3_route_free(route * route);
|
2014-05-18 00:24:07 -04:00
|
|
|
|
2014-05-31 14:38:12 -04:00
|
|
|
route * r3_tree_match_route(const node *n, match_entry * entry);
|
2014-05-18 00:56:53 -04:00
|
|
|
|
2014-05-17 23:59:30 -04:00
|
|
|
#define METHOD_GET 2
|
|
|
|
#define METHOD_POST 2<<1
|
2014-05-22 12:21:40 -04:00
|
|
|
#define METHOD_PUT 2<<2
|
|
|
|
#define METHOD_DELETE 2<<3
|
|
|
|
#define METHOD_PATCH 2<<4
|
|
|
|
#define METHOD_HEAD 2<<5
|
|
|
|
#define METHOD_OPTIONS 2<<6
|
2014-05-17 23:59:30 -04:00
|
|
|
|
2014-05-23 00:08:06 -04:00
|
|
|
|
|
|
|
|
2014-05-28 09:09:37 -04:00
|
|
|
int r3_pattern_to_opcode(const char * pattern, int pattern_len);
|
2014-05-23 00:08:06 -04:00
|
|
|
|
|
|
|
enum { NODE_COMPARE_STR, NODE_COMPARE_PCRE, NODE_COMPARE_OPCODE };
|
2014-05-22 23:25:25 -04:00
|
|
|
|
2014-05-23 10:58:20 -04:00
|
|
|
enum { OP_EXPECT_MORE_DIGITS = 1, OP_EXPECT_MORE_WORDS, OP_EXPECT_NOSLASH, OP_EXPECT_NODASH, OP_EXPECT_MORE_ALPHA };
|
2014-05-22 23:18:15 -04:00
|
|
|
|
2014-06-01 06:06:12 -04:00
|
|
|
#ifdef ENABLE_JSON
|
|
|
|
json_object * r3_edge_to_json_object(const edge * e);
|
|
|
|
json_object * r3_node_to_json_object(const node * n);
|
|
|
|
json_object * r3_route_to_json_object(const route * r);
|
2014-05-26 09:39:36 -04:00
|
|
|
|
2014-06-01 06:06:12 -04:00
|
|
|
const char * r3_node_to_json_string_ext(const node * n, int options);
|
|
|
|
const char * r3_node_to_json_pretty_string(const node * n);
|
|
|
|
const char * r3_node_to_json_string(const node * n);
|
2014-06-01 08:23:11 -04:00
|
|
|
#endif
|
2014-06-01 08:20:51 -04:00
|
|
|
|
2014-06-01 04:48:54 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2014-06-01 06:06:12 -04:00
|
|
|
#endif
|
2014-05-26 09:39:36 -04:00
|
|
|
|
2014-05-22 10:21:29 -04:00
|
|
|
#endif /* !R3_NODE_H */
|