1
0
Fork 0

Brendan was getting pissed off about inconsistent indentation styles.

Convert all tabs to 4 spaces. All code must now use 4 space indents.

svn path=/trunk/avl/; revision=4492
This commit is contained in:
Michael Smith 2003-03-15 02:10:19 +00:00
parent 6198c728e1
commit d0cf9fe000
13 changed files with 1530 additions and 1530 deletions

758
avl/avl.c

File diff suppressed because it is too large Load Diff

108
avl/avl.h
View File

@ -2,7 +2,7 @@
* Copyright (C) 1995 by Sam Rushing <rushing@nightmare.com>
*/
/* $Id: avl.h,v 1.5 2003/03/09 22:56:46 karl Exp $ */
/* $Id: avl.h,v 1.6 2003/03/15 02:10:18 msmith Exp $ */
#ifndef __AVL_H
#define __AVL_H
@ -22,24 +22,24 @@ extern "C" {
#endif
typedef struct avl_node_tag {
void * key;
struct avl_node_tag * left;
struct avl_node_tag * right;
struct avl_node_tag * parent;
void * key;
struct avl_node_tag * left;
struct avl_node_tag * right;
struct avl_node_tag * parent;
/*
* The lower 2 bits of <rank_and_balance> specify the balance
* factor: 00==-1, 01==0, 10==+1.
* The rest of the bits are used for <rank>
*/
unsigned long rank_and_balance;
unsigned long rank_and_balance;
#ifndef NO_THREAD
rwlock_t rwlock;
#endif
} avl_node;
#define AVL_GET_BALANCE(n) ((int)(((n)->rank_and_balance & 3) - 1))
#define AVL_GET_BALANCE(n) ((int)(((n)->rank_and_balance & 3) - 1))
#define AVL_GET_RANK(n) (((n)->rank_and_balance >> 2))
#define AVL_GET_RANK(n) (((n)->rank_and_balance >> 2))
#define AVL_SET_BALANCE(n,b) \
((n)->rank_and_balance) = \
@ -51,11 +51,11 @@ typedef struct avl_node_tag {
struct _avl_tree;
typedef int (*avl_key_compare_fun_type) (void * compare_arg, void * a, void * b);
typedef int (*avl_iter_fun_type) (void * key, void * iter_arg);
typedef int (*avl_iter_index_fun_type) (unsigned long index, void * key, void * iter_arg);
typedef int (*avl_free_key_fun_type) (void * key);
typedef int (*avl_key_printer_fun_type) (char *, void *);
typedef int (*avl_key_compare_fun_type) (void * compare_arg, void * a, void * b);
typedef int (*avl_iter_fun_type) (void * key, void * iter_arg);
typedef int (*avl_iter_index_fun_type) (unsigned long index, void * key, void * iter_arg);
typedef int (*avl_free_key_fun_type) (void * key);
typedef int (*avl_key_printer_fun_type) (char *, void *);
/*
* <compare_fun> and <compare_arg> let us associate a particular compare
@ -63,11 +63,11 @@ typedef int (*avl_key_printer_fun_type) (char *, void *);
*/
typedef struct _avl_tree {
avl_node * root;
unsigned long height;
unsigned long length;
avl_key_compare_fun_type compare_fun;
void * compare_arg;
avl_node * root;
unsigned long height;
unsigned long length;
avl_key_compare_fun_type compare_fun;
void * compare_arg;
#ifndef NO_THREAD
rwlock_t rwlock;
#endif
@ -77,66 +77,66 @@ avl_tree * avl_tree_new (avl_key_compare_fun_type compare_fun, void * compare_ar
avl_node * avl_node_new (void * key, avl_node * parent);
void avl_tree_free (
avl_tree * tree,
avl_free_key_fun_type free_key_fun
avl_tree * tree,
avl_free_key_fun_type free_key_fun
);
int avl_insert (
avl_tree * ob,
void * key
avl_tree * ob,
void * key
);
int avl_delete (
avl_tree * tree,
void * key,
avl_free_key_fun_type free_key_fun
avl_tree * tree,
void * key,
avl_free_key_fun_type free_key_fun
);
int avl_get_by_index (
avl_tree * tree,
unsigned long index,
void ** value_address
avl_tree * tree,
unsigned long index,
void ** value_address
);
int avl_get_by_key (
avl_tree * tree,
void * key,
void ** value_address
avl_tree * tree,
void * key,
void ** value_address
);
int avl_iterate_inorder (
avl_tree * tree,
avl_iter_fun_type iter_fun,
void * iter_arg
avl_tree * tree,
avl_iter_fun_type iter_fun,
void * iter_arg
);
int avl_iterate_index_range (
avl_tree * tree,
avl_tree * tree,
avl_iter_index_fun_type iter_fun,
unsigned long low,
unsigned long high,
void * iter_arg
unsigned long low,
unsigned long high,
void * iter_arg
);
int avl_get_span_by_key (
avl_tree * tree,
void * key,
unsigned long * low,
unsigned long * high
avl_tree * tree,
void * key,
unsigned long * low,
unsigned long * high
);
int avl_get_span_by_two_keys (
avl_tree * tree,
void * key_a,
void * key_b,
unsigned long * low,
unsigned long * high
avl_tree * tree,
void * key_a,
void * key_b,
unsigned long * low,
unsigned long * high
);
int avl_verify (avl_tree * tree);
void avl_print_tree (
avl_tree * tree,
avl_tree * tree,
avl_key_printer_fun_type key_printer
);
@ -149,15 +149,15 @@ avl_node *avl_get_next(avl_node * node);
/* These two are from David Ascher <david_ascher@brown.edu> */
int avl_get_item_by_key_most (
avl_tree * tree,
void * key,
void ** value_address
avl_tree * tree,
void * key,
void ** value_address
);
int avl_get_item_by_key_least (
avl_tree * tree,
void * key,
void ** value_address
avl_tree * tree,
void * key,
void ** value_address
);
/* optional locking stuff */

View File

@ -11,75 +11,75 @@ int _printer(char *buff, void *key);
int main(int argc, char **argv)
{
int i, max_nodes;
avl_tree *tree;
avl_node *node;
int i, max_nodes;
avl_tree *tree;
avl_node *node;
max_nodes = 25;
max_nodes = 25;
if (argc == 2) {
max_nodes = atoi(argv[1]);
if (max_nodes == 0)
max_nodes = 10;
}
if (argc == 2) {
max_nodes = atoi(argv[1]);
if (max_nodes == 0)
max_nodes = 10;
}
printf("avl test... max_nodes = %d...\n", max_nodes);
printf("avl test... max_nodes = %d...\n", max_nodes);
tree = avl_tree_new(_compare, NULL);
tree = avl_tree_new(_compare, NULL);
printf("Filling tree...\n");
for (i = 0; i < max_nodes; i++) {
avl_insert(tree, (void *)rand());
}
printf("Traversing tree...\n");
node = avl_get_first(tree);
while (node) {
i = (int)node->key;
printf("Filling tree...\n");
for (i = 0; i < max_nodes; i++) {
avl_insert(tree, (void *)rand());
}
printf("Traversing tree...\n");
node = avl_get_first(tree);
while (node) {
i = (int)node->key;
printf("...%5d\n", i);
printf("...%5d\n", i);
node = avl_get_next(node);
}
node = avl_get_next(node);
}
printf("Trying to go backwards...\n");
node = tree->root->right;
while (node) {
i = (int)node->key;
printf("...%5d\n", i);
node = avl_get_prev(node);
}
printf("Trying to go backwards...\n");
node = tree->root->right;
while (node) {
i = (int)node->key;
printf("...%5d\n", i);
node = avl_get_prev(node);
}
printf("Printing tree...\n");
avl_print_tree(tree, _printer);
printf("Printing tree...\n");
avl_print_tree(tree, _printer);
avl_tree_free(tree, _free);
return 0;
avl_tree_free(tree, _free);
return 0;
}
int _compare(void *compare_arg, void *a, void *b)
{
int i, j;
int i, j;
i = (int)a;
j = (int)b;
i = (int)a;
j = (int)b;
if (i > j)
return 1;
if (j > i)
return -1;
return 0;
if (i > j)
return 1;
if (j > i)
return -1;
return 0;
}
int _free(void *key)
{
return 1;
return 1;
}
int _printer(char *buff, void *key)
{
return snprintf(buff, 25, "%d", (int)key);
return snprintf(buff, 25, "%d", (int)key);
}

View File

@ -33,50 +33,50 @@ int _free_vars(void *key);
http_parser_t *httpp_create_parser(void)
{
return (http_parser_t *)malloc(sizeof(http_parser_t));
return (http_parser_t *)malloc(sizeof(http_parser_t));
}
void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
{
http_varlist_t *list;
http_varlist_t *list;
parser->req_type = httpp_req_none;
parser->uri = NULL;
parser->vars = avl_tree_new(_compare_vars, NULL);
parser->queryvars = avl_tree_new(_compare_vars, NULL);
parser->req_type = httpp_req_none;
parser->uri = NULL;
parser->vars = avl_tree_new(_compare_vars, NULL);
parser->queryvars = avl_tree_new(_compare_vars, NULL);
/* now insert the default variables */
list = defaults;
while (list != NULL) {
httpp_setvar(parser, list->var.name, list->var.value);
list = list->next;
}
/* now insert the default variables */
list = defaults;
while (list != NULL) {
httpp_setvar(parser, list->var.name, list->var.value);
list = list->next;
}
}
static int split_headers(char *data, unsigned long len, char **line)
{
/* first we count how many lines there are
** and set up the line[] array
*/
int lines = 0;
/* first we count how many lines there are
** and set up the line[] array
*/
int lines = 0;
unsigned long i;
line[lines] = data;
for (i = 0; i < len && lines < MAX_HEADERS; i++) {
if (data[i] == '\r')
data[i] = '\0';
if (data[i] == '\n') {
lines++;
data[i] = '\0';
if (i + 1 < len) {
if (data[i + 1] == '\n' || data[i + 1] == '\r')
break;
line[lines] = &data[i + 1];
line[lines] = data;
for (i = 0; i < len && lines < MAX_HEADERS; i++) {
if (data[i] == '\r')
data[i] = '\0';
if (data[i] == '\n') {
lines++;
data[i] = '\0';
if (i + 1 < len) {
if (data[i + 1] == '\n' || data[i + 1] == '\r')
break;
line[lines] = &data[i + 1];
}
}
}
}
}
i++;
while (data[i] == '\n') i++;
i++;
while (data[i] == '\n') i++;
return lines;
}
@ -84,194 +84,194 @@ static int split_headers(char *data, unsigned long len, char **line)
static void parse_headers(http_parser_t *parser, char **line, int lines)
{
int i,l;
int whitespace, where, slen;
char *name = NULL;
char *value = NULL;
int whitespace, where, slen;
char *name = NULL;
char *value = NULL;
/* parse the name: value lines. */
for (l = 1; l < lines; l++) {
where = 0;
whitespace = 0;
name = line[l];
value = NULL;
slen = strlen(line[l]);
for (i = 0; i < slen; i++) {
if (line[l][i] == ':') {
whitespace = 1;
line[l][i] = '\0';
} else {
if (whitespace) {
whitespace = 0;
while (i < slen && line[l][i] == ' ')
i++;
/* parse the name: value lines. */
for (l = 1; l < lines; l++) {
where = 0;
whitespace = 0;
name = line[l];
value = NULL;
slen = strlen(line[l]);
for (i = 0; i < slen; i++) {
if (line[l][i] == ':') {
whitespace = 1;
line[l][i] = '\0';
} else {
if (whitespace) {
whitespace = 0;
while (i < slen && line[l][i] == ' ')
i++;
if (i < slen)
value = &line[l][i];
break;
}
}
}
if (name != NULL && value != NULL) {
httpp_setvar(parser, _lowercase(name), value);
name = NULL;
value = NULL;
}
}
if (i < slen)
value = &line[l][i];
break;
}
}
}
if (name != NULL && value != NULL) {
httpp_setvar(parser, _lowercase(name), value);
name = NULL;
value = NULL;
}
}
}
int httpp_parse_response(http_parser_t *parser, char *http_data, unsigned long len, char *uri)
{
char *data;
char *line[MAX_HEADERS];
int lines, slen,i, whitespace=0, where=0,code;
char *version=NULL, *resp_code=NULL, *message=NULL;
char *data;
char *line[MAX_HEADERS];
int lines, slen,i, whitespace=0, where=0,code;
char *version=NULL, *resp_code=NULL, *message=NULL;
if(http_data == NULL)
return 0;
if(http_data == NULL)
return 0;
/* make a local copy of the data, including 0 terminator */
data = (char *)malloc(len+1);
if (data == NULL) return 0;
memcpy(data, http_data, len);
data[len] = 0;
/* make a local copy of the data, including 0 terminator */
data = (char *)malloc(len+1);
if (data == NULL) return 0;
memcpy(data, http_data, len);
data[len] = 0;
lines = split_headers(data, len, line);
lines = split_headers(data, len, line);
/* In this case, the first line contains:
* VERSION RESPONSE_CODE MESSAGE, such as HTTP/1.0 200 OK
*/
slen = strlen(line[0]);
version = line[0];
for(i=0; i < slen; i++) {
if(line[0][i] == ' ') {
line[0][i] = 0;
whitespace = 1;
} else if(whitespace) {
whitespace = 0;
where++;
if(where == 1)
resp_code = &line[0][i];
else {
message = &line[0][i];
break;
}
}
}
/* In this case, the first line contains:
* VERSION RESPONSE_CODE MESSAGE, such as HTTP/1.0 200 OK
*/
slen = strlen(line[0]);
version = line[0];
for(i=0; i < slen; i++) {
if(line[0][i] == ' ') {
line[0][i] = 0;
whitespace = 1;
} else if(whitespace) {
whitespace = 0;
where++;
if(where == 1)
resp_code = &line[0][i];
else {
message = &line[0][i];
break;
}
}
}
if(version == NULL || resp_code == NULL || message == NULL) {
free(data);
return 0;
}
if(version == NULL || resp_code == NULL || message == NULL) {
free(data);
return 0;
}
httpp_setvar(parser, HTTPP_VAR_ERROR_CODE, resp_code);
code = atoi(resp_code);
if(code < 200 || code >= 300) {
httpp_setvar(parser, HTTPP_VAR_ERROR_MESSAGE, message);
}
httpp_setvar(parser, HTTPP_VAR_ERROR_CODE, resp_code);
code = atoi(resp_code);
if(code < 200 || code >= 300) {
httpp_setvar(parser, HTTPP_VAR_ERROR_MESSAGE, message);
}
httpp_setvar(parser, HTTPP_VAR_URI, uri);
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");
httpp_setvar(parser, HTTPP_VAR_URI, uri);
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");
parse_headers(parser, line, lines);
parse_headers(parser, line, lines);
free(data);
free(data);
return 1;
return 1;
}
static int hex(char c)
{
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if(c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return -1;
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if(c >= 'a' && c <= 'f')
return c - 'a' + 10;
else
return -1;
}
static char *url_escape(char *src)
{
int len = strlen(src);
unsigned char *decoded;
int i;
char *dst;
int done = 0;
int len = strlen(src);
unsigned char *decoded;
int i;
char *dst;
int done = 0;
decoded = calloc(1, len + 1);
decoded = calloc(1, len + 1);
dst = decoded;
dst = decoded;
for(i=0; i < len; i++) {
switch(src[i]) {
case '%':
if(i+2 >= len) {
free(decoded);
return NULL;
}
if(hex(src[i+1]) == -1 || hex(src[i+2]) == -1 ) {
free(decoded);
return NULL;
}
for(i=0; i < len; i++) {
switch(src[i]) {
case '%':
if(i+2 >= len) {
free(decoded);
return NULL;
}
if(hex(src[i+1]) == -1 || hex(src[i+2]) == -1 ) {
free(decoded);
return NULL;
}
*dst++ = hex(src[i+1]) * 16 + hex(src[i+2]);
i+= 2;
break;
case '#':
done = 1;
break;
case 0:
free(decoded);
return NULL;
break;
default:
*dst++ = src[i];
break;
}
if(done)
break;
}
*dst++ = hex(src[i+1]) * 16 + hex(src[i+2]);
i+= 2;
break;
case '#':
done = 1;
break;
case 0:
free(decoded);
return NULL;
break;
default:
*dst++ = src[i];
break;
}
if(done)
break;
}
*dst = 0; /* null terminator */
*dst = 0; /* null terminator */
return decoded;
return decoded;
}
/** TODO: This is almost certainly buggy in some cases */
static void parse_query(http_parser_t *parser, char *query)
{
int len;
int i=0;
char *key = query;
char *val=NULL;
int len;
int i=0;
char *key = query;
char *val=NULL;
if(!query || !*query)
return;
if(!query || !*query)
return;
len = strlen(query);
len = strlen(query);
while(i<len) {
switch(query[i]) {
case '&':
query[i] = 0;
if(val && key)
httpp_set_query_param(parser, key, val);
key = query+i+1;
break;
case '=':
query[i] = 0;
val = query+i+1;
break;
}
i++;
}
while(i<len) {
switch(query[i]) {
case '&':
query[i] = 0;
if(val && key)
httpp_set_query_param(parser, key, val);
key = query+i+1;
break;
case '=':
query[i] = 0;
val = query+i+1;
break;
}
i++;
}
if(val && key) {
httpp_set_query_param(parser, key, val);
}
if(val && key) {
httpp_set_query_param(parser, key, val);
}
}
/* The old shoutcast procotol. Don't look at this, it's really nasty */
@ -288,7 +288,7 @@ int httpp_parse_icy(http_parser_t *parser, char *http_data, unsigned long len)
memcpy(data, http_data, len);
data[len] = 0;
lines = split_headers(data, len, line);
lines = split_headers(data, len, line);
/* Now, this protocol looks like:
* sourcepassword\n
@ -313,261 +313,261 @@ int httpp_parse_icy(http_parser_t *parser, char *http_data, unsigned long len)
int httpp_parse(http_parser_t *parser, char *http_data, unsigned long len)
{
char *data, *tmp;
char *line[MAX_HEADERS]; /* limited to 32 lines, should be more than enough */
int i;
int lines;
char *req_type = NULL;
char *uri = NULL;
char *version = NULL;
int whitespace, where, slen;
char *data, *tmp;
char *line[MAX_HEADERS]; /* limited to 32 lines, should be more than enough */
int i;
int lines;
char *req_type = NULL;
char *uri = NULL;
char *version = NULL;
int whitespace, where, slen;
if (http_data == NULL)
return 0;
if (http_data == NULL)
return 0;
/* make a local copy of the data, including 0 terminator */
data = (char *)malloc(len+1);
if (data == NULL) return 0;
memcpy(data, http_data, len);
data[len] = 0;
/* make a local copy of the data, including 0 terminator */
data = (char *)malloc(len+1);
if (data == NULL) return 0;
memcpy(data, http_data, len);
data[len] = 0;
lines = split_headers(data, len, line);
lines = split_headers(data, len, line);
/* parse the first line special
** the format is:
** REQ_TYPE URI VERSION
** eg:
** GET /index.html HTTP/1.0
*/
where = 0;
whitespace = 0;
slen = strlen(line[0]);
req_type = line[0];
for (i = 0; i < slen; i++) {
if (line[0][i] == ' ') {
whitespace = 1;
line[0][i] = '\0';
} else {
/* we're just past the whitespace boundry */
if (whitespace) {
whitespace = 0;
where++;
switch (where) {
case 1:
uri = &line[0][i];
break;
case 2:
version = &line[0][i];
break;
}
}
}
}
/* parse the first line special
** the format is:
** REQ_TYPE URI VERSION
** eg:
** GET /index.html HTTP/1.0
*/
where = 0;
whitespace = 0;
slen = strlen(line[0]);
req_type = line[0];
for (i = 0; i < slen; i++) {
if (line[0][i] == ' ') {
whitespace = 1;
line[0][i] = '\0';
} else {
/* we're just past the whitespace boundry */
if (whitespace) {
whitespace = 0;
where++;
switch (where) {
case 1:
uri = &line[0][i];
break;
case 2:
version = &line[0][i];
break;
}
}
}
}
if (strcasecmp("GET", req_type) == 0) {
parser->req_type = httpp_req_get;
} else if (strcasecmp("POST", req_type) == 0) {
parser->req_type = httpp_req_post;
} else if (strcasecmp("HEAD", req_type) == 0) {
parser->req_type = httpp_req_head;
} else if (strcasecmp("SOURCE", req_type) == 0) {
parser->req_type = httpp_req_source;
} else if (strcasecmp("PLAY", req_type) == 0) {
parser->req_type = httpp_req_play;
} else if (strcasecmp("STATS", req_type) == 0) {
parser->req_type = httpp_req_stats;
} else {
parser->req_type = httpp_req_unknown;
}
if (strcasecmp("GET", req_type) == 0) {
parser->req_type = httpp_req_get;
} else if (strcasecmp("POST", req_type) == 0) {
parser->req_type = httpp_req_post;
} else if (strcasecmp("HEAD", req_type) == 0) {
parser->req_type = httpp_req_head;
} else if (strcasecmp("SOURCE", req_type) == 0) {
parser->req_type = httpp_req_source;
} else if (strcasecmp("PLAY", req_type) == 0) {
parser->req_type = httpp_req_play;
} else if (strcasecmp("STATS", req_type) == 0) {
parser->req_type = httpp_req_stats;
} else {
parser->req_type = httpp_req_unknown;
}
if (uri != NULL && strlen(uri) > 0) {
char *query;
if((query = strchr(uri, '?')) != NULL) {
*query = 0;
query++;
parse_query(parser, query);
}
if (uri != NULL && strlen(uri) > 0) {
char *query;
if((query = strchr(uri, '?')) != NULL) {
*query = 0;
query++;
parse_query(parser, query);
}
parser->uri = strdup(uri);
} else {
parser->uri = strdup(uri);
} else {
free(data);
return 0;
}
if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL)) {
tmp[0] = '\0';
if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0)) {
httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
httpp_setvar(parser, HTTPP_VAR_VERSION, &tmp[1]);
} else {
free(data);
return 0;
}
} else {
free(data);
return 0;
}
if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL)) {
tmp[0] = '\0';
if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0)) {
httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
httpp_setvar(parser, HTTPP_VAR_VERSION, &tmp[1]);
} else {
free(data);
return 0;
}
} else {
free(data);
return 0;
}
if (parser->req_type != httpp_req_none && parser->req_type != httpp_req_unknown) {
switch (parser->req_type) {
case httpp_req_get:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
break;
case httpp_req_post:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
break;
case httpp_req_head:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
break;
case httpp_req_source:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
break;
case httpp_req_play:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
break;
case httpp_req_stats:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
break;
default:
break;
}
} else {
free(data);
return 0;
}
if (parser->req_type != httpp_req_none && parser->req_type != httpp_req_unknown) {
switch (parser->req_type) {
case httpp_req_get:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
break;
case httpp_req_post:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
break;
case httpp_req_head:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
break;
case httpp_req_source:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
break;
case httpp_req_play:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
break;
case httpp_req_stats:
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
break;
default:
break;
}
} else {
free(data);
return 0;
}
if (parser->uri != NULL) {
httpp_setvar(parser, HTTPP_VAR_URI, parser->uri);
} else {
free(data);
return 0;
}
if (parser->uri != NULL) {
httpp_setvar(parser, HTTPP_VAR_URI, parser->uri);
} else {
free(data);
return 0;
}
parse_headers(parser, line, lines);
parse_headers(parser, line, lines);
free(data);
free(data);
return 1;
return 1;
}
void httpp_setvar(http_parser_t *parser, char *name, char *value)
{
http_var_t *var;
http_var_t *var;
if (name == NULL || value == NULL)
return;
if (name == NULL || value == NULL)
return;
var = (http_var_t *)malloc(sizeof(http_var_t));
if (var == NULL) return;
var = (http_var_t *)malloc(sizeof(http_var_t));
if (var == NULL) return;
var->name = strdup(name);
var->value = strdup(value);
var->name = strdup(name);
var->value = strdup(value);
if (httpp_getvar(parser, name) == NULL) {
avl_insert(parser->vars, (void *)var);
} else {
avl_delete(parser->vars, (void *)var, _free_vars);
avl_insert(parser->vars, (void *)var);
}
if (httpp_getvar(parser, name) == NULL) {
avl_insert(parser->vars, (void *)var);
} else {
avl_delete(parser->vars, (void *)var, _free_vars);
avl_insert(parser->vars, (void *)var);
}
}
char *httpp_getvar(http_parser_t *parser, char *name)
{
http_var_t var;
http_var_t *found;
http_var_t var;
http_var_t *found;
var.name = name;
var.value = NULL;
var.name = name;
var.value = NULL;
if (avl_get_by_key(parser->vars, (void *)&var, (void **)&found) == 0)
return found->value;
else
return NULL;
if (avl_get_by_key(parser->vars, (void *)&var, (void **)&found) == 0)
return found->value;
else
return NULL;
}
void httpp_set_query_param(http_parser_t *parser, char *name, char *value)
{
http_var_t *var;
http_var_t *var;
if (name == NULL || value == NULL)
return;
if (name == NULL || value == NULL)
return;
var = (http_var_t *)malloc(sizeof(http_var_t));
if (var == NULL) return;
var = (http_var_t *)malloc(sizeof(http_var_t));
if (var == NULL) return;
var->name = strdup(name);
var->value = url_escape(value);
var->name = strdup(name);
var->value = url_escape(value);
if (httpp_get_query_param(parser, name) == NULL) {
avl_insert(parser->queryvars, (void *)var);
} else {
avl_delete(parser->queryvars, (void *)var, _free_vars);
avl_insert(parser->queryvars, (void *)var);
}
if (httpp_get_query_param(parser, name) == NULL) {
avl_insert(parser->queryvars, (void *)var);
} else {
avl_delete(parser->queryvars, (void *)var, _free_vars);
avl_insert(parser->queryvars, (void *)var);
}
}
char *httpp_get_query_param(http_parser_t *parser, char *name)
{
http_var_t var;
http_var_t *found;
http_var_t var;
http_var_t *found;
var.name = name;
var.value = NULL;
var.name = name;
var.value = NULL;
if (avl_get_by_key(parser->queryvars, (void *)&var, (void **)&found) == 0)
return found->value;
else
return NULL;
if (avl_get_by_key(parser->queryvars, (void *)&var, (void **)&found) == 0)
return found->value;
else
return NULL;
}
void httpp_clear(http_parser_t *parser)
{
parser->req_type = httpp_req_none;
if (parser->uri)
free(parser->uri);
parser->uri = NULL;
avl_tree_free(parser->vars, _free_vars);
avl_tree_free(parser->queryvars, _free_vars);
parser->vars = NULL;
parser->req_type = httpp_req_none;
if (parser->uri)
free(parser->uri);
parser->uri = NULL;
avl_tree_free(parser->vars, _free_vars);
avl_tree_free(parser->queryvars, _free_vars);
parser->vars = NULL;
}
void httpp_destroy(http_parser_t *parser)
{
httpp_clear(parser);
free(parser);
httpp_clear(parser);
free(parser);
}
char *_lowercase(char *str)
{
long i;
for (i = 0; i < strlen(str); i++)
str[i] = tolower(str[i]);
long i;
for (i = 0; i < strlen(str); i++)
str[i] = tolower(str[i]);
return str;
return str;
}
int _compare_vars(void *compare_arg, void *a, void *b)
{
http_var_t *vara, *varb;
http_var_t *vara, *varb;
vara = (http_var_t *)a;
varb = (http_var_t *)b;
vara = (http_var_t *)a;
varb = (http_var_t *)b;
return strcmp(vara->name, varb->name);
return strcmp(vara->name, varb->name);
}
int _free_vars(void *key)
{
http_var_t *var;
http_var_t *var;
var = (http_var_t *)key;
var = (http_var_t *)key;
if (var->name)
free(var->name);
if (var->value)
free(var->value);
free(var);
if (var->name)
free(var->name);
if (var->value)
free(var->value);
free(var);
return 1;
return 1;
}

