hpack_parser.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_PARSER_H
20 #define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_PARSER_H
21 
23 
24 #include <stdint.h>
25 
26 #include <vector>
27 
28 #include <grpc/slice.h>
29 
34 
35 // IWYU pragma: no_include <type_traits>
36 
37 namespace grpc_core {
38 
39 // Top level interface for parsing a sequence of header, continuation frames.
40 class HPackParser {
41  public:
42  // What kind of stream boundary is provided by this frame?
43  enum class Boundary : uint8_t {
44  // More continuations are expected
45  None,
46  // This marks the end of headers, so data frames should follow
48  // This marks the end of headers *and* the end of the stream
50  };
51  // What kind of priority is represented in the next frame
52  enum class Priority : uint8_t {
53  // No priority field
54  None,
55  // Yes there's a priority field
56  Included
57  };
58  // Details about a frame we only need to know for logging
59  struct LogInfo {
60  // The stream ID
62  // Headers or trailers?
63  enum Type : uint8_t {
67  };
69  // Client or server?
70  bool is_client;
71  };
72 
73  HPackParser();
74  ~HPackParser();
75 
76  // Non-copyable/movable
77  HPackParser(const HPackParser&) = delete;
78  HPackParser& operator=(const HPackParser&) = delete;
79 
80  // Begin parsing a new frame
81  // Sink receives each parsed header,
82  void BeginFrame(grpc_metadata_batch* metadata_buffer,
83  uint32_t metadata_size_limit, Boundary boundary,
84  Priority priority, LogInfo log_info);
85  // Start throwing away any received headers after parsing them.
86  void StopBufferingFrame() { metadata_buffer_ = nullptr; }
87  // Parse one slice worth of data
88  grpc_error_handle Parse(const grpc_slice& slice, bool is_last);
89  // Reset state ready for the next BeginFrame
90  void FinishFrame();
91 
92  // Retrieve the associated hpack table (for tests, debugging)
93  HPackTable* hpack_table() { return &table_; }
94  // Is the current frame a boundary of some sort
95  bool is_boundary() const { return boundary_ != Boundary::None; }
96  // Is the current frame the end of a stream
97  bool is_eof() const { return boundary_ == Boundary::EndOfStream; }
98 
99  private:
100  // Helper classes: see implementation
101  class Parser;
102  class Input;
103  class String;
104 
105  grpc_error_handle ParseInput(Input input, bool is_last);
106  bool ParseInputInner(Input* input);
107 
108  // Target metadata buffer
110 
111  // Bytes that could not be parsed last parsing round
112  std::vector<uint8_t> unparsed_bytes_;
113  // Buffer kind of boundary
114  // TODO(ctiller): see if we can move this argument to Parse, and avoid
115  // buffering.
117  // Buffer priority
118  // TODO(ctiller): see if we can move this argument to Parse, and avoid
119  // buffering.
122  // Length of frame so far.
125  // Information for logging
127 
128  // hpack table
130 };
131 
132 } // namespace grpc_core
133 
134 /* wraps grpc_chttp2_hpack_parser_parse to provide a frame level parser for
135  the transport */
139  const grpc_slice& slice,
140  int is_last);
141 
142 #endif /* GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HPACK_PARSER_H */
grpc_core::HPackParser::LogInfo
Definition: hpack_parser.h:59
grpc_core::HPackParser::frame_length_
uint32_t frame_length_
Definition: hpack_parser.h:123
grpc_chttp2_header_parser_parse
grpc_error_handle grpc_chttp2_header_parser_parse(void *hpack_parser, grpc_chttp2_transport *t, grpc_chttp2_stream *s, const grpc_slice &slice, int is_last)
Definition: hpack_parser.cc:1353
grpc_core::HPackParser::LogInfo::kDontKnow
@ kDontKnow
Definition: hpack_parser.h:66
grpc_core::HPackParser::LogInfo::kTrailers
@ kTrailers
Definition: hpack_parser.h:65
grpc_core::HPackParser::Priority
Priority
Definition: hpack_parser.h:52
metadata_batch.h
priority
int priority
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:286
grpc_core::HPackParser::metadata_buffer_
grpc_metadata_batch * metadata_buffer_
Definition: hpack_parser.h:109
slice.h
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::HPackParser::FinishFrame
void FinishFrame()
Definition: hpack_parser.cc:1328
grpc_core::HPackParser::StopBufferingFrame
void StopBufferingFrame()
Definition: hpack_parser.h:86
grpc_core::HPackParser
Definition: hpack_parser.h:40
grpc_core::HPackParser::ParseInput
grpc_error_handle ParseInput(Input input, bool is_last)
Definition: hpack_parser.cc:1290
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_core::HPackParser::Boundary::None
@ None
grpc_chttp2_stream
Definition: src/core/ext/transport/chttp2/transport/internal.h:456
grpc_core::HPackParser::operator=
HPackParser & operator=(const HPackParser &)=delete
grpc_core::HPackParser::Boundary::EndOfHeaders
@ EndOfHeaders
grpc_core::HPackParser::Boundary
Boundary
Definition: hpack_parser.h:43
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_core::HPackParser::boundary_
Boundary boundary_
Definition: hpack_parser.h:116
grpc_core::HPackParser::~HPackParser
~HPackParser()
grpc_core::HPackParser::table_
HPackTable table_
Definition: hpack_parser.h:129
grpc_core::HPackParser::Parse
grpc_error_handle Parse(const grpc_slice &slice, bool is_last)
Definition: hpack_parser.cc:1277
grpc_core::HPackParser::Boundary::EndOfStream
@ EndOfStream
grpc_core::HPackParser::hpack_table
HPackTable * hpack_table()
Definition: hpack_parser.h:93
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
grpc_core::HPackParser::HPackParser
HPackParser()
grpc_core::HPackParser::unparsed_bytes_
std::vector< uint8_t > unparsed_bytes_
Definition: hpack_parser.h:112
grpc_core::HPackParser::is_boundary
bool is_boundary() const
Definition: hpack_parser.h:95
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_core::HPackParser::LogInfo::Type
Type
Definition: hpack_parser.h:63
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::HPackParser::is_eof
bool is_eof() const
Definition: hpack_parser.h:97
error.h
grpc_core::HPackParser::LogInfo::type
Type type
Definition: hpack_parser.h:68
grpc_core::HPackParser::metadata_size_limit_
uint32_t metadata_size_limit_
Definition: hpack_parser.h:124
stdint.h
grpc_core::HPackTable
Definition: hpack_parser_table.h:36
grpc_core::HPackParser::priority_
Priority priority_
Definition: hpack_parser.h:120
frame.h
grpc_core::HPackParser::Priority::Included
@ Included
grpc_core::HPackParser::dynamic_table_updates_allowed_
uint8_t dynamic_table_updates_allowed_
Definition: hpack_parser.h:121
hpack_parser_table.h
grpc_core::HPackParser::LogInfo::is_client
bool is_client
Definition: hpack_parser.h:70
grpc_core::HPackParser::Priority::None
@ None
grpc_core::HPackParser::LogInfo::stream_id
uint32_t stream_id
Definition: hpack_parser.h:61
grpc_chttp2_transport
Definition: src/core/ext/transport/chttp2/transport/internal.h:238
grpc_core::HPackParser::ParseInputInner
bool ParseInputInner(Input *input)
Definition: hpack_parser.cc:1305
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc_core::HPackParser::BeginFrame
void BeginFrame(grpc_metadata_batch *metadata_buffer, uint32_t metadata_size_limit, Boundary boundary, Priority priority, LogInfo log_info)
Definition: hpack_parser.cc:1265
grpc_error
Definition: error_internal.h:42
grpc_core::HPackParser::LogInfo::kHeaders
@ kHeaders
Definition: hpack_parser.h:64
grpc_metadata_batch
Definition: metadata_batch.h:1259
grpc_core::HPackParser::log_info_
LogInfo log_info_
Definition: hpack_parser.h:126
port_platform.h


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