OSUOSL/Nero are experiencing Internet connectivity problems. This affects us as we're hosted with OSUOSL. We apologize for the inconvenience.

httpp.c 23.9 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 57
/* For avl tree manipulation */
static void parse_query(avl_tree *tree, const char *query, size_t len);
static const char *_httpp_get_param(avl_tree *tree, const char *name);
58
static void _httpp_set_param_nocopy(avl_tree *tree, char *name, char *value, int replace);
59
static void _httpp_set_param(avl_tree *tree, const char *name, const char *value);
60
static http_var_t *_httpp_get_param_var(avl_tree *tree, const char *name);
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
httpp_request_info_t httpp_request_info(httpp_request_type_e req)
{
#if 0
#define HTTPP_REQUEST_IS_SAFE                       ((httpp_request_info_t)0x0001U)
#define HTTPP_REQUEST_IS_IDEMPOTENT                 ((httpp_request_info_t)0x0002U)
#define HTTPP_REQUEST_IS_CACHEABLE                  ((httpp_request_info_t)0x0004U)
#define HTTPP_REQUEST_HAS_RESPONSE_BODY             ((httpp_request_info_t)0x0010U)
#define HTTPP_REQUEST_HAS_REQUEST_BODY              ((httpp_request_info_t)0x0100U)
#define HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY     ((httpp_request_info_t)0x0200U)
#endif
    switch (req) {
        /* offical methods */
        case httpp_req_get:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_IS_CACHEABLE|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY;
        break;
        case httpp_req_post:
            return HTTPP_REQUEST_IS_CACHEABLE|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_REQUEST_BODY;
        break;
        case httpp_req_put:
            return HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_REQUEST_BODY;
        break;
        case httpp_req_head:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_IS_CACHEABLE;
        break;
        case httpp_req_options:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY;
        break;
        case httpp_req_delete:
            return HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_HAS_RESPONSE_BODY;
        break;
        case httpp_req_trace:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_IS_IDEMPOTENT|HTTPP_REQUEST_HAS_RESPONSE_BODY;
        break;
        case httpp_req_connect:
            return HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_REQUEST_BODY;
        break;

        /* Icecast specific methods */
        case httpp_req_source:
            return HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_REQUEST_BODY;
        break;
        case httpp_req_play:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY;
        break;
        case httpp_req_stats:
            return HTTPP_REQUEST_IS_SAFE|HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY;
        break;

        /* Virtual and other methods */
        case httpp_req_none:
        case httpp_req_unknown:
        default:
            return HTTPP_REQUEST_HAS_RESPONSE_BODY|HTTPP_REQUEST_HAS_OPTIONAL_REQUEST_BODY;
        break;
    }
}

Jack Moffitt's avatar
Jack Moffitt committed
119 120
http_parser_t *httpp_create_parser(void)
{
121
    http_parser_t *parser = calloc(1, sizeof(http_parser_t));
122

123
    parser->refc = 1;
124 125 126 127
    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);
128
    parser->postvars = avl_tree_new(_compare_vars, NULL);
129

130 131 132 133 134 135 136
    return parser;
}

void httpp_initialize(http_parser_t *parser, http_varlist_t *defaults)
{
    http_varlist_t *list;

137 138 139
    /* now insert the default variables */
    list = defaults;
    while (list != NULL) {
140 141 142 143 144 145
        size_t i;

        for (i = 0; i < list->var.values; i++) {
            httpp_setvar(parser, list->var.name, list->var.value[i]);
        }

146 147
        list = list->next;
    }
Jack Moffitt's avatar
Jack Moffitt committed
148 149
}

150
static int split_headers(char *data, unsigned long len, char **line)
Jack Moffitt's avatar
Jack Moffitt committed
151
{
152 153 154 155
    /* first we count how many lines there are 
    ** and set up the line[] array     
    */
    int lines = 0;
156
    unsigned long i;
157 158 159 160 161 162 163
    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';
164 165
            if (lines >= MAX_HEADERS)
                return MAX_HEADERS;
166 167 168 169
            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
170
            }
