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 #ifndef h2o__absprio_h
23 #define h2o__absprio_h
24 
25 #include <assert.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 
29 #define H2O_ABSPRIO_DEFAULT_URGENCY 3
30 #define H2O_ABSPRIO_NUM_URGENCY_LEVELS 8
31 
32 typedef struct h2o_absprio_t {
33     uint8_t urgency : 3;
34     uint8_t incremental : 1;
35 } h2o_absprio_t;
36 
37 extern h2o_absprio_t h2o_absprio_default;
38 
39 void h2o_absprio_parse_priority(const char *s, size_t len, h2o_absprio_t *prio);
40 /**
41  * Convert urgency value in absolute priority header to HTTP2 weight, having Chromium as a client in mind.
42  */
43 static uint16_t h2o_absprio_urgency_to_chromium_weight(uint8_t urgency);
44 
45 /* inline functions */
46 
h2o_absprio_urgency_to_chromium_weight(uint8_t urgency)47 inline uint16_t h2o_absprio_urgency_to_chromium_weight(uint8_t urgency)
48 {
49     uint16_t weight;
50     assert(urgency < H2O_ABSPRIO_NUM_URGENCY_LEVELS);
51     /* formula excerpted from:
52      * https://quiche.googlesource.com/quiche/+/8cbe7bfa5c6efa7a42652e36fabf8d21879894be/spdy/core/spdy_protocol.cc#50 */
53     const float ksteps = 255.9f / (float)(H2O_ABSPRIO_NUM_URGENCY_LEVELS - 1);
54     weight = (uint16_t)(ksteps * ((H2O_ABSPRIO_NUM_URGENCY_LEVELS - 1) - urgency)) + 1;
55     return weight;
56 }
57 
58 #endif
59