View File

@ -17,25 +17,25 @@
#define HTTPP_VAR_ICYPASSWORD "__icy_password"
typedef enum httpp_request_type_tag {
httpp_req_none, httpp_req_get, httpp_req_post, httpp_req_head,
httpp_req_source, httpp_req_play, httpp_req_stats, httpp_req_unknown
httpp_req_none, httpp_req_get, httpp_req_post, httpp_req_head,
httpp_req_source, httpp_req_play, httpp_req_stats, httpp_req_unknown
} httpp_request_type_e;
typedef struct http_var_tag {
char *name;
char *value;
char *name;
char *value;
} http_var_t;
typedef struct http_varlist_tag {
http_var_t var;
struct http_varlist_tag *next;
http_var_t var;
struct http_varlist_tag *next;
} http_varlist_t;
typedef struct http_parser_tag {
httpp_request_type_e req_type;
char *uri;
avl_tree *vars;
avl_tree *queryvars;
httpp_request_type_e req_type;
char *uri;
avl_tree *vars;
avl_tree *queryvars;
} http_parser_t;
http_parser_t *httpp_create_parser(void);

View File

@ -6,54 +6,54 @@
int main(int argc, char **argv)
{
char buff[8192];
int readed;
http_parser_t parser;
avl_node *node;
http_var_t *var;
char buff[8192];
int readed;
http_parser_t parser;
avl_node *node;
http_var_t *var;
httpp_initialize(&parser, NULL);
httpp_initialize(&parser, NULL);
readed = fread(buff, 1, 8192, stdin);
if (httpp_parse(&parser, buff, readed)) {
printf("Parse succeeded...\n\n");
printf("Request was ");
switch (parser.req_type) {
case httpp_req_none:
printf(" none\n");
break;
case httpp_req_unknown:
printf(" unknown\n");
break;
case httpp_req_get:
printf(" get\n");
break;
case httpp_req_post:
printf(" post\n");
break;
case httpp_req_head:
printf(" head\n");
break;
}
printf("Version was 1.%d\n", parser.version);
node = avl_get_first(parser.vars);
while (node) {
var = (http_var_t *)node->key;
if (var)
printf("Iterating variable(s): %s = %s\n", var->name, var->value);
node = avl_get_next(node);
}
} else {
printf("Parse failed...\n");
}
readed = fread(buff, 1, 8192, stdin);
if (httpp_parse(&parser, buff, readed)) {
printf("Parse succeeded...\n\n");
printf("Request was ");
switch (parser.req_type) {
case httpp_req_none:
printf(" none\n");
break;
case httpp_req_unknown:
printf(" unknown\n");
break;
case httpp_req_get:
printf(" get\n");
break;
case httpp_req_post:
printf(" post\n");
break;
case httpp_req_head:
printf(" head\n");
break;
}
printf("Version was 1.%d\n", parser.version);
node = avl_get_first(parser.vars);
while (node) {
var = (http_var_t *)node->key;
if (var)
printf("Iterating variable(s): %s = %s\n", var->name, var->value);
node = avl_get_next(node);
}
} else {
printf("Parse failed...\n");
}
printf("Destroying parser...\n");
httpp_destroy(&parser);
printf("Destroying parser...\n");
httpp_destroy(&parser);
return 0;
return 0;
}

