httpp.c 6.05 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Httpp.c
**
** http parsing engine
*/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "thread.h"
#include "avl.h"
#include "httpp.h"

14 15 16 17
#ifdef _WIN32
#define strcasecmp stricmp
#endif

Jack Moffitt's avatar
Jack Moffitt committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* 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);

	/* now insert the default variables */
	list = defaults;
	while (list != NULL) {
		httpp_setvar(parser, list->var.name, list->var.value);
		list = list->next;
	}
}

int httpp_parse(http_parser_t *parser, char *http_data, unsigned long len)
{
	char *data, *tmp;
	char *line[32]; /* limited to 32 lines, should be more than enough */
	int i, l, retlen;
	int lines;
54 55 56 57 58
	char *req_type = NULL;
	char *uri = NULL;
	char *version = NULL;
	char *name = NULL;
	char *value = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	int whitespace, where;
	int slen;

	if (http_data == NULL)
		return 0;

	/* make a local copy of the data */
	data = (char *)malloc(len);
	if (data == NULL) return 0;
	memcpy(data, http_data, len);

	/* first we count how many lines there are 
	** and set up the line[] array	 
	*/
	lines = 0;
	line[lines] = data;
	for (i = 0; i < len; i++) {
		if (data[i] == '\r')
			data[i] = '\0';
		if (data[i] == '\n') {
			lines++;
			if (i + 1 < len)
				if (data[i + 1] == '\n' || data[i + 1] == '\r') {
					data[i] = '\0';
					break;
				}
			data[i] = '\0';
			if (i < len - 1)
				line[lines] = &data[i + 1];
		}
	}

	i++;
	while (data[i] == '\n') i++;
	retlen = i;

	/* 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 (uri != NULL && strlen(uri) > 0)
		parser->uri = strdup(uri);
	else
		parser->uri = NULL;

	if ((version != NULL) && ((tmp = strchr(version, '/')) != NULL)) {
		tmp[0] = '\0';
		if ((strlen(version) > 0) && (strlen(&tmp[1]) > 0)) {
150
			httpp_setvar(parser, HTTPP_VAR_PROTOCOL, version);
Jack Moffitt's avatar
Jack Moffitt committed
151 152 153 154 155 156 157 158 159 160 161 162 163
			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:
164
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "GET");
Jack Moffitt's avatar
Jack Moffitt committed
165 166
			break;
		case httpp_req_post:
167
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "POST");
Jack Moffitt's avatar
Jack Moffitt committed
168 169
			break;
		case httpp_req_head:
170
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
Jack Moffitt's avatar
Jack Moffitt committed
171 172
			break;
		case httpp_req_source:
173
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "SOURCE");
Jack Moffitt's avatar
Jack Moffitt committed
174 175
			break;
		case httpp_req_play:
176
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PLAY");
Jack Moffitt's avatar
Jack Moffitt committed
177 178
			break;
		case httpp_req_stats:
179
			httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "STATS");
Jack Moffitt's avatar
Jack Moffitt committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
			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;
	}

	/* 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;
		}
	}

	free(data);

	return retlen;
}

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;
        var.value = NULL;

	if (avl_get_by_key(parser->vars, (void *)&var, (void **)&found) == 0)
		return found->value;
	else
		return NULL;
}

void httpp_destroy(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);
	parser->vars = NULL;
}

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;
}