Home | History | Annotate | Download | only in transport
      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 typedef struct {
     42   uint32_t filter_elems_sum;
     43   uint32_t max_table_size;
     44   uint32_t max_table_elems;
     45   uint32_t cap_table_elems;
     46   /** if non-zero, advertise to the decoder that we'll start using a table
     47       of this size */
     48   uint8_t advertise_table_size_change;
     49   /** maximum number of bytes we'll use for the decode table (to guard against
     50       peers ooming us by setting decode table size high) */
     51   uint32_t max_usable_size;
     52   /* one before the lowest usable table index */
     53   uint32_t tail_remote_index;
     54   uint32_t table_size;
     55   uint32_t table_elems;
     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   uint8_t filter_elems[GRPC_CHTTP2_HPACKC_NUM_VALUES];
     63 
     64   /* entry tables for keys & elems: these tables track values that have been
     65      seen and *may* be in the decompressor table */
     66   grpc_slice entries_keys[GRPC_CHTTP2_HPACKC_NUM_VALUES];
     67   grpc_mdelem entries_elems[GRPC_CHTTP2_HPACKC_NUM_VALUES];
     68   uint32_t indices_keys[GRPC_CHTTP2_HPACKC_NUM_VALUES];
     69   uint32_t indices_elems[GRPC_CHTTP2_HPACKC_NUM_VALUES];
     70 
     71   uint16_t* table_elem_size;
     72 } grpc_chttp2_hpack_compressor;
     73 
     74 void grpc_chttp2_hpack_compressor_init(grpc_chttp2_hpack_compressor* c);
     75 void grpc_chttp2_hpack_compressor_destroy(grpc_chttp2_hpack_compressor* c);
     76 void grpc_chttp2_hpack_compressor_set_max_table_size(
     77     grpc_chttp2_hpack_compressor* c, uint32_t max_table_size);
     78 void grpc_chttp2_hpack_compressor_set_max_usable_size(
     79     grpc_chttp2_hpack_compressor* c, uint32_t max_table_size);
     80 
     81 typedef struct {
     82   uint32_t stream_id;
     83   bool is_eof;
     84   bool use_true_binary_metadata;
     85   size_t max_frame_size;
     86   grpc_transport_one_way_stats* stats;
     87 } grpc_encode_header_options;
     88 
     89 void grpc_chttp2_encode_header(grpc_chttp2_hpack_compressor* c,
     90                                grpc_mdelem** extra_headers,
     91                                size_t extra_headers_size,
     92                                grpc_metadata_batch* metadata,
     93                                const grpc_encode_header_options* options,
     94                                grpc_slice_buffer* outbuf);
     95 
     96 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_H */
     97