blob: f53de7cb50ae779cf43e5277ecc719a86f765962 [file] [log] [blame]
Amaury Denoyelle4652a592021-08-24 15:50:32 +02001#include <haproxy/qpack-enc.h>
Amaury Denoyellee0930fc2021-08-24 16:17:38 +02002
3#include <haproxy/buf.h>
4#include <haproxy/intops.h>
5
6/* Returns the byte size required to encode <i> as a <prefix_size>-prefix
7 * integer.
8 */
9static size_t qpack_get_prefix_int_size(int i, int prefix_size)
10{
11 int n = (1 << prefix_size) - 1;
12 if (i < n) {
13 return 1;
14 }
15 else {
16 size_t result = 0;
17 while (i) {
18 ++result;
19 i >>= 7;
20 }
21 return 1 + result;
22 }
23}
24
25/* Encode the integer <i> in the buffer <out> in a <prefix_size>-bit prefix
26 * integer. The caller must ensure there is enough size in the buffer. The
27 * prefix is OR-ed with <before_prefix> byte.
28 *
29 * Returns 0 if success else non-zero.
30 */
31static int qpack_encode_prefix_integer(struct buffer *out, int i, int prefix_size, unsigned char before_prefix)
32{
33 BUG_ON(!prefix_size);
34
35 if (i < (1 << prefix_size) - 1) {
36 if (b_data(out) < 1)
37 return 1;
38
39 b_putchr(out, before_prefix | i);
40 }
41 else {
42 if (b_data(out) < 2)
43 return 1;
44
45 b_putchr(out, before_prefix | ((1 << prefix_size) - 1));
46 b_putchr(out, i - ((1 << prefix_size) - 1));
47 }
48
49 return 0;
50}
51
52/* Returns 0 on success else non-zero. */
53int qpack_encode_int_status(struct buffer *out, unsigned int status)
54{
55 int status_size, idx = 0;
56
57 switch (status) {
58 case 103: idx = 24; break;
59 case 200: idx = 25; break;
60 case 304: idx = 26; break;
61 case 404: idx = 27; break;
62 case 503: idx = 28; break;
63 case 100: idx = 63; break;
64 case 204: idx = 64; break;
65 case 206: idx = 65; break;
66 case 302: idx = 66; break;
67 case 400: idx = 67; break;
68 case 403: idx = 68; break;
69 case 421: idx = 69; break;
70 case 425: idx = 70; break;
71 case 500: idx = 71; break;
72
73 default:
74 BUG_ON(1);
75 break;
76 }
77
78 status_size = qpack_get_prefix_int_size(idx, 6);
79 if (b_room(out) < status_size)
80 return 1;
81
82 //b_quic_enc_int(out, idx);
83 qpack_encode_prefix_integer(out, idx, 6, 0xc0);
84 return 0;
85}
86
87/* Returns 0 on success else non-zero. */
88int qpack_encode_field_section_line(struct buffer *out)
89{
90 if (b_room(out) < 2)
91 return 1;
92
93 char qpack_field_section[] = {
94 '\x00', /* required insert count */
95 '\x00', /* S + delta base */
96 };
97 b_putblk(out, qpack_field_section, 2);
98
99 return 0;
100}
101
102#define QPACK_LFL_WLN_BIT 0x20 // Literal field line with literal name
103
104/* Encode a header in literal field line with literal name.
105 * Returns 0 on success else non-zero.
106 */
107int qpack_encode_header(struct buffer *out, const struct ist n, const struct ist v)
108{
109 int i;
110 size_t sz = qpack_get_prefix_int_size(n.len, 3) + n.len +
111 qpack_get_prefix_int_size(v.len, 7) + v.len;
112
113 if (sz > b_room(out))
114 return 1;
115
116 /* literal field line with literal name
117 * | 0 | 0 | 1 | N | H | . | . | . |
118 * N :(allow an intermediary to add the header in a dynamic table)
119 * H: huffman encoded
120 * name len
121 */
122 qpack_encode_prefix_integer(out, n.len, 3, QPACK_LFL_WLN_BIT);
123 /* name */
124 for (i = 0; i < n.len; ++i)
125 b_putchr(out, n.ptr[i]);
126
127 /* | 0 | . | . | . | . | . | . | . |
128 * value len
129 */
130 qpack_encode_prefix_integer(out, v.len, 7, 0x00);
131 /* value */
132 for (i = 0; i < v.len; ++i)
133 b_putchr(out, v.ptr[i]);
134
135 return 0;
136}