171 172
        }
    }
Jack Moffitt's avatar
Jack Moffitt committed
173

174
    i++;
175
    while (i < len && data[i] == '\n') i++;
176 177 178 179 180 181

    return lines;
}

static void parse_headers(http_parser_t *parser, char **line, int lines)
{
182 183
    int i, l;
    int whitespace, slen;
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
    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;
        }
    }
217 218
}

219
int httpp_parse_response(http_parser_t *parser, const char *http_data, unsigned long len, const char *uri)
220
{
221 222 223 224
    char *data;
    char *line[MAX_HEADERS];
    int lines, slen,i, whitespace=0, where=0,code;
    char *version=NULL, *resp_code=NULL, *message=NULL;
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
    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;
277 278
}

279 280 281 282 283 284 285 286 287 288 289 290 291
int httpp_parse_postdata(http_parser_t *parser, const char *body_data, size_t len)
{
    const char *header = httpp_getvar(parser, "content-type");

    if (strcasecmp(header, "application/x-www-form-urlencoded") != 0) {
        return -1;
    }

    parse_query(parser->postvars, body_data, len);

    return 0;
}

292 293
static int hex(char c)
{
294 295 296 297 298 299 300 301
    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;
302 303
}

304
static char *url_unescape(const char *src, size_t len)
305
{
306
    unsigned char *decoded;
307
    size_t i;
308 309 310 311 312
    char *dst;
    int done = 0;

    decoded = calloc(1, len + 1);

313
    dst = (char *)decoded;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

    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;
330 331 332
        case '+':
            *dst++ = ' ';
            break;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        case '#':
            done = 1;
            break;
        case 0:
            free(decoded);
            return NULL;
            break;
        default:
            *dst++ = src[i];
            break;
        }
        if(done)
            break;
    }

    *dst = 0; /* null terminator */

350
    return (char *)decoded;
351 352
}

353
static void parse_query_element(avl_tree *tree, const char *start, const char *mid, const char *end)
354
{
355 356 357 358 359 360 361
    size_t keylen;
    char *key;
    size_t valuelen;
    char *value;

    if (start >= end)
        return;
362

363
    if (!mid)
364 365
        return;

366 367
    keylen = mid - start;
    valuelen = end - mid - 1;
368

369 370 371 372 373 374 375 376 377 378
    /* REVIEW: We ignore keys with empty values. */
    if (!keylen || !valuelen)
        return;

    key = malloc(keylen + 1);
    memcpy(key, start, keylen);
    key[keylen] = 0;

    value = url_unescape(mid + 1, valuelen);

379
    _httpp_set_param_nocopy(tree, key, value, 0);
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
}

static void parse_query(avl_tree *tree, const char *query, size_t len)
{
    const char *start = query;
    const char *mid = NULL;
    size_t i;

    if (!query || !*query)
        return;

    for (i = 0; i < len; i++) {
        switch (query[i]) {
            case '&':
                parse_query_element(tree, start, mid, &(query[i]));
                start = &(query[i + 1]);
                mid = NULL;
397
            break;
398 399
            case '=':
                mid = &(query[i]);
400 401 402 403
            break;
        }
    }

404
    parse_query_element(tree, start, mid, &(query[i]));
405 406
}

407
int httpp_parse(http_parser_t *parser, const char *http_data, unsigned long len)
408
{
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
    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) {
Philipp Schafft's avatar
Philipp Schafft committed
449 450
                    case 1:
                        uri = &line[0][i];
451
                    break;
Philipp Schafft's avatar
Philipp Schafft committed
452 453
                    case 2:
                        version = &line[0][i];
454
                    break;
455 456 457 458 459
                    case 3:
                        /* There is an extra element in the request line. This is not HTTP. */
                        free(data);
                        return 0;
                    break;
460 461 462 463 464
                }
            }
        }
    }

465
    parser->req_type = httpp_str_to_method(req_type);
