httpp.c 14.1 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3
/* Httpp.c
**
** http parsing engine
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** Copyright (C) 2014 Michael Smith <msmith@icecast.org>,
**                    Ralph Giles <giles@xiph.org>,
**                    Ed "oddsock" Zaleski <oddsock@xiph.org>,
**                    Karl Heyes <karl@xiph.org>,
**                    Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the
** Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
** Boston, MA  02110-1301, USA.
**
Jack Moffitt's avatar
Jack Moffitt committed
26 27
*/

28 29 30 31
#ifdef HAVE_CONFIG_H
 #include <config.h>
#endif

Michael Smith's avatar
Michael Smith committed
32 33
#include <stdio.h>

Jack Moffitt's avatar
Jack Moffitt committed
34 35 36
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
37 38 39
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
Jack Moffitt's avatar
Jack Moffitt committed
40

41
#include <avl/avl.h>
Jack Moffitt's avatar
Jack Moffitt committed
42 43
#include "httpp.h"

Michael Smith's avatar
Michael Smith committed
44 45
#define MAX_HEADERS 32

Jack Moffitt's avatar
Jack Moffitt committed
46 47 48
/* internal functions */

/* misc */
49
static char *_lowercase(char *str);
Jack Moffitt's avatar
Jack Moffitt committed
50 51

/* for avl trees */
brendan's avatar
brendan committed
52 53
static int _compare_vars(void *compare_arg, void *a, void *b);
static int _free_vars(void *key);
Jack Moffitt's avatar
Jack Moffitt committed
54 55 56

http_parser_t *httpp_create_parser(void)
{
57
    return (http_parser_t *)malloc(sizeof(http_parser_t));
Jack Moffitt's avatar
Jack Moffitt committed
58 59 60 61
}

void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
{
62 63 64 65 66 67 68 69 70 71 72 73 74
    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);

    /* now insert the default variables */
    list = defaults;
    while (list != NULL) {
        httpp_setvar(parser, list->var.name, list->var.value);
        list = list->next;
    }
Jack Moffitt's avatar
Jack Moffitt committed
75 76
}

77
static int split_headers(char *data, unsigned long len, char **line)
Jack Moffitt's avatar
Jack Moffitt committed
78
{
79 80 81 82
    /* first we count how many lines there are 
    ** and set up the line[] array     
    */
    int lines = 0;
83
    unsigned long i;
84 85 86 87 88 89 90
    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';
91 92
            if (lines >= MAX_HEADERS)
                return MAX_HEADERS;
93 94 95 96
            if (i + 1 < len) {
                if (data[i + 1] == '\n' || data[i + 1] == '\r')
                    break;
                line[lines] = &data[i + 1];
Michael Smith's avatar
Michael Smith committed
97
            }
98 99
        }
    }
Jack Moffitt's avatar
Jack Moffitt committed
100

101
    i++;
102
    while (i < len && data[i] == '\n') i++;
103 104 105 106 107 108

    return lines;
}

static void parse_headers(http_parser_t *parser, char **line, int lines)
{
109 110
    int i, l;
    int whitespace, slen;
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
    char *name = NULL;
    char *value = NULL;

    /* parse the name: value lines. */
    for (l = 1; l < lines; l++) {
        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;
        }
    }
144 145
}

146
int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri)
147
{
148 149 150 151
    char *data;
    char *line[MAX_HEADERS];
    int lines, slen,i, whitespace=0, where=0,code;
    char *version=NULL, *resp_code=NULL, *message=NULL;
152
    
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    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;

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

    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_URI, uri);
    httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "NONE");

    parse_headers(parser, line, lines);

    free(data);

    return 1;
204 205
}

206 207
static int hex(char c)
{
208 209 210 211 212 213 214 215
    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;
216 217
}

218
static char *url_escape(const char *src)
219
{
220 221 222 223 224 225 226 227
    int len = strlen(src);
    unsigned char *decoded;
    int i;
    char *dst;
    int done = 0;

    decoded = calloc(1, len + 1);

228
    dst = (char *)decoded;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    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;
245 246 247
        case '+':
            *dst++ = ' ';
            break;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        case '#':
            done = 1;
            break;
        case 0:
            free(decoded);
            return NULL;
            break;
        default:
            *dst++ = src[i];
            break;
        }
        if(done)
            break;
    }

    *dst = 0; /* null terminator */

265
    return (char *)decoded;
266 267 268 269 270
}

/** TODO: This is almost certainly buggy in some cases */
static void parse_query(http_parser_t *parser, char *query)
{
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
    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);
    }
300 301
}

302
int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
303
{
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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;

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

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

355
    parser->req_type = httpp_str_to_method(req_type);
356 357 358 359

    if (uri != NULL && strlen(uri) > 0) {
        char *query;
        if((query = strchr(uri, '?')) != NULL) {
360
            httpp_setvar(parser, HTTPP_VAR_RAWURI, uri);
Karl Heyes's avatar
Karl Heyes committed
361
            httpp_setvar(parser, HTTPP_VAR_QUERYARGS, query);
362 363 364 365 366 367 368
            *query = 0;
            query++;
            parse_query(parser, query);
        }

        parser->uri = strdup(uri);
    } else {
369 370 371
        free(data);
        return 0;
    }
Jack Moffitt's avatar
Jack Moffitt committed
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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;
395 396 397
        case httpp_req_put:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PUT");
            break;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        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;
    }

    parse_headers(parser, line, lines);

    free(data);

    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
