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 | */ |
| 79 | int hpack_encode_header(struct chunk *out, const struct ist n, const struct ist v) |
| 80 | { |
| 81 | int len = out->len; |
| 82 | int size = out->size; |
| 83 | |
| 84 | if (len >= size) |
| 85 | return 0; |
| 86 | |
| 87 | /* check a few very common response header fields to encode them using |
| 88 | * the static header table. The tests are sorted by size to help the |
| 89 | * compiler factor out the common sizes. |
| 90 | */ |
| 91 | if (isteq(n, ist("date"))) |
| 92 | out->str[len++] = 0x61; // literal with indexing -- name="date" (idx 33) |
| 93 | else if (isteq(n, ist("etag"))) |
| 94 | out->str[len++] = 0x62; // literal with indexing -- name="etag" (idx 34) |
| 95 | else if (isteq(n, ist("server"))) |
| 96 | out->str[len++] = 0x76; // literal with indexing -- name="server" (idx 54) |
| 97 | else if (isteq(n, ist("location"))) |
| 98 | out->str[len++] = 0x6e; // literal with indexing -- name="location" (idx 46) |
| 99 | else if (isteq(n, ist("content-type"))) |
| 100 | out->str[len++] = 0x5f; // literal with indexing -- name="content-type" (idx 31) |
| 101 | else if (isteq(n, ist("last-modified"))) |
| 102 | out->str[len++] = 0x6c; // literal with indexing -- name="last-modified" (idx 44) |
| 103 | else if (isteq(n, ist("accept-ranges"))) |
| 104 | out->str[len++] = 0x51; // literal with indexing -- name="accept-ranges" (idx 17) |
| 105 | else if (isteq(n, ist("cache-control"))) |
| 106 | out->str[len++] = 0x58; // literal with indexing -- name="cache-control" (idx 24) |
| 107 | else if (isteq(n, ist("content-length"))) |
| 108 | out->str[len++] = 0x5c; // literal with indexing -- name="content-length" (idx 28) |
| 109 | else if (len_to_bytes(n.len) && len + len_to_bytes(n.len) + n.len <= size) { |
| 110 | out->str[len++] = 0x00; /* literal without indexing -- new name */ |
| 111 | |
| 112 | len = hpack_encode_len(out->str, len, n.len); |
| 113 | memcpy(out->str + len, n.ptr, n.len); |
| 114 | len += n.len; |
| 115 | } |
| 116 | else { |
| 117 | /* header field name too large for the buffer */ |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* copy literal header field value */ |
| 122 | if (!len_to_bytes(v.len) || len + len_to_bytes(v.len) + v.len > size) { |
| 123 | /* header value too large for the buffer */ |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | len = hpack_encode_len(out->str, len, v.len); |
| 128 | memcpy(out->str + len, v.ptr, v.len); |
| 129 | len += v.len; |
| 130 | |
| 131 | out->len = len; |
| 132 | return 1; |
| 133 | } |