2001-09-09 22:24:39 -04:00
|
|
|
/* Httpp.c
|
|
|
|
**
|
|
|
|
** http parsing engine
|
|
|
|
*/
|
|
|
|
|
2003-03-08 11:05:38 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2003-03-09 17:56:46 -05:00
|
|
|
#include <avl/avl.h>
|
2001-09-09 22:24:39 -04:00
|
|
|
#include "httpp.h"
|
|
|
|
|
2001-10-20 00:41:54 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define strcasecmp stricmp
|
|
|
|
#endif
|
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
#define MAX_HEADERS 32
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
/* internal functions */
|
|
|
|
|
|
|
|
/* misc */
|
|
|
|
char *_lowercase(char *str);
|
|
|
|
|
|
|
|
/* for avl trees */
|
|
|
|
int _compare_vars(void *compare_arg, void *a, void *b);
|
|
|
|
int _free_vars(void *key);
|
|
|
|
|
|
|
|
http_parser_t *httpp_create_parser(void)
|
|
|
|
{
|
|
|
|
return (http_parser_t *)malloc(sizeof(http_parser_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
|
|
|
|
{
|
|
|
|
http_varlist_t *list;
|
|
|
|
|
|
|
|
parser->req_type = httpp_req_none;
|
|
|
|
parser->uri = NULL;
|
|
|
|
parser->vars = avl_tree_new(_compare_vars, NULL);
|
2003-01-15 18:46:56 -05:00
|
|
|
parser->queryvars = avl_tree_new(_compare_vars, NULL);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* now insert the default variables */
|
|
|
|
list = defaults;
|
|
|
|
while (list != NULL) {
|
|
|
|
httpp_setvar(parser, list->var.name, list->var.value);
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
static int split_headers(char *data, unsigned long len, char **line)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
/* first we count how many lines there are
|
|
|
|
** and set up the line[] array
|
|
|
|
*/
|
2003-01-17 04:01:04 -05:00
|
|
|
int lines = 0;
|
|
|
|
unsigned long i;
|
2001-09-09 22:24:39 -04:00
|
|
|
line[lines] = data;
|
2002-04-05 04:28:26 -05:00
|
|
|
for (i = 0; i < len && lines < MAX_HEADERS; i++) {
|
2001-09-09 22:24:39 -04:00
|
|
|
if (data[i] == '\r')
|
|
|
|
data[i] = '\0';
|
|
|
|
if (data[i] == '\n') {
|
|
|
|
lines++;
|
|
|
|
data[i] = '\0';
|
2002-02-11 04:11:18 -05:00
|
|
|
if (i + 1 < len) {
|
|
|
|
if (data[i + 1] == '\n' || data[i + 1] == '\r')
|
|
|
|
break;
|
2001-09-09 22:24:39 -04:00
|
|
|
line[lines] = &data[i + 1];
|
2002-02-11 04:11:18 -05:00
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
while (data[i] == '\n') i++;
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int httpp_parse_response(http_parser_t *parser, char *http_data, unsigned long len, char *uri)
|
|
|
|
{
|
2003-01-15 18:46:56 -05:00
|
|
|
char *data;
|
|
|
|
char *line[MAX_HEADERS];
|
|
|
|
int lines, slen,i, whitespace=0, where=0,code;
|
|
|
|
char *version=NULL, *resp_code=NULL, *message=NULL;
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
if(http_data == NULL)
|
|
|
|
return 0;
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
/* 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);
|
2003-01-15 18:46:56 -05:00
|
|
|
data[len] = 0;
|
|
|
|
|
|
|
|
lines = split_headers(data, len, line);
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
if(version == NULL || resp_code == NULL || message == NULL) {
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
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);
|
2002-08-16 10:22:44 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
parse_headers(parser, line, lines);
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-12-31 01:28:39 -05:00
|
|
|
static int hex(char c)
|
|
|
|
{
|
2003-01-15 18:46:56 -05:00
|
|
|
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;
|
2002-12-31 01:28:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *url_escape(char *src)
|
|
|
|
{
|
2003-01-15 18:46:56 -05:00
|
|
|
int len = strlen(src);
|
|
|
|
unsigned char *decoded;
|
|
|
|
int i;
|
|
|
|
char *dst;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
decoded = calloc(1, len + 1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
*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 */
|
|
|
|
|
|
|
|
return decoded;
|
2002-12-31 01:28:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** TODO: This is almost certainly buggy in some cases */
|
|
|
|
static void parse_query(http_parser_t *parser, char *query)
|
|
|
|
{
|
2003-01-15 18:46:56 -05:00
|
|
|
int len;
|
|
|
|
int i=0;
|
|
|
|
char *key = query;
|
|
|
|
char *val=NULL;
|
|
|
|
|
|
|
|
if(!query || !*query)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(val && key) {
|
|
|
|
httpp_set_query_param(parser, key, val);
|
|
|
|
}
|
2002-12-31 01:28:39 -05:00
|
|
|
}
|
|
|
|
|
2003-03-07 23:57:02 -05:00
|
|
|
/* The old shoutcast procotol. Don't look at this, it's really nasty */
|
|
|
|
int httpp_parse_icy(http_parser_t *parser, char *http_data, unsigned long len)
|
|
|
|
{
|
|
|
|
char *data;
|
|
|
|
char *line[MAX_HEADERS];
|
|
|
|
int lines;
|
|
|
|
|
|
|
|
if(http_data == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
data = malloc(len + 1);
|
|
|
|
memcpy(data, http_data, len);
|
|
|
|
data[len] = 0;
|
|
|
|
|
|
|
|
lines = split_headers(data, len, line);
|
|
|
|
|
|
|
|
/* Now, this protocol looks like:
|
|
|
|
* sourcepassword\n
|
|
|
|
* headers: as normal\n"
|
|
|
|
* \n
|
|
|
|
*/
|
|
|
|
|
|
|
|
parser->req_type = httpp_req_source;
|
|
|
|
httpp_setvar(parser, HTTPP_VAR_URI, "/");
|
|
|
|
httpp_setvar(parser, HTTPP_VAR_ICYPASSWORD, line[0]);
|
|
|
|
httpp_setvar(parser, HTTPP_VAR_PROTOCOL, "ICY");
|
2003-03-08 00:27:17 -05:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
|
2003-03-07 23:57:02 -05:00
|
|
|
/* This protocol is evil */
|
|
|
|
httpp_setvar(parser, HTTPP_VAR_VERSION, "666");
|
|
|
|
|
|
|
|
parse_headers(parser, line, lines);
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
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;
|
2003-01-15 18:46:56 -05:00
|
|
|
int whitespace, where, slen;
|
2002-08-05 10:48:04 -04:00
|
|
|
|
|
|
|
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);
|
2003-01-15 18:46:56 -05:00
|
|
|
data[len] = 0;
|
2002-08-05 10:48:04 -04:00
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
lines = split_headers(data, len, line);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
if (uri != NULL && strlen(uri) > 0) {
|
|
|
|
char *query;
|
|
|
|
if((query = strchr(uri, '?')) != NULL) {
|
|
|
|
*query = 0;
|
|
|
|
query++;
|
|
|
|
parse_query(parser, query);
|
|
|
|
}
|
2002-12-31 01:28:39 -05:00
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
parser->uri = strdup(uri);
|
2003-03-07 23:57:02 -05:00
|
|
|
} else {
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL)) {
|
|
|
|
tmp[0] = '\0';
|
|
|
|
if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0)) {
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
|
2001-09-09 22:24:39 -04:00
|
|
|
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:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
case httpp_req_post:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
case httpp_req_head:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
case httpp_req_source:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
case httpp_req_play:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
case httpp_req_stats:
|
2001-10-20 03:40:09 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
|
2001-09-09 22:24:39 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
if (parser->uri != NULL) {
|
2001-09-09 22:24:39 -04:00
|
|
|
httpp_setvar(parser, HTTPP_VAR_URI, parser->uri);
|
|
|
|
} else {
|
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-01-15 18:46:56 -05:00
|
|
|
parse_headers(parser, line, lines);
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
|
2002-08-05 10:48:04 -04:00
|
|
|
return 1;
|
2001-09-09 22:24:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void httpp_setvar(http_parser_t *parser, char *name, char *value)
|
|
|
|
{
|
|
|
|
http_var_t *var;
|
|
|
|
|
|
|
|
if (name == NULL || value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var = (http_var_t *)malloc(sizeof(http_var_t));
|
|
|
|
if (var == NULL) return;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *httpp_getvar(http_parser_t *parser, char *name)
|
|
|
|
{
|
|
|
|
http_var_t var;
|
|
|
|
http_var_t *found;
|
|
|
|
|
|
|
|
var.name = name;
|
2003-01-15 18:46:56 -05:00
|
|
|
var.value = NULL;
|
2001-09-09 22:24:39 -04:00
|
|
|
|
|
|
|
if (avl_get_by_key(parser->vars, (void *)&var, (void **)&found) == 0)
|
|
|
|
return found->value;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-12-31 01:28:39 -05:00
|
|
|
void httpp_set_query_param(http_parser_t *parser, char *name, char *value)
|
|
|
|
{
|
|
|
|
http_var_t *var;
|
|
|
|
|
|
|
|
if (name == NULL || value == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var = (http_var_t *)malloc(sizeof(http_var_t));
|
|
|
|
if (var == NULL) return;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *httpp_get_query_param(http_parser_t *parser, char *name)
|
|
|
|
{
|
|
|
|
http_var_t var;
|
|
|
|
http_var_t *found;
|
|
|
|
|
|
|
|
var.name = name;
|
2003-01-16 00:48:31 -05:00
|
|
|
var.value = NULL;
|
2002-12-31 01:28:39 -05:00
|
|
|
|
|
|
|
if (avl_get_by_key(parser->queryvars, (void *)&var, (void **)&found) == 0)
|
|
|
|
return found->value;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-03 11:04:56 -04:00
|
|
|
void httpp_clear(http_parser_t *parser)
|
2001-09-09 22:24:39 -04:00
|
|
|
{
|
|
|
|
parser->req_type = httpp_req_none;
|
|
|
|
if (parser->uri)
|
|
|
|
free(parser->uri);
|
|
|
|
parser->uri = NULL;
|
|
|
|
avl_tree_free(parser->vars, _free_vars);
|
2002-12-31 01:28:39 -05:00
|
|
|
avl_tree_free(parser->queryvars, _free_vars);
|
2001-09-09 22:24:39 -04:00
|
|
|
parser->vars = NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-03 11:04:56 -04:00
|
|
|
void httpp_destroy(http_parser_t *parser)
|
|
|
|
{
|
2003-01-15 18:46:56 -05:00
|
|
|
httpp_clear(parser);
|
|
|
|
free(parser);
|
2002-05-03 11:04:56 -04:00
|
|
|
}
|
|
|
|
|
2001-09-09 22:24:39 -04:00
|
|
|
char *_lowercase(char *str)
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
for (i = 0; i < strlen(str); i++)
|
|
|
|
str[i] = tolower(str[i]);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _compare_vars(void *compare_arg, void *a, void *b)
|
|
|
|
{
|
|
|
|
http_var_t *vara, *varb;
|
|
|
|
|
|
|
|
vara = (http_var_t *)a;
|
|
|
|
varb = (http_var_t *)b;
|
|
|
|
|
|
|
|
return strcmp(vara->name, varb->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int _free_vars(void *key)
|
|
|
|
{
|
|
|
|
http_var_t *var;
|
|
|
|
|
|
|
|
var = (http_var_t *)key;
|
|
|
|
|
|
|
|
if (var->name)
|
|
|
|
free(var->name);
|
|
|
|
if (var->value)
|
|
|
|
free(var->value);
|
|
|
|
free(var);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|