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