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