-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathhttp_specific_formatter.h
More file actions
256 lines (207 loc) · 9.04 KB
/
http_specific_formatter.h
File metadata and controls
256 lines (207 loc) · 9.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#pragma once
#include <bitset>
#include <functional>
#include <list>
#include <regex>
#include <string>
#include <vector>
#include "envoy/formatter/substitution_formatter.h"
#include "envoy/stream_info/stream_info.h"
#include "source/common/common/utility.h"
#include "source/common/formatter/substitution_format_utility.h"
#include "absl/container/flat_hash_map.h"
#include "absl/types/optional.h"
namespace Envoy {
namespace Formatter {
/**
* FormatterProvider for local_reply_body. It returns the string from `local_reply_body` argument.
*/
class LocalReplyBodyFormatter : public FormatterProvider {
public:
LocalReplyBodyFormatter() = default;
// Formatter::format
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
/**
* FormatterProvider for access log type. It returns the string from `access_log_type` argument.
*/
class AccessLogTypeFormatter : public FormatterProvider {
public:
AccessLogTypeFormatter() = default;
// Formatter::format
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
class HeaderFormatter {
public:
HeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
absl::optional<size_t> max_length);
protected:
absl::optional<std::string> format(OptRef<const Http::HeaderMap> headers) const;
Protobuf::Value formatValue(OptRef<const Http::HeaderMap> headers) const;
private:
const Http::HeaderEntry* findHeader(OptRef<const Http::HeaderMap> headers) const;
Http::LowerCaseString main_header_;
Http::LowerCaseString alternative_header_;
absl::optional<size_t> max_length_;
};
/**
* FormatterProvider for headers byte size.
*/
class HeadersByteSizeFormatter : public FormatterProvider {
public:
// TODO(taoxuy): Add RequestTrailers here.
enum class HeaderType { RequestHeaders, ResponseHeaders, ResponseTrailers };
HeadersByteSizeFormatter(const HeaderType header_type);
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
private:
uint64_t extractHeadersByteSize(OptRef<const Http::RequestHeaderMap> request_headers,
OptRef<const Http::ResponseHeaderMap> response_headers,
OptRef<const Http::ResponseTrailerMap> response_trailers) const;
const HeaderType header_type_{};
};
/**
* FormatterProvider for request headers.
*/
class RequestHeaderFormatter : public FormatterProvider, HeaderFormatter {
public:
RequestHeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
/**
* FormatterProvider for response headers.
*/
class ResponseHeaderFormatter : public FormatterProvider, HeaderFormatter {
public:
ResponseHeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
/**
* FormatterProvider for response trailers.
*/
class ResponseTrailerFormatter : public FormatterProvider, HeaderFormatter {
public:
ResponseTrailerFormatter(absl::string_view main_header, absl::string_view alternative_header,
absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
/**
* FormatterProvider for trace ID.
*/
class TraceIDFormatter : public FormatterProvider {
public:
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
};
class GrpcStatusFormatter : public FormatterProvider, HeaderFormatter {
public:
enum Format {
CamelString,
SnakeString,
Number,
};
GrpcStatusFormatter(const std::string& main_header, const std::string& alternative_header,
absl::optional<size_t> max_length, Format format);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
static Format parseFormat(absl::string_view format);
private:
const Format format_;
};
class QueryParameterFormatter : public FormatterProvider {
public:
QueryParameterFormatter(absl::string_view parameter_key, absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
private:
const std::string parameter_key_;
absl::optional<size_t> max_length_;
};
class QueryParametersFormatter : public FormatterProvider {
public:
static absl::StatusOr<FormatterProviderPtr>
create(absl::string_view decoding, absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> decoding(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
QueryParametersFormatter(bool decode_bool, absl::optional<size_t> max_length)
: decode_bool_(decode_bool), max_length_(max_length) {}
private:
const bool decode_bool_{};
absl::optional<size_t> max_length_;
};
class PathFormatter : public FormatterProvider {
public:
enum PathFormatterOption {
OriginalPathOrPath,
PathOnly,
OriginalPathOnly,
};
static absl::StatusOr<FormatterProviderPtr>
create(absl::string_view with_query, absl::string_view option, absl::optional<size_t> max_length);
// FormatterProvider
absl::optional<std::string> format(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
Protobuf::Value formatValue(const Context& context,
const StreamInfo::StreamInfo& stream_info) const override;
PathFormatter(bool with_query, PathFormatterOption option, absl::optional<size_t> max_length)
: with_query_(with_query), option_(option), max_length_(max_length) {}
private:
const bool with_query_{};
const PathFormatterOption option_{};
absl::optional<size_t> max_length_;
};
class BuiltInHttpCommandParser : public CommandParser {
public:
BuiltInHttpCommandParser() = default;
// CommandParser
FormatterProviderPtr parse(absl::string_view command, absl::string_view subcommand,
absl::optional<size_t> max_length) const override;
private:
using FormatterProviderCreateFunc =
std::function<FormatterProviderPtr(absl::string_view, absl::optional<size_t>)>;
using FormatterProviderLookupTbl =
absl::flat_hash_map<absl::string_view, std::pair<CommandSyntaxChecker::CommandSyntaxFlags,
FormatterProviderCreateFunc>>;
static const FormatterProviderLookupTbl& getKnownFormatters();
};
class DefaultBuiltInHttpCommandParserFactory : public BuiltInCommandParserFactory {
public:
std::string name() const override;
CommandParserPtr createCommandParser() const override;
};
DECLARE_FACTORY(DefaultBuiltInHttpCommandParserFactory);
} // namespace Formatter
} // namespace Envoy