430 431
}

Karl Heyes's avatar
Karl Heyes committed
432 433 434 435 436 437 438 439 440 441 442
void httpp_deletevar(http_parser_t *parser, const char *name)
{
    http_var_t var;

    if (parser == NULL || name == NULL)
        return;
    var.name = (char*)name;
    var.value = NULL;
    avl_delete(parser->vars, (void *)&var, _free_vars);
}

443
void httpp_setvar(http_parser_t *parser, const char *name, const char *value)
Jack Moffitt's avatar
Jack Moffitt committed
444
{
445
    http_var_t *var;
Jack Moffitt's avatar
Jack Moffitt committed
446

447 448
    if (name == NULL || value == NULL)
        return;
Jack Moffitt's avatar
Jack Moffitt committed
449

450 451
    var = (http_var_t *)malloc(sizeof(http_var_t));
    if (var == NULL) return;
Jack Moffitt's avatar
Jack Moffitt committed
452

453 454
    var->name = strdup(name);
    var->value = strdup(value);
Jack Moffitt's avatar
Jack Moffitt committed
455

456 457 458 459 460 461
    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);
    }
Jack Moffitt's avatar
Jack Moffitt committed
462 463
}

464
const char *httpp_getvar(http_parser_t *parser, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
465
{
466 467
    http_var_t var;
    http_var_t *found;
468
    void *fp;
Jack Moffitt's avatar
Jack Moffitt committed
469

Karl Heyes's avatar
Karl Heyes committed
470 471 472
    if (parser == NULL || name == NULL)
        return NULL;

473
    fp = &found;
474
    var.name = (char*)name;
475
    var.value = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
476

477
    if (avl_get_by_key(parser->vars, &var, fp) == 0)
478 479 480
        return found->value;
    else
        return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
481 482
}

483
void httpp_set_query_param(http_parser_t *parser, const char *name, const char *value)
484
{
485
    http_var_t *var;
486

487 488
    if (name == NULL || value == NULL)
        return;
489

490 491
    var = (http_var_t *)malloc(sizeof(http_var_t));
    if (var == NULL) return;
492

493 494
    var->name = strdup(name);
    var->value = url_escape(value);
495

496 497 498 499 500 501
    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);
    }
502 503
}

504
const char *httpp_get_query_param(http_parser_t *parser, const char *name)
505
{
506 507
    http_var_t var;
    http_var_t *found;
508
    void *fp;
509

510
    fp = &found;
511
    var.name = (char *)name;
512
    var.value = NULL;
513

514
    if (avl_get_by_key(parser->queryvars, (void *)&var, fp) == 0)
515 516 517
        return found->value;
    else
        return NULL;
518 519
}

520
void httpp_clear(http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
521
{
522 523 524 525 526 527 528
    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;
Jack Moffitt's avatar
Jack Moffitt committed
529 530
}

531 532
void httpp_destroy(http_parser_t *parser)
{
533 534
    httpp_clear(parser);
    free(parser);
535 536
}

537
static char *_lowercase(char *str)
Jack Moffitt's avatar
Jack Moffitt committed
538
{
539
    char *p = str;
540
    for (; *p != '\0'; p++)
541
        *p = tolower(*p);
Jack Moffitt's avatar
Jack Moffitt committed
542

543
    return str;
Jack Moffitt's avatar
Jack Moffitt committed
544 545
}

brendan's avatar
brendan committed
546
static int _compare_vars(void *compare_arg, void *a, void *b)
Jack Moffitt's avatar
Jack Moffitt committed
547
{
548
    http_var_t *vara, *varb;
Jack Moffitt's avatar
Jack Moffitt committed
549

550 551
    vara = (http_var_t *)a;
    varb = (http_var_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
552

553
    return strcmp(vara->name, varb->name);
Jack Moffitt's avatar
Jack Moffitt committed
554 555
}

brendan's avatar
brendan committed
556
static int _free_vars(void *key)
Jack Moffitt's avatar
Jack Moffitt committed
557
{
558
    http_var_t *var;
Jack Moffitt's avatar
Jack Moffitt committed
559

560
    var = (http_var_t *)key;
Jack Moffitt's avatar
Jack Moffitt committed
561

562 563 564 565 566
    if (var->name)
        free(var->name);
    if (var->value)
        free(var->value);
    free(var);
Jack Moffitt's avatar
Jack Moffitt committed
567

568
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
569 570
}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
httpp_request_type_e httpp_str_to_method(const char * method) {
    if (strcasecmp("GET", method) == 0) {
        return httpp_req_get;
    } else if (strcasecmp("POST", method) == 0) {
        return httpp_req_post;
    } else if (strcasecmp("PUT", method) == 0) {
        return httpp_req_put;
    } else if (strcasecmp("HEAD", method) == 0) {
        return httpp_req_head;
    } else if (strcasecmp("SOURCE", method) == 0) {
        return httpp_req_source;
    } else if (strcasecmp("PLAY", method) == 0) {
        return httpp_req_play;
    } else if (strcasecmp("STATS", method) == 0) {
        return httpp_req_stats;
    } else {
        return httpp_req_unknown;
    }
}