blob: fca2539f4a950283d3c080cc6745453bcd1f82e5 [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
Amaury Denoyellefccffe02021-09-30 14:47:32 +020057 if (status < 100 || status > 599)
58 return 1;
59
Amaury Denoyellee0930fc2021-08-24 16:17:38 +020060 switch (status) {
61 case 103: idx = 24; break;
62 case 200: idx = 25; break;
63 case 304: idx = 26; break;
64 case 404: idx = 27; break;
65 case 503: idx = 28; break;
66 case 100: idx = 63; break;
67 case 204: idx = 64; break;
68 case 206: idx = 65; break;
69 case 302: idx = 66; break;
70 case 400: idx = 67; break;
71 case 403: idx = 68; break;
72 case 421: idx = 69; break;
73 case 425: idx = 70; break;
74 case 500: idx = 71; break;
75
Amaury Denoyelle3a590c72021-09-30 14:47:55 +020076 /* status code not in QPACK static table, idx is null. */
77 default: break;
Amaury Denoyellee0930fc2021-08-24 16:17:38 +020078 }
79
Amaury Denoyelle3a590c72021-09-30 14:47:55 +020080 if (idx) {
81 /* status code present in QPACK static table
82 * -> indexed field line
83 */
84 status_size = qpack_get_prefix_int_size(idx, 6);
85 if (b_room(out) < status_size)
86 return 1;
87
88 qpack_encode_prefix_integer(out, idx, 6, 0xc0);
89 }
90 else {
91 /* status code not present in QPACK static table
92 * -> literal field line with name reference
93 */
94 char a, b, c;
95 a = '0' + status / 100;
96 status -= (status / 100 * 100);
97 b = '0' + status / 10;
98 status -= (status / 10 * 10);
99 c = '0' + status;
100
101 /* field name */
102 if (qpack_encode_prefix_integer(out, 24, 4, 0x50))
103 return 1;
104
105 /* field value length */
106 if (qpack_encode_prefix_integer(out, 3, 7, 0x00))
107 return 1;
108
109 if (b_room(out) < 3)
110 return 1;
111
112 b_putchr(out, a);
113 b_putchr(out, b);
114 b_putchr(out, c);
115 }
Amaury Denoyellee0930fc2021-08-24 16:17:38 +0200116
Amaury Denoyellee0930fc2021-08-24 16:17:38 +0200117 return 0;
118}
119
120/* Returns 0 on success else non-zero. */
121int qpack_encode_field_section_line(struct buffer *out)
122{
Amaury Denoyellee0930fc2021-08-24 16:17:38 +0200123 char qpack_field_section[] = {
124 '\x00', /* required insert count */
125 '\x00', /* S + delta base */
126 };
Amaury Denoyelle3cae4042021-11-08 08:57:18 +0100127
128 if (b_room(out) < 2)
129 return 1;
130
Amaury Denoyellee0930fc2021-08-24 16:17:38 +0200131 b_putblk(out, qpack_field_section, 2);
132
133 return 0;
134}
135
136#define QPACK_LFL_WLN_BIT 0x20 // Literal field line with literal name
137
138/* Encode a header in literal field line with literal name.
139 * Returns 0 on success else non-zero.
140 */
141int qpack_encode_header(struct buffer *out, const struct ist n, const struct ist v)
142{
143 int i;
144 size_t sz = qpack_get_prefix_int_size(n.len, 3) + n.len +
145 qpack_get_prefix_int_size(v.len, 7) + v.len;
146
147 if (sz > b_room(out))
148 return 1;
149
150 /* literal field line with literal name
151 * | 0 | 0 | 1 | N | H | . | . | . |
152 * N :(allow an intermediary to add the header in a dynamic table)
153 * H: huffman encoded
154 * name len
155 */
156 qpack_encode_prefix_integer(out, n.len, 3, QPACK_LFL_WLN_BIT);
157 /* name */
158 for (i = 0; i < n.len; ++i)
159 b_putchr(out, n.ptr[i]);
160
161 /* | 0 | . | . | . | . | . | . | . |
162 * value len
163 */
164 qpack_encode_prefix_integer(out, v.len, 7, 0x00);
165 /* value */
166 for (i = 0; i < v.len; ++i)
167 b_putchr(out, v.ptr[i]);
168
169 return 0;
170}