198
log/log.c
View File

@ -28,13 +28,13 @@ static int _initialized = 0;
typedef struct log_tag
{
int in_use;
int in_use;
int level;
int level;
char *filename;
FILE *logfile;
char *filename;
FILE *logfile;
char *buffer;
} log_t;
@ -47,26 +47,26 @@ static void _unlock_logger();
void log_initialize()
{
int i;
int i;
if (_initialized) return;
if (_initialized) return;
for (i = 0; i < LOG_MAXLOGS; i++) {
loglist[i].in_use = 0;
loglist[i].level = 2;
loglist[i].filename = NULL;
loglist[i].logfile = NULL;
loglist[i].buffer = NULL;
}
for (i = 0; i < LOG_MAXLOGS; i++) {
loglist[i].in_use = 0;
loglist[i].level = 2;
loglist[i].filename = NULL;
loglist[i].logfile = NULL;
loglist[i].buffer = NULL;
}
/* initialize mutexes */
/* initialize mutexes */
#ifndef _WIN32
pthread_mutex_init(&_logger_mutex, NULL);
pthread_mutex_init(&_logger_mutex, NULL);
#else
InitializeCriticalSection(&_logger_mutex);
InitializeCriticalSection(&_logger_mutex);
#endif
_initialized = 1;
_initialized = 1;
}
int log_open_file(FILE *file)
@ -75,34 +75,34 @@ int log_open_file(FILE *file)
if(file == NULL) return LOG_EINSANE;
log_id = _get_log_id();
if (log_id < 0) return LOG_ENOMORELOGS;
log_id = _get_log_id();
if (log_id < 0) return LOG_ENOMORELOGS;
loglist[log_id].logfile = file;
if (loglist[log_id].logfile != NULL) {
loglist[log_id].filename = NULL;
} else {
_release_log_id(log_id);
return LOG_ECANTOPEN;
}