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