466 467 468 469

    if (uri != NULL && strlen(uri) > 0) {
        char *query;
        if((query = strchr(uri, '?')) != NULL) {
470
            httpp_setvar(parser, HTTPP_VAR_RAWURI, uri);
Karl Heyes's avatar
Karl Heyes committed
471
            httpp_setvar(parser, HTTPP_VAR_QUERYARGS, query);
472 473
            *query = 0;
            query++;
474
            parse_query(parser->queryvars, query, strlen(query));
475 476 477 478
        }

        parser->uri = strdup(uri);
    } else {
479 480 481
        free(data);
        return 0;
    }
Jack Moffitt's avatar
Jack Moffitt committed
482

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    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;
505 506 507
        case httpp_req_put:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "PUT");
            break;
508 509 510
        case httpp_req_head:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "HEAD");
            break;
511 512 513 514 515 516 517 518 519 520 521 522
        case httpp_req_options:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "OPTIONS");
            break;
        case httpp_req_delete:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "DELETE");
            break;
        case httpp_req_trace:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "TRACE");
            break;
        case httpp_req_connect:
            httpp_setvar(parser, HTTPP_VAR_REQ_TYPE, "CONNECT");
            break;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        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
552 553
}

Karl Heyes's avatar
Karl Heyes committed
554 555 556 557 558 559
void httpp_deletevar(http_parser_t *parser, const char *name)
{
    http_var_t var;

    if (parser == NULL || name == NULL)
        return;
560 561
    memset(&var, 0, sizeof(var));

Karl Heyes's avatar
Karl Heyes committed
562
    var.name = (char*)name;
563

Karl Heyes's avatar
Karl Heyes committed
564 565 566
    avl_delete(parser->vars, (void *)&var, _free_vars);
}

567
void httpp_setvar(http_parser_t *parser, const char *name, const char *value)
Jack Moffitt's avatar
Jack Moffitt committed
568
{
569
    http_var_t *var;
Jack Moffitt's avatar
Jack Moffitt committed
570

571 572
    if (name == NULL || value == NULL)
        return;
Jack Moffitt's avatar
Jack Moffitt committed
573

574
    var = (http_var_t *)calloc(1, sizeof(http_var_t));
575
    if (var == NULL) return;
Jack Moffitt's avatar
Jack Moffitt committed
576

577 578 579 580 581 582
    var->value = calloc(1, sizeof(*var->value));
    if (!var->value) {
        free(var);
        return;
    }

583
    var->name = strdup(name);
584 585
    var->values = 1;
    var->value[0] = strdup(value);
Jack Moffitt's avatar
Jack Moffitt committed
586

587 588 589 590 591 592
    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
593 594
}

