Line data Source code
1 : #include "source/extensions/formatter/req_without_query/req_without_query.h" 2 : 3 : #include <string> 4 : 5 : #include "source/common/http/utility.h" 6 : #include "source/common/protobuf/utility.h" 7 : 8 : namespace Envoy { 9 : namespace Extensions { 10 : namespace Formatter { 11 : 12 : namespace { 13 : 14 0 : void truncate(std::string& str, absl::optional<size_t> max_length) { 15 0 : if (!max_length) { 16 0 : return; 17 0 : } 18 : 19 0 : str = str.substr(0, max_length.value()); 20 0 : } 21 : 22 : } // namespace 23 : 24 : ReqWithoutQuery::ReqWithoutQuery(const std::string& main_header, 25 : const std::string& alternative_header, 26 : absl::optional<size_t> max_length) 27 0 : : main_header_(main_header), alternative_header_(alternative_header), max_length_(max_length) {} 28 : 29 : absl::optional<std::string> 30 : ReqWithoutQuery::formatWithContext(const Envoy::Formatter::HttpFormatterContext& context, 31 0 : const StreamInfo::StreamInfo&) const { 32 0 : const Http::HeaderEntry* header = findHeader(context.requestHeaders()); 33 0 : if (!header) { 34 0 : return absl::nullopt; 35 0 : } 36 : 37 0 : std::string val = Http::Utility::stripQueryString(header->value()); 38 0 : truncate(val, max_length_); 39 : 40 0 : return val; 41 0 : } 42 : 43 : ProtobufWkt::Value 44 : ReqWithoutQuery::formatValueWithContext(const Envoy::Formatter::HttpFormatterContext& context, 45 0 : const StreamInfo::StreamInfo&) const { 46 0 : const Http::HeaderEntry* header = findHeader(context.requestHeaders()); 47 0 : if (!header) { 48 0 : return ValueUtil::nullValue(); 49 0 : } 50 : 51 0 : std::string val = Http::Utility::stripQueryString(header->value()); 52 0 : truncate(val, max_length_); 53 0 : return ValueUtil::stringValue(val); 54 0 : } 55 : 56 0 : const Http::HeaderEntry* ReqWithoutQuery::findHeader(const Http::HeaderMap& headers) const { 57 0 : const auto header = headers.get(main_header_); 58 : 59 0 : if (header.empty() && !alternative_header_.get().empty()) { 60 0 : const auto alternate_header = headers.get(alternative_header_); 61 : // TODO(https://github.com/envoyproxy/envoy/issues/13454): Potentially log all header values. 62 0 : return alternate_header.empty() ? nullptr : alternate_header[0]; 63 0 : } 64 : 65 0 : return header.empty() ? nullptr : header[0]; 66 0 : } 67 : 68 : ::Envoy::Formatter::FormatterProviderPtr 69 : ReqWithoutQueryCommandParser::parse(const std::string& command, const std::string& subcommand, 70 0 : absl::optional<size_t>& max_length) const { 71 0 : if (command == "REQ_WITHOUT_QUERY") { 72 0 : std::string main_header, alternative_header; 73 : 74 0 : Envoy::Formatter::SubstitutionFormatUtils::parseSubcommandHeaders(subcommand, main_header, 75 0 : alternative_header); 76 0 : return std::make_unique<ReqWithoutQuery>(main_header, alternative_header, max_length); 77 0 : } 78 : 79 0 : return nullptr; 80 0 : } 81 : 82 : } // namespace Formatter 83 : } // namespace Extensions 84 : } // namespace Envoy