Coverage Report

Created: 2025-08-29 06:21

/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
285
{
31
285
    h2o_iovec_t iter = h2o_iovec_init(s, len), value;
32
285
    const char *token;
33
285
    size_t token_len;
34
35
8.60k
    while ((token = h2o_next_token(&iter, ',', ',', &token_len, &value)) != NULL) {
36
8.32k
        if (token_len != 1) {
37
            /* Currently only "u=" and "i=" are supported. Thus token_len should always be 1.
38
             * Ignore unknown keys. */
39
5.75k
            continue;
40
5.75k
        }
41
42
2.57k
        if (token[0] == 'u') {
43
1.40k
            H2O_BUILD_ASSERT(H2O_ABSPRIO_NUM_URGENCY_LEVELS < 10);
44
1.40k
            if (value.base != NULL && value.len == 1 && '0' <= value.base[0] &&
45
1.40k
                value.base[0] <= '0' + H2O_ABSPRIO_NUM_URGENCY_LEVELS - 1)
46
297
                prio->urgency = value.base[0] - '0';
47
1.40k
        } else if (token[0] == 'i') {
48
586
            if (value.base != NULL) {
49
484
                if (value.len == 2 && value.base[0] == '?') {
50
                    /* value should contain '?0' or '?1' */
51
70
                    if (value.base[1] == '0')
52
19
                        prio->incremental = 0;
53
51
                    else if (value.base[1] == '1')
54
22
                        prio->incremental = 1;
55
56
                    /* All other cases mean invalid format. Just ignore. */
57
70
                }
58
484
            } else {
59
                /* value omitted, meaning that i is true */
60
102
                prio->incremental = 1;
61
102
            }
62
586
        }
63
2.57k
    }
64
285
}