1
#pragma once
2

            
3
#include <bitset>
4
#include <functional>
5
#include <list>
6
#include <regex>
7
#include <string>
8
#include <vector>
9

            
10
#include "envoy/formatter/substitution_formatter.h"
11
#include "envoy/stream_info/stream_info.h"
12

            
13
#include "source/common/common/utility.h"
14
#include "source/common/formatter/substitution_format_utility.h"
15

            
16
#include "absl/container/flat_hash_map.h"
17
#include "absl/types/optional.h"
18

            
19
namespace Envoy {
20
namespace Formatter {
21

            
22
/**
23
 * FormatterProvider for local_reply_body. It returns the string from `local_reply_body` argument.
24
 */
25
class LocalReplyBodyFormatter : public FormatterProvider {
26
public:
27
95
  LocalReplyBodyFormatter() = default;
28

            
29
  // Formatter::format
30
  absl::optional<std::string> format(const Context& context,
31
                                     const StreamInfo::StreamInfo& stream_info) const override;
32
  Protobuf::Value formatValue(const Context& context,
33
                              const StreamInfo::StreamInfo& stream_info) const override;
34
};
35

            
36
/**
37
 * FormatterProvider for access log type. It returns the string from `access_log_type` argument.
38
 */
39
class AccessLogTypeFormatter : public FormatterProvider {
40
public:
41
133
  AccessLogTypeFormatter() = default;
42

            
43
  // Formatter::format
44
  absl::optional<std::string> format(const Context& context,
45
                                     const StreamInfo::StreamInfo& stream_info) const override;
46
  Protobuf::Value formatValue(const Context& context,
47
                              const StreamInfo::StreamInfo& stream_info) const override;
48
};
49

            
50
class HeaderFormatter {
51
public:
52
  HeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
53
                  absl::optional<size_t> max_length);
54

            
55
protected:
56
  absl::optional<std::string> format(OptRef<const Http::HeaderMap> headers) const;
57
  Protobuf::Value formatValue(OptRef<const Http::HeaderMap> headers) const;
58

            
59
private:
60
  const Http::HeaderEntry* findHeader(OptRef<const Http::HeaderMap> headers) const;
61

            
62
  Http::LowerCaseString main_header_;
63
  Http::LowerCaseString alternative_header_;
64
  absl::optional<size_t> max_length_;
65
};
66

            
67
/**
68
 * FormatterProvider for headers byte size.
69
 */
70
class HeadersByteSizeFormatter : public FormatterProvider {
71
public:
72
  // TODO(taoxuy): Add RequestTrailers here.
73
  enum class HeaderType { RequestHeaders, ResponseHeaders, ResponseTrailers };
74

            
75
  HeadersByteSizeFormatter(const HeaderType header_type);
76

            
77
  absl::optional<std::string> format(const Context& context,
78
                                     const StreamInfo::StreamInfo& stream_info) const override;
79
  Protobuf::Value formatValue(const Context& context,
80
                              const StreamInfo::StreamInfo& stream_info) const override;
81

            
82
private:
83
  uint64_t extractHeadersByteSize(OptRef<const Http::RequestHeaderMap> request_headers,
84
                                  OptRef<const Http::ResponseHeaderMap> response_headers,
85
                                  OptRef<const Http::ResponseTrailerMap> response_trailers) const;
86
  const HeaderType header_type_{};
87
};
88

            
89
/**
90
 * FormatterProvider for request headers.
91
 */
92
class RequestHeaderFormatter : public FormatterProvider, HeaderFormatter {
93
public:
94
  RequestHeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
95
                         absl::optional<size_t> max_length);
96

            
97
  // FormatterProvider
98
  absl::optional<std::string> format(const Context& context,
99
                                     const StreamInfo::StreamInfo& stream_info) const override;
100
  Protobuf::Value formatValue(const Context& context,
101
                              const StreamInfo::StreamInfo& stream_info) const override;
102
};
103

            
104
/**
105
 * FormatterProvider for response headers.
106
 */
107
class ResponseHeaderFormatter : public FormatterProvider, HeaderFormatter {
108
public:
109
  ResponseHeaderFormatter(absl::string_view main_header, absl::string_view alternative_header,
110
                          absl::optional<size_t> max_length);
111

            
112
  // FormatterProvider
113
  absl::optional<std::string> format(const Context& context,
114
                                     const StreamInfo::StreamInfo& stream_info) const override;
115
  Protobuf::Value formatValue(const Context& context,
116
                              const StreamInfo::StreamInfo& stream_info) const override;
117
};
118

            
119
/**
120
 * FormatterProvider for response trailers.
121
 */
122
class ResponseTrailerFormatter : public FormatterProvider, HeaderFormatter {
123
public:
124
  ResponseTrailerFormatter(absl::string_view main_header, absl::string_view alternative_header,
125
                           absl::optional<size_t> max_length);
126

            
127
  // FormatterProvider
128
  absl::optional<std::string> format(const Context& context,
129
                                     const StreamInfo::StreamInfo& stream_info) const override;
130
  Protobuf::Value formatValue(const Context& context,
131
                              const StreamInfo::StreamInfo& stream_info) const override;
132
};
133

            
134
/**
135
 * FormatterProvider for trace ID.
136
 */
