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