1
#pragma once
2

            
3
#include "envoy/data/accesslog/v3/accesslog.pb.h"
4
#include "envoy/http/header_map.h"
5

            
6
#include "source/common/tracing/null_span_impl.h"
7

            
8
namespace Envoy {
9
namespace Formatter {
10

            
11
using AccessLogType = envoy::data::accesslog::v3::AccessLogType;
12

            
13
/**
14
 * Substitution formatter context for access logs or formatters.
15
 */
16
class Context {
17
public:
18
  /**
19
   * Interface for a context extension which can be used to provide non-HTTP specific data to
20
   * formatters. This could be used for non-HTTP protocols to provide protocol specific data to
21
   * formatters.
22
   */
23
  class Extension {
24
  public:
25
175
    virtual ~Extension() = default;
26
  };
27

            
28
  /**
29
   * Constructor that uses the provided request/response headers, response trailers, local reply
30
   * body, and access log type. Any of the parameters can be nullptr/empty.
31
   *
32
   * @param request_headers supplies the request headers.
33
   * @param response_headers supplies the response headers.
34
   * @param response_trailers supplies the response trailers.
35
   * @param local_reply_body supplies the local reply body.
36
   * @param log_type supplies the access log type.
37
   * @param active_span supplies the active span.
38
   */
39
  Context(const Http::RequestHeaderMap* request_headers = nullptr,
40
          const Http::ResponseHeaderMap* response_headers = nullptr,
41
          const Http::ResponseTrailerMap* response_trailers = nullptr,
42
          absl::string_view local_reply_body = {}, AccessLogType log_type = AccessLogType::NotSet,
43
          const Tracing::Span* active_span = nullptr)
44
356266
      : local_reply_body_(local_reply_body), request_headers_(makeOptRefFromPtr(request_headers)),
45
356266
        response_headers_(makeOptRefFromPtr(response_headers)),
46
356266
        response_trailers_(makeOptRefFromPtr(response_trailers)),
47
356266
        active_span_(makeOptRefFromPtr(active_span)), log_type_(log_type) {}
48

            
49
  /**
50
   * Set or overwrite the request headers.
51
   * @param request_headers supplies the request headers.
52
   */
53
145
  Context& setRequestHeaders(const Http::RequestHeaderMap& request_headers) {
54
145
    request_headers_ = request_headers;
55
145
    return *this;
56
145
  }
57

            
58
  /**
59
   * Set or overwrite the response headers.
60
   * @param response_headers supplies the response headers.
61
   */
62
111
  Context& setResponseHeaders(const Http::ResponseHeaderMap& response_headers) {
63
111
    response_headers_ = response_headers;
64
111
    return *this;
65
111
  }
66

            
67
  /**
68
   * Set or overwrite the response trailers.
69
   * @param response_trailers supplies the response trailers.
70
   */
71
107
  Context& setResponseTrailers(const Http::ResponseTrailerMap& response_trailers) {
72
107
    response_trailers_ = response_trailers;
73
107
    return *this;
74
107
  }
75

            
76
  /**
77
   * Set or overwrite the local reply body.
78
   * @param local_reply_body supplies the local reply body.
79
   */
80
  Context& setLocalReplyBody(absl::string_view local_reply_body) {
81
    local_reply_body_ = local_reply_body;
82
    return *this;
83
  }
84

            
85
  /**
86
   * Set or overwrite the access log type.
87
   * @param log_type supplies the access log type.
88
   */
89
19
  Context& setAccessLogType(AccessLogType log_type) {
90
19
    log_type_ = log_type;
91
19
    return *this;
92
19
  }
93

            
94
  /**
95
   * @return OptRef<const Http::RequestHeaderMap> the request headers.
96
   */
97
434556
  OptRef<const Http::RequestHeaderMap> requestHeaders() const { return request_headers_; }
98

            
99
  /**
100
   * @return OptRef<const Http::ResponseHeaderMap> the response headers.
101
   */
102
72893
  OptRef<const Http::ResponseHeaderMap> responseHeaders() const { return response_headers_; }
103

            
104
  /**
105
   * @return OptRef<const Http::ResponseTrailerMap> the response trailers.
106
   */
107
420
  OptRef<const Http::ResponseTrailerMap> responseTrailers() const { return response_trailers_; }
108

            
109
  /**
110
   * @return absl::string_view the local reply body. Empty if no local reply body.
111
   */
112
86
  absl::string_view localReplyBody() const { return local_reply_body_; }
113

            
114
  /**
115
   * @return AccessLog::AccessLogType the type of access log. NotSet if this is not used for
116
   * access logging.
117
   */
118
607
  AccessLogType accessLogType() const { return log_type_; }
119

            
120
  /**
121
   * Set or overwrite the active span.
122
   * @param active_span supplies the active span.
123
   */
124
2
  Context& setActiveSpan(const Tracing::Span& active_span) {
125
2
    active_span_ = makeOptRefFromPtr(&active_span);
126
2
    return *this;
127
2
  }
128

            
129
  /**
130
   * @return OptRef<const Tracing::Span> the active span.
131
   */
132
58
  OptRef<const Tracing::Span> activeSpan() const { return active_span_; }
133

            
134
  /**
135
   * Set the context extension.
136
   * @param extension supplies the context extension.
137
   */
138
73
  Context& setExtension(const Extension& extension) {
139
73
    extension_ = extension;
140
73
    return *this;
141
73
  }
142

            
143
  /**
144
   * @return OptRef<const ContextExtension> the context extension.
145
   */
146
1
  OptRef<const Extension> extension() const { return extension_; }
147

            
148
  /**
149
   * @return OptRef<const ExtensionType> the context extension casted to the specified type.
150
   */
151
206
  template <class Type> OptRef<const Type> typedExtension() const {
152
206
    const Type* typed_extension = dynamic_cast<const Type*>(extension_.ptr());
153
206
    return makeOptRefFromPtr(typed_extension);
154
206
  }
155

            
156
private:
157
  absl::string_view local_reply_body_;
158
  OptRef<const Http::RequestHeaderMap> request_headers_;
159
  OptRef<const Http::ResponseHeaderMap> response_headers_;
160
  OptRef<const Http::ResponseTrailerMap> response_trailers_;
161
  OptRef<const Extension> extension_;
162
  OptRef<const Tracing::Span> active_span_;
163
  AccessLogType log_type_{AccessLogType::NotSet};
164
};
165

            
166
} // namespace Formatter
167
} // namespace Envoy