File: header_parser.c

package info (click to toggle)
freshplayerplugin 0.3.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 5,324 kB
  • ctags: 13,257
  • sloc: ansic: 45,542; cpp: 26,176; yacc: 1,707; lex: 910; python: 176; sh: 87; makefile: 21
file content (134 lines) | stat: -rw-r--r-- 3,860 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
54
55
56
57
58
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
/*
 * Copyright © 2013-2015  Rinat Ibragimov
 *
 * This file is part of FreshPlayerPlugin.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "header_parser.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>


struct parsed_headers_s *
hp_parse_headers(const char *headers)
{
    struct parsed_headers_s *ph = calloc(1, sizeof(*ph));
    const char *delimiter = "\r\n";
    char *headers_copy, *saveptr, *part;
    unsigned int k;

    if (headers == NULL) {
        // all fields zeroed
        return ph;
    }

    // count headers, determine http code
    headers_copy = strdup(headers);
    ph->cnt = 0;
    ph->http_code = 200;
    part = strtok_r(headers_copy, delimiter, &saveptr);
    if (part) {
        int major = 0, minor = 0, code = 0, ret;
        ret = sscanf(part, "HTTP/%6d.%6d %6d", &major, &minor, &code);
        if (ret >= 3) {
            ph->http_code = code;
        }
        part = strtok_r(NULL, delimiter, &saveptr);
    }
    while (part) {
        ph->cnt ++;
        part = strtok_r(NULL, delimiter, &saveptr);
    }
    free(headers_copy);

    // parse headers to name and value
    headers_copy = strdup(headers);
    if (ph->cnt > 0) {
        ph->name = malloc(sizeof(char *) * ph->cnt);
        ph->value = malloc(sizeof(char *) * ph->cnt);
    }
    part = strtok_r(headers_copy, delimiter, &saveptr);

    // save status line
    if (part) {
        ph->status_line = strdup(part);
        part = strtok_r(NULL, delimiter, &saveptr);
    }

    // save headers
    k = 0;
    while (part && k < ph->cnt) {
        char *colon = strchr(part, ':');
        if (colon) {
            *colon = 0;
            ph->name[k] = strdup(part);
            char *value_start = colon + 1;
            while (isspace(*value_start))
                value_start ++;
            ph->value[k] = strdup(value_start);
            *colon = ':';
        } else {
            ph->name[k] = strdup(part);
            ph->value[k] = strdup("");
        }

        part = strtok_r(NULL, delimiter, &saveptr);
        k ++;
    }
    free(headers_copy);

    return ph;
}

void
hp_free_parsed_headers(struct parsed_headers_s *ph)
{
    for (unsigned int k = 0; k < ph->cnt; k ++) {
        free(ph->name[k]);
        free(ph->value[k]);
    }
    free(ph->name);
    free(ph->value);
    free(ph->status_line);
    free(ph);
}

const char *
hp_get_header_value(struct parsed_headers_s *ph, const char *name)
{
    for (unsigned int k = 0; k < ph->cnt; k ++) {
        if (strcasecmp(ph->name[k], name) == 0)
            return ph->value[k];
    }
    return NULL;
}

int
hp_header_exists(struct parsed_headers_s *ph, const char *name)
{
    for (unsigned int k = 0; k < ph->cnt; k ++) {
        if (strcasecmp(ph->name[k], name) == 0)
            return 1;
    }
    return 0;
}