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