137
class TraceIDFormatter : public FormatterProvider {
138
public:
139
  absl::optional<std::string> format(const Context& context,
140
                                     const StreamInfo::StreamInfo& stream_info) const override;
141
  Protobuf::Value formatValue(const Context& context,
142
                              const StreamInfo::StreamInfo& stream_info) const override;
143
};
144

            
145
class GrpcStatusFormatter : public FormatterProvider, HeaderFormatter {
146
public:
147
  enum Format {
148
    CamelString,
149
    SnakeString,
150
    Number,
151
  };
152

            
153
  GrpcStatusFormatter(const std::string& main_header, const std::string& alternative_header,
154
                      absl::optional<size_t> max_length, Format format);
155

            
156
  // FormatterProvider
157
  absl::optional<std::string> format(const Context& context,
158
                                     const StreamInfo::StreamInfo& stream_info) const override;
159
  Protobuf::Value formatValue(const Context& context,
160
                              const StreamInfo::StreamInfo& stream_info) const override;
161

            
162
  static Format parseFormat(absl::string_view format);
163

            
164
private:
165
  const Format format_;
166
};
167

            
168
class QueryParameterFormatter : public FormatterProvider {
169
public:
170
  QueryParameterFormatter(absl::string_view parameter_key, absl::optional<size_t> max_length);
171

            
172
  // FormatterProvider
173
  absl::optional<std::string> format(const Context& context,
174
                                     const StreamInfo::StreamInfo& stream_info) const override;
175
  Protobuf::Value formatValue(const Context& context,
176
                              const StreamInfo::StreamInfo& stream_info) const override;
177

            
178
private:
179
  const std::string parameter_key_;
180
  absl::optional<size_t> max_length_;
181
};
182

            
183
class QueryParametersFormatter : public FormatterProvider {
184
public:
185
  enum DecodeOption {
186
    Original,
187
    Decoded,
188
  };
189

            
190
  static DecodeOption parseDecodeOption(absl::string_view decoding);
191

            
192
  // FormatterProvider
193
  absl::optional<std::string> format(const Context& context,
194
                                     const StreamInfo::StreamInfo& stream_info) const override;
195
  Protobuf::Value formatValue(const Context& context,
196
                              const StreamInfo::StreamInfo& stream_info) const override;
197

            
198
  QueryParametersFormatter(DecodeOption option, absl::optional<size_t> max_length)
199
6
      : option_(option), max_length_(max_length) {}
200

            
201
private:
202
  const DecodeOption option_;
203
  absl::optional<size_t> max_length_;
204
};
205

            
206
class PathFormatter : public FormatterProvider {
207
public:
208
  enum PathFormatterOption {
209
    OriginalPathOrPath,
210
    PathOnly,
211
    OriginalPathOnly,
212
  };
213

            
214
  static absl::StatusOr<FormatterProviderPtr>
215
  create(absl::string_view with_query, absl::string_view option, absl::optional<size_t> max_length);
216

            
217
  // FormatterProvider
218
  absl::optional<std::string> format(const Context& context,
219
                                     const StreamInfo::StreamInfo& stream_info) const override;
220
  Protobuf::Value formatValue(const Context& context,
221
                              const StreamInfo::StreamInfo& stream_info) const override;
222

            
223
  PathFormatter(bool with_query, PathFormatterOption option, absl::optional<size_t> max_length)
224
13
      : with_query_(with_query), option_(option), max_length_(max_length) {}
225

            
226
private:
227
  const bool with_query_{};
228
  const PathFormatterOption option_{};
229
  absl::optional<size_t> max_length_;
230
};
231

            
232
class BuiltInHttpCommandParser : public CommandParser {
233
public:
234
536
  BuiltInHttpCommandParser() = default;
235

            
236
  // CommandParser
237
  FormatterProviderPtr parse(absl::string_view command, absl::string_view subcommand,
238
                             absl::optional<size_t> max_length) const override;
239

            
240
private:
241
  using FormatterProviderCreateFunc =
242
      std::function<FormatterProviderPtr(absl::string_view, absl::optional<size_t>)>;
243

            
244
  using FormatterProviderLookupTbl =
245
      absl::flat_hash_map<absl::string_view, std::pair<CommandSyntaxChecker::CommandSyntaxFlags,
246
                                                       FormatterProviderCreateFunc>>;
247
  static const FormatterProviderLookupTbl& getKnownFormatters();
248
};
249

            
250
class DefaultBuiltInHttpCommandParserFactory : public BuiltInCommandParserFactory {
251
public:
252
  std::string name() const override;
253
  CommandParserPtr createCommandParser() const override;
254
};
255

            
256
DECLARE_FACTORY(DefaultBuiltInHttpCommandParserFactory);
257

            
258
} // namespace Formatter
259
} // namespace Envoy