blob: 73a5fd16718f76db5a0891055ed95dcaa9cca122 [file] [log] [blame]
Willy Tarreau1be4f3d2017-09-21 14:35:57 +02001/*
2 * HPACK decompressor (RFC7541)
3 *
4 * Copyright (C) 2014-2017 Willy Tarreau <willy@haproxy.org>
5 * Copyright (C) 2017 HAProxy Technologies
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33#include <common/hpack-enc.h>
34#include <common/http-hdr.h>
35#include <common/ist.h>
36
37#include <types/global.h>
38
39/* returns the number of bytes required to encode the string length <len>. The
40 * number of usable bits is an integral multiple of 7 plus 6 for the last byte.
41 * The maximum number of bytes returned is 4 (2097279 max length). Larger values
42 * return 0.
43 */
44static inline int len_to_bytes(size_t len)
45{
Willy Tarreau1526f192018-12-10 13:36:56 +010046 ssize_t slen = len;
47
48 slen -= 127;
49 if (__builtin_expect(slen < 0, 1))
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020050 return 1;
Willy Tarreau1526f192018-12-10 13:36:56 +010051 if (slen < (1 << 14)) {
52 if (__builtin_expect(slen < (1 << 7), 1))
53 return 2;
54 else
55 return 3;
56 }
57 if (slen < (1 << 21))
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020058 return 4;
59 return 0;
60}
61
62/* Encode <len> into <out>+<pos> and return the new position. The caller is
63 * responsible for checking for available room using len_to_bytes() first.
64 */
65static inline int hpack_encode_len(char *out, int pos, int len)
66{
67 int code = len - 127;
68
69 if (code < 0) {
70 out[pos++] = len;
71 } else {
72 out[pos++] = 127;
73 for (; code >= 128; code >>= 7)
74 out[pos++] = code | 128;
75 out[pos++] = code;
76 }
77 return pos;
78}
79
80
81/* Tries to encode header whose name is <n> and value <v> into the chunk <out>.
82 * Returns non-zero on success, 0 on failure (buffer full).
83 */
Willy Tarreau83061a82018-07-13 11:56:34 +020084int hpack_encode_header(struct buffer *out, const struct ist n,
85 const struct ist v)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020086{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020087 int len = out->data;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020088 int size = out->size;
89
90 if (len >= size)
91 return 0;
92
93 /* check a few very common response header fields to encode them using
94 * the static header table. The tests are sorted by size to help the
95 * compiler factor out the common sizes.
96 */
97 if (isteq(n, ist("date")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +020098 out->area[len++] = 0x61; // literal with indexing -- name="date" (idx 33)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020099 else if (isteq(n, ist("etag")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200100 out->area[len++] = 0x62; // literal with indexing -- name="etag" (idx 34)
Willy Tarreaua40782b2018-12-02 12:43:18 +0100101 else if (isteq(n, ist(":path")))
102 out->area[len++] = 0x44; // literal with indexing -- name=":path" (idx 4)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200103 else if (isteq(n, ist("server")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200104 out->area[len++] = 0x76; // literal with indexing -- name="server" (idx 54)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200105 else if (isteq(n, ist("location")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200106 out->area[len++] = 0x6e; // literal with indexing -- name="location" (idx 46)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200107 else if (isteq(n, ist("content-type")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200108 out->area[len++] = 0x5f; // literal with indexing -- name="content-type" (idx 31)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200109 else if (isteq(n, ist("last-modified")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200110 out->area[len++] = 0x6c; // literal with indexing -- name="last-modified" (idx 44)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200111 else if (isteq(n, ist("accept-ranges")))
Willy Tarreau4bf194c2018-11-20 04:47:38 +0100112 out->area[len++] = 0x52; // literal with indexing -- name="accept-ranges" (idx 18)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200113 else if (isteq(n, ist("cache-control")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200114 out->area[len++] = 0x58; // literal with indexing -- name="cache-control" (idx 24)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200115 else if (isteq(n, ist("content-length")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200116 out->area[len++] = 0x5c; // literal with indexing -- name="content-length" (idx 28)
Willy Tarreau75710152018-12-11 06:46:03 +0100117 else if (len_to_bytes(n.len) && len + 1 + len_to_bytes(n.len) + n.len <= size) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200118 out->area[len++] = 0x00; /* literal without indexing -- new name */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200119 len = hpack_encode_len(out->area, len, n.len);
Willy Tarreauac73ae02018-12-11 06:27:06 +0100120 ist2bin(out->area + len, n);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200121 len += n.len;
122 }
123 else {
124 /* header field name too large for the buffer */
125 return 0;
126 }
127
128 /* copy literal header field value */
129 if (!len_to_bytes(v.len) || len + len_to_bytes(v.len) + v.len > size) {
130 /* header value too large for the buffer */
131 return 0;
132 }
133
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200134 len = hpack_encode_len(out->area, len, v.len);
135 memcpy(out->area + len, v.ptr, v.len);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200136 len += v.len;
137
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200138 out->data = len;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200139 return 1;
140}