595
const char *httpp_getvar(http_parser_t *parser, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
596
{
597 598
    http_var_t var;
    http_var_t *found;
599
    void *fp;
Jack Moffitt's avatar
Jack Moffitt committed
600

Karl Heyes's avatar
Karl Heyes committed
601 602 603
    if (parser == NULL || name == NULL)
        return NULL;

604
    fp = &found;
605
    memset(&var, 0, sizeof(var));
606
    var.name = (char*)name;
Jack Moffitt's avatar
Jack Moffitt committed
607

608 609 610 611 612
    if (avl_get_by_key(parser->vars, &var, fp) == 0) {
        if (!found->values)
            return NULL;
        return found->value[0];
    } else {
613
        return NULL;
614
    }
Jack Moffitt's avatar
Jack Moffitt committed
615 616
}

617
static void _httpp_set_param_nocopy(avl_tree *tree, char *name, char *value, int replace)
618
{
619 620
    http_var_t *var, *found;
    char **n;
621

622 623
    if (name == NULL || value == NULL)
        return;
624

625
    found = _httpp_get_param_var(tree, name);
626

627 628 629 630 631 632 633
    if (replace || !found) {
        var = (http_var_t *)calloc(1, sizeof(http_var_t));
        if (var == NULL) {
            free(name);
            free(value);
            return;
        }
634

635
        var->name = name;
636
    } else {
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        free(name);
        var = found;
    }

    n = realloc(var->value, sizeof(*n)*(var->values + 1));
    if (!n) {
        if (replace || !found) {
            free(name);
            free(var);
        }
        free(value);
        return;
    }

    var->value = n;
    var->value[var->values++] = value;

    if (replace && found) {
        avl_delete(tree, (void *)found, _free_vars);
656
        avl_insert(tree, (void *)var);
657 658
    } else if (!found) {
        avl_insert(tree, (void *)var);
659
    }
660 661
}

662 663 664 665 666
static void _httpp_set_param(avl_tree *tree, const char *name, const char *value)
{
    if (name == NULL || value == NULL)
        return;

667
    _httpp_set_param_nocopy(tree, strdup(name), url_unescape(value, strlen(value)), 1);
668 669
}

670
static http_var_t *_httpp_get_param_var(avl_tree *tree, const char *name)
671
{
672 673
    http_var_t var;
    http_var_t *found;
674
    void *fp;
675

676
    fp = &found;
677
    memset(&var, 0, sizeof(var));
678
    var.name = (char *)name;
679

680
    if (avl_get_by_key(tree, (void *)&var, fp) == 0)
681
        return found;
682 683
    else
        return NULL;
684
}
685 686 687 688 689 690 691 692 693 694 695 696
static const char *_httpp_get_param(avl_tree *tree, const char *name)
{
    http_var_t *res = _httpp_get_param_var(tree, name);

    if (!res)
        return NULL;

    if (!res->values)
        return NULL;

    return res->value[0];
}
697

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
void httpp_set_query_param(http_parser_t *parser, const char *name, const char *value)
{
    return _httpp_set_param(parser->queryvars, name, value);
}

const char *httpp_get_query_param(http_parser_t *parser, const char *name)
{
    return _httpp_get_param(parser->queryvars, name);
}

void httpp_set_post_param(http_parser_t *parser, const char *name, const char *value)
{
    return _httpp_set_param(parser->postvars, name, value);
}

const char *httpp_get_post_param(http_parser_t *parser, const char *name)
{
    return _httpp_get_param(parser->postvars, name);
}

718 719 720 721 722 723 724 725 726 727
const http_var_t *httpp_get_param_var(http_parser_t *parser, const char *name)
{
    http_var_t *ret = _httpp_get_param_var(parser->postvars, name);

    if (ret)
        return ret;

    return _httpp_get_param_var(parser->queryvars, name);
}

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
const http_var_t *httpp_get_any_var(http_parser_t *parser, httpp_ns_t ns, const char *name)
{
    avl_tree *tree = NULL;

    if (!parser || !name)
        return NULL;

    switch (ns) {
        case HTTPP_NS_VAR:
            if (name[0] != '_' || name[1] != '_')
                return NULL;
            tree = parser->vars;
        break;
        case HTTPP_NS_HEADER:
            if (name[0] == '_' && name[1] == '_')
                return NULL;
            tree = parser->vars;
        break;
        case HTTPP_NS_QUERY_STRING:
            tree = parser->queryvars;
        break;
        case HTTPP_NS_POST_BODY:
            tree = parser->postvars;
        break;
    }

    if (!tree)
        return NULL;

    return _httpp_get_param_var(tree, name);
}

char ** httpp_get_any_key(http_parser_t *parser, httpp_ns_t ns)
{
    avl_tree *tree = NULL;
    avl_node *avlnode;
    char **ret;
    size_t len;
    size_t pos = 0;

    if (!parser)
        return NULL;

    switch (ns) {
        case HTTPP_NS_VAR:
        case HTTPP_NS_HEADER:
            tree = parser->vars;
        break;
        case HTTPP_NS_QUERY_STRING:
            tree = parser->queryvars;
        break;
        case HTTPP_NS_POST_BODY:
            tree = parser->postvars;
        break;
    }

    if (!tree)
        return NULL;

    ret = calloc(8, sizeof(*ret));
    if (!ret)
        return NULL;

    len = 8;

    for (avlnode = avl_get_first(tree); avlnode; avlnode = avl_get_next(avlnode)) {
        http_var_t *var = avlnode->key;

        if (ns == HTTPP_NS_VAR) {
            if (var->name[0] != '_' || var->name[1] != '_') {
                continue;
            }
        } else if (ns == HTTPP_NS_HEADER) {
            if (var->name[0] == '_' && var->name[1] == '_') {
                continue;
            }
        }

        if (pos == (len-1)) {
            char **n = realloc(ret, sizeof(*ret)*(len + 8));
            if (!n) {
                httpp_free_any_key(ret);
                return NULL;
            }
            memset(n + len, 0, sizeof(*n)*8);
            ret = n;
            len += 8;
        }

        ret[pos] = strdup(var->name);
        if (!ret[pos]) {
            httpp_free_any_key(ret);
            return NULL;
        }

        pos++;
    }

    return ret;
}

void httpp_free_any_key(char **keys)
{
    char **p;

    if (!keys)
        return;

    for (p = keys; *p; p++) {
        free(*p);
    }
    free(keys);
}

842 843 844 845 846 847 848 849 850 851
const char *httpp_get_param(http_parser_t *parser, const char *name)
{
    const char *ret = _httpp_get_param(parser->postvars, name);

    if (ret)
        return ret;

    return _httpp_get_param(parser->queryvars, name);
}

852
static void httpp_clear(http_parser_t *parser)
Jack Moffitt's avatar
Jack Moffitt committed
853
{
854 855 856 857 858 859
    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);
860
    avl_tree_free(parser->postvars, _free_vars);
861
    parser->vars = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
862 863
}

