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