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