3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/transport/chttp2/transport/hpack_table.h"
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/string_util.h>
30 #include "src/core/lib/debug/trace.h"
31 #include "src/core/lib/gpr/murmur_hash.h"
32 #include "src/core/lib/surface/validate_metadata.h"
33 #include "src/core/lib/transport/static_metadata.h"
35 extern grpc_core::TraceFlag grpc_http_trace;
52 {":path", "/index.html"},
72 {"accept-charset", ""},
74 {"accept-encoding", "gzip, deflate"},
76 {"accept-language", ""},
78 {"accept-ranges", ""},
82 {"access-control-allow-origin", ""},
88 {"authorization", ""},
90 {"cache-control", ""},
92 {"content-disposition", ""},
94 {"content-encoding", ""},
96 {"content-language", ""},
98 {"content-length", ""},
100 {"content-location", ""},
102 {"content-range", ""},
104 {"content-type", ""},
122 {"if-modified-since", ""},
124 {"if-none-match", ""},
128 {"if-unmodified-since", ""},
130 {"last-modified", ""},
136 {"max-forwards", ""},
138 {"proxy-authenticate", ""},
140 {"proxy-authorization", ""},
154 {"strict-transport-security", ""},
156 {"transfer-encoding", ""},
164 {"www-authenticate", ""},
167 static uint32_t entries_for_bytes(uint32_t bytes) {
168 return (bytes + GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD - 1) /
169 GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
172 void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl* tbl) {
175 memset(tbl, 0, sizeof(*tbl));
176 tbl->current_table_bytes = tbl->max_bytes =
177 GRPC_CHTTP2_INITIAL_HPACK_TABLE_SIZE;
178 tbl->max_entries = tbl->cap_entries =
179 entries_for_bytes(tbl->current_table_bytes);
180 tbl->ents = static_cast<grpc_mdelem*>(
181 gpr_malloc(sizeof(*tbl->ents) * tbl->cap_entries));
182 memset(tbl->ents, 0, sizeof(*tbl->ents) * tbl->cap_entries);
183 for (i = 1; i <= GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
184 tbl->static_ents[i - 1] = grpc_mdelem_from_slices(
185 grpc_slice_intern(grpc_slice_from_static_string(static_table[i].key)),
187 grpc_slice_from_static_string(static_table[i].value)));
191 void grpc_chttp2_hptbl_destroy(grpc_chttp2_hptbl* tbl) {
193 for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
194 GRPC_MDELEM_UNREF(tbl->static_ents[i]);
196 for (i = 0; i < tbl->num_ents; i++) {
197 GRPC_MDELEM_UNREF(tbl->ents[(tbl->first_ent + i) % tbl->cap_entries]);
202 grpc_mdelem grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl* tbl,
203 uint32_t tbl_index) {
204 /* Static table comes first, just return an entry from it */
205 if (tbl_index <= GRPC_CHTTP2_LAST_STATIC_ENTRY) {
206 return tbl->static_ents[tbl_index - 1];
208 /* Otherwise, find the value in the list of valid entries */
209 tbl_index -= (GRPC_CHTTP2_LAST_STATIC_ENTRY + 1);
210 if (tbl_index < tbl->num_ents) {
212 (tbl->num_ents - 1u - tbl_index + tbl->first_ent) % tbl->cap_entries;
213 return tbl->ents[offset];
215 /* Invalid entry: return error */
219 /* Evict one element from the table */
220 static void evict1(grpc_chttp2_hptbl* tbl) {
221 grpc_mdelem first_ent = tbl->ents[tbl->first_ent];
222 size_t elem_bytes = GRPC_SLICE_LENGTH(GRPC_MDKEY(first_ent)) +
223 GRPC_SLICE_LENGTH(GRPC_MDVALUE(first_ent)) +
224 GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
225 GPR_ASSERT(elem_bytes <= tbl->mem_used);
226 tbl->mem_used -= static_cast<uint32_t>(elem_bytes);
227 tbl->first_ent = ((tbl->first_ent + 1) % tbl->cap_entries);
229 GRPC_MDELEM_UNREF(first_ent);
232 static void rebuild_ents(grpc_chttp2_hptbl* tbl, uint32_t new_cap) {
234 static_cast<grpc_mdelem*>(gpr_malloc(sizeof(*ents) * new_cap));
237 for (i = 0; i < tbl->num_ents; i++) {
238 ents[i] = tbl->ents[(tbl->first_ent + i) % tbl->cap_entries];
242 tbl->cap_entries = new_cap;
246 void grpc_chttp2_hptbl_set_max_bytes(grpc_chttp2_hptbl* tbl,
247 uint32_t max_bytes) {
248 if (tbl->max_bytes == max_bytes) {
251 if (GRPC_TRACE_FLAG_ENABLED(grpc_http_trace)) {
252 gpr_log(GPR_INFO, "Update hpack parser max size to %d", max_bytes);
254 while (tbl->mem_used > max_bytes) {
257 tbl->max_bytes = max_bytes;
260 grpc_error* grpc_chttp2_hptbl_set_current_table_size(grpc_chttp2_hptbl* tbl,
262 if (tbl->current_table_bytes == bytes) {
263 return GRPC_ERROR_NONE;
265 if (bytes > tbl->max_bytes) {
268 "Attempt to make hpack table %d bytes when max is %d bytes",
269 bytes, tbl->max_bytes);
270 grpc_error* err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
274 if (GRPC_TRACE_FLAG_ENABLED(grpc_http_trace)) {
275 gpr_log(GPR_INFO, "Update hpack parser table size to %d", bytes);
277 while (tbl->mem_used > bytes) {
280 tbl->current_table_bytes = bytes;
281 tbl->max_entries = entries_for_bytes(bytes);
282 if (tbl->max_entries > tbl->cap_entries) {
283 rebuild_ents(tbl, GPR_MAX(tbl->max_entries, 2 * tbl->cap_entries));
284 } else if (tbl->max_entries < tbl->cap_entries / 3) {
285 uint32_t new_cap = GPR_MAX(tbl->max_entries, 16u);
286 if (new_cap != tbl->cap_entries) {
287 rebuild_ents(tbl, new_cap);
290 return GRPC_ERROR_NONE;
293 grpc_error* grpc_chttp2_hptbl_add(grpc_chttp2_hptbl* tbl, grpc_mdelem md) {
294 /* determine how many bytes of buffer this entry represents */
295 size_t elem_bytes = GRPC_SLICE_LENGTH(GRPC_MDKEY(md)) +
296 GRPC_SLICE_LENGTH(GRPC_MDVALUE(md)) +
297 GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD;
299 if (tbl->current_table_bytes > tbl->max_bytes) {
303 "HPACK max table size reduced to %d but not reflected by hpack "
304 "stream (still at %d)",
305 tbl->max_bytes, tbl->current_table_bytes);
306 grpc_error* err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
311 /* we can't add elements bigger than the max table size */
312 if (elem_bytes > tbl->current_table_bytes) {
313 /* HPACK draft 10 section 4.4 states:
314 * If the size of the new entry is less than or equal to the maximum
315 * size, that entry is added to the table. It is not an error to
316 * attempt to add an entry that is larger than the maximum size; an
317 * attempt to add an entry larger than the entire table causes
319 * to be emptied of all existing entries, and results in an
322 while (tbl->num_ents) {
325 return GRPC_ERROR_NONE;
328 /* evict entries to ensure no overflow */
330 static_cast<size_t>(tbl->current_table_bytes) - tbl->mem_used) {
334 /* copy the finalized entry in */
335 tbl->ents[(tbl->first_ent + tbl->num_ents) % tbl->cap_entries] =
338 /* update accounting values */
340 tbl->mem_used += static_cast<uint32_t>(elem_bytes);
341 return GRPC_ERROR_NONE;
344 grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find(
345 const grpc_chttp2_hptbl* tbl, grpc_mdelem md) {
346 grpc_chttp2_hptbl_find_result r = {0, 0};
349 /* See if the string is in the static table */
350 for (i = 0; i < GRPC_CHTTP2_LAST_STATIC_ENTRY; i++) {
351 grpc_mdelem ent = tbl->static_ents[i];
352 if (!grpc_slice_eq(GRPC_MDKEY(md), GRPC_MDKEY(ent))) continue;
354 r.has_value = grpc_slice_eq(GRPC_MDVALUE(md), GRPC_MDVALUE(ent));
355 if (r.has_value) return r;
358 /* Scan the dynamic table */
359 for (i = 0; i < tbl->num_ents; i++) {
360 uint32_t idx = static_cast<uint32_t>(tbl->num_ents - i +
361 GRPC_CHTTP2_LAST_STATIC_ENTRY);
362 grpc_mdelem ent = tbl->ents[(tbl->first_ent + i) % tbl->cap_entries];
363 if (!grpc_slice_eq(GRPC_MDKEY(md), GRPC_MDKEY(ent))) continue;
365 r.has_value = grpc_slice_eq(GRPC_MDVALUE(md), GRPC_MDVALUE(ent));
366 if (r.has_value) return r;
372 static size_t get_base64_encoded_size(size_t raw_length) {
373 static const uint8_t tail_xtra[3] = {0, 2, 3};
374 return raw_length / 3 * 4 + tail_xtra[raw_length % 3];
377 size_t grpc_chttp2_get_size_in_hpack_table(grpc_mdelem elem,
378 bool use_true_binary_metadata) {
379 const uint8_t* key_buf = GRPC_SLICE_START_PTR(GRPC_MDKEY(elem));
380 size_t key_len = GRPC_SLICE_LENGTH(GRPC_MDKEY(elem));
381 size_t overhead_and_key = 32 + key_len;
382 size_t value_len = GRPC_SLICE_LENGTH(GRPC_MDVALUE(elem));
383 if (grpc_key_is_binary_header(key_buf, key_len)) {
384 return overhead_and_key + (use_true_binary_metadata
386 : get_base64_encoded_size(value_len));
388 return overhead_and_key + value_len;