summaryrefslogtreecommitdiffstats
path: root/include/haproxy/hpack-enc.h
blob: 7511c5da089fb8479e400b282da0ebdca5b07983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * HPACK compressor (RFC7541)
 *
 * Copyright (C) 2014-2020 Willy Tarreau <willy@haproxy.org>
 * Copyright (C) 2017 HAProxy Technologies
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef _COMMON_HPACK_ENC_H
#define _COMMON_HPACK_ENC_H

#include <string.h>
#include <import/ist.h>
#include <haproxy/api.h>
#include <haproxy/buf-t.h>
#include <haproxy/http-t.h>

int hpack_encode_header(struct buffer *out, const struct ist n,
			const struct ist v);

/* Returns the number of bytes required to encode the string length <len>. The
 * number of usable bits is an integral multiple of 7 plus 6 for the last byte.
 * The maximum number of bytes returned is 4 (2097279 max length). Larger values
 * return 0.
 */
static inline int hpack_len_to_bytes(size_t len)
{
	ssize_t slen = len;

	slen -= 127;
	if (__builtin_expect(slen < 0, 1))
		return 1;
	if (slen < (1 << 14)) {
		if (__builtin_expect(slen < (1 << 7), 1))
			return 2;
		else
			return 3;
	}
	if (slen < (1 << 21))
		return 4;
	return 0;
}

/* Encodes <len> into <out>+<pos> and return the new position. The caller is
 * responsible for checking for available room using hpack_len_to_bytes()
 * first.
 */
static inline int hpack_encode_len(char *out, int pos, int len)
{
	int code = len - 127;

	if (code < 0) {
		out[pos++] = len;
	} else {
		out[pos++] = 127;
		for (; code >= 128; code >>= 7)
			out[pos++] = code | 128;
		out[pos++] = code;
	}
	return pos;
}

/* Tries to encode header field index <idx> with short value <val> into the
 * aligned buffer <out>. Returns non-zero on success, 0 on failure (buffer
 * full). The caller is responsible for ensuring that the length of <val> is
 * strictly lower than 127, and that <idx> is lower than 64 (static list only),
 * and that the buffer is aligned (head==0).
 */
static inline int hpack_encode_short_idx(struct buffer *out, int idx, struct ist val)
{
	if (out->data + 2 + val.len > out->size)
		return 0;

	/* literal header field with incremental indexing */
	out->area[out->data++] = idx | 0x40;
	out->area[out->data++] = val.len;
	ist2bin(&out->area[out->data], val);
	out->data += val.len;
	return 1;
}

/* Tries to encode header field index <idx> with long value <val> into the
 * aligned buffer <out>. Returns non-zero on success, 0 on failure (buffer
 * full). The caller is responsible for ensuring <idx> is lower than 64 (static
 * list only), and that the buffer is aligned (head==0).
 */
static inline int hpack_encode_long_idx(struct buffer *out, int idx, struct ist val)
{
	int len = out->data;

	if (!hpack_len_to_bytes(val.len) ||
	    1 + len + hpack_len_to_bytes(val.len) + val.len > out->size)
		return 0;

	/* emit literal with indexing (7541#6.2.1) :
	 * [ 0 | 1 | Index (6+) ]
	 */
	out->area[len++] = idx | 0x40;
	len = hpack_encode_len(out->area, len, val.len);
	memcpy(out->area + len, val.ptr, val.len);
	len += val.len;

	out->data = len;
	return 1;
}

/* Tries to encode a :status pseudo-header with the integer status <status>
 * into the aligned buffer <out>. Returns non-zero on success, 0 on failure
 * (buffer full). The caller is responsible for ensuring that the status is
 * comprised between 100 and 999 inclusive and that the buffer is aligned. It's
 * inlined because it's easily optimizable by the compiler.
 */
static inline int hpack_encode_int_status(struct buffer *out, unsigned int status)
{
	int len = out->data;
	int size = out->size;
	unsigned char c = 0;

	/* try to emit a single byte code */
	len++;
	if (__builtin_expect(len > size, 0))
		goto fail;

	c = (status <= 304) ?
		(status <= 204) ?
			(status == 204) ? 0x89 :
			(status == 200) ? 0x88 :
		0: /* > 204 */
			(status == 304) ? 0x8b :
			(status == 206) ? 0x8a :
		0:
		(status <= 404) ?
			(status == 404) ? 0x8d :
			(status == 400) ? 0x8c :
		0: /* > 404 */
			(status == 500) ? 0x8e :
		0;

	if (c)
		goto last;

	/* fall back to literal */
	len += 4;
	if (__builtin_expect(len > size, 0))
		goto fail;

	/* basic encoding of the status code */
	out->area[len - 5] = 0x48; // indexed name -- name=":status" (idx 8)
	out->area[len - 4] = 0x03; // 3 bytes status
	out->area[len - 3] = '0' + status / 100;
	out->area[len - 2] = '0' + status / 10 % 10;
	c = '0' + status % 10;
 last:
	out->area[len - 1] = c;
	out->data = len;
	return 1;
 fail:
	return 0;
}

