/proc/self/cwd/source/extensions/filters/network/mongo_proxy/codec_impl.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstdint> |
4 | | #include <list> |
5 | | #include <string> |
6 | | #include <vector> |
7 | | |
8 | | #include "source/common/common/logger.h" |
9 | | #include "source/extensions/filters/network/mongo_proxy/codec.h" |
10 | | |
11 | | namespace Envoy { |
12 | | namespace Extensions { |
13 | | namespace NetworkFilters { |
14 | | namespace MongoProxy { |
15 | | |
16 | | class MessageImpl : public virtual Message { |
17 | | public: |
18 | | MessageImpl(int32_t request_id, uint32_t response_to) |
19 | 17.3k | : request_id_(request_id), response_to_(response_to) {} |
20 | | |
21 | | virtual void fromBuffer(uint32_t message_length, Buffer::Instance& data) PURE; |
22 | | |
23 | | // Mongo::Message |
24 | 9.75k | int32_t requestId() const override { return request_id_; } |
25 | 3.35k | int32_t responseTo() const override { return response_to_; } |
26 | | |
27 | | protected: |
28 | | std::string documentListToString(const std::list<Bson::DocumentSharedPtr>& documents) const; |
29 | | |
30 | | const int32_t request_id_; |
31 | | const int32_t response_to_; |
32 | | }; |
33 | | |
34 | | class GetMoreMessageImpl : public MessageImpl, |
35 | | public GetMoreMessage, |
36 | | Logger::Loggable<Logger::Id::mongo> { |
37 | | public: |
38 | | using MessageImpl::MessageImpl; |
39 | | |
40 | | // MessageImpl |
41 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
42 | | |
43 | | // Mongo::Message |
44 | | std::string toString(bool full) const override; |
45 | | |
46 | | // Mongo::GetMoreMessage |
47 | | bool operator==(const GetMoreMessage& rhs) const override; |
48 | 0 | bool operator==(const GetMoreMessageImpl& rhs) const { |
49 | 0 | return operator==(static_cast<const GetMoreMessage&>(rhs)); |
50 | 0 | } |
51 | 0 | const std::string& fullCollectionName() const override { return full_collection_name_; } |
52 | 0 | void fullCollectionName(const std::string& name) override { full_collection_name_ = name; } |
53 | 0 | int32_t numberToReturn() const override { return number_to_return_; } |
54 | 0 | void numberToReturn(int32_t to_return) override { number_to_return_ = to_return; } |
55 | 0 | int64_t cursorId() const override { return cursor_id_; } |
56 | 0 | void cursorId(int64_t cursor_id) override { cursor_id_ = cursor_id; } |
57 | | |
58 | | private: |
59 | | std::string full_collection_name_; |
60 | | int32_t number_to_return_{}; |
61 | | int64_t cursor_id_{}; |
62 | | }; |
63 | | |
64 | | class InsertMessageImpl : public MessageImpl, |
65 | | public InsertMessage, |
66 | | Logger::Loggable<Logger::Id::mongo> { |
67 | | public: |
68 | | using MessageImpl::MessageImpl; |
69 | | |
70 | | // MessageImpl |
71 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
72 | | |
73 | | // Mongo::Message |
74 | | std::string toString(bool full) const override; |
75 | | |
76 | | // Mongo::InsertMessage |
77 | | bool operator==(const InsertMessage& rhs) const override; |
78 | 0 | bool operator==(const InsertMessageImpl& rhs) const { |
79 | 0 | return operator==(static_cast<const InsertMessage&>(rhs)); |
80 | 0 | } |
81 | 0 | int32_t flags() const override { return flags_; } |
82 | 0 | void flags(int32_t flags) override { flags_ = flags; } |
83 | 318 | const std::string& fullCollectionName() const override { return full_collection_name_; } |
84 | 0 | void fullCollectionName(const std::string& name) override { full_collection_name_ = name; } |
85 | 0 | const std::list<Bson::DocumentSharedPtr>& documents() const override { return documents_; } |
86 | 0 | std::list<Bson::DocumentSharedPtr>& documents() override { return documents_; } |
87 | | |
88 | | private: |
89 | | int32_t flags_{}; |
90 | | std::string full_collection_name_; |
91 | | std::list<Bson::DocumentSharedPtr> documents_; |
92 | | }; |
93 | | |
94 | | class KillCursorsMessageImpl : public MessageImpl, |
95 | | public KillCursorsMessage, |
96 | | Logger::Loggable<Logger::Id::mongo> { |
97 | | public: |
98 | | using MessageImpl::MessageImpl; |
99 | | |
100 | | // MessageImpl |
101 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
102 | | |
103 | | // Mongo::Message |
104 | | std::string toString(bool full) const override; |
105 | | |
106 | | // Mongo::KillCursorsMessage |
107 | | bool operator==(const KillCursorsMessage& rhs) const override; |
108 | 0 | bool operator==(const KillCursorsMessageImpl& rhs) const { |
109 | 0 | return operator==(static_cast<const KillCursorsMessage&>(rhs)); |
110 | 0 | } |
111 | 0 | int32_t numberOfCursorIds() const override { return number_of_cursor_ids_; } |
112 | 0 | void numberOfCursorIds(int32_t number_of_cursor_ids) override { |
113 | 0 | number_of_cursor_ids_ = number_of_cursor_ids; |
114 | 0 | } |
115 | 0 | const std::vector<int64_t>& cursorIds() const override { return cursor_ids_; } |
116 | 0 | void cursorIds(std::vector<int64_t>&& cursor_ids) override { |
117 | 0 | cursor_ids_ = std::move(cursor_ids); |
118 | 0 | } |
119 | | |
120 | | private: |
121 | | int32_t number_of_cursor_ids_{}; |
122 | | std::vector<int64_t> cursor_ids_; |
123 | | }; |
124 | | |
125 | | class QueryMessageImpl : public MessageImpl, |
126 | | public QueryMessage, |
127 | | Logger::Loggable<Logger::Id::mongo> { |
128 | | public: |
129 | | using MessageImpl::MessageImpl; |
130 | | |
131 | | // MessageImpl |
132 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
133 | | |
134 | | // Mongo::Message |
135 | | std::string toString(bool full) const override; |
136 | | |
137 | | // Mongo::QueryMessage |
138 | | bool operator==(const QueryMessage& rhs) const override; |
139 | 0 | bool operator==(const QueryMessageImpl& rhs) const { |
140 | 0 | return operator==(static_cast<const QueryMessage&>(rhs)); |
141 | 0 | } |
142 | 39.0k | int32_t flags() const override { return flags_; } |
143 | 0 | void flags(int32_t flags) override { flags_ = flags; } |
144 | 18.1k | const std::string& fullCollectionName() const override { return full_collection_name_; } |
145 | 0 | void fullCollectionName(const std::string& name) override { full_collection_name_ = name; } |
146 | 0 | int32_t numberToSkip() const override { return number_to_skip_; } |
147 | 0 | void numberToSkip(int32_t skip) override { number_to_skip_ = skip; } |
148 | 0 | int32_t numberToReturn() const override { return number_to_return_; } |
149 | 0 | void numberToReturn(int32_t to_return) override { number_to_return_ = to_return; } |
150 | 41.9k | const Bson::Document* query() const override { return query_.get(); } |
151 | 0 | void query(Bson::DocumentSharedPtr&& query) override { query_ = std::move(query); } |
152 | 0 | const Bson::Document* returnFieldsSelector() const override { |
153 | 0 | return return_fields_selector_.get(); |
154 | 0 | } |
155 | 0 | void returnFieldsSelector(Bson::DocumentSharedPtr&& fields) override { |
156 | 0 | return_fields_selector_ = std::move(fields); |
157 | 0 | } |
158 | | |
159 | | private: |
160 | | int32_t flags_{}; |
161 | | std::string full_collection_name_; |
162 | | int32_t number_to_skip_{}; |
163 | | int32_t number_to_return_{}; |
164 | | Bson::DocumentSharedPtr query_; |
165 | | Bson::DocumentSharedPtr return_fields_selector_; |
166 | | }; |
167 | | |
168 | | class ReplyMessageImpl : public MessageImpl, |
169 | | public ReplyMessage, |
170 | | Logger::Loggable<Logger::Id::mongo> { |
171 | | public: |
172 | | using MessageImpl::MessageImpl; |
173 | | |
174 | | // MessageImpl |
175 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
176 | | |
177 | | // Mongo::Message |
178 | | std::string toString(bool full) const override; |
179 | | |
180 | | // Mongo::ReplyMessage |
181 | | bool operator==(const ReplyMessage& rhs) const override; |
182 | 0 | bool operator==(const ReplyMessageImpl& rhs) const { |
183 | 0 | return operator==(static_cast<const ReplyMessage&>(rhs)); |
184 | 0 | } |
185 | 5.30k | int32_t flags() const override { return flags_; } |
186 | 0 | void flags(int32_t flags) override { flags_ = flags; } |
187 | 2.65k | int64_t cursorId() const override { return cursor_id_; } |
188 | 0 | void cursorId(int64_t cursor_id) override { cursor_id_ = cursor_id; } |
189 | 0 | int32_t startingFrom() const override { return starting_from_; } |
190 | 0 | void startingFrom(int32_t starting_from) override { starting_from_ = starting_from; } |
191 | 0 | int32_t numberReturned() const override { return number_returned_; } |
192 | 0 | void numberReturned(int32_t number_returned) override { number_returned_ = number_returned; } |
193 | 2.75k | const std::list<Bson::DocumentSharedPtr>& documents() const override { return documents_; } |
194 | 0 | std::list<Bson::DocumentSharedPtr>& documents() override { return documents_; } |
195 | | |
196 | | private: |
197 | | int32_t flags_{}; |
198 | | int64_t cursor_id_{}; |
199 | | int32_t starting_from_{}; |
200 | | int32_t number_returned_{}; |
201 | | std::list<Bson::DocumentSharedPtr> documents_; |
202 | | }; |
203 | | |
204 | | // OP_COMMAND message. |
205 | | class CommandMessageImpl : public MessageImpl, |
206 | | public CommandMessage, |
207 | | Logger::Loggable<Logger::Id::mongo> { |
208 | | public: |
209 | | using MessageImpl::MessageImpl; |
210 | | |
211 | | // MessageImpl. |
212 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
213 | | std::string toString(bool full) const override; |
214 | | |
215 | | // CommandMessageImpl accessors. |
216 | | bool operator==(const CommandMessage& rhs) const override; |
217 | 0 | bool operator==(const CommandMessageImpl& rhs) const { |
218 | 0 | return operator==(static_cast<const CommandMessage&>(rhs)); |
219 | 0 | } |
220 | 0 | std::string database() const override { return database_; } |
221 | 0 | void database(std::string database) override { database_ = database; } |
222 | 0 | std::string commandName() const override { return command_name_; } |
223 | 0 | void commandName(std::string command_name) override { command_name_ = command_name; } |
224 | 0 | const Bson::Document* metadata() const override { return metadata_.get(); } |
225 | 0 | void metadata(Bson::DocumentSharedPtr&& metadata) override { metadata_ = std::move(metadata); } |
226 | 0 | const Bson::Document* commandArgs() const override { return command_args_.get(); } |
227 | 0 | void commandArgs(Bson::DocumentSharedPtr&& command_args) override { |
228 | 0 | command_args_ = std::move(command_args); |
229 | 0 | } |
230 | 0 | const std::list<Bson::DocumentSharedPtr>& inputDocs() const override { return input_docs_; } |
231 | 0 | std::list<Bson::DocumentSharedPtr>& inputDocs() override { return input_docs_; } |
232 | | |
233 | | private: |
234 | | std::string database_; |
235 | | std::string command_name_; |
236 | | Bson::DocumentSharedPtr metadata_; |
237 | | Bson::DocumentSharedPtr command_args_; |
238 | | std::list<Bson::DocumentSharedPtr> input_docs_; |
239 | | }; |
240 | | |
241 | | // OP_COMMANDREPLY message. |
242 | | class CommandReplyMessageImpl : public MessageImpl, |
243 | | public CommandReplyMessage, |
244 | | Logger::Loggable<Logger::Id::mongo> { |
245 | | public: |
246 | | using MessageImpl::MessageImpl; |
247 | | |
248 | | // MessageImpl. |
249 | | void fromBuffer(uint32_t message_length, Buffer::Instance& data) override; |
250 | | std::string toString(bool full) const override; |
251 | | |
252 | | // CommandMessageReplyImpl accessors. |
253 | | bool operator==(const CommandReplyMessage& rhs) const override; |
254 | 0 | bool operator==(const CommandReplyMessageImpl& rhs) const { |
255 | 0 | return operator==(static_cast<const CommandReplyMessage&>(rhs)); |
256 | 0 | } |
257 | 0 | const Bson::Document* metadata() const override { return metadata_.get(); } |
258 | 0 | void metadata(Bson::DocumentSharedPtr&& metadata) override { metadata_ = std::move(metadata); } |
259 | 0 | const Bson::Document* commandReply() const override { return command_reply_.get(); } |
260 | 0 | void commandReply(Bson::DocumentSharedPtr&& command_reply) override { |
261 | 0 | command_reply_ = std::move(command_reply); |
262 | 0 | } |
263 | 0 | const std::list<Bson::DocumentSharedPtr>& outputDocs() const override { return output_docs_; } |
264 | 0 | std::list<Bson::DocumentSharedPtr>& outputDocs() override { return output_docs_; } |
265 | | |
266 | | private: |
267 | | Bson::DocumentSharedPtr metadata_; |
268 | | Bson::DocumentSharedPtr command_reply_; |
269 | | std::list<Bson::DocumentSharedPtr> output_docs_; |
270 | | }; |
271 | | |
272 | | class DecoderImpl : public Decoder, Logger::Loggable<Logger::Id::mongo> { |
273 | | public: |
274 | 2.14k | DecoderImpl(DecoderCallbacks& callbacks) : callbacks_(callbacks) {} |
275 | | |
276 | | // Mongo::Decoder |
277 | | void onData(Buffer::Instance& data) override; |
278 | | |
279 | | private: |
280 | | bool decode(Buffer::Instance& data); |
281 | | |
282 | | DecoderCallbacks& callbacks_; |
283 | | }; |
284 | | |
285 | | class EncoderImpl : public Encoder, Logger::Loggable<Logger::Id::mongo> { |
286 | | public: |
287 | 0 | EncoderImpl(Buffer::Instance& output) : output_(output) {} |
288 | | |
289 | | // Mongo::Encoder |
290 | | void encodeGetMore(const GetMoreMessage& message) override; |
291 | | void encodeInsert(const InsertMessage& message) override; |
292 | | void encodeKillCursors(const KillCursorsMessage& message) override; |
293 | | void encodeQuery(const QueryMessage& message) override; |
294 | | void encodeReply(const ReplyMessage& message) override; |
295 | | void encodeCommand(const CommandMessage& message) override; |
296 | | void encodeCommandReply(const CommandReplyMessage& message) override; |
297 | | |
298 | | private: |
299 | | void encodeCommonHeader(int32_t total_size, const Message& message, Message::OpCode op); |
300 | | |
301 | | Buffer::Instance& output_; |
302 | | }; |
303 | | |
304 | | } // namespace MongoProxy |
305 | | } // namespace NetworkFilters |
306 | | } // namespace Extensions |
307 | | } // namespace Envoy |