Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 44 | static 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 | */ |
| 60 | static 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 Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 79 | int hpack_encode_header(struct buffer *out, const struct ist n, |
| 80 | const struct ist v) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 81 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 82 | int len = out->data; |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 83 | 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 Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 93 | out->area[len++] = 0x61; // literal with indexing -- name="date" (idx 33) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 94 | else if (isteq(n, ist("etag"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 95 | out->area[len++] = 0x62; // literal with indexing -- name="etag" (idx 34) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 96 | else if (isteq(n, ist("server"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 97 | out->area[len++] = 0x76; // literal with indexing -- name="server" (idx 54) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 98 | else if (isteq(n, ist("location"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 99 | out->area[len++] = 0x6e; // literal with indexing -- name="location" (idx 46) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 100 | else if (isteq(n, ist("content-type"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 101 | out->area[len++] = 0x5f; // literal with indexing -- name="content-type" (idx 31) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 102 | else if (isteq(n, ist("last-modified"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 103 | out->area[len++] = 0x6c; // literal with indexing -- name="last-modified" (idx 44) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 104 | else if (isteq(n, ist("accept-ranges"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 105 | out->area[len++] = 0x51; // literal with indexing -- name="accept-ranges" (idx 17) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 106 | else if (isteq(n, ist("cache-control"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 107 | out->area[len++] = 0x58; // literal with indexing -- name="cache-control" (idx 24) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 108 | else if (isteq(n, ist("content-length"))) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 109 | out->area[len++] = 0x5c; // literal with indexing -- name="content-length" (idx 28) |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 110 | else if (len_to_bytes(n.len) && len + len_to_bytes(n.len) + n.len <= size) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 111 | out->area[len++] = 0x00; /* literal without indexing -- new name */ |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 112 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 113 | len = hpack_encode_len(out->area, len, n.len); |
| 114 | memcpy(out->area + len, n.ptr, n.len); |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 115 | len += n.len; |
| 116 | } |
| 117 | else { |
| 118 | /* header field name too large for the buffer */ |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* copy literal header field value */ |
| 123 | if (!len_to_bytes(v.len) || len + len_to_bytes(v.len) + v.len > size) { |
| 124 | /* header value too large for the buffer */ |
| 125 | return 0; |
| 126 | } |
| 127 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 128 | len = hpack_encode_len(out->area, len, v.len); |
| 129 | memcpy(out->area + len, v.ptr, v.len); |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 130 | len += v.len; |
| 131 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 132 | out->data = len; |
Willy Tarreau | 1be4f3d | 2017-09-21 14:35:57 +0200 | [diff] [blame] | 133 | return 1; |
| 134 | } |