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