Coverage Report

Created: 2025-07-18 06:42

/src/h2o/lib/common/absprio.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2019 Fastly
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
23
#include <ctype.h>
24
#include "h2o.h"
25
#include "h2o/absprio.h"
26
27
h2o_absprio_t h2o_absprio_default = {H2O_ABSPRIO_DEFAULT_URGENCY, 0};
28
29
void h2o_absprio_parse_priority(const char *s, size_t len, h2o_absprio_t *prio)
30
297
{
31
297
    h2o_iovec_t iter = h2o_iovec_init(s, len), value;
32
297
    const char *token;
33
297
    size_t token_len;
34
35
6.07k
    while ((token = h2o_next_token(&iter, ',', ',', &token_len, &value)) != NULL) {
36
5.78k
        if (token_len != 1) {
37
            /* Currently only "u=" and "i=" are supported. Thus token_len should always be 1.
38
             * Ignore unknown keys. */
39
3.31k
            continue;
40
3.31k
        }
41
42
2.47k
        if (token[0] == 'u') {
43
1.51k
            H2O_BUILD_ASSERT(H2O_ABSPRIO_NUM_URGENCY_LEVELS < 10);
44
1.51k
            if (value.base != NULL && value.len == 1 && '0' <= value.base[0] &&
45
1.51k
                value.base[0] <= '0' + H2O_ABSPRIO_NUM_URGENCY_LEVELS - 1)
46
426
                prio->urgency = value.base[0] - '0';
47
1.51k
        } else if (token[0] == 'i') {
48
384
            if (value.base != NULL) {
49
271
                if (value.len == 2 && value.base[0] == '?') {
50
                    /* value should contain '?0' or '?1' */
51
81
                    if (value.base[1] == '0')
52
19
                        prio->incremental = 0;
53
62
                    else if (value.base[1] == '1')
54
21
                        prio->incremental = 1;
55
56
                    /* All other cases mean invalid format. Just ignore. */
57
81
                }
58
271
            } else {
59
                /* value omitted, meaning that i is true */
60
113
                prio->incremental = 1;
61
113
            }
62
384
        }
63
2.47k
    }
64
297
}