Line data Source code
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 309 : LocalReplyBodyFormatter() = default;
28 :
29 : // Formatter::format
30 : absl::optional<std::string>
31 : formatWithContext(const HttpFormatterContext& context,
32 : const StreamInfo::StreamInfo& stream_info) const override;
33 : ProtobufWkt::Value
34 : formatValueWithContext(const HttpFormatterContext& context,
35 : const StreamInfo::StreamInfo& stream_info) const override;
36 : };
37 :
38 : /**
39 : * FormatterProvider for access log type. It returns the string from `access_log_type` argument.
40 : */
41 : class AccessLogTypeFormatter : public FormatterProvider {
42 : public:
43 0 : AccessLogTypeFormatter() = default;
44 :
45 : // Formatter::format
46 : absl::optional<std::string>
47 : formatWithContext(const HttpFormatterContext& context,
48 : const StreamInfo::StreamInfo& stream_info) const override;
49 : ProtobufWkt::Value
50 : formatValueWithContext(const HttpFormatterContext& context,
51 : const StreamInfo::StreamInfo& stream_info) const override;
52 : };
53 :
54 : class HeaderFormatter {
55 : public:
56 : HeaderFormatter(const std::string& main_header, const std::string& alternative_header,
57 : absl::optional<size_t> max_length);
58 :
59 : protected:
60 : absl::optional<std::string> format(const Http::HeaderMap& headers) const;
61 : ProtobufWkt::Value formatValue(const Http::HeaderMap& headers) const;
62 :
63 : private:
64 : const Http::HeaderEntry* findHeader(const Http::HeaderMap& headers) const;
65 :
66 : Http::LowerCaseString main_header_;
67 : Http::LowerCaseString alternative_header_;
68 : absl::optional<size_t> max_length_;
69 : };
70 :
71 : /**
72 : * FormatterProvider for headers byte size.
73 : */
74 : class HeadersByteSizeFormatter : public FormatterProvider {
75 : public:
76 : // TODO(taoxuy): Add RequestTrailers here.
77 : enum class HeaderType { RequestHeaders, ResponseHeaders, ResponseTrailers };
78 :
79 : HeadersByteSizeFormatter(const HeaderType header_type);
80 :
81 : absl::optional<std::string>
82 : formatWithContext(const HttpFormatterContext& context,
83 : const StreamInfo::StreamInfo& stream_info) const override;
84 : ProtobufWkt::Value
85 : formatValueWithContext(const HttpFormatterContext& context,
86 : const StreamInfo::StreamInfo& stream_info) const override;
87 :
88 : private:
89 : uint64_t extractHeadersByteSize(const Http::RequestHeaderMap& request_headers,
90 : const Http::ResponseHeaderMap& response_headers,
91 : const Http::ResponseTrailerMap& response_trailers) const;
92 : HeaderType header_type_;
93 : };
94 :
95 : /**
96 : * FormatterProvider for request headers.
97 : */
98 : class RequestHeaderFormatter : public FormatterProvider, HeaderFormatter {
99 : public:
100 : RequestHeaderFormatter(const std::string& main_header, const std::string& alternative_header,
101 : absl::optional<size_t> max_length);
102 :
103 : // FormatterProvider
104 : absl::optional<std::string>
105 : formatWithContext(const HttpFormatterContext& context,
106 : const StreamInfo::StreamInfo& stream_info) const override;
107 : ProtobufWkt::Value
108 : formatValueWithContext(const HttpFormatterContext& context,
109 : const StreamInfo::StreamInfo& stream_info) const override;
110 : };
111 :
112 : /**
113 : * FormatterProvider for response headers.
114 : */
115 : class ResponseHeaderFormatter : public FormatterProvider, HeaderFormatter {
116 : public:
117 : ResponseHeaderFormatter(const std::string& main_header, const std::string& alternative_header,
118 : absl::optional<size_t> max_length);
119 :
120 : // FormatterProvider
121 : absl::optional<std::string>
122 : formatWithContext(const HttpFormatterContext& context,
123 : const StreamInfo::StreamInfo& stream_info) const override;
124 : ProtobufWkt::Value
125 : formatValueWithContext(const HttpFormatterContext& context,
126 : const StreamInfo::StreamInfo& stream_info) const override;
127 : };
128 :
129 : /**
130 : * FormatterProvider for response trailers.
131 : */
132 : class ResponseTrailerFormatter : public FormatterProvider, HeaderFormatter {
133 : public:
134 : ResponseTrailerFormatter(const std::string& main_header, const std::string& alternative_header,
135 : absl::optional<size_t> max_length);
136 :
137 : // FormatterProvider
138 : absl::optional<std::string>
139 : formatWithContext(const HttpFormatterContext& context,
140 : const StreamInfo::StreamInfo& stream_info) const override;
141 : ProtobufWkt::Value
142 : formatValueWithContext(const HttpFormatterContext& context,
143 : const StreamInfo::StreamInfo& stream_info) const override;
144 : };
145 :
146 : class GrpcStatusFormatter : public FormatterProvider, HeaderFormatter {
147 : public:
148 : enum Format {
149 : CamelString,
150 : SnakeString,
151 : Number,
152 : };
153 :
154 : GrpcStatusFormatter(const std::string& main_header, const std::string& alternative_header,
155 : absl::optional<size_t> max_length, Format format);
156 :
157 : // FormatterProvider
158 : absl::optional<std::string>
159 : formatWithContext(const HttpFormatterContext& context,
160 : const StreamInfo::StreamInfo& stream_info) const override;
161 : ProtobufWkt::Value
162 : formatValueWithContext(const HttpFormatterContext& context,
163 : const StreamInfo::StreamInfo& stream_info) const override;
164 :
165 : static Format parseFormat(absl::string_view format);
166 :
167 : private:
168 : const Format format_;
169 : };
170 :
171 : /**
172 : * FormatterProvider for request headers from StreamInfo (rather than the request_headers param).
173 : * Purely for testing.
174 : */
175 : class StreamInfoRequestHeaderFormatter : public FormatterProvider, HeaderFormatter {
176 : public:
177 : StreamInfoRequestHeaderFormatter(const std::string& main_header,
178 : const std::string& alternative_header,
179 : absl::optional<size_t> max_length);
180 :
181 : // FormatterProvider
182 : absl::optional<std::string>
183 : formatWithContext(const HttpFormatterContext& context,
184 : const StreamInfo::StreamInfo& stream_info) const override;
185 : ProtobufWkt::Value
186 : formatValueWithContext(const HttpFormatterContext& context,
187 : const StreamInfo::StreamInfo& stream_info) const override;
188 : };
189 :
190 : class HttpBuiltInCommandParser : public CommandParser {
191 : public:
192 57 : HttpBuiltInCommandParser() = default;
193 :
194 : // CommandParser
195 : FormatterProviderPtr parse(const std::string& command, const std::string& subcommand,
196 : absl::optional<size_t>& max_length) const override;
197 :
198 0 : static const CommandParser& builtInCommandParser() {
199 0 : CONSTRUCT_ON_FIRST_USE(HttpBuiltInCommandParser);
200 0 : }
201 :
202 : private:
203 : using FormatterProviderCreateFunc =
204 : std::function<FormatterProviderPtr(const std::string&, absl::optional<size_t>&)>;
205 :
206 : using FormatterProviderLookupTbl =
207 : absl::flat_hash_map<absl::string_view, std::pair<CommandSyntaxChecker::CommandSyntaxFlags,
208 : FormatterProviderCreateFunc>>;
209 : static const FormatterProviderLookupTbl& getKnownFormatters();
210 : };
211 :
212 : /**
213 : * Util class for HTTP access log format.
214 : */
215 : class HttpSubstitutionFormatUtils {
216 : public:
217 : static FormatterPtr defaultSubstitutionFormatter();
218 : };
219 :
220 : } // namespace Formatter
221 : } // namespace Envoy
|