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 | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 8 | #include "http_response.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | #include "http_utility.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 10 | #include "logging.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 11 | #include "mutual_tls.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 12 | #include "ssl_key_handler.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 13 | #include "utility.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 15 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 16 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 17 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 18 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 19 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 20 | #include <boost/beast/_experimental/test/stream.hpp> |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 21 | #include <boost/beast/core/buffers_generator.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 22 | #include <boost/beast/core/flat_static_buffer.hpp> |
Myung Bae | a4326fe | 2023-01-10 14:29:24 -0600 | [diff] [blame] | 23 | #include <boost/beast/http/error.hpp> |
Ed Tanous | 918ef25 | 2022-05-25 10:40:41 -0700 | [diff] [blame] | 24 | #include <boost/beast/http/parser.hpp> |
| 25 | #include <boost/beast/http/read.hpp> |
Ed Tanous | 918ef25 | 2022-05-25 10:40:41 -0700 | [diff] [blame] | 26 | #include <boost/beast/http/write.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 27 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | #include <boost/beast/websocket.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 29 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 30 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 31 | #include <chrono> |
| 32 | #include <vector> |
| 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | namespace crow |
| 35 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 36 | |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 37 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 38 | static int connectionCount = 0; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 40 | // request body limit size set by the bmcwebHttpReqBodyLimitMb option |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 41 | constexpr uint64_t httpReqBodyLimit = 1024UL * 1024UL * |
| 42 | bmcwebHttpReqBodyLimitMb; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 43 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 44 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 45 | |
| 46 | constexpr uint32_t httpHeaderLimit = 8192; |
| 47 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 48 | template <typename> |
| 49 | struct IsTls : std::false_type |
| 50 | {}; |
| 51 | |
| 52 | template <typename T> |
| 53 | struct IsTls<boost::beast::ssl_stream<T>> : std::true_type |
| 54 | {}; |
| 55 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 56 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 57 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 58 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | { |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 60 | using self_type = Connection<Adaptor, Handler>; |
| 61 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | public: |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 63 | Connection(Handler* handlerIn, boost::asio::steady_timer&& timerIn, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 64 | std::function<std::string()>& getCachedDateStrF, |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 65 | Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 66 | adaptor(std::move(adaptorIn)), |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 67 | handler(handlerIn), timer(std::move(timerIn)), |
| 68 | getCachedDateStr(getCachedDateStrF) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | { |
| 70 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 72 | parser->header_limit(httpHeaderLimit); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 73 | |
| 74 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 75 | prepareMutualTls(); |
| 76 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 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 | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 80 | BMCWEB_LOG_DEBUG("{} Connection open, total {}", logPtr(this), |
| 81 | connectionCount); |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | ~Connection() |
| 85 | { |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 86 | res.setCompleteRequestHandler(nullptr); |
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 | { |
Michal Orzel | 7aa9ab0 | 2023-08-18 16:59:11 +0200 | [diff] [blame] | 106 | boost::asio::ip::address ipAddress; |
| 107 | if (getClientIp(ipAddress)) |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 108 | { |
Michal Orzel | 7aa9ab0 | 2023-08-18 16:59:11 +0200 | [diff] [blame] | 109 | return true; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 110 | } |
Michal Orzel | 7aa9ab0 | 2023-08-18 16:59:11 +0200 | [diff] [blame] | 111 | |
| 112 | mtlsSession = verifyMtlsUser(ipAddress, ctx); |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 113 | if (mtlsSession) |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 114 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 115 | BMCWEB_LOG_DEBUG("{} Generating TLS session: {}", logPtr(this), |
| 116 | mtlsSession->uniqueId); |
Ed Tanous | 7c8e064 | 2022-02-21 12:11:14 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
Ed Tanous | 40aa058 | 2021-07-14 13:24:40 -0700 | [diff] [blame] | 122 | void prepareMutualTls() |
| 123 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 124 | if constexpr (IsTls<Adaptor>::value) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 125 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 126 | std::error_code error; |
| 127 | std::filesystem::path caPath(ensuressl::trustStorePath); |
| 128 | auto caAvailable = !std::filesystem::is_empty(caPath, error); |
| 129 | caAvailable = caAvailable && !error; |
| 130 | if (caAvailable && persistent_data::SessionStore::getInstance() |
| 131 | .getAuthMethodsConfig() |
| 132 | .tls) |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 133 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 134 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
| 135 | std::string id = "bmcweb"; |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 136 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 137 | const char* cStr = id.c_str(); |
| 138 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 139 | const auto* idC = reinterpret_cast<const unsigned char*>(cStr); |
| 140 | int ret = SSL_set_session_id_context( |
| 141 | adaptor.native_handle(), idC, |
| 142 | static_cast<unsigned int>(id.length())); |
| 143 | if (ret == 0) |
| 144 | { |
| 145 | BMCWEB_LOG_ERROR("{} failed to set SSL id", logPtr(this)); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | adaptor.set_verify_callback( |
| 150 | std::bind_front(&self_type::tlsVerifyCallback, this)); |
| 151 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 154 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 155 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 156 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 159 | void start() |
| 160 | { |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 161 | if (connectionCount >= 100) |
| 162 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 163 | BMCWEB_LOG_CRITICAL("{}Max connection count exceeded.", |
| 164 | logPtr(this)); |
Ed Tanous | 6fbdbca | 2021-12-06 14:36:06 -0800 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 168 | startDeadline(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 169 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 170 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 171 | // asynchronous "start" |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 172 | if constexpr (IsTls<Adaptor>::value) |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 173 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 174 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 175 | [this, self(shared_from_this())]( |
| 176 | const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 177 | if (ec) |
| 178 | { |
| 179 | return; |
| 180 | } |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 181 | afterSslHandshake(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 182 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
| 186 | doReadHeaders(); |
| 187 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 188 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 189 | |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 190 | void afterSslHandshake() |
| 191 | { |
| 192 | // If http2 is enabled, negotiate the protocol |
| 193 | if constexpr (bmcwebEnableHTTP2) |
| 194 | { |
| 195 | const unsigned char* alpn = nullptr; |
| 196 | unsigned int alpnlen = 0; |
| 197 | SSL_get0_alpn_selected(adaptor.native_handle(), &alpn, &alpnlen); |
| 198 | if (alpn != nullptr) |
| 199 | { |
| 200 | std::string_view selectedProtocol( |
| 201 | std::bit_cast<const char*>(alpn), alpnlen); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 202 | BMCWEB_LOG_DEBUG("ALPN selected protocol \"{}\" len: {}", |
| 203 | selectedProtocol, alpnlen); |
Ed Tanous | fca2cbe | 2021-01-28 14:49:59 -0800 | [diff] [blame] | 204 | if (selectedProtocol == "h2") |
| 205 | { |
| 206 | auto http2 = |
| 207 | std::make_shared<HTTP2Connection<Adaptor, Handler>>( |
| 208 | std::move(adaptor), handler, getCachedDateStr); |
| 209 | http2->start(); |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | doReadHeaders(); |
| 216 | } |
| 217 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 218 | void handle() |
| 219 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 220 | std::error_code reqEc; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 221 | if (!parser) |
| 222 | { |
| 223 | return; |
| 224 | } |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 225 | crow::Request& thisReq = req.emplace(parser->release(), reqEc); |
| 226 | if (reqEc) |
| 227 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 228 | BMCWEB_LOG_DEBUG("Request failed to construct{}", reqEc.message()); |
Gunnar Mills | 262f115 | 2022-12-20 15:18:47 -0600 | [diff] [blame] | 229 | res.result(boost::beast::http::status::bad_request); |
| 230 | completeRequest(res); |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 231 | return; |
| 232 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 233 | thisReq.session = userSession; |
| 234 | |
Ivan Mikhaylov | f65b0be | 2021-04-19 10:05:30 +0000 | [diff] [blame] | 235 | // Fetch the client IP address |
| 236 | readClientIp(); |
| 237 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | // Check for HTTP version 1.1. |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 239 | if (thisReq.version() == 11) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 240 | { |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 241 | if (thisReq.getHeaderValue(boost::beast::http::field::host).empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 243 | res.result(boost::beast::http::status::bad_request); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 244 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 245 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 248 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 249 | BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this), |
| 250 | thisReq.version() / 10, thisReq.version() % 10, |
| 251 | thisReq.methodString(), thisReq.target(), |
| 252 | thisReq.ipAddress.to_string()); |
Ed Tanous | d32c4fa | 2021-09-14 13:16:51 -0700 | [diff] [blame] | 253 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 254 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 255 | |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 256 | thisReq.ioService = static_cast<decltype(thisReq.ioService)>( |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 257 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 258 | |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 259 | if (res.completed) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 260 | { |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 261 | completeRequest(res); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 262 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | } |
Ed Tanous | 4cdc2e8 | 2023-01-13 10:03:22 -0800 | [diff] [blame] | 264 | keepAlive = thisReq.keepAlive(); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 265 | if constexpr (!std::is_same_v<Adaptor, boost::beast::test::stream>) |
Ed Tanous | 1d3c14a | 2021-09-22 18:54:40 -0700 | [diff] [blame] | 266 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 267 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
| 268 | if (!crow::authentication::isOnAllowlist(req->url().path(), |
| 269 | req->method()) && |
| 270 | thisReq.session == nullptr) |
| 271 | { |
| 272 | BMCWEB_LOG_WARNING("Authentication failed"); |
| 273 | forward_unauthorized::sendUnauthorized( |
| 274 | req->url().encoded_path(), |
| 275 | req->getHeaderValue("X-Requested-With"), |
| 276 | req->getHeaderValue("Accept"), res); |
| 277 | completeRequest(res); |
| 278 | return; |
| 279 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame] | 280 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 281 | } |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 282 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 283 | BMCWEB_LOG_DEBUG("Setting completion handler"); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 284 | asyncResp->res.setCompleteRequestHandler( |
| 285 | [self(shared_from_this())](crow::Response& thisRes) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 286 | self->completeRequest(thisRes); |
| 287 | }); |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 288 | bool isSse = |
| 289 | isContentTypeAllowed(req->getHeaderValue("Accept"), |
| 290 | http_helpers::ContentType::EventStream, false); |
V-Sanjana | 88ada3b | 2023-04-13 15:18:31 +0530 | [diff] [blame] | 291 | if ((thisReq.isUpgrade() && |
| 292 | boost::iequals( |
| 293 | thisReq.getHeaderValue(boost::beast::http::field::upgrade), |
| 294 | "websocket")) || |
Ed Tanous | 6fde95f | 2023-06-01 07:33:34 -0700 | [diff] [blame] | 295 | isSse) |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 296 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 297 | asyncResp->res.setCompleteRequestHandler( |
| 298 | [self(shared_from_this())](crow::Response& thisRes) { |
| 299 | if (thisRes.result() != boost::beast::http::status::ok) |
| 300 | { |
| 301 | // When any error occurs before handle upgradation, |
| 302 | // the result in response will be set to respective |
| 303 | // error. By default the Result will be OK (200), |
| 304 | // which implies successful handle upgrade. Response |
| 305 | // needs to be sent over this connection only on |
| 306 | // failure. |
| 307 | self->completeRequest(thisRes); |
| 308 | return; |
| 309 | } |
| 310 | }); |
| 311 | handler->handleUpgrade(thisReq, asyncResp, std::move(adaptor)); |
Ed Tanous | 6c7f01d | 2021-08-25 13:42:35 -0700 | [diff] [blame] | 312 | return; |
| 313 | } |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 314 | std::string_view expected = |
| 315 | req->getHeaderValue(boost::beast::http::field::if_none_match); |
| 316 | if (!expected.empty()) |
| 317 | { |
| 318 | res.setExpectedHash(expected); |
| 319 | } |
Ed Tanous | 596b203 | 2021-09-13 10:32:22 -0700 | [diff] [blame] | 320 | handler->handle(thisReq, asyncResp); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 321 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 322 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 323 | bool isAlive() |
| 324 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 325 | if constexpr (IsTls<Adaptor>::value) |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 326 | { |
| 327 | return adaptor.next_layer().is_open(); |
| 328 | } |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 329 | else if constexpr (std::is_same_v<Adaptor, boost::beast::test::stream>) |
| 330 | { |
| 331 | return true; |
| 332 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 333 | else |
| 334 | { |
| 335 | return adaptor.is_open(); |
| 336 | } |
| 337 | } |
| 338 | void close() |
| 339 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 340 | if constexpr (IsTls<Adaptor>::value) |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 341 | { |
| 342 | adaptor.next_layer().close(); |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 343 | if (mtlsSession != nullptr) |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 344 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 345 | BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this), |
| 346 | mtlsSession->uniqueId); |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 347 | persistent_data::SessionStore::getInstance().removeSession( |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 348 | mtlsSession); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 349 | } |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 350 | } |
| 351 | else |
| 352 | { |
| 353 | adaptor.close(); |
| 354 | } |
| 355 | } |
| 356 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 357 | void completeRequest(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 358 | { |
Ed Tanous | f79b7a5 | 2021-09-22 19:04:29 -0700 | [diff] [blame] | 359 | if (!req) |
| 360 | { |
| 361 | return; |
| 362 | } |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 363 | res = std::move(thisRes); |
Ed Tanous | 4cdc2e8 | 2023-01-13 10:03:22 -0800 | [diff] [blame] | 364 | res.keepAlive(keepAlive); |
Ed Tanous | 5ae6f92 | 2023-01-09 10:45:53 -0800 | [diff] [blame] | 365 | |
Ed Tanous | ed5f895 | 2023-06-22 14:06:22 -0700 | [diff] [blame] | 366 | completeResponseFields(*req, res); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 367 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 368 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 369 | { |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 370 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 371 | return; |
| 372 | } |
Ed Tanous | 291d709 | 2022-04-13 12:34:57 -0700 | [diff] [blame] | 373 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 374 | doWrite(res); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 375 | |
| 376 | // delete lambda with self shared_ptr |
| 377 | // to enable connection destruction |
John Edward Broadbent | 4147b8a | 2021-07-19 16:52:24 -0700 | [diff] [blame] | 378 | res.setCompleteRequestHandler(nullptr); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 381 | void readClientIp() |
| 382 | { |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 383 | boost::asio::ip::address ip; |
| 384 | boost::system::error_code ec = getClientIp(ip); |
| 385 | if (ec) |
| 386 | { |
| 387 | return; |
| 388 | } |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 389 | if (!req) |
| 390 | { |
| 391 | return; |
| 392 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 393 | req->ipAddress = ip; |
| 394 | } |
| 395 | |
| 396 | boost::system::error_code getClientIp(boost::asio::ip::address& ip) |
| 397 | { |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 398 | boost::system::error_code ec; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 399 | BMCWEB_LOG_DEBUG("Fetch the client IP address"); |
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); |
| 412 | return ec; |
| 413 | } |
| 414 | ip = endpoint.address(); |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 415 | } |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 416 | return ec; |
Sunitha Harish | c0ea7ae | 2020-10-30 02:37:30 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 419 | private: |
| 420 | void doReadHeaders() |
| 421 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 422 | BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this)); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 423 | if (!parser) |
| 424 | { |
| 425 | return; |
| 426 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 427 | // Clean up any previous Connection. |
| 428 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 429 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 430 | [this, |
| 431 | self(shared_from_this())](const boost::system::error_code& ec, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 432 | std::size_t bytesTransferred) { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 433 | BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this), |
| 434 | bytesTransferred); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 435 | bool errorWhileReading = false; |
| 436 | if (ec) |
| 437 | { |
| 438 | errorWhileReading = true; |
Myung Bae | a4326fe | 2023-01-10 14:29:24 -0600 | [diff] [blame] | 439 | if (ec == boost::beast::http::error::end_of_stream) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 440 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 441 | BMCWEB_LOG_WARNING("{} Error while reading: {}", |
| 442 | logPtr(this), ec.message()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 443 | } |
| 444 | else |
| 445 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 446 | BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this), |
| 447 | ec.message()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 448 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
| 452 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 453 | // websocket, treat as an error |
| 454 | if (!isAlive() && |
| 455 | !boost::beast::websocket::is_upgrade(parser->get())) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 456 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 457 | errorWhileReading = true; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | cancelDeadlineTimer(); |
| 462 | |
| 463 | if (errorWhileReading) |
| 464 | { |
| 465 | close(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 466 | BMCWEB_LOG_DEBUG("{} from read(1)", logPtr(this)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | |
| 470 | readClientIp(); |
| 471 | |
| 472 | boost::asio::ip::address ip; |
| 473 | if (getClientIp(ip)) |
| 474 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 475 | BMCWEB_LOG_DEBUG("Unable to get client IP"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 476 | } |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 477 | if constexpr (!std::is_same_v<Adaptor, boost::beast::test::stream>) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 478 | { |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 479 | #ifndef BMCWEB_INSECURE_DISABLE_AUTHX |
| 480 | boost::beast::http::verb method = parser->get().method(); |
| 481 | userSession = crow::authentication::authenticate( |
| 482 | ip, res, method, parser->get().base(), mtlsSession); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 483 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 484 | bool loggedIn = userSession != nullptr; |
| 485 | if (!loggedIn) |
| 486 | { |
| 487 | const boost::optional<uint64_t> contentLength = |
| 488 | parser->content_length(); |
| 489 | if (contentLength && |
| 490 | *contentLength > loggedOutPostBodyLimit) |
| 491 | { |
| 492 | BMCWEB_LOG_DEBUG("Content length greater than limit {}", |
| 493 | *contentLength); |
| 494 | close(); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | BMCWEB_LOG_DEBUG("Starting quick deadline"); |
| 499 | } |
Nan Zhou | a43ea82 | 2022-05-27 00:42:44 +0000 | [diff] [blame] | 500 | #endif // BMCWEB_INSECURE_DISABLE_AUTHX |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 501 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 502 | |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 503 | if (parser->is_done()) |
| 504 | { |
| 505 | handle(); |
| 506 | return; |
| 507 | } |
| 508 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 509 | doRead(); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 510 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void doRead() |
| 514 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 515 | BMCWEB_LOG_DEBUG("{} doRead", logPtr(this)); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 516 | if (!parser) |
| 517 | { |
| 518 | return; |
| 519 | } |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 520 | startDeadline(); |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 521 | boost::beast::http::async_read_some( |
| 522 | adaptor, buffer, *parser, |
| 523 | [this, |
| 524 | self(shared_from_this())](const boost::system::error_code& ec, |
| 525 | std::size_t bytesTransferred) { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 526 | BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this), |
| 527 | bytesTransferred); |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 528 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 529 | if (ec) |
| 530 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 531 | BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this), |
| 532 | ec.message()); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 533 | close(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 534 | BMCWEB_LOG_DEBUG("{} from read(1)", logPtr(this)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 535 | return; |
| 536 | } |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 537 | |
| 538 | // If the user is logged in, allow them to send files incrementally |
| 539 | // one piece at a time. If authentication is disabled then there is |
| 540 | // no user session hence always allow to send one piece at a time. |
| 541 | if (userSession != nullptr) |
| 542 | { |
| 543 | cancelDeadlineTimer(); |
| 544 | } |
| 545 | if (!parser->is_done()) |
| 546 | { |
| 547 | doRead(); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | cancelDeadlineTimer(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 552 | handle(); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 553 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 556 | void afterDoWrite(const std::shared_ptr<self_type>& /*self*/, |
| 557 | const boost::system::error_code& ec, |
| 558 | std::size_t bytesTransferred) |
| 559 | { |
| 560 | BMCWEB_LOG_DEBUG("{} async_write {} bytes", logPtr(this), |
| 561 | bytesTransferred); |
| 562 | |
| 563 | cancelDeadlineTimer(); |
| 564 | |
| 565 | if (ec) |
| 566 | { |
| 567 | BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this)); |
| 568 | return; |
| 569 | } |
| 570 | if (!keepAlive) |
| 571 | { |
| 572 | close(); |
| 573 | BMCWEB_LOG_DEBUG("{} from write(1)", logPtr(this)); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this)); |
| 578 | res.clear(); |
| 579 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 580 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 581 | // newly created parser |
| 582 | buffer.consume(buffer.size()); |
| 583 | |
| 584 | userSession = nullptr; |
| 585 | |
| 586 | // Destroy the Request via the std::optional |
| 587 | req.reset(); |
| 588 | doReadHeaders(); |
| 589 | } |
| 590 | |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 591 | void doWrite(crow::Response& thisRes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 592 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 593 | BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this)); |
Nan Zhou | 72374eb | 2022-01-27 17:06:51 -0800 | [diff] [blame] | 594 | thisRes.preparePayload(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 595 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 596 | startDeadline(); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 597 | boost::beast::async_write(adaptor, thisRes.generator(), |
| 598 | std::bind_front(&self_type::afterDoWrite, |
| 599 | this, shared_from_this())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 602 | void cancelDeadlineTimer() |
| 603 | { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 604 | timer.cancel(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 607 | void startDeadline() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 608 | { |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 609 | // Timer is already started so no further action is required. |
| 610 | if (timerStarted) |
| 611 | { |
| 612 | return; |
| 613 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 614 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 615 | std::chrono::seconds timeout(15); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 616 | |
| 617 | std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); |
| 618 | timer.expires_after(timeout); |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 619 | timer.async_wait([weakSelf](const boost::system::error_code& ec) { |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 620 | // Note, we are ignoring other types of errors here; If the timer |
| 621 | // failed for any reason, we should still close the connection |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 622 | std::shared_ptr<Connection<Adaptor, Handler>> self = |
| 623 | weakSelf.lock(); |
| 624 | if (!self) |
| 625 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 626 | BMCWEB_LOG_CRITICAL("{} Failed to capture connection", |
| 627 | logPtr(self.get())); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 628 | return; |
| 629 | } |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 630 | |
| 631 | self->timerStarted = false; |
| 632 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 633 | if (ec == boost::asio::error::operation_aborted) |
| 634 | { |
| 635 | // Canceled wait means the path succeeeded. |
| 636 | return; |
| 637 | } |
| 638 | if (ec) |
| 639 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 640 | BMCWEB_LOG_CRITICAL("{} timer failed {}", logPtr(self.get()), |
| 641 | ec); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 644 | BMCWEB_LOG_WARNING("{}Connection timed out, closing", |
| 645 | logPtr(self.get())); |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 646 | |
| 647 | self->close(); |
| 648 | }); |
| 649 | |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 650 | timerStarted = true; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 651 | BMCWEB_LOG_DEBUG("{} timer started", logPtr(this)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 654 | Adaptor adaptor; |
| 655 | Handler* handler; |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 656 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 658 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 659 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 660 | parser; |
| 661 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 662 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 664 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 665 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 666 | |
John Edward Broadbent | 59b98b2 | 2021-07-13 15:36:32 -0700 | [diff] [blame] | 667 | std::shared_ptr<persistent_data::UserSession> userSession; |
Boleslaw Ogonczyk Makowski | b496307 | 2023-02-06 09:59:58 +0100 | [diff] [blame] | 668 | std::shared_ptr<persistent_data::UserSession> mtlsSession; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 669 | |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 670 | boost::asio::steady_timer timer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 671 | |
Ed Tanous | 4cdc2e8 | 2023-01-13 10:03:22 -0800 | [diff] [blame] | 672 | bool keepAlive = true; |
| 673 | |
Ed Tanous | 7d243eb | 2023-01-23 15:57:41 -0800 | [diff] [blame] | 674 | bool timerStarted = false; |
| 675 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 676 | std::function<std::string()>& getCachedDateStr; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 677 | |
| 678 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 679 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 5dfb5b2 | 2021-12-03 11:24:53 -0800 | [diff] [blame] | 680 | |
| 681 | using std::enable_shared_from_this< |
| 682 | Connection<Adaptor, Handler>>::weak_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 683 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 684 | } // namespace crow |