Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 3 | #pragma once |
Ed Tanous | 7531298 | 2021-02-11 14:26:02 -0800 | [diff] [blame] | 4 | #include "bmcweb_config.h" |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 5 | |
Ed Tanous | d093c99 | 2023-01-19 19:01:49 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" |
Nan Zhou | d055a34 | 2022-05-25 01:15:34 +0000 | [diff] [blame] | 7 | #include "authentication.hpp" |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 8 | #include "complete_response_fields.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "forward_unauthorized.hpp" |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 10 | #include "http2_connection.hpp" |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 11 | #include "http_body.hpp" |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 12 | #include "http_connect_types.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 13 | #include "http_request.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 14 | #include "http_response.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | #include "http_utility.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 16 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 17 | #include "mutual_tls.hpp" |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include "sessions.hpp" |
Ed Tanous | 18f8f60 | 2023-07-18 10:07:23 -0700 | [diff] [blame] | 19 | #include "str_utility.hpp" |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 20 | #include "utility.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 22 | #include <boost/asio/error.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 23 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 24 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 25 | #include <boost/asio/ssl/stream_base.hpp> |
| 26 | #include <boost/asio/ssl/verify_context.hpp> |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 27 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 28 | #include <boost/beast/_experimental/test/stream.hpp> |
Ed Tanous | 4d69861 | 2024-02-06 14:57:24 -0800 | [diff] [blame] | 29 | #include <boost/beast/core/buffers_generator.hpp> |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 30 | #include <boost/beast/core/detect_ssl.hpp> |
| 31 | #include <boost/beast/core/error.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 32 | #include <boost/beast/core/flat_static_buffer.hpp> |
Myung Bae | a4326fe | 2023-01-10 14:29:24 -0600 | [diff] [blame] | 33 | #include <boost/beast/http/error.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 34 | #include <boost/beast/http/field.hpp> |
Ed Tanous | 4d69861 | 2024-02-06 14:57:24 -0800 | [diff] [blame] | 35 | #include <boost/beast/http/message_generator.hpp> |
Ed Tanous | 918ef25 | 2022-05-25 10:40:41 -0700 | [diff] [blame] | 36 | #include <boost/beast/http/parser.hpp> |
| 37 | #include <boost/beast/http/read.hpp> |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 38 | #include <boost/beast/http/rfc7230.hpp> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 39 | #include <boost/beast/http/status.hpp> |
| 40 | #include <boost/beast/http/verb.hpp> |
| 41 | #include <boost/none.hpp> |
| 42 | #include <boost/optional/optional.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 43 | |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 44 | #include <bit> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 45 | #include <chrono> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 46 | #include <cstddef> |
| 47 | #include <cstdint> |
| 48 | #include <functional> |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 49 | #include <memory> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 50 | #include <optional> |
| 51 | #include <string> |
| 52 | #include <string_view> |
| 53 | #include <system_error> |
| 54 | #include <type_traits> |
| 55 | #include <utility> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | namespace crow |
| 58 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 59 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 60 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 61 | static int connectionCount = 0; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 62 | |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 63 | // request body limit size set by the BMCWEB_HTTP_BODY_LIMIT option |
| 64 | constexpr uint64_t httpReqBodyLimit = 1024UL * 1024UL * BMCWEB_HTTP_BODY_LIMIT; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 65 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 66 | constexpr uint64_t loggedOutPostBodyLimit = 4096U; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 67 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 68 | constexpr uint32_t httpHeaderLimit = 8192U; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 69 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 70 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 71 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 72 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | { |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 74 | using self_type = Connection<Adaptor, Handler>; |
| 75 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | public: |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 77 | Connection(Handler* handlerIn, HttpType httpTypeIn, |
| 78 | boost::asio::steady_timer&& timerIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 79 | std::function<std::string()>& getCachedDateStrF, |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 80 | boost::asio::ssl::stream<Adaptor>&& adaptorIn) : |
| 81 | httpType(httpTypeIn), adaptor(std::move(adaptorIn)), handler(handlerIn), |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 82 | timer(std::move(timerIn)), getCachedDateStr(getCachedDateStrF) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 84 | initParser(); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 85 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 86 | connectionCount++; |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 87 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 88 | BMCWEB_LOG_DEBUG("{} Connection created, total {}", logPtr(this), |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 89 | connectionCount); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | ~Connection() |
| 93 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 94 | res.releaseCompleteRequestHandler(); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 95 | cancelDeadlineTimer(); |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 96 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 97 | connectionCount--; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 98 | BMCWEB_LOG_DEBUG("{} Connection closed, total {}", logPtr(this), |
| 99 | connectionCount); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 102 | Connection(const Connection&) = delete; |
| 103 | Connection(Connection&&) = delete; |
| 104 | Connection& operator=(const Connection&) = delete; |
| 105 | Connection& operator=(Connection&&) = delete; |
| 106 | |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 107 | bool tlsVerifyCallback(bool preverified, |
| 108 | boost::asio::ssl::verify_context& ctx) |
| 109 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 110 | BMCWEB_LOG_DEBUG("{} tlsVerifyCallback called with preverified {}", |
| 111 | logPtr(this), preverified); |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 112 | if (preverified) |
| 113 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 114 | mtlsSession = verifyMtlsUser(ip, ctx); |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 115 | if (mtlsSession) |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 116 | { |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 117 | BMCWEB_LOG_DEBUG("{} Generated TLS session: {}", logPtr(this), |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 118 | mtlsSession->uniqueId); |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 121 | const persistent_data::AuthConfigMethods& c = |
| 122 | persistent_data::SessionStore::getInstance().getAuthMethodsConfig(); |
| 123 | if (c.tlsStrict) |
| 124 | { |
Ed Tanous | 463a0e3 | 2024-10-14 11:21:48 -0700 | [diff] [blame] | 125 | BMCWEB_LOG_DEBUG( |
| 126 | "{} TLS is in strict mode, returning preverified as is.", |
| 127 | logPtr(this)); |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 128 | return preverified; |
| 129 | } |
| 130 | // If tls strict mode is disabled |
| 131 | // We always return true to allow full auth flow for resources that |
| 132 | // don't require auth |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 136 | bool prepareMutualTls() |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 137 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 138 | BMCWEB_LOG_DEBUG("prepareMutualTls"); |
| 139 | |
| 140 | constexpr std::string_view id = "bmcweb"; |
| 141 | |
| 142 | const char* idPtr = id.data(); |
| 143 | const auto* idCPtr = std::bit_cast<const unsigned char*>(idPtr); |
| 144 | auto idLen = static_cast<unsigned int>(id.length()); |
| 145 | int ret = |
| 146 | SSL_set_session_id_context(adaptor.native_handle(), idCPtr, idLen); |
| 147 | if (ret == 0) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 148 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 149 | BMCWEB_LOG_ERROR("{} failed to set SSL id", logPtr(this)); |
| 150 | return false; |
| 151 | } |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 152 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 153 | BMCWEB_LOG_DEBUG("set_verify_callback"); |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 154 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 155 | boost::system::error_code ec; |
| 156 | adaptor.set_verify_callback( |
| 157 | std::bind_front(&self_type::tlsVerifyCallback, this), ec); |
| 158 | if (ec) |
| 159 | { |
| 160 | BMCWEB_LOG_ERROR("Failed to set verify callback {}", ec); |
| 161 | return false; |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return true; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 167 | void afterDetectSsl(const std::shared_ptr<self_type>& /*self*/, |
| 168 | boost::beast::error_code ec, bool isTls) |
| 169 | { |
| 170 | if (ec) |
| 171 | { |
| 172 | BMCWEB_LOG_ERROR("Couldn't detect ssl ", ec); |
| 173 | return; |
| 174 | } |
| 175 | BMCWEB_LOG_DEBUG("{} TLS was detected as {}", logPtr(this), isTls); |
| 176 | if (isTls) |
| 177 | { |
| 178 | if (httpType != HttpType::HTTPS && httpType != HttpType::BOTH) |
| 179 | { |
| 180 | BMCWEB_LOG_WARNING( |
| 181 | "{} Connection closed due to incompatible type", |
| 182 | logPtr(this)); |
| 183 | return; |
| 184 | } |
| 185 | httpType = HttpType::HTTPS; |
| 186 | adaptor.async_handshake( |
| 187 | boost::asio::ssl::stream_base::server, buffer.data(), |
| 188 | std::bind_front(&self_type::afterSslHandshake, this, |
| 189 | shared_from_this())); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if (httpType != HttpType::HTTP && httpType != HttpType::BOTH) |
| 194 | { |
| 195 | BMCWEB_LOG_WARNING( |
| 196 | "{} Connection closed due to incompatible type", |
| 197 | logPtr(this)); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | httpType = HttpType::HTTP; |
| 202 | BMCWEB_LOG_INFO("Starting non-SSL session"); |
| 203 | doReadHeaders(); |
| 204 | } |
| 205 | } |
| 206 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | void start() |
| 208 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 209 | BMCWEB_LOG_DEBUG("{} Connection started, total {}", logPtr(this), |
| 210 | connectionCount); |
Gunnar Mills | 4f63be0 | 2023-10-25 09:14:07 -0500 | [diff] [blame] | 211 | if (connectionCount >= 200) |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 212 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 213 | BMCWEB_LOG_CRITICAL("{} Max connection count exceeded.", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 214 | logPtr(this)); |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 218 | if constexpr (BMCWEB_MUTUAL_TLS_AUTH) |
| 219 | { |
| 220 | if (!prepareMutualTls()) |
| 221 | { |
| 222 | BMCWEB_LOG_ERROR("{} Failed to prepare mTLS", logPtr(this)); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 227 | startDeadline(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 228 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 229 | readClientIp(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 230 | boost::beast::async_detect_ssl( |
| 231 | adaptor.next_layer(), buffer, |
| 232 | std::bind_front(&self_type::afterDetectSsl, this, |
| 233 | shared_from_this())); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 234 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 235 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 236 | void afterSslHandshake(const std::shared_ptr<self_type>& /*self*/, |
| 237 | const boost::system::error_code& ec, |
| 238 | size_t bytesParsed) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 239 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 240 | buffer.consume(bytesParsed); |
| 241 | if (ec) |
| 242 | { |
| 243 | BMCWEB_LOG_ERROR("{} SSL handshake failed", logPtr(this)); |
| 244 | return; |
| 245 | } |
| 246 | BMCWEB_LOG_DEBUG("{} SSL handshake succeeded", logPtr(this)); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 247 | // If http2 is enabled, negotiate the protocol |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 248 | if constexpr (BMCWEB_EXPERIMENTAL_HTTP2) |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 249 | { |
| 250 | const unsigned char* alpn = nullptr; |
| 251 | unsigned int alpnlen = 0; |
| 252 | SSL_get0_alpn_selected(adaptor.native_handle(), &alpn, &alpnlen); |
| 253 | if (alpn != nullptr) |
| 254 | { |
| 255 | std::string_view selectedProtocol( |
| 256 | std::bit_cast<const char*>(alpn), alpnlen); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 257 | BMCWEB_LOG_DEBUG("ALPN selected protocol \"{}\" len: {}", |
| 258 | selectedProtocol, alpnlen); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 259 | if (selectedProtocol == "h2") |
| 260 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 261 | upgradeToHttp2(); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | doReadHeaders(); |
| 268 | } |
| 269 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 270 | void initParser() |
| 271 | { |
| 272 | boost::beast::http::request_parser<bmcweb::HttpBody>& instance = |
Ed Tanous | 38afdb9 | 2024-12-11 23:57:53 -0800 | [diff] [blame] | 273 | parser.emplace(); |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 274 | |
| 275 | // reset header limit for newly created parser |
| 276 | instance.header_limit(httpHeaderLimit); |
| 277 | |
| 278 | // Initially set no body limit. We don't yet know if the user is |
| 279 | // authenticated. |
| 280 | instance.body_limit(boost::none); |
| 281 | } |
| 282 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 283 | void upgradeToHttp2() |
| 284 | { |
Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 285 | auto http2 = std::make_shared<HTTP2Connection<Adaptor, Handler>>( |
| 286 | std::move(adaptor), handler, getCachedDateStr, httpType); |
| 287 | if (http2settings.empty()) |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 288 | { |
Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 289 | http2->start(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 290 | } |
| 291 | else |
| 292 | { |
Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 293 | http2->startFromSettings(http2settings); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 297 | // returns whether connection was upgraded |
| 298 | bool doUpgrade(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 299 | { |
| 300 | using boost::beast::http::field; |
| 301 | using boost::beast::http::token_list; |
| 302 | |
| 303 | bool isSse = |
| 304 | isContentTypeAllowed(req->getHeaderValue("Accept"), |
| 305 | http_helpers::ContentType::EventStream, false); |
| 306 | |
| 307 | bool isWebsocket = false; |
| 308 | bool isH2c = false; |
| 309 | // Check connection header is upgrade |
| 310 | if (token_list{req->req[field::connection]}.exists("upgrade")) |
| 311 | { |
| 312 | BMCWEB_LOG_DEBUG("{} Connection: Upgrade header was present", |
| 313 | logPtr(this)); |
| 314 | // Parse if upgrade is h2c or websocket |
| 315 | token_list upgrade{req->req[field::upgrade]}; |
| 316 | isWebsocket = upgrade.exists("websocket"); |
| 317 | isH2c = upgrade.exists("h2c"); |
| 318 | BMCWEB_LOG_DEBUG("{} Upgrade isWebsocket: {} isH2c: {}", |
| 319 | logPtr(this), isWebsocket, isH2c); |
| 320 | } |
| 321 | |
| 322 | if (BMCWEB_EXPERIMENTAL_HTTP2 && isH2c) |
| 323 | { |
| 324 | std::string_view base64settings = req->req[field::http2_settings]; |
| 325 | if (utility::base64Decode<true>(base64settings, http2settings)) |
| 326 | { |
| 327 | res.result(boost::beast::http::status::switching_protocols); |
| 328 | res.addHeader(boost::beast::http::field::connection, "Upgrade"); |
| 329 | res.addHeader(boost::beast::http::field::upgrade, "h2c"); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // websocket and SSE are only allowed on GET |
| 334 | if (req->req.method() == boost::beast::http::verb::get) |
| 335 | { |
| 336 | if (isWebsocket || isSse) |
| 337 | { |
| 338 | asyncResp->res.setCompleteRequestHandler( |
| 339 | [self(shared_from_this())](crow::Response& thisRes) { |
| 340 | if (thisRes.result() != boost::beast::http::status::ok) |
| 341 | { |
| 342 | // When any error occurs before handle upgradation, |
| 343 | // the result in response will be set to respective |
| 344 | // error. By default the Result will be OK (200), |
| 345 | // which implies successful handle upgrade. Response |
| 346 | // needs to be sent over this connection only on |
| 347 | // failure. |
| 348 | self->completeRequest(thisRes); |
| 349 | return; |
| 350 | } |
| 351 | }); |
| 352 | BMCWEB_LOG_INFO("{} Upgrading socket", logPtr(this)); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 353 | if (httpType == HttpType::HTTP) |
| 354 | { |
| 355 | handler->handleUpgrade(req, asyncResp, |
| 356 | std::move(adaptor.next_layer())); |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | handler->handleUpgrade(req, asyncResp, std::move(adaptor)); |
| 361 | } |
| 362 | |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 369 | void handle() |
| 370 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 371 | std::error_code reqEc; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 372 | if (!parser) |
| 373 | { |
| 374 | return; |
| 375 | } |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 376 | req = std::make_shared<crow::Request>(parser->release(), reqEc); |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 377 | if (reqEc) |
| 378 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 379 | BMCWEB_LOG_DEBUG("Request failed to construct{}", reqEc.message()); |
Gunnar Mills | 262f115 | 2022-12-20 15:18:47 -0600 | [diff] [blame] | 380 | res.result(boost::beast::http::status::bad_request); |
| 381 | completeRequest(res); |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 382 | return; |
| 383 | } |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 384 | req->session = userSession; |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 385 | accept = req->getHeaderValue("Accept"); |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 386 | // Fetch the client IP address |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 387 | req->ipAddress = ip; |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 388 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 389 | // Check for HTTP version 1.1. |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 390 | if (req->version() == 11) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 391 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 392 | if (req->getHeaderValue(boost::beast::http::field::host).empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 393 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 394 | res.result(boost::beast::http::status::bad_request); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 395 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 396 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 399 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 400 | BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this), |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 401 | req->version() / 10, req->version() % 10, |
| 402 | req->methodString(), req->target(), |
| 403 | req->ipAddress.to_string()); |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 404 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 405 | if (res.completed) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 406 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 407 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 408 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 409 | } |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 410 | keepAlive = req->keepAlive(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 411 | |
| 412 | if (authenticationEnabled) |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 413 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 414 | if (!crow::authentication::isOnAllowlist(req->url().path(), |
| 415 | req->method()) && |
| 416 | req->session == nullptr) |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 417 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 418 | BMCWEB_LOG_WARNING("Authentication failed"); |
| 419 | forward_unauthorized::sendUnauthorized( |
| 420 | req->url().encoded_path(), |
| 421 | req->getHeaderValue("X-Requested-With"), |
| 422 | req->getHeaderValue("Accept"), res); |
| 423 | completeRequest(res); |
| 424 | return; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 425 | } |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 426 | } |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 427 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 428 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 429 | BMCWEB_LOG_DEBUG("Setting completion handler"); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 430 | asyncResp->res.setCompleteRequestHandler( |
| 431 | [self(shared_from_this())](crow::Response& thisRes) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 432 | self->completeRequest(thisRes); |
| 433 | }); |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 434 | if (doUpgrade(asyncResp)) |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 435 | { |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 436 | return; |
| 437 | } |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 438 | std::string_view expected = |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 439 | req->getHeaderValue(boost::beast::http::field::if_none_match); |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 440 | if (!expected.empty()) |
| 441 | { |
Ed Tanous | 37b912f | 2025-03-20 18:24:30 -0700 | [diff] [blame] | 442 | asyncResp->res.setExpectedHash(expected); |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 443 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 444 | handler->handle(req, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 445 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 446 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 447 | void hardClose() |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 448 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 449 | BMCWEB_LOG_DEBUG("{} Closing socket", logPtr(this)); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 450 | adaptor.next_layer().close(); |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void tlsShutdownComplete(const std::shared_ptr<self_type>& self, |
| 454 | const boost::system::error_code& ec) |
| 455 | { |
| 456 | if (ec) |
| 457 | { |
| 458 | BMCWEB_LOG_WARNING("{} Failed to shut down TLS cleanly {}", |
| 459 | logPtr(self.get()), ec); |
| 460 | } |
| 461 | self->hardClose(); |
| 462 | } |
| 463 | |
| 464 | void gracefulClose() |
| 465 | { |
| 466 | BMCWEB_LOG_DEBUG("{} Socket close requested", logPtr(this)); |
Ed Tanous | 3281bcf | 2024-06-25 16:02:05 -0700 | [diff] [blame] | 467 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 468 | if (httpType == HttpType::HTTPS) |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 469 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 470 | if (mtlsSession != nullptr) |
| 471 | { |
| 472 | BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this), |
| 473 | mtlsSession->uniqueId); |
| 474 | persistent_data::SessionStore::getInstance().removeSession( |
| 475 | mtlsSession); |
| 476 | } |
| 477 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 478 | adaptor.async_shutdown(std::bind_front( |
| 479 | &self_type::tlsShutdownComplete, this, shared_from_this())); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 480 | } |
| 481 | else |
| 482 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 483 | hardClose(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 487 | void completeRequest(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 488 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 489 | res = std::move(thisRes); |
Ed Tanous | 4cdc2e8 | 2023-01-13 10:03:22 -0800 | [diff] [blame] | 490 | res.keepAlive(keepAlive); |
Ed Tanous | 5ae6f92 | 2023-01-09 10:45:53 -0800 | [diff] [blame] | 491 | |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 492 | completeResponseFields(accept, res); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 493 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 494 | |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 495 | doWrite(); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 496 | |
| 497 | // delete lambda with self shared_ptr |
| 498 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 499 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 502 | void readClientIp() |
| 503 | { |
| 504 | boost::system::error_code ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 505 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 506 | boost::asio::ip::tcp::endpoint endpoint = |
| 507 | boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 508 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 509 | if (ec) |
| 510 | { |
| 511 | // If remote endpoint fails keep going. "ClientOriginIPAddress" |
| 512 | // will be empty. |
| 513 | BMCWEB_LOG_ERROR("Failed to get the client's IP Address. ec : {}", |
| 514 | ec); |
| 515 | return; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 516 | } |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 517 | ip = endpoint.address(); |
| 518 | } |
| 519 | |
| 520 | void disableAuth() |
| 521 | { |
| 522 | authenticationEnabled = false; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 525 | private: |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 526 | uint64_t getContentLengthLimit() |
| 527 | { |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 528 | if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH) |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 529 | { |
Ed Tanous | 8332831 | 2024-05-09 15:48:09 -0700 | [diff] [blame] | 530 | if (userSession == nullptr) |
| 531 | { |
| 532 | return loggedOutPostBodyLimit; |
| 533 | } |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 534 | } |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 535 | |
| 536 | return httpReqBodyLimit; |
| 537 | } |
| 538 | |
| 539 | // Returns true if content length was within limits |
| 540 | // Returns false if content length error has been returned |
| 541 | bool handleContentLengthError() |
| 542 | { |
| 543 | if (!parser) |
| 544 | { |
Manojkiran Eda | efff2b5 | 2024-06-18 18:01:46 +0530 | [diff] [blame] | 545 | BMCWEB_LOG_CRITICAL("Parser was null"); |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 546 | return false; |
| 547 | } |
| 548 | const boost::optional<uint64_t> contentLength = |
| 549 | parser->content_length(); |
| 550 | if (!contentLength) |
| 551 | { |
| 552 | BMCWEB_LOG_DEBUG("{} No content length available", logPtr(this)); |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | uint64_t maxAllowedContentLength = getContentLengthLimit(); |
| 557 | |
| 558 | if (*contentLength > maxAllowedContentLength) |
| 559 | { |
| 560 | // If the users content limit is between the logged in |
| 561 | // and logged out limits They probably just didn't log |
| 562 | // in |
| 563 | if (*contentLength > loggedOutPostBodyLimit && |
| 564 | *contentLength < httpReqBodyLimit) |
| 565 | { |
| 566 | BMCWEB_LOG_DEBUG( |
| 567 | "{} Content length {} valid, but greater than logged out" |
| 568 | " limit of {}. Setting unauthorized", |
| 569 | logPtr(this), *contentLength, loggedOutPostBodyLimit); |
| 570 | res.result(boost::beast::http::status::unauthorized); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | // Otherwise they're over both limits, so inform |
| 575 | // them |
| 576 | BMCWEB_LOG_DEBUG( |
| 577 | "{} Content length {} was greater than global limit {}." |
| 578 | " Setting payload too large", |
| 579 | logPtr(this), *contentLength, httpReqBodyLimit); |
| 580 | res.result(boost::beast::http::status::payload_too_large); |
| 581 | } |
| 582 | |
| 583 | keepAlive = false; |
| 584 | doWrite(); |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 591 | void afterReadHeaders(const std::shared_ptr<self_type>& /*self*/, |
| 592 | const boost::system::error_code& ec, |
| 593 | std::size_t bytesTransferred) |
| 594 | { |
| 595 | BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this), |
| 596 | bytesTransferred); |
| 597 | |
| 598 | if (ec) |
| 599 | { |
| 600 | cancelDeadlineTimer(); |
| 601 | |
| 602 | if (ec == boost::beast::http::error::header_limit) |
| 603 | { |
| 604 | BMCWEB_LOG_ERROR("{} Header field too large, closing", |
| 605 | logPtr(this), ec.message()); |
| 606 | |
| 607 | res.result(boost::beast::http::status:: |
| 608 | request_header_fields_too_large); |
| 609 | keepAlive = false; |
| 610 | doWrite(); |
| 611 | return; |
| 612 | } |
| 613 | if (ec == boost::beast::http::error::end_of_stream) |
| 614 | { |
| 615 | BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this), |
| 616 | ec); |
| 617 | hardClose(); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | BMCWEB_LOG_DEBUG("{} Closing socket due to read error {}", |
| 622 | logPtr(this), ec.message()); |
| 623 | gracefulClose(); |
| 624 | |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | if (!parser) |
| 629 | { |
| 630 | BMCWEB_LOG_ERROR("Parser was unexpectedly null"); |
| 631 | return; |
| 632 | } |
| 633 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 634 | if (authenticationEnabled) |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 635 | { |
| 636 | boost::beast::http::verb method = parser->get().method(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 637 | userSession = authentication::authenticate( |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 638 | ip, res, method, parser->get().base(), mtlsSession); |
| 639 | } |
| 640 | |
| 641 | std::string_view expect = |
| 642 | parser->get()[boost::beast::http::field::expect]; |
| 643 | if (bmcweb::asciiIEquals(expect, "100-continue")) |
| 644 | { |
| 645 | res.result(boost::beast::http::status::continue_); |
| 646 | doWrite(); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | if (!handleContentLengthError()) |
| 651 | { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | parser->body_limit(getContentLengthLimit()); |
| 656 | |
| 657 | if (parser->is_done()) |
| 658 | { |
| 659 | handle(); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | doRead(); |
| 664 | } |
| 665 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 666 | void doReadHeaders() |
| 667 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 668 | BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this)); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 669 | if (!parser) |
| 670 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 671 | BMCWEB_LOG_CRITICAL("Parser was not initialized."); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 672 | return; |
| 673 | } |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 674 | |
| 675 | if (httpType == HttpType::HTTP) |
| 676 | { |
| 677 | boost::beast::http::async_read_header( |
| 678 | adaptor.next_layer(), buffer, *parser, |
| 679 | std::bind_front(&self_type::afterReadHeaders, this, |
| 680 | shared_from_this())); |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | boost::beast::http::async_read_header( |
| 685 | adaptor, buffer, *parser, |
| 686 | std::bind_front(&self_type::afterReadHeaders, this, |
| 687 | shared_from_this())); |
| 688 | } |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 689 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 690 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 691 | void afterRead(const std::shared_ptr<self_type>& /*self*/, |
| 692 | const boost::system::error_code& ec, |
| 693 | std::size_t bytesTransferred) |
| 694 | { |
| 695 | BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this), |
| 696 | bytesTransferred); |
| 697 | |
| 698 | if (ec) |
| 699 | { |
| 700 | BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this), |
| 701 | ec.message()); |
| 702 | if (ec == boost::beast::http::error::body_limit) |
| 703 | { |
| 704 | if (handleContentLengthError()) |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 705 | { |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 706 | BMCWEB_LOG_CRITICAL("Body length limit reached, " |
| 707 | "but no content-length " |
| 708 | "available? Should never happen"); |
| 709 | res.result( |
| 710 | boost::beast::http::status::internal_server_error); |
| 711 | keepAlive = false; |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 712 | doWrite(); |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 713 | } |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 714 | return; |
| 715 | } |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 716 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 717 | gracefulClose(); |
| 718 | return; |
| 719 | } |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 720 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 721 | // If the user is logged in, allow them to send files |
| 722 | // incrementally one piece at a time. If authentication is |
| 723 | // disabled then there is no user session hence always allow to |
| 724 | // send one piece at a time. |
| 725 | if (userSession != nullptr) |
| 726 | { |
| 727 | cancelDeadlineTimer(); |
| 728 | } |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 729 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 730 | if (!parser) |
| 731 | { |
| 732 | BMCWEB_LOG_ERROR("Parser was unexpectedly null"); |
| 733 | return; |
| 734 | } |
| 735 | if (!parser->is_done()) |
| 736 | { |
| 737 | doRead(); |
| 738 | return; |
| 739 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 740 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 741 | cancelDeadlineTimer(); |
| 742 | handle(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void doRead() |
| 746 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 747 | BMCWEB_LOG_DEBUG("{} doRead", logPtr(this)); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 748 | if (!parser) |
| 749 | { |
| 750 | return; |
| 751 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 752 | startDeadline(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 753 | if (httpType == HttpType::HTTP) |
| 754 | { |
| 755 | boost::beast::http::async_read_some( |
| 756 | adaptor.next_layer(), buffer, *parser, |
| 757 | std::bind_front(&self_type::afterRead, this, |
| 758 | shared_from_this())); |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | boost::beast::http::async_read_some( |
| 763 | adaptor, buffer, *parser, |
| 764 | std::bind_front(&self_type::afterRead, this, |
| 765 | shared_from_this())); |
| 766 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 769 | void afterDoWrite(const std::shared_ptr<self_type>& /*self*/, |
| 770 | const boost::system::error_code& ec, |
| 771 | std::size_t bytesTransferred) |
| 772 | { |
Ed Tanous | 0242baf | 2024-05-16 19:52:47 -0700 | [diff] [blame] | 773 | BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this), |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 774 | bytesTransferred, ec); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 775 | |
| 776 | cancelDeadlineTimer(); |
| 777 | |
Ed Tanous | 0242baf | 2024-05-16 19:52:47 -0700 | [diff] [blame] | 778 | if (ec == boost::system::errc::operation_would_block || |
| 779 | ec == boost::system::errc::resource_unavailable_try_again) |
| 780 | { |
| 781 | doWrite(); |
| 782 | return; |
| 783 | } |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 784 | if (ec) |
| 785 | { |
| 786 | BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this)); |
| 787 | return; |
| 788 | } |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 789 | |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 790 | if (res.result() == boost::beast::http::status::switching_protocols) |
| 791 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 792 | upgradeToHttp2(); |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 796 | if (res.result() == boost::beast::http::status::continue_) |
| 797 | { |
| 798 | // Reset the result to ok |
| 799 | res.result(boost::beast::http::status::ok); |
| 800 | doRead(); |
| 801 | return; |
| 802 | } |
| 803 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 804 | if (!keepAlive) |
| 805 | { |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 806 | BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket", |
| 807 | logPtr(this)); |
| 808 | |
| 809 | gracefulClose(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 810 | return; |
| 811 | } |
| 812 | |
| 813 | BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this)); |
| 814 | res.clear(); |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 815 | initParser(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 816 | |
| 817 | userSession = nullptr; |
| 818 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 819 | req->clear(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 820 | doReadHeaders(); |
| 821 | } |
| 822 | |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 823 | void doWrite() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 824 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 825 | BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this)); |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 826 | res.preparePayload(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 827 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 828 | startDeadline(); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 829 | if (httpType == HttpType::HTTP) |
| 830 | { |
| 831 | boost::beast::async_write( |
| 832 | adaptor.next_layer(), |
| 833 | boost::beast::http::message_generator(std::move(res.response)), |
| 834 | std::bind_front(&self_type::afterDoWrite, this, |
| 835 | shared_from_this())); |
| 836 | } |
| 837 | else |
| 838 | { |
| 839 | boost::beast::async_write( |
| 840 | adaptor, |
| 841 | boost::beast::http::message_generator(std::move(res.response)), |
| 842 | std::bind_front(&self_type::afterDoWrite, this, |
| 843 | shared_from_this())); |
| 844 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 847 | void cancelDeadlineTimer() |
| 848 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 849 | timer.cancel(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 852 | void afterTimerWait(const std::weak_ptr<self_type>& weakSelf, |
| 853 | const boost::system::error_code& ec) |
| 854 | { |
| 855 | // Note, we are ignoring other types of errors here; If the timer |
| 856 | // failed for any reason, we should still close the connection |
| 857 | std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock(); |
| 858 | if (!self) |
| 859 | { |
| 860 | if (ec == boost::asio::error::operation_aborted) |
| 861 | { |
| 862 | BMCWEB_LOG_DEBUG( |
| 863 | "{} Timer canceled on connection being destroyed", |
| 864 | logPtr(self.get())); |
| 865 | } |
| 866 | else |
| 867 | { |
| 868 | BMCWEB_LOG_CRITICAL("{} Failed to capture connection", |
| 869 | logPtr(self.get())); |
| 870 | } |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | self->timerStarted = false; |
| 875 | |
| 876 | if (ec) |
| 877 | { |
| 878 | if (ec == boost::asio::error::operation_aborted) |
| 879 | { |
| 880 | BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get())); |
| 881 | return; |
| 882 | } |
| 883 | BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec); |
| 884 | } |
| 885 | |
| 886 | BMCWEB_LOG_WARNING("{} Connection timed out, hard closing", |
| 887 | logPtr(self.get())); |
| 888 | |
| 889 | self->hardClose(); |
| 890 | } |
| 891 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 892 | void startDeadline() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 893 | { |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 894 | // Timer is already started so no further action is required. |
| 895 | if (timerStarted) |
| 896 | { |
| 897 | return; |
| 898 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 899 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 900 | std::chrono::seconds timeout(15); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 901 | |
| 902 | std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); |
| 903 | timer.expires_after(timeout); |
Ed Tanous | 116370d | 2024-10-08 10:37:28 -0700 | [diff] [blame] | 904 | timer.async_wait(std::bind_front(&self_type::afterTimerWait, this, |
| 905 | weak_from_this())); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 906 | |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 907 | timerStarted = true; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 908 | BMCWEB_LOG_DEBUG("{} timer started", logPtr(this)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 911 | bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH; |
| 912 | HttpType httpType = HttpType::BOTH; |
| 913 | |
| 914 | boost::asio::ssl::stream<Adaptor> adaptor; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 915 | Handler* handler; |
Ed Tanous | 1d1d778 | 2024-04-09 12:54:08 -0700 | [diff] [blame] | 916 | |
| 917 | boost::asio::ip::address ip; |
| 918 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 919 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 920 | // re-created on Connection reset |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 921 | std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 922 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 923 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 924 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 925 | std::shared_ptr<crow::Request> req; |
Ed Tanous | 89cda63 | 2024-04-16 08:45:54 -0700 | [diff] [blame] | 926 | std::string accept; |
Ed Tanous | cd7dbb3 | 2025-02-01 12:37:56 -0800 | [diff] [blame] | 927 | std::string http2settings; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 928 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 929 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 930 | std::shared_ptr<persistent_data::UserSession> userSession; |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 931 | std::shared_ptr<persistent_data::UserSession> mtlsSession; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 932 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 933 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 934 | |
Ed Tanous | 4cdc2e8 | 2023-01-13 10:03:22 -0800 | [diff] [blame] | 935 | bool keepAlive = true; |
| 936 | |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 937 | bool timerStarted = false; |
| 938 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 939 | std::function<std::string()>& getCachedDateStr; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 940 | |
| 941 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 942 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 943 | |
| 944 | using std::enable_shared_from_this< |
| 945 | Connection<Adaptor, Handler>>::weak_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 946 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 947 | } // namespace crow |