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