864 865 866 867 868 869 870 871 872 873 874
int httpp_addref(http_parser_t *parser)
{
    if (!parser)
        return -1;

    parser->refc++;

    return 0;
}

int httpp_release(http_parser_t *parser)
875
{
876 877 878 879 880 881 882
    if (!parser)
        return -1;

    parser->refc--;
    if (parser->refc)
        return 0;

883 884
    httpp_clear(parser);
    free(parser);
885 886

    return 0;
887 888
}

889
static char *_lowercase(char *str)
Jack Moffitt's avatar
Jack Moffitt committed
890
{
891
    char *p = str;
892
    for (; *p != '\0'; p++)
893
        *p = tolower(*p);
Jack Moffitt's avatar
Jack Moffitt committed
894

895
    return str;
Jack Moffitt's avatar
Jack Moffitt committed
896 897
}

brendan's avatar
brendan committed
898
static int _compare_vars(void *compare_arg, void *a, void *b)
Jack Moffitt's avatar
Jack Moffitt committed
899
{
900
    http_var_t *vara, *varb;
Jack Moffitt's avatar
Jack Moffitt committed
901

902 903
    vara = (http_var_t *)a;
    varb = (http_var_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
904

905
    return strcmp(vara->name, varb->name);
Jack Moffitt's avatar
Jack Moffitt committed
906 907
}

brendan's avatar
brendan committed
908
static int _free_vars(void *key)
Jack Moffitt's avatar
Jack Moffitt committed
909
{
910 911
    http_var_t *var = (http_var_t *)key;
    size_t i;
Jack Moffitt's avatar
Jack Moffitt committed
912

913
    free(var->name);
914

915 916
    for (i = 0; i < var->values; i++) {
        free(var->value[i]);
917
    }
918 919
    free(var->value);
    free(var);
Jack Moffitt's avatar
Jack Moffitt committed
920

921
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
922 923
}

924 925 926 927 928 929 930 931 932
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;
933 934 935 936 937 938 939 940
    } else if (strcasecmp("OPTIONS", method) == 0) {
        return httpp_req_options;
    } else if (strcasecmp("DELETE", method) == 0) {
        return httpp_req_delete;
    } else if (strcasecmp("TRACE", method) == 0) {
        return httpp_req_trace;
    } else if (strcasecmp("CONNECT", method) == 0) {
        return httpp_req_connect;
941 942 943 944 945 946 947 948 949 950 951
    } 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;
    }
}