/* Tries to encode a :status pseudo-header with the integer status <status>
 * also represented by <str> into the aligned buffer <out>. Returns non-zero
 * on success or 0 on failure (buffer full). The caller is responsible for
 * ensuring that the status is comprised between 100 and 999 inclusive, that
 * <str> contains a valid representation of the numerical value, and that the
 * buffer is aligned. This version is preferred when the caller already knows
 * a string representation of the status because it avoids the computation in
 * the uncompressed case. It's inlined because it's easily optimizable.
 */
static inline int hpack_encode_str_status(struct buffer *out, unsigned int status, struct ist str)
{
	/* don't try too hard, we already have the ASCII value for less common cases */
	if (status == 200 || status == 304) {
		if (out->data >= out->size)
			return 0;
		out->area[out->data] = (status == 304) ? 0x8b : 0x88;
		out->data++;
		return 1;
	}
	return hpack_encode_short_idx(out, 8, str); // name=":status" (idx 8)
}

/* Tries to encode a :method pseudo-header with the method in <meth>, which
 * also exists as a string in <str>, into the aligned buffer <out>. Returns
 * non-zero on success or 0 on failure (buffer full). The caller is responsible
 * for ensuring that the string matches <meth>, that it's smaller than 127
 * bytes, and that the buffer is aligned. If <meth> is unknown then using
 * HTTP_METH_OTHER will lead to the string being encoded as a literal. It's
 * inlined because it's easily optimizable.
 */
static inline int hpack_encode_method(struct buffer *out, enum http_meth_t meth, struct ist str)
{
	if (out->data < out->size && meth == HTTP_METH_GET)
		out->area[out->data++] = 0x82; // indexed field : idx[02]=(":method", "GET")
	else if (out->data < out->size && meth == HTTP_METH_POST)
		out->area[out->data++] = 0x83; // indexed field : idx[03]=(":method", "POST")
	else
		return hpack_encode_short_idx(out, 2, str); // name=":method" (idx 2)
	return 1;
}

/* Tries to encode a :scheme pseudo-header with the scheme in <scheme>, into
 * the aligned buffer <out>. Returns non-zero on success or 0 on failure
 * (buffer full). Only "http" and "https" are recognized and handled as indexed
 * values, others are turned into short literals. The caller is responsible for
 * ensuring that the scheme is smaller than 127 bytes, and that the buffer is
 * aligned. Normally the compiler will detect constant strings in the comparison
 * if the code remains inlined.
 */
static inline int hpack_encode_scheme(struct buffer *out, struct ist scheme)
{
	if (out->data < out->size && isteq(scheme, ist("https")))
		out->area[out->data++] = 0x87; // indexed field : idx[07]=(":scheme", "https")
	else if (out->data < out->size && isteq(scheme, ist("http")))
		out->area[out->data++] = 0x86; // indexed field : idx[06]=(":scheme", "http")
	else
		return hpack_encode_short_idx(out, 6, scheme); // name=":scheme" (idx 6)
	return 1;
}

/* Tries to encode a :path pseudo-header with the path in <path>, into the
 * aligned buffer <out>. Returns non-zero on success or 0 on failure (buffer
 * full). The well-known values "/" and "/index.html" are recognized, and other
 * ones are handled as literals. The caller is responsible for ensuring that
 * the buffer is aligned.  Normally the compiler will detect constant strings
 * in the comparison if the code remains inlined.
 */
static inline int hpack_encode_path(struct buffer *out, struct ist path)
{
	if (out->data < out->size && isteq(path, ist("/")))
		out->area[out->data++] = 0x84; // indexed field : idx[04]=(":path", "/")
	else if (out->data < out->size && isteq(path, ist("/index.html")))
		out->area[out->data++] = 0x85; // indexed field : idx[05]=(":path", "/index.html")
	else if (path.len < 127)
		return hpack_encode_short_idx(out, 4, path); // name=":path" (idx 4)
	else
		return hpack_encode_long_idx(out, 4, path); // name=":path" (idx 4)
	return 1;
}


#endif /* _COMMON_HPACK_ENC_H */