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