/src/h2o/lib/http3/frame.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2019 Fastly, Kazuho Oku |
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 | | #include "h2o/http3_common.h" |
23 | | |
24 | | size_t h2o_http3_priority_update_frame_capacity(h2o_http3_priority_update_frame_t *frame) |
25 | 0 | { |
26 | 0 | return 4 /* type */ + 8 /* frame length */ + 8 /* element */ + frame->value.len; |
27 | 0 | } |
28 | | |
29 | | uint8_t *h2o_http3_encode_priority_update_frame(uint8_t *dst, const h2o_http3_priority_update_frame_t *frame) |
30 | 0 | { |
31 | 0 | dst = quicly_encodev(dst, frame->element_is_push ? H2O_HTTP3_FRAME_TYPE_PRIORITY_UPDATE_PUSH |
32 | 0 | : H2O_HTTP3_FRAME_TYPE_PRIORITY_UPDATE_REQUEST); |
33 | 0 | dst = quicly_encodev(dst, quicly_encodev_capacity(frame->element) + frame->value.len); |
34 | 0 | dst = quicly_encodev(dst, frame->element); |
35 | 0 | memcpy(dst, frame->value.base, frame->value.len); |
36 | |
|
37 | 0 | return dst; |
38 | 0 | } |
39 | | |
40 | | int h2o_http3_decode_priority_update_frame(h2o_http3_priority_update_frame_t *frame, int is_push, const uint8_t *payload, |
41 | | size_t len, const char **err_desc) |
42 | 0 | { |
43 | 0 | const uint8_t *src = payload, *end = src + len; |
44 | |
|
45 | 0 | frame->element_is_push = is_push; |
46 | |
|
47 | 0 | if (src == end) |
48 | 0 | return H2O_HTTP3_ERROR_FRAME; |
49 | 0 | if ((frame->element = quicly_decodev(&src, end)) == UINT64_MAX) { |
50 | 0 | *err_desc = "invalid PRIORITY frame"; |
51 | 0 | return H2O_HTTP3_ERROR_FRAME; |
52 | 0 | } |
53 | 0 | if (frame->element_is_push) { |
54 | 0 | if (!(!quicly_stream_is_client_initiated(frame->element) && quicly_stream_is_unidirectional(frame->element))) |
55 | 0 | return H2O_HTTP3_ERROR_FRAME; |
56 | 0 | } else { |
57 | 0 | if (!(quicly_stream_is_client_initiated(frame->element) && !quicly_stream_is_unidirectional(frame->element))) |
58 | 0 | return H2O_HTTP3_ERROR_FRAME; |
59 | 0 | } |
60 | 0 | frame->value = h2o_iovec_init(src, end - src); |
61 | |
|
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | | size_t h2o_http3_goaway_frame_capacity(quicly_stream_id_t stream_or_push_id) |
66 | 0 | { |
67 | 0 | return 1 /* type */ |
68 | 0 | + 1 /* length field. length should be less than 64, so 1 byte should be enough to represent it */ |
69 | 0 | + quicly_encodev_capacity(stream_or_push_id); |
70 | 0 | } |
71 | | |
72 | | uint8_t *h2o_http3_encode_goaway_frame(uint8_t *dst, quicly_stream_id_t stream_or_push_id) |
73 | 0 | { |
74 | 0 | *dst++ = H2O_HTTP3_FRAME_TYPE_GOAWAY; /* type */ |
75 | 0 | *dst++ = quicly_encodev_capacity(stream_or_push_id); /* payload length */ |
76 | 0 | dst = quicly_encodev(dst, stream_or_push_id); |
77 | |
|
78 | 0 | return dst; |
79 | 0 | } |
80 | | |
81 | | int h2o_http3_decode_goaway_frame(h2o_http3_goaway_frame_t *frame, const uint8_t *payload, size_t len, const char **err_desc) |
82 | 0 | { |
83 | 0 | const uint8_t *src = payload, *end = src + len; |
84 | |
|
85 | 0 | if ((frame->stream_or_push_id = quicly_decodev(&src, end)) == UINT64_MAX) |
86 | 0 | goto Fail; |
87 | | |
88 | 0 | if (src != end) { |
89 | | /* there was an extra byte(s) after a valid QUIC variable-length integer */ |
90 | 0 | goto Fail; |
91 | 0 | } |
92 | | |
93 | 0 | return 0; |
94 | | |
95 | 0 | Fail: |
96 | 0 | *err_desc = "Invalid GOAWAY frame"; |
97 | 0 | return H2O_HTTP3_ERROR_FRAME; |
98 | 0 | } |