Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/extensions/filters/network/thrift_proxy/thrift_object_impl.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "source/extensions/filters/network/thrift_proxy/decoder.h"
4
#include "source/extensions/filters/network/thrift_proxy/filters/filter.h"
5
#include "source/extensions/filters/network/thrift_proxy/thrift_object.h"
6
7
namespace Envoy {
8
namespace Extensions {
9
namespace NetworkFilters {
10
namespace ThriftProxy {
11
12
/**
13
 * ThriftBase is a base class for decoding Thrift objects. It implements methods from
14
 * DecoderEventHandler to automatically delegate to an underlying ThriftBase so that, for example,
15
 * the fieldBegin call for a struct field nested within a list is automatically delegated down the
16
 * object hierarchy to the correct ThriftBase subclass.
17
 */
18
class ThriftBase : public DecoderEventHandler {
19
public:
20
  ThriftBase(ThriftBase* parent);
21
  ~ThriftBase() override = default;
22
23
  // DecoderEventHandler
24
0
  FilterStatus passthroughData(Buffer::Instance&) override { return FilterStatus::Continue; }
25
0
  FilterStatus transportBegin(MessageMetadataSharedPtr) override { return FilterStatus::Continue; }
26
0
  FilterStatus transportEnd() override { return FilterStatus::Continue; }
27
0
  FilterStatus messageBegin(MessageMetadataSharedPtr) override { return FilterStatus::Continue; }
28
0
  FilterStatus messageEnd() override { return FilterStatus::Continue; }
29
  FilterStatus structBegin(absl::string_view name) override;
30
  FilterStatus structEnd() override;
31
  FilterStatus fieldBegin(absl::string_view name, FieldType& field_type,
32
                          int16_t& field_id) override;
33
  FilterStatus fieldEnd() override;
34
  FilterStatus boolValue(bool& value) override;
35
  FilterStatus byteValue(uint8_t& value) override;
36
  FilterStatus int16Value(int16_t& value) override;
37
  FilterStatus int32Value(int32_t& value) override;
38
  FilterStatus int64Value(int64_t& value) override;
39
  FilterStatus doubleValue(double& value) override;
40
  FilterStatus stringValue(absl::string_view value) override;
41
  FilterStatus mapBegin(FieldType& key_type, FieldType& value_type, uint32_t& size) override;
42
  FilterStatus mapEnd() override;
43
  FilterStatus listBegin(FieldType& elem_type, uint32_t& size) override;
44
  FilterStatus listEnd() override;
45
  FilterStatus setBegin(FieldType& elem_type, uint32_t& size) override;
46
  FilterStatus setEnd() override;
47
48
  // Invoked when the current delegate is complete. Completion implies that the delegate is fully
49
  // specified (all list values processed, all struct fields processed, etc).
50
  virtual void delegateComplete();
51
52
protected:
53
  ThriftBase* parent_;
54
  ThriftBase* delegate_{nullptr};
55
};
56
57
/**
58
 * ThriftValueBase is a base class for all struct field values, list values, set values, map keys,
59
 * and map values.
60
 */
61
class ThriftValueBase : public ThriftValue, public ThriftBase {
62
public:
63
  ThriftValueBase(ThriftBase* parent, FieldType value_type)
64
0
      : ThriftBase(parent), value_type_(value_type) {}
65
0
  ~ThriftValueBase() override = default;
66
67
  // ThriftValue
68
0
  FieldType type() const override { return value_type_; }
69
70
protected:
71
  const FieldType value_type_;
72
};
73
74
class ThriftStructValueImpl;
75
76
/**
77
 * ThriftField represents a field in a thrift Struct. It always delegates DecoderEventHandler
78
 * methods to a subclass of ThriftValueBase.
79
 */
80
class ThriftFieldImpl : public ThriftField, public ThriftBase {
81
public:
82
  ThriftFieldImpl(ThriftStructValueImpl* parent, absl::string_view name, FieldType field_type,
83
                  int16_t field_id);
84
85
  // DecoderEventHandler
86
  FilterStatus fieldEnd() override;
87
88
  // ThriftField
89
0
  FieldType fieldType() const override { return field_type_; }
90
0
  int16_t fieldId() const override { return field_id_; }
91
0
  const ThriftValue& getValue() const override { return *value_; }
92
93
private:
94
  std::string name_;
95
  FieldType field_type_;
96
  int16_t field_id_;
97
  ThriftValuePtr value_;
98
};
99
100
/**
101
 * ThriftStructValueImpl implements ThriftStruct.
102
 */
103
class ThriftStructValueImpl : public ThriftStructValue, public ThriftValueBase {
104
public:
105
0
  ThriftStructValueImpl(ThriftBase* parent) : ThriftValueBase(parent, FieldType::Struct) {}
106
107
  // DecoderEventHandler
108
  FilterStatus structBegin(absl::string_view name) override;
109
  FilterStatus structEnd() override;
110
  FilterStatus fieldBegin(absl::string_view name, FieldType& field_type,
111
                          int16_t& field_id) override;
112
113
  // ThriftStructValue
114
0
  const ThriftFieldPtrList& fields() const override { return fields_; }
115
116
private:
117
  // ThriftValue
118
0
  const void* getValue() const override { return this; };
119
120
  ThriftFieldPtrList fields_;
121
};
122
123
/**
124
 * ThriftListValueImpl represents Thrift lists.
125
 */
126
class ThriftListValueImpl : public ThriftListValue, public ThriftValueBase {
127
public:
128
0
  ThriftListValueImpl(ThriftBase* parent) : ThriftValueBase(parent, FieldType::List) {}
129
130
  // DecoderEventHandler
131
  FilterStatus listBegin(FieldType& elem_type, uint32_t& size) override;
132
  FilterStatus listEnd() override;
133
134
  // ThriftListValue
135
0
  const ThriftValuePtrList& elements() const override { return elements_; }
136
0
  FieldType elementType() const override { return elem_type_; }
137
138
  void delegateComplete() override;
139
140
protected:
141
  // ThriftValue
142
0
  const void* getValue() const override { return this; };
143
144
  FieldType elem_type_{FieldType::Stop};
145
  uint32_t remaining_{0};
146
  ThriftValuePtrList elements_;
147
};
148
149
/**
150
 * ThriftSetValueImpl represents Thrift sets.
151
 */
152
class ThriftSetValueImpl : public ThriftSetValue, public ThriftValueBase {
153
public:
154
0
  ThriftSetValueImpl(ThriftBase* parent) : ThriftValueBase(parent, FieldType::Set) {}
155
156
  // DecoderEventHandler
157
  FilterStatus setBegin(FieldType& elem_type, uint32_t& size) override;
158
  FilterStatus setEnd() override;
159
160
  // ThriftSetValue
161
0
  const ThriftValuePtrList& elements() const override { return elements_; }
162
0
  FieldType elementType() const override { return elem_type_; }
163
164
  void delegateComplete() override;
165
166
protected:
167
  // ThriftValue
168
0
  const void* getValue() const override { return this; };
169
170
  FieldType elem_type_{FieldType::Stop};
171
  uint32_t remaining_{0};
172
  ThriftValuePtrList elements_; // maintain original order
173
};
174
175
/**
176
 * ThriftMapValueImpl represents Thrift maps.
177
 */
178
class ThriftMapValueImpl : public ThriftMapValue, public ThriftValueBase {
179
public:
180
0
  ThriftMapValueImpl(ThriftBase* parent) : ThriftValueBase(parent, FieldType::Map) {}
181
182
  // DecoderEventHandler
183
  FilterStatus mapBegin(FieldType& key_type, FieldType& elem_type, uint32_t& size) override;
184
  FilterStatus mapEnd() override;
185
186
  // ThriftMapValue
187
0
  const ThriftValuePtrPairList& elements() const override { return elements_; }
188
0
  FieldType keyType() const override { return key_type_; }
189
0
  FieldType valueType() const override { return elem_type_; }
190
191
  void delegateComplete() override;
192
193
protected:
194
  // ThriftValue
195
0
  const void* getValue() const override { return this; };
196
197
  FieldType key_type_{FieldType::Stop};
198
  FieldType elem_type_{FieldType::Stop};
199
  uint32_t remaining_{0};
200
  ThriftValuePtrPairList elements_; // maintain original order
201
};
202
203
/**
204
 * ThriftValueImpl represents primitive Thrift types, including strings.
205
 */
206
class ThriftValueImpl : public ThriftValueBase {
207
public:
208
0
  ThriftValueImpl(ThriftBase* parent, FieldType value_type) : ThriftValueBase(parent, value_type) {}
209
210
  // DecoderEventHandler
211
  FilterStatus boolValue(bool& value) override;
212
  FilterStatus byteValue(uint8_t& value) override;
213
  FilterStatus int16Value(int16_t& value) override;
214
  FilterStatus int32Value(int32_t& value) override;
215
  FilterStatus int64Value(int64_t& value) override;
216
  FilterStatus doubleValue(double& value) override;
217
  FilterStatus stringValue(absl::string_view value) override;
218
219
protected:
220
  // ThriftValue
221
  const void* getValue() const override;
222
223
private:
224
  union {
225
    bool bool_value_;
226
    uint8_t byte_value_;
227
    int16_t int16_value_;
228
    int32_t int32_value_;
229
    int64_t int64_value_;
230
    double double_value_;
231
  };
232
  std::string string_value_;
233
};
234
235
/**
236
 * ThriftObjectImpl is a generic representation of a Thrift struct.
237
 */
238
class ThriftObjectImpl : public ThriftObject,
239
                         public ThriftStructValueImpl,
240
                         public DecoderCallbacks {
241
public:
242
  ThriftObjectImpl(Transport& transport, Protocol& protocol);
243
244
  // DecoderCallbacks
245
0
  DecoderEventHandler& newDecoderEventHandler() override { return *this; }
246
0
  FilterStatus transportEnd() override {
247
0
    complete_ = true;
248
0
    return FilterStatus::Continue;
249
0
  }
250
0
  bool passthroughEnabled() const override { return false; }
251
0
  bool isRequest() const override { return false; }
252
0
  bool headerKeysPreserveCase() const override { return false; }
253
254
  // ThriftObject
255
  bool onData(Buffer::Instance& buffer) override;
256
257
  // ThriftStruct
258
0
  const ThriftFieldPtrList& fields() const override { return ThriftStructValueImpl::fields(); }
259
260
private:
261
  DecoderPtr decoder_;
262
  bool complete_{false};
263
};
264
265
} // namespace ThriftProxy
266
} // namespace NetworkFilters
267
} // namespace Extensions
268
} // namespace Envoy