hpack_encoder.h
Go to the documentation of this file.
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 
23 
24 #include <stddef.h>
25 
26 #include <cstdint>
27 #include <utility>
28 #include <vector>
29 
30 #include "absl/strings/match.h"
31 #include "absl/strings/string_view.h"
32 
34 #include <grpc/slice.h>
35 #include <grpc/status.h>
36 
46 
48 
49 namespace grpc_core {
50 
52  class SliceIndex;
53 
54  public:
55  HPackCompressor() = default;
56  ~HPackCompressor() = default;
57 
58  // Maximum table size we'll actually use.
59  static constexpr uint32_t kMaxTableSize = 1024 * 1024;
60 
61  void SetMaxTableSize(uint32_t max_table_size);
62  void SetMaxUsableSize(uint32_t max_table_size);
63 
66  }
67 
74  };
75 
76  template <typename HeaderSet>
78  const HeaderSet& headers, grpc_slice_buffer* output) {
79  Framer framer(options, this, output);
80  headers.Encode(&framer);
81  }
82 
83  class Framer {
84  public:
87  ~Framer() { FinishFrame(true); }
88 
89  Framer(const Framer&) = delete;
90  Framer& operator=(const Framer&) = delete;
91 
92  void Encode(const Slice& key, const Slice& value);
93  void Encode(HttpPathMetadata, const Slice& value);
96  void Encode(GrpcTimeoutMetadata, Timestamp deadline);
101  void Encode(UserAgentMetadata, const Slice& slice);
105  void Encode(GrpcTagsBinMetadata, const Slice& slice);
106  void Encode(GrpcTraceBinMetadata, const Slice& slice);
108  if (slice.empty()) return;
110  Slice::FromStaticString("grpc-message"), slice.Ref());
111  }
112  template <typename Which>
113  void Encode(Which, const typename Which::ValueType& value) {
114  const Slice& slice = MetadataValueAsSlice<Which>(value);
115  if (absl::EndsWith(Which::key(), "-bin")) {
118  } else {
121  }
122  }
123 
124  private:
125  friend class SliceIndex;
126 
127  struct FramePrefix {
128  // index (in output_) of the header for the frame
129  size_t header_idx;
130  // number of bytes in 'output' when we started the frame - used to
131  // calculate frame length
133  };
134 
136  void FinishFrame(bool is_header_boundary);
137  void EnsureSpace(size_t need_bytes);
138 
140  void EmitIndexed(uint32_t index);
142  Slice value_slice);
144  Slice value_slice);
146  Slice value_slice);
148  Slice value_slice);
150  Slice value_slice);
151 
153  Slice value, uint32_t transport_length);
155  Slice value);
156 
157  size_t CurrentFrameSize() const;
158  void Add(Slice slice);
159  uint8_t* AddTiny(size_t len);
160 
161  // maximum size of a frame
162  const size_t max_frame_size_;
163  bool is_first_frame_ = true;
165  const bool is_end_of_stream_;
166  // output stream id
172  };
173 
174  private:
175  static constexpr size_t kNumFilterValues = 64;
176  static constexpr uint32_t kNumCachedGrpcStatusValues = 16;
177 
178  // maximum number of bytes we'll use for the decode table (to guard against
179  // peers ooming us by setting decode table size high)
181  // if non-zero, advertise to the decoder that we'll start using a table
182  // of this size
185 
186  class SliceIndex {
187  public:
188  void EmitTo(absl::string_view key, const Slice& value, Framer* framer);
189 
190  private:
191  struct ValueIndex {
193  : value(std::move(value)), index(index) {}
196  };
197  std::vector<ValueIndex> values_;
198  };
199 
203  };
204 
205  // Index into table_ for the te:trailers metadata element
207  // Index into table_ for the content-type metadata element
209  // Index into table_ for the user-agent metadata element
211  // Cached grpc-status values
213  // Cached grpc-encoding values
215  // Cached grpc-accept-encoding value
217  // The grpc-accept-encoding string referred to by grpc_accept_encoding_index_
219  // Index of something that was sent with grpc-tags-bin
221  // Index of something that was sent with grpc-trace-bin
223  // The user-agent string referred to by user_agent_index_
227  std::vector<PreviousTimeout> previous_timeouts_;
228 };
229 
230 } // namespace grpc_core
231 
232 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_ENCODER_H */
trace.h
grpc_core::HPackCompressor::Framer::FramePrefix::output_length_at_start_of_frame
size_t output_length_at_start_of_frame
Definition: hpack_encoder.h:132
grpc_core::HPackCompressor::user_agent_
Slice user_agent_
Definition: hpack_encoder.h:224
slice.h
grpc_core::HPackCompressor::EncodeHeaderOptions::is_end_of_stream
bool is_end_of_stream
Definition: hpack_encoder.h:70
grpc_core::HPackCompressor::HPackCompressor
HPackCompressor()=default
grpc_core::GrpcTimeoutMetadata
Definition: metadata_batch.h:59
metadata_batch.h
grpc_core::HPackCompressor::content_type_index_
uint32_t content_type_index_
Definition: hpack_encoder.h:208
grpc_core::HPackCompressor::grpc_accept_encoding_
CompressionAlgorithmSet grpc_accept_encoding_
Definition: hpack_encoder.h:218
grpc_core::CompressionAlgorithmSet
Definition: compression_internal.h:52
slice.h
grpc_core::HttpMethodMetadata
Definition: metadata_batch.h:136
grpc_core::HPackCompressor::grpc_accept_encoding_index_
uint32_t grpc_accept_encoding_index_
Definition: hpack_encoder.h:216
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::Slice
Definition: src/core/lib/slice/slice.h:282
grpc_core::HttpPathMetadata
Definition: metadata_batch.h:262
options
double_dict options[]
Definition: capstone_test.c:55
grpc_core::HPackCompressor::Framer::EnsureSpace
void EnsureSpace(size_t need_bytes)
Definition: hpack_encoder.cc:126
grpc_compression_algorithm
grpc_compression_algorithm
Definition: compression_types.h:60
grpc_core::slice_detail::StaticConstructors< Slice >::FromStaticString
static Slice FromStaticString(const char *s)
Definition: src/core/lib/slice/slice.h:201
grpc_core::HPackEncoderTable
Definition: hpack_encoder_table.h:31
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_core::HPackCompressor::Framer
Definition: hpack_encoder.h:83
grpc_core::HttpSchemeMetadata::ValueType
ValueType
Definition: metadata_batch.h:116
grpc_core::HPackCompressor::user_agent_index_
uint32_t user_agent_index_
Definition: hpack_encoder.h:210
grpc_core::GrpcMessageMetadata
Definition: metadata_batch.h:220
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
grpc_core::GrpcEncodingMetadata
Definition: metadata_batch.h:175
grpc_core::HPackCompressor::SliceIndex::values_
std::vector< ValueIndex > values_
Definition: hpack_encoder.h:197
status
absl::Status status
Definition: rls.cc:251
grpc_core::HPackCompressor::~HPackCompressor
~HPackCompressor()=default
grpc_core::HttpStatusMetadata
Definition: metadata_batch.h:320
grpc_core::GrpcAcceptEncodingMetadata
Definition: metadata_batch.h:187
grpc_core::HPackCompressor::EncodeHeaderOptions::stats
grpc_transport_one_way_stats * stats
Definition: hpack_encoder.h:73
grpc_core::HPackCompressor::kNumCachedGrpcStatusValues
static constexpr uint32_t kNumCachedGrpcStatusValues
Definition: hpack_encoder.h:176
grpc_core::HPackCompressor::kMaxTableSize
static constexpr uint32_t kMaxTableSize
Definition: hpack_encoder.h:59
grpc_core::HPackCompressor::Framer::EmitLitHdrWithNonBinaryStringKeyNotIdx
void EmitLitHdrWithNonBinaryStringKeyNotIdx(Slice key_slice, Slice value_slice)
Definition: hpack_encoder.cc:318
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_core::HPackCompressor::EncodeHeaders
void EncodeHeaders(const EncodeHeaderOptions &options, const HeaderSet &headers, grpc_slice_buffer *output)
Definition: hpack_encoder.h:77
grpc_core::TeMetadata
Definition: metadata_batch.h:71
grpc_core::HPackCompressor::Framer::Encode
void Encode(Which, const typename Which::ValueType &value)
Definition: hpack_encoder.h:113
grpc_core::HPackCompressor::cached_grpc_encoding_
uint32_t cached_grpc_encoding_[GRPC_COMPRESS_ALGORITHMS_COUNT]
Definition: hpack_encoder.h:214
grpc_core::HPackCompressor::SliceIndex::ValueIndex::value
Slice value
Definition: hpack_encoder.h:194
grpc_http_trace
grpc_core::TraceFlag grpc_http_trace
grpc_core::HPackCompressor::cached_grpc_status_
uint32_t cached_grpc_status_[kNumCachedGrpcStatusValues]
Definition: hpack_encoder.h:212
grpc_core::HPackCompressor::EncodeHeaderOptions
Definition: hpack_encoder.h:68
status.h
grpc_core::Timeout
Definition: timeout_encoding.h:33
timeout_encoding.h
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::HPackCompressor::Framer::EmitLitHdrWithBinaryStringKeyNotIdx
void EmitLitHdrWithBinaryStringKeyNotIdx(Slice key_slice, Slice value_slice)
Definition: hpack_encoder.cc:282
grpc_core::HPackCompressor::SliceIndex::EmitTo
void EmitTo(absl::string_view key, const Slice &value, Framer *framer)
Definition: hpack_encoder.cc:335
grpc_core::HPackCompressor::SliceIndex
Definition: hpack_encoder.h:186
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::HPackCompressor::Framer::operator=
Framer & operator=(const Framer &)=delete
grpc_core::HPackCompressor::Framer::Add
void Add(Slice slice)
Definition: hpack_encoder.cc:134
grpc_core::HPackCompressor
Definition: hpack_encoder.h:51
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc_core::GrpcTagsBinMetadata
Definition: metadata_batch.h:250
grpc_core::HPackCompressor::kNumFilterValues
static constexpr size_t kNumFilterValues
Definition: hpack_encoder.h:175
grpc_core::HttpAuthorityMetadata
Definition: metadata_batch.h:256
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_core::HPackCompressor::Framer::AddTiny
uint8_t * AddTiny(size_t len)
Definition: hpack_encoder.cc:154
grpc_core::HttpMethodMetadata::ValueType
ValueType
Definition: metadata_batch.h:138
grpc_core::HPackCompressor::te_index_
uint32_t te_index_
Definition: hpack_encoder.h:206
grpc_core::HPackCompressor::max_usable_size_
uint32_t max_usable_size_
Definition: hpack_encoder.h:180
grpc_core::HPackCompressor::PreviousTimeout::index
uint32_t index
Definition: hpack_encoder.h:202
time.h
grpc_core::HPackCompressor::authority_index_
SliceIndex authority_index_
Definition: hpack_encoder.h:226
grpc_core::HPackCompressor::Framer::BeginFrame
FramePrefix BeginFrame()
Definition: hpack_encoder.cc:115
grpc_core::UserAgentMetadata
Definition: metadata_batch.h:214
grpc_core::HPackCompressor::Framer::CurrentFrameSize
size_t CurrentFrameSize() const
Definition: hpack_encoder.cc:79
grpc_core::HPackCompressor::EncodeHeaderOptions::use_true_binary_metadata
bool use_true_binary_metadata
Definition: hpack_encoder.h:71
grpc_core::HPackCompressor::Framer::FramePrefix
Definition: hpack_encoder.h:127
grpc_core::HPackCompressor::SetMaxTableSize
void SetMaxTableSize(uint32_t max_table_size)
Definition: hpack_encoder.cc:643
grpc_core::HPackCompressor::Framer::prefix_
FramePrefix prefix_
Definition: hpack_encoder.h:171
grpc_transport_one_way_stats
Definition: transport.h:243
grpc_core::GrpcStatusMetadata
Definition: metadata_batch.h:293
grpc_core::HPackCompressor::Framer::EncodeIndexedKeyWithBinaryValue
void EncodeIndexedKeyWithBinaryValue(uint32_t *index, absl::string_view key, Slice value)
Definition: hpack_encoder.cc:511
Json::ValueType
ValueType
Type of the value held by a Value object.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:463
grpc_core::HPackCompressor::EncodeHeaderOptions::stream_id
uint32_t stream_id
Definition: hpack_encoder.h:69
grpc_core::HPackCompressor::Framer::FramePrefix::header_idx
size_t header_idx
Definition: hpack_encoder.h:129
grpc_core::HPackCompressor::PreviousTimeout
Definition: hpack_encoder.h:200
grpc_core::TraceFlag
Definition: debug/trace.h:63
grpc_core::HPackCompressor::Framer::stats_
grpc_transport_one_way_stats *const stats_
Definition: hpack_encoder.h:169
compression_types.h
grpc_core::HPackCompressor::Framer::EmitLitHdrWithNonBinaryStringKeyIncIdx
void EmitLitHdrWithNonBinaryStringKeyIncIdx(Slice key_slice, Slice value_slice)
Definition: hpack_encoder.cc:270
value
const char * value
Definition: hpack_parser_table.cc:165
compression_internal.h
grpc_core::HPackCompressor::Framer::FinishFrame
void FinishFrame(bool is_header_boundary)
Definition: hpack_encoder.cc:87
grpc_core::ContentTypeMetadata::ValueType
ValueType
Definition: metadata_batch.h:97
grpc_core::HPackCompressor::SetMaxUsableSize
void SetMaxUsableSize(uint32_t max_table_size)
Definition: hpack_encoder.cc:638
key
const char * key
Definition: hpack_parser_table.cc:164
grpc_core::HPackCompressor::PreviousTimeout::timeout
Timeout timeout
Definition: hpack_encoder.h:201
grpc_core::HttpSchemeMetadata
Definition: metadata_batch.h:114
grpc_core::HPackCompressor::grpc_tags_bin_index_
uint32_t grpc_tags_bin_index_
Definition: hpack_encoder.h:220
grpc_core::HPackCompressor::grpc_trace_bin_index_
uint32_t grpc_trace_bin_index_
Definition: hpack_encoder.h:222
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
grpc_core::HPackCompressor::Framer::max_frame_size_
const size_t max_frame_size_
Definition: hpack_encoder.h:162
grpc_core::HPackCompressor::Framer::Encode
void Encode(GrpcMessageMetadata, const Slice &slice)
Definition: hpack_encoder.h:107
hpack_constants.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
GRPC_COMPRESS_ALGORITHMS_COUNT
@ GRPC_COMPRESS_ALGORITHMS_COUNT
Definition: compression_types.h:65
grpc_core::HPackCompressor::SliceIndex::ValueIndex
Definition: hpack_encoder.h:191
grpc_core::HPackCompressor::Framer::is_first_frame_
bool is_first_frame_
Definition: hpack_encoder.h:163
grpc_core::HPackCompressor::path_index_
SliceIndex path_index_
Definition: hpack_encoder.h:225
grpc_core::HPackCompressor::Framer::EncodeAlwaysIndexed
void EncodeAlwaysIndexed(uint32_t *index, absl::string_view key, Slice value, uint32_t transport_length)
Definition: hpack_encoder.cc:498
transport.h
grpc_core::HPackCompressor::Framer::use_true_binary_metadata_
const bool use_true_binary_metadata_
Definition: hpack_encoder.h:164
hpack_encoder_table.h
grpc_core::HPackCompressor::EncodeHeaderOptions::max_frame_size
size_t max_frame_size
Definition: hpack_encoder.h:72
grpc_core::HPackCompressor::Framer::~Framer
~Framer()
Definition: hpack_encoder.h:87
grpc_core::ContentTypeMetadata
Definition: metadata_batch.h:92
grpc_core::hpack_constants::kInitialTableSize
static constexpr uint32_t kInitialTableSize
Definition: hpack_constants.h:27
grpc_core::HPackCompressor::SliceIndex::ValueIndex::index
uint32_t index
Definition: hpack_encoder.h:195
grpc_slice_buffer
Definition: include/grpc/impl/codegen/slice.h:83
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_core::HPackCompressor::test_only_table_size
uint32_t test_only_table_size() const
Definition: hpack_encoder.h:64
grpc_core::HPackCompressor::SliceIndex::ValueIndex::ValueIndex
ValueIndex(Slice value, uint32_t index)
Definition: hpack_encoder.h:192
grpc_core::HPackCompressor::Framer::output_
grpc_slice_buffer *const output_
Definition: hpack_encoder.h:168
grpc_core::HPackCompressor::Framer::AdvertiseTableSizeChange
void AdvertiseTableSizeChange()
Definition: hpack_encoder.cc:330
grpc_core::HPackCompressor::Framer::is_end_of_stream_
const bool is_end_of_stream_
Definition: hpack_encoder.h:165
method
NSString * method
Definition: ProtoMethod.h:28
grpc_core::HPackCompressor::previous_timeouts_
std::vector< PreviousTimeout > previous_timeouts_
Definition: hpack_encoder.h:227
grpc_core::HPackCompressor::Framer::compressor_
HPackCompressor *const compressor_
Definition: hpack_encoder.h:170
grpc_core::HPackCompressor::advertise_table_size_change_
bool advertise_table_size_change_
Definition: hpack_encoder.h:183
grpc_core::HPackEncoderTable::test_only_table_size
uint32_t test_only_table_size() const
Definition: hpack_encoder_table.h:46
grpc_core::TeMetadata::ValueType
ValueType
Definition: metadata_batch.h:76
grpc_core::GrpcTraceBinMetadata
Definition: metadata_batch.h:244
grpc_core::HPackCompressor::Framer::Framer
Framer(const EncodeHeaderOptions &options, HPackCompressor *compressor, grpc_slice_buffer *output)
Definition: hpack_encoder.cc:653
grpc_core::HPackCompressor::table_
HPackEncoderTable table_
Definition: hpack_encoder.h:184
absl::EndsWith
bool EndsWith(absl::string_view text, absl::string_view suffix) noexcept
Definition: third_party/abseil-cpp/absl/strings/match.h:68
grpc_core::HPackCompressor::Framer::stream_id_
const uint32_t stream_id_
Definition: hpack_encoder.h:167
grpc_core::HPackCompressor::Framer::EmitLitHdrWithBinaryStringKeyIncIdx
void EmitLitHdrWithBinaryStringKeyIncIdx(Slice key_slice, Slice value_slice)
Definition: hpack_encoder.cc:294
grpc_core::HPackCompressor::Framer::EmitIndexed
void EmitIndexed(uint32_t index)
Definition: hpack_encoder.cc:160
port_platform.h
grpc_core::HPackCompressor::Framer::Encode
void Encode(const Slice &key, const Slice &value)
Definition: hpack_encoder.cc:381


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:13