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