blob: c4c8ea2408825016f8057d9c141ce6818c2594b5 [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{
46 if (len < 127)
47 return 1;
48 if (len < 127 + (1 << 7))
49 return 2;
50 if (len < 127 + (1 << 14))
51 return 3;
52 if (len < 127 + (1 << 21))
53 return 4;
54 return 0;
55}
56
57/* Encode <len> into <out>+<pos> and return the new position. The caller is
58 * responsible for checking for available room using len_to_bytes() first.
59 */
60static inline int hpack_encode_len(char *out, int pos, int len)
61{
62 int code = len - 127;
63
64 if (code < 0) {
65 out[pos++] = len;
66 } else {
67 out[pos++] = 127;
68 for (; code >= 128; code >>= 7)
69 out[pos++] = code | 128;
70 out[pos++] = code;
71 }
72 return pos;
73}
74
75
76/* Tries to encode header whose name is <n> and value <v> into the chunk <out>.
77 * Returns non-zero on success, 0 on failure (buffer full).
78 */
Willy Tarreau83061a82018-07-13 11:56:34 +020079int hpack_encode_header(struct buffer *out, const struct ist n,
80 const struct ist v)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020081{
Willy Tarreau843b7cb2018-07-13 10:54:26 +020082 int len = out->data;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020083 int size = out->size;
84
85 if (len >= size)
86 return 0;
87
88 /* check a few very common response header fields to encode them using
89 * the static header table. The tests are sorted by size to help the
90 * compiler factor out the common sizes.
91 */
92 if (isteq(n, ist("date")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +020093 out->area[len++] = 0x61; // literal with indexing -- name="date" (idx 33)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020094 else if (isteq(n, ist("etag")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +020095 out->area[len++] = 0x62; // literal with indexing -- name="etag" (idx 34)
Willy Tarreaua40782b2018-12-02 12:43:18 +010096 else if (isteq(n, ist(":path")))
97 out->area[len++] = 0x44; // literal with indexing -- name=":path" (idx 4)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +020098 else if (isteq(n, ist("server")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +020099 out->area[len++] = 0x76; // literal with indexing -- name="server" (idx 54)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200100 else if (isteq(n, ist("location")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200101 out->area[len++] = 0x6e; // literal with indexing -- name="location" (idx 46)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200102 else if (isteq(n, ist("content-type")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200103 out->area[len++] = 0x5f; // literal with indexing -- name="content-type" (idx 31)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200104 else if (isteq(n, ist("last-modified")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200105 out->area[len++] = 0x6c; // literal with indexing -- name="last-modified" (idx 44)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200106 else if (isteq(n, ist("accept-ranges")))
Willy Tarreau4bf194c2018-11-20 04:47:38 +0100107 out->area[len++] = 0x52; // literal with indexing -- name="accept-ranges" (idx 18)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200108 else if (isteq(n, ist("cache-control")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200109 out->area[len++] = 0x58; // literal with indexing -- name="cache-control" (idx 24)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200110 else if (isteq(n, ist("content-length")))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200111 out->area[len++] = 0x5c; // literal with indexing -- name="content-length" (idx 28)
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200112 else if (len_to_bytes(n.len) && len + len_to_bytes(n.len) + n.len <= size) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200113 out->area[len++] = 0x00; /* literal without indexing -- new name */
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200114
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200115 len = hpack_encode_len(out->area, len, n.len);
116 memcpy(out->area + len, n.ptr, n.len);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200117 len += n.len;
118 }
119 else {
120 /* header field name too large for the buffer */
121 return 0;
122 }
123
124 /* copy literal header field value */
125 if (!len_to_bytes(v.len) || len + len_to_bytes(v.len) + v.len > size) {
126 /* header value too large for the buffer */
127 return 0;
128 }
129
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200130 len = hpack_encode_len(out->area, len, v.len);
131 memcpy(out->area + len, v.ptr, v.len);
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200132 len += v.len;
133
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200134 out->data = len;
Willy Tarreau1be4f3d2017-09-21 14:35:57 +0200135 return 1;
136}