/proc/self/cwd/source/extensions/filters/network/thrift_proxy/thrift_object.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <list> |
4 | | #include <memory> |
5 | | |
6 | | #include "envoy/buffer/buffer.h" |
7 | | #include "envoy/common/exception.h" |
8 | | |
9 | | #include "source/common/common/utility.h" |
10 | | #include "source/extensions/filters/network/thrift_proxy/thrift.h" |
11 | | |
12 | | namespace Envoy { |
13 | | namespace Extensions { |
14 | | namespace NetworkFilters { |
15 | | namespace ThriftProxy { |
16 | | |
17 | | class ThriftBase; |
18 | | |
19 | | /** |
20 | | * ThriftValue is a field or container (list, set, or map) element. |
21 | | */ |
22 | | class ThriftValue { |
23 | | public: |
24 | 0 | virtual ~ThriftValue() = default; |
25 | | |
26 | | /** |
27 | | * @return FieldType the type of this value |
28 | | */ |
29 | | virtual FieldType type() const PURE; |
30 | | |
31 | | /** |
32 | | * @return const T& pointer to the value, provided that it can be cast to the given type |
33 | | * @throw EnvoyException if the type T does not match the type |
34 | | */ |
35 | 0 | template <typename T> const T& getValueTyped() const { |
36 | | // Use the Traits template to determine what FieldType the value must have to be cast to T |
37 | | // and throw if the value's type doesn't match. |
38 | 0 | FieldType expected_field_type = Traits<T>::getFieldType(); |
39 | 0 | if (expected_field_type != type()) { |
40 | 0 | ExceptionUtil::throwEnvoyException(fmt::format("expected field type {}, got {}", |
41 | 0 | static_cast<int>(expected_field_type), |
42 | 0 | static_cast<int>(type()))); |
43 | 0 | } |
44 | |
|
45 | 0 | return *static_cast<const T*>(getValue()); |
46 | 0 | } Unexecuted instantiation: long const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<long>() const Unexecuted instantiation: bool const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<bool>() const Unexecuted instantiation: Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftStructValue const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftStructValue>() const Unexecuted instantiation: Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftListValue const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftListValue>() const Unexecuted instantiation: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >() const Unexecuted instantiation: int const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<int>() const Unexecuted instantiation: short const& Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::getValueTyped<short>() const |
47 | | |
48 | | protected: |
49 | | /** |
50 | | * @return void* pointing to the underlying value, to be dynamically cast in getValueTyped |
51 | | */ |
52 | | virtual const void* getValue() const PURE; |
53 | | |
54 | | private: |
55 | | /** |
56 | | * Traits allows getValueTyped() to enforce that the field type is being cast to the desired type. |
57 | | */ |
58 | | template <typename T> class Traits { |
59 | | public: |
60 | | // Compilation failures where T does not have a member getFieldType typically mean that |
61 | | // getValueTyped was called with a type T that is not used to encode Thrift values. |
62 | | // The specializations below encode the valid types for Thrift primitive types. |
63 | 0 | static FieldType getFieldType() { return T::getFieldType(); } Unexecuted instantiation: Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::Traits<Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftStructValue>::getFieldType() Unexecuted instantiation: Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftValue::Traits<Envoy::Extensions::NetworkFilters::ThriftProxy::ThriftListValue>::getFieldType() |
64 | | }; |
65 | | }; |
66 | | |
67 | | // Explicit specializations of ThriftValue::Types for primitive types. |
68 | | template <> class ThriftValue::Traits<bool> { |
69 | | public: |
70 | 0 | static FieldType getFieldType() { return FieldType::Bool; } |
71 | | }; |
72 | | |
73 | | template <> class ThriftValue::Traits<uint8_t> { |
74 | | public: |
75 | 0 | static FieldType getFieldType() { return FieldType::Byte; } |
76 | | }; |
77 | | |
78 | | template <> class ThriftValue::Traits<int16_t> { |
79 | | public: |
80 | 0 | static FieldType getFieldType() { return FieldType::I16; } |
81 | | }; |
82 | | |
83 | | template <> class ThriftValue::Traits<int32_t> { |
84 | | public: |
85 | 0 | static FieldType getFieldType() { return FieldType::I32; } |
86 | | }; |
87 | | |
88 | | template <> class ThriftValue::Traits<int64_t> { |
89 | | public: |
90 | 0 | static FieldType getFieldType() { return FieldType::I64; } |
91 | | }; |
92 | | |
93 | | template <> class ThriftValue::Traits<double> { |
94 | | public: |
95 | 0 | static FieldType getFieldType() { return FieldType::Double; } |
96 | | }; |
97 | | |
98 | | template <> class ThriftValue::Traits<std::string> { |
99 | | public: |
100 | 0 | static FieldType getFieldType() { return FieldType::String; } |
101 | | }; |
102 | | |
103 | | using ThriftValuePtr = std::unique_ptr<ThriftValue>; |
104 | | using ThriftValuePtrList = std::list<ThriftValuePtr>; |
105 | | using ThriftValuePtrPairList = std::list<std::pair<ThriftValuePtr, ThriftValuePtr>>; |
106 | | |
107 | | /** |
108 | | * ThriftField is a field within a ThriftStruct. |
109 | | */ |
110 | | class ThriftField { |
111 | | public: |
112 | 0 | virtual ~ThriftField() = default; |
113 | | |
114 | | /** |
115 | | * @return FieldType this field's type |
116 | | */ |
117 | | virtual FieldType fieldType() const PURE; |
118 | | |
119 | | /** |
120 | | * @return int16_t the field's identifier |
121 | | */ |
122 | | virtual int16_t fieldId() const PURE; |
123 | | |
124 | | /** |
125 | | * @return const ThriftValue& containing the field's value |
126 | | */ |
127 | | virtual const ThriftValue& getValue() const PURE; |
128 | | }; |
129 | | |
130 | | using ThriftFieldPtr = std::unique_ptr<ThriftField>; |
131 | | using ThriftFieldPtrList = std::list<ThriftFieldPtr>; |
132 | | |
133 | | /** |
134 | | * ThriftListValue is an ordered list of ThriftValues. |
135 | | */ |
136 | | class ThriftListValue { |
137 | | public: |
138 | 0 | virtual ~ThriftListValue() = default; |
139 | | |
140 | | /** |
141 | | * @return const ThriftValuePtrList& containing the ThriftValues that comprise the list |
142 | | */ |
143 | | virtual const ThriftValuePtrList& elements() const PURE; |
144 | | |
145 | | /** |
146 | | * @return FieldType of the underlying elements |
147 | | */ |
148 | | virtual FieldType elementType() const PURE; |
149 | | |
150 | | /** |
151 | | * Used by ThriftValue::Traits to enforce type safety. |
152 | | */ |
153 | 0 | static FieldType getFieldType() { return FieldType::List; } |
154 | | }; |
155 | | |
156 | | /** |
157 | | * ThriftSetValue is a set of ThriftValues, maintained in their original order. |
158 | | */ |
159 | | class ThriftSetValue { |
160 | | public: |
161 | 0 | virtual ~ThriftSetValue() = default; |
162 | | |
163 | | /** |
164 | | * @return const ThriftValuePtrList& containing the ThriftValues that comprise the set |
165 | | */ |
166 | | virtual const ThriftValuePtrList& elements() const PURE; |
167 | | |
168 | | /** |
169 | | * @return FieldType of the underlying elements |
170 | | */ |
171 | | virtual FieldType elementType() const PURE; |
172 | | |
173 | | /** |
174 | | * Used by ThriftValue::Traits to enforce type safety. |
175 | | */ |
176 | 0 | static FieldType getFieldType() { return FieldType::Set; } |
177 | | }; |
178 | | |
179 | | /** |
180 | | * ThriftMapValue is a map of pairs of ThriftValues, maintained in their original order. |
181 | | */ |
182 | | class ThriftMapValue { |
183 | | public: |
184 | 0 | virtual ~ThriftMapValue() = default; |
185 | | |
186 | | /** |
187 | | * @return const ThriftValuePtrPairList& containing the ThriftValue key-value pairs that comprise |
188 | | * the map. |
189 | | */ |
190 | | virtual const ThriftValuePtrPairList& elements() const PURE; |
191 | | |
192 | | /** |
193 | | * @return FieldType of the underlying keys |
194 | | */ |
195 | | virtual FieldType keyType() const PURE; |
196 | | |
197 | | /** |
198 | | * @return FieldType of the underlying values |
199 | | */ |
200 | | virtual FieldType valueType() const PURE; |
201 | | |
202 | | /** |
203 | | * Used by ThriftValue::Traits to enforce type safety. |
204 | | */ |
205 | 0 | static FieldType getFieldType() { return FieldType::Map; } |
206 | | }; |
207 | | |
208 | | /** |
209 | | * ThriftStructValue is a sequence of ThriftFields. |
210 | | */ |
211 | | class ThriftStructValue { |
212 | | public: |
213 | 0 | virtual ~ThriftStructValue() = default; |
214 | | |
215 | | /** |
216 | | * @return const ThriftFieldPtrList& containing the ThriftFields that comprise the struct. |
217 | | */ |
218 | | virtual const ThriftFieldPtrList& fields() const PURE; |
219 | | |
220 | | /** |
221 | | * Used by ThriftValue::Traits to enforce type safety. |
222 | | */ |
223 | 0 | static FieldType getFieldType() { return FieldType::Struct; } |
224 | | }; |
225 | | |
226 | | /** |
227 | | * ThriftObject is a ThriftStructValue that can be read from a Buffer::Instance. |
228 | | */ |
229 | | class ThriftObject : public ThriftStructValue { |
230 | | public: |
231 | | ~ThriftObject() override = default; |
232 | | |
233 | | /* |
234 | | * Consumes bytes from the buffer until a single complete Thrift struct has been consumed. |
235 | | * @param buffer starting with a Thrift struct |
236 | | * @return true when a single complete struct has been consumed; false if more data is needed to |
237 | | * complete decoding |
238 | | * @throw EnvoyException if the struct is invalid |
239 | | */ |
240 | | virtual bool onData(Buffer::Instance& buffer) PURE; |
241 | | }; |
242 | | |
243 | | using ThriftObjectPtr = std::unique_ptr<ThriftObject>; |
244 | | |
245 | | } // namespace ThriftProxy |
246 | | } // namespace NetworkFilters |
247 | | } // namespace Extensions |
248 | | } // namespace Envoy |