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