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_ENCODER_H 20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <grpc/slice.h> 25 #include <grpc/slice_buffer.h> 26 #include "src/core/ext/transport/chttp2/transport/frame.h" 27 #include "src/core/lib/transport/metadata.h" 28 #include "src/core/lib/transport/metadata_batch.h" 29 #include "src/core/lib/transport/transport.h" 30 31 // This should be <= 8. We use 6 to save space. 32 #define GRPC_CHTTP2_HPACKC_NUM_VALUES_BITS 6 33 #define GRPC_CHTTP2_HPACKC_NUM_VALUES (1 << GRPC_CHTTP2_HPACKC_NUM_VALUES_BITS) 34 /* initial table size, per spec */ 35 #define GRPC_CHTTP2_HPACKC_INITIAL_TABLE_SIZE 4096 36 /* maximum table size we'll actually use */ 37 #define GRPC_CHTTP2_HPACKC_MAX_TABLE_SIZE (1024 * 1024) 38 39 extern grpc_core::TraceFlag grpc_http_trace; 40 41 struct grpc_chttp2_hpack_compressor { 42 uint32_t max_table_size; 43 uint32_t max_table_elems; 44 uint32_t cap_table_elems; 45 /** maximum number of bytes we'll use for the decode table (to guard against 46 peers ooming us by setting decode table size high) */ 47 uint32_t max_usable_size; 48 /* one before the lowest usable table index */ 49 uint32_t tail_remote_index; 50 uint32_t table_size; 51 uint32_t table_elems; 52 uint16_t* table_elem_size; 53 /** if non-zero, advertise to the decoder that we'll start using a table 54 of this size */ 55 uint8_t advertise_table_size_change; 56 57 /* filter tables for elems: this tables provides an approximate 58 popularity count for particular hashes, and are used to determine whether 59 a new literal should be added to the compression table or not. 60 They track a single integer that counts how often a particular value has 61 been seen. When that count reaches max (255), all values are halved. */ 62 uint32_t filter_elems_sum; 63 uint8_t filter_elems[GRPC_CHTTP2_HPACKC_NUM_VALUES]; 64 65 /* entry tables for keys & elems: these tables track values that have been 66 seen and *may* be in the decompressor table */ 67 struct { 68 struct { 69 grpc_mdelem value; 70 uint32_t index; 71 } entries[GRPC_CHTTP2_HPACKC_NUM_VALUES]; 72 } elem_table; /* Metadata table management */ 73 struct { 74 struct { 75 /* Only store the slice refcount - we do not need the byte buffer or 76 length of the slice since we only need to store a mapping between the 77 identity of the slice and the corresponding HPACK index. Since the 78 slice *must* be static or interned, the refcount is sufficient to 79 establish identity. */ 80 grpc_slice_refcount* value; 81 uint32_t index; 82 } entries[GRPC_CHTTP2_HPACKC_NUM_VALUES]; 83 } key_table; /* Key table management */ 84 }; 85 86 void grpc_chttp2_hpack_compressor_init(grpc_chttp2_hpack_compressor* c); 87 void grpc_chttp2_hpack_compressor_destroy(grpc_chttp2_hpack_compressor* c); 88 void grpc_chttp2_hpack_compressor_set_max_table_size( 89 grpc_chttp2_hpack_compressor* c, uint32_t max_table_size); 90 void grpc_chttp2_hpack_compressor_set_max_usable_size( 91 grpc_chttp2_hpack_compressor* c, uint32_t max_table_size); 92 93 struct grpc_encode_header_options { 94 uint32_t stream_id; 95 bool is_eof; 96 bool use_true_binary_metadata; 97 size_t max_frame_size; 98 grpc_transport_one_way_stats* stats; 99 }; 100 void grpc_chttp2_encode_header(grpc_chttp2_hpack_compressor* c, 101 grpc_mdelem** extra_headers, 102 size_t extra_headers_size, 103 grpc_metadata_batch* metadata, 104 const grpc_encode_header_options* options, 105 grpc_slice_buffer* outbuf); 106 107 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_H */ 108