|
33 | 33 | #include "proxy/http2/Http2DebugNames.h" |
34 | 34 | #include "proxy/http/HttpDebugNames.h" |
35 | 35 | #include "proxy/http/HttpSM.h" |
| 36 | +#include "proxy/logging/Log.h" |
| 37 | +#include "proxy/logging/LogAccess.h" |
36 | 38 |
|
37 | 39 | #include "iocore/net/TLSSNISupport.h" |
38 | 40 |
|
@@ -76,6 +78,132 @@ const int buffer_size_index[HTTP2_FRAME_TYPE_MAX] = { |
76 | 78 | BUFFER_SIZE_INDEX_16K, // HTTP2_FRAME_TYPE_CONTINUATION |
77 | 79 | }; |
78 | 80 |
|
| 81 | +/** Fetch a header field value from a decoded HTTP/2 request. |
| 82 | + * |
| 83 | + * @param[in] hdr The decoded request header block to inspect. |
| 84 | + * @param[in] name The field name to look up. |
| 85 | + * @return The field value, or an empty view if the field is absent. |
| 86 | + */ |
| 87 | +std::string_view |
| 88 | +get_header_field_value(HTTPHdr const &hdr, std::string_view name) |
| 89 | +{ |
| 90 | + if (auto const *field = hdr.field_find(name); field != nullptr) { |
| 91 | + return field->value_get(); |
| 92 | + } |
| 93 | + |
| 94 | + return {}; |
| 95 | +} |
| 96 | + |
| 97 | +/** Build a best-effort request target for access logging. |
| 98 | + * |
| 99 | + * @param[in] method The decoded request method or :method pseudo-header. |
| 100 | + * @param[in] scheme The decoded :scheme pseudo-header, if present. |
| 101 | + * @param[in] authority The decoded :authority pseudo-header, if present. |
| 102 | + * @param[in] path The decoded :path pseudo-header, if present. |
| 103 | + * @return A synthesized request target suitable for transaction logging. |
| 104 | + */ |
| 105 | +std::string |
| 106 | +synthesize_client_request_target(std::string_view method, std::string_view scheme, std::string_view authority, |
| 107 | + std::string_view path) |
| 108 | +{ |
| 109 | + if (method == static_cast<std::string_view>(HTTP_METHOD_CONNECT)) { |
| 110 | + if (!authority.empty()) { |
| 111 | + return std::string(authority); |
| 112 | + } |
| 113 | + if (!path.empty()) { |
| 114 | + return std::string(path); |
| 115 | + } |
| 116 | + return {}; |
| 117 | + } |
| 118 | + |
| 119 | + if (!scheme.empty() && !authority.empty()) { |
| 120 | + std::string url; |
| 121 | + url.reserve(scheme.size() + authority.size() + path.size() + 4); |
| 122 | + url.append(scheme); |
| 123 | + url.append("://"); |
| 124 | + url.append(authority); |
| 125 | + if (!path.empty()) { |
| 126 | + url.append(path); |
| 127 | + } else { |
| 128 | + url.push_back('/'); |
| 129 | + } |
| 130 | + return url; |
| 131 | + } |
| 132 | + |
| 133 | + if (!path.empty()) { |
| 134 | + return std::string(path); |
| 135 | + } |
| 136 | + |
| 137 | + if (!authority.empty()) { |
| 138 | + return std::string(authority); |
| 139 | + } |
| 140 | + |
| 141 | + return {}; |
| 142 | +} |
| 143 | + |
| 144 | +/** Emit a best-effort access log entry for a malformed decoded request. |
| 145 | + * |
| 146 | + * This is used when the request is rejected before stream processing reaches |
| 147 | + * the point of creating an @c HttpSM. |
| 148 | + * |
| 149 | + * @param[in] stream The stream whose decoded request headers were rejected. |
| 150 | + * @return None. |
| 151 | + */ |
| 152 | +void |
| 153 | +log_malformed_request_access(Http2Stream *stream) |
| 154 | +{ |
| 155 | + if (stream == nullptr || stream->get_sm() != nullptr || stream->trailing_header_is_possible() || |
| 156 | + stream->is_outbound_connection()) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + auto const *request = stream->get_receive_header(); |
| 161 | + if (request == nullptr || !request->valid() || request->type_get() != HTTPType::REQUEST) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + auto *proxy_session = stream->get_proxy_ssn(); |
| 166 | + if (proxy_session == nullptr) { |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + LogAccess::PreTransactionLogData data; |
| 171 | + data.client_request.create(HTTPType::REQUEST, request->version_get()); |
| 172 | + data.client_request.copy(request); |
| 173 | + data.has_client_request = true; |
| 174 | + data.client_connection_is_ssl = proxy_session->ssl() != nullptr; |
| 175 | + |
| 176 | + auto const method = get_header_field_value(*request, PSEUDO_HEADER_METHOD); |
| 177 | + auto const scheme = get_header_field_value(*request, PSEUDO_HEADER_SCHEME); |
| 178 | + auto const authority = get_header_field_value(*request, PSEUDO_HEADER_AUTHORITY); |
| 179 | + auto const path = get_header_field_value(*request, PSEUDO_HEADER_PATH); |
| 180 | + |
| 181 | + if (!method.empty()) { |
| 182 | + data.method.assign(method.data(), method.size()); |
| 183 | + } else { |
| 184 | + auto const method_value = const_cast<HTTPHdr *>(request)->method_get(); |
| 185 | + data.method.assign(method_value.data(), method_value.size()); |
| 186 | + } |
| 187 | + |
| 188 | + data.scheme.assign(scheme.data(), scheme.size()); |
| 189 | + data.authority.assign(authority.data(), authority.size()); |
| 190 | + data.path.assign(path.data(), path.size()); |
| 191 | + data.url = synthesize_client_request_target(data.method, data.scheme, data.authority, data.path); |
| 192 | + |
| 193 | + ats_ip_copy(reinterpret_cast<sockaddr *>(&data.client_addr), proxy_session->get_remote_addr()); |
| 194 | + ats_ip_copy(reinterpret_cast<sockaddr *>(&data.local_addr), proxy_session->get_local_addr()); |
| 195 | + |
| 196 | + ink_hrtime const now = ink_get_hrtime(); |
| 197 | + data.milestones[TS_MILESTONE_SM_START] = now; |
| 198 | + data.milestones[TS_MILESTONE_UA_BEGIN] = now; |
| 199 | + data.milestones[TS_MILESTONE_UA_FIRST_READ] = now; |
| 200 | + data.milestones[TS_MILESTONE_UA_READ_HEADER_DONE] = now; |
| 201 | + data.milestones[TS_MILESTONE_SM_FINISH] = now; |
| 202 | + |
| 203 | + LogAccess access(data); |
| 204 | + Log::access(&access); |
| 205 | +} |
| 206 | + |
79 | 207 | inline unsigned |
80 | 208 | read_rcv_buffer(char *buf, size_t bufsize, unsigned &nbytes, const Http2Frame &frame) |
81 | 209 | { |
@@ -496,6 +624,7 @@ Http2ConnectionState::rcv_headers_frame(const Http2Frame &frame) |
496 | 624 | return Http2Error(Http2ErrorClass::HTTP2_ERROR_CLASS_CONNECTION, Http2ErrorCode::HTTP2_ERROR_ENHANCE_YOUR_CALM, |
497 | 625 | "recv headers enhance your calm"); |
498 | 626 | } else { |
| 627 | + log_malformed_request_access(stream); |
499 | 628 | return Http2Error(Http2ErrorClass::HTTP2_ERROR_CLASS_STREAM, Http2ErrorCode::HTTP2_ERROR_PROTOCOL_ERROR, |
500 | 629 | "recv headers malformed request"); |
501 | 630 | } |
@@ -1108,6 +1237,7 @@ Http2ConnectionState::rcv_continuation_frame(const Http2Frame &frame) |
1108 | 1237 | return Http2Error(Http2ErrorClass::HTTP2_ERROR_CLASS_CONNECTION, Http2ErrorCode::HTTP2_ERROR_ENHANCE_YOUR_CALM, |
1109 | 1238 | "continuation enhance your calm"); |
1110 | 1239 | } else { |
| 1240 | + log_malformed_request_access(stream); |
1111 | 1241 | return Http2Error(Http2ErrorClass::HTTP2_ERROR_CLASS_CONNECTION, Http2ErrorCode::HTTP2_ERROR_PROTOCOL_ERROR, |
1112 | 1242 | "continuation malformed request"); |
1113 | 1243 | } |
|
0 commit comments