1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19 #ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_TABLE_H 20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_TABLE_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/slice.h> 25 #include "src/core/lib/iomgr/error.h" 26 #include "src/core/lib/transport/metadata.h" 27 28 /* HPACK header table */ 29 30 /* last index in the static table */ 31 #define GRPC_CHTTP2_LAST_STATIC_ENTRY 61 32 33 /* Initial table size as per the spec */ 34 #define GRPC_CHTTP2_INITIAL_HPACK_TABLE_SIZE 4096 35 /* Maximum table size that we'll use */ 36 #define GRPC_CHTTP2_MAX_HPACK_TABLE_SIZE GRPC_CHTTP2_INITIAL_HPACK_TABLE_SIZE 37 /* Per entry overhead bytes as per the spec */ 38 #define GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD 32 39 #if 0 40 /* Maximum number of entries we could possibly fit in the table, given defined 41 overheads */ 42 #define GRPC_CHTTP2_MAX_TABLE_COUNT \ 43 ((GRPC_CHTTP2_MAX_HPACK_TABLE_SIZE + GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD - 1) / \ 44 GRPC_CHTTP2_HPACK_ENTRY_OVERHEAD) 45 #endif 46 47 /* hpack decoder table */ 48 typedef struct { 49 /* the first used entry in ents */ 50 uint32_t first_ent; 51 /* how many entries are in the table */ 52 uint32_t num_ents; 53 /* the amount of memory used by the table, according to the hpack algorithm */ 54 uint32_t mem_used; 55 /* the max memory allowed to be used by the table, according to the hpack 56 algorithm */ 57 uint32_t max_bytes; 58 /* the currently agreed size of the table, according to the hpack algorithm */ 59 uint32_t current_table_bytes; 60 /* Maximum number of entries we could possibly fit in the table, given defined 61 overheads */ 62 uint32_t max_entries; 63 /* Number of entries allocated in ents */ 64 uint32_t cap_entries; 65 /* a circular buffer of headers - this is stored in the opposite order to 66 what hpack specifies, in order to simplify table management a little... 67 meaning lookups need to SUBTRACT from the end position */ 68 grpc_mdelem* ents; 69 grpc_mdelem static_ents[GRPC_CHTTP2_LAST_STATIC_ENTRY]; 70 } grpc_chttp2_hptbl; 71 72 /* initialize a hpack table */ 73 void grpc_chttp2_hptbl_init(grpc_chttp2_hptbl* tbl); 74 void grpc_chttp2_hptbl_destroy(grpc_chttp2_hptbl* tbl); 75 void grpc_chttp2_hptbl_set_max_bytes(grpc_chttp2_hptbl* tbl, 76 uint32_t max_bytes); 77 grpc_error* grpc_chttp2_hptbl_set_current_table_size(grpc_chttp2_hptbl* tbl, 78 uint32_t bytes); 79 80 /* lookup a table entry based on its hpack index */ 81 grpc_mdelem grpc_chttp2_hptbl_lookup(const grpc_chttp2_hptbl* tbl, 82 uint32_t index); 83 /* add a table entry to the index */ 84 grpc_error* grpc_chttp2_hptbl_add(grpc_chttp2_hptbl* tbl, 85 grpc_mdelem md) GRPC_MUST_USE_RESULT; 86 87 size_t grpc_chttp2_get_size_in_hpack_table(grpc_mdelem elem, 88 bool use_true_binary_metadata); 89 90 /* Returns the static hpack table index that corresponds to /a elem. Returns 0 91 if /a elem is not statically stored or if it is not in the static hpack 92 table */ 93 uint8_t grpc_chttp2_get_static_hpack_table_index(grpc_mdelem md); 94 95 /* Find a key/value pair in the table... returns the index in the table of the 96 most similar entry, or 0 if the value was not found */ 97 typedef struct { 98 uint32_t index; 99 int has_value; 100 } grpc_chttp2_hptbl_find_result; 101 grpc_chttp2_hptbl_find_result grpc_chttp2_hptbl_find( 102 const grpc_chttp2_hptbl* tbl, grpc_mdelem md); 103 104 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_TABLE_H */ 105