Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 2 | #include "config.h" |
| 3 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 4 | #include "http_response.h" |
| 5 | #include "logging.h" |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 6 | #include "timer_queue.h" |
| 7 | #include "utility.h" |
| 8 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 9 | #include "authorization.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 10 | #include "http_utility.hpp" |
| 11 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 12 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 13 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 14 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 15 | #include <boost/asio/ip/tcp.hpp> |
Ed Tanous | 2f1ebcd | 2019-02-13 19:39:07 -0800 | [diff] [blame] | 16 | #include <boost/asio/ssl.hpp> |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 17 | #include <boost/beast/core/flat_static_buffer.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 | #include <boost/beast/http.hpp> |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 19 | #include <boost/beast/ssl/ssl_stream.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 20 | #include <boost/beast/websocket.hpp> |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 21 | #include <json_html_serializer.hpp> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 22 | #include <security_headers.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 23 | #include <ssl_key_handler.hpp> |
| 24 | |
Manojkiran Eda | 4425044 | 2020-06-16 12:51:38 +0530 | [diff] [blame] | 25 | #include <atomic> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | #include <chrono> |
| 27 | #include <vector> |
| 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | namespace crow |
| 30 | { |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | inline void prettyPrintJson(crow::Response& res) |
| 33 | { |
Ed Tanous | 57fce80 | 2019-05-21 13:00:34 -0700 | [diff] [blame] | 34 | json_html_util::dumpHtml(res.body(), res.jsonValue); |
| 35 | |
Ed Tanous | 93ef580 | 2019-01-03 10:15:41 -0800 | [diff] [blame] | 36 | res.addHeader("Content-Type", "text/html;charset=UTF-8"); |
Ed Tanous | 257f579 | 2018-03-17 14:40:09 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 39 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 40 | static std::atomic<int> connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 41 | #endif |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 42 | |
Adriana Kobylak | 0e1cf26 | 2019-12-05 13:57:57 -0600 | [diff] [blame] | 43 | // request body limit size set by the BMCWEB_HTTP_REQ_BODY_LIMIT_MB option |
| 44 | constexpr unsigned int httpReqBodyLimit = |
| 45 | 1024 * 1024 * BMCWEB_HTTP_REQ_BODY_LIMIT_MB; |
Jennifer Lee | acb7cfb | 2018-06-07 16:08:15 -0700 | [diff] [blame] | 46 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 47 | constexpr uint64_t loggedOutPostBodyLimit = 4096; |
| 48 | |
| 49 | constexpr uint32_t httpHeaderLimit = 8192; |
| 50 | |
| 51 | // drop all connections after 1 minute, this time limit was chosen |
| 52 | // arbitrarily and can be adjusted later if needed |
| 53 | static constexpr const size_t loggedInAttempts = |
| 54 | (60 / timerQueueTimeoutSeconds); |
| 55 | |
| 56 | static constexpr const size_t loggedOutAttempts = |
| 57 | (15 / timerQueueTimeoutSeconds); |
| 58 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 59 | template <typename Adaptor, typename Handler> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 60 | class Connection : |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 61 | public std::enable_shared_from_this<Connection<Adaptor, Handler>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | { |
| 63 | public: |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 64 | Connection(Handler* handlerIn, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | std::function<std::string()>& get_cached_date_str_f, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 66 | detail::TimerQueue& timerQueueIn, Adaptor adaptorIn) : |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 67 | adaptor(std::move(adaptorIn)), |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 68 | handler(handlerIn), getCachedDateStr(get_cached_date_str_f), |
| 69 | timerQueue(timerQueueIn) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
| 71 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | parser->body_limit(httpReqBodyLimit); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 73 | parser->header_limit(httpHeaderLimit); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | req.emplace(parser->get()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 75 | |
| 76 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 77 | auto caAvailable = !std::filesystem::is_empty( |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 78 | std::filesystem::path(ensuressl::trustStorePath)); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 79 | if (caAvailable && persistent_data::SessionStore::getInstance() |
| 80 | .getAuthMethodsConfig() |
| 81 | .tls) |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 82 | { |
| 83 | adaptor.set_verify_mode(boost::asio::ssl::verify_peer); |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 84 | std::string id = "bmcweb"; |
| 85 | int ret = SSL_set_session_id_context( |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 86 | adaptor.native_handle(), |
Ed Tanous | e7d1a1c | 2020-09-28 09:36:35 -0700 | [diff] [blame] | 87 | reinterpret_cast<const unsigned char*>(id.c_str()), |
| 88 | static_cast<unsigned int>(id.length())); |
| 89 | if (ret == 0) |
| 90 | { |
| 91 | BMCWEB_LOG_ERROR << this << " failed to set SSL id"; |
| 92 | } |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 95 | adaptor.set_verify_callback([this]( |
| 96 | bool preverified, |
| 97 | boost::asio::ssl::verify_context& ctx) { |
| 98 | // do nothing if TLS is disabled |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 99 | if (!persistent_data::SessionStore::getInstance() |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 100 | .getAuthMethodsConfig() |
| 101 | .tls) |
| 102 | { |
| 103 | BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 104 | return true; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // We always return true to allow full auth flow |
| 108 | if (!preverified) |
| 109 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 110 | BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
| 114 | X509_STORE_CTX* cts = ctx.native_handle(); |
| 115 | if (cts == nullptr) |
| 116 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 117 | BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // Get certificate |
| 122 | X509* peerCert = |
| 123 | X509_STORE_CTX_get_current_cert(ctx.native_handle()); |
| 124 | if (peerCert == nullptr) |
| 125 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 126 | BMCWEB_LOG_DEBUG << this |
| 127 | << " Cannot get current TLS certificate."; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | // Check if certificate is OK |
| 132 | int error = X509_STORE_CTX_get_error(cts); |
| 133 | if (error != X509_V_OK) |
| 134 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 135 | BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | // Check that we have reached final certificate in chain |
| 139 | int32_t depth = X509_STORE_CTX_get_error_depth(cts); |
| 140 | if (depth != 0) |
| 141 | |
| 142 | { |
| 143 | BMCWEB_LOG_DEBUG |
| 144 | << this << " Certificate verification in progress (depth " |
| 145 | << depth << "), waiting to reach final depth"; |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | BMCWEB_LOG_DEBUG << this |
| 150 | << " Certificate verification of final depth"; |
| 151 | |
| 152 | // Verify KeyUsage |
| 153 | bool isKeyUsageDigitalSignature = false; |
| 154 | bool isKeyUsageKeyAgreement = false; |
| 155 | |
| 156 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 157 | X509_get_ext_d2i(peerCert, NID_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 158 | |
| 159 | if (usage == nullptr) |
| 160 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 161 | BMCWEB_LOG_DEBUG << this << " TLS usage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
| 165 | for (int i = 0; i < usage->length; i++) |
| 166 | { |
| 167 | if (KU_DIGITAL_SIGNATURE & usage->data[i]) |
| 168 | { |
| 169 | isKeyUsageDigitalSignature = true; |
| 170 | } |
| 171 | if (KU_KEY_AGREEMENT & usage->data[i]) |
| 172 | { |
| 173 | isKeyUsageKeyAgreement = true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) |
| 178 | { |
| 179 | BMCWEB_LOG_DEBUG << this |
| 180 | << " Certificate ExtendedKeyUsage does " |
| 181 | "not allow provided certificate to " |
| 182 | "be used for user authentication"; |
| 183 | return true; |
| 184 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 185 | ASN1_BIT_STRING_free(usage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 186 | |
| 187 | // Determine that ExtendedKeyUsage includes Client Auth |
| 188 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 189 | stack_st_ASN1_OBJECT* extUsage = |
| 190 | static_cast<stack_st_ASN1_OBJECT*>(X509_get_ext_d2i( |
| 191 | peerCert, NID_ext_key_usage, nullptr, nullptr)); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 192 | |
| 193 | if (extUsage == nullptr) |
| 194 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 195 | BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 196 | return true; |
| 197 | } |
| 198 | |
| 199 | bool isExKeyUsageClientAuth = false; |
| 200 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 201 | { |
| 202 | if (NID_client_auth == |
| 203 | OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i))) |
| 204 | { |
| 205 | isExKeyUsageClientAuth = true; |
| 206 | break; |
| 207 | } |
| 208 | } |
Zbigniew Kurzynski | 09d02f8 | 2020-03-30 13:41:42 +0200 | [diff] [blame] | 209 | sk_ASN1_OBJECT_free(extUsage); |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 210 | |
| 211 | // Certificate has to have proper key usages set |
| 212 | if (!isExKeyUsageClientAuth) |
| 213 | { |
| 214 | BMCWEB_LOG_DEBUG << this |
| 215 | << " Certificate ExtendedKeyUsage does " |
| 216 | "not allow provided certificate to " |
| 217 | "be used for user authentication"; |
| 218 | return true; |
| 219 | } |
| 220 | std::string sslUser; |
| 221 | // Extract username contained in CommonName |
| 222 | sslUser.resize(256, '\0'); |
| 223 | |
| 224 | int status = X509_NAME_get_text_by_NID( |
| 225 | X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), |
| 226 | static_cast<int>(sslUser.size())); |
| 227 | |
| 228 | if (status == -1) |
| 229 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 230 | BMCWEB_LOG_DEBUG |
| 231 | << this << " TLS cannot get username to create session"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
| 235 | size_t lastChar = sslUser.find('\0'); |
| 236 | if (lastChar == std::string::npos || lastChar == 0) |
| 237 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 238 | BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | sslUser.resize(lastChar); |
| 242 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 243 | session = |
| 244 | persistent_data::SessionStore::getInstance() |
| 245 | .generateUserSession( |
| 246 | sslUser, persistent_data::PersistenceType::TIMEOUT); |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 247 | if (auto sp = session.lock()) |
| 248 | { |
| 249 | BMCWEB_LOG_DEBUG << this |
| 250 | << " Generating TLS session: " << sp->uniqueId; |
| 251 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 252 | return true; |
| 253 | }); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 254 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 255 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 256 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 257 | connectionCount++; |
| 258 | BMCWEB_LOG_DEBUG << this << " Connection open, total " |
| 259 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 260 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 261 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 262 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | ~Connection() |
| 264 | { |
| 265 | res.completeRequestHandler = nullptr; |
| 266 | cancelDeadlineTimer(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 267 | #ifdef BMCWEB_ENABLE_DEBUG |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 268 | connectionCount--; |
| 269 | BMCWEB_LOG_DEBUG << this << " Connection closed, total " |
| 270 | << connectionCount; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 271 | #endif |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 274 | Adaptor& socket() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | { |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 276 | return adaptor; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | void start() |
| 280 | { |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 281 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 282 | startDeadline(0); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 283 | // TODO(ed) Abstract this to a more clever class with the idea of an |
| 284 | // asynchronous "start" |
| 285 | if constexpr (std::is_same_v<Adaptor, |
| 286 | boost::beast::ssl_stream< |
| 287 | boost::asio::ip::tcp::socket>>) |
| 288 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 289 | adaptor.async_handshake(boost::asio::ssl::stream_base::server, |
| 290 | [this, self(shared_from_this())]( |
| 291 | const boost::system::error_code& ec) { |
| 292 | if (ec) |
| 293 | { |
| 294 | return; |
| 295 | } |
| 296 | doReadHeaders(); |
| 297 | }); |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 298 | } |
| 299 | else |
| 300 | { |
| 301 | doReadHeaders(); |
| 302 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 303 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 304 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 305 | void handle() |
| 306 | { |
| 307 | cancelDeadlineTimer(); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 308 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 309 | bool isInvalidRequest = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 310 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 311 | // Check for HTTP version 1.1. |
| 312 | if (req->version() == 11) |
| 313 | { |
| 314 | if (req->getHeaderValue(boost::beast::http::field::host).empty()) |
| 315 | { |
| 316 | isInvalidRequest = true; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 317 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 320 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 321 | BMCWEB_LOG_INFO << "Request: " |
| 322 | << " " << this << " HTTP/" << req->version() / 10 << "." |
| 323 | << req->version() % 10 << ' ' << req->methodString() |
| 324 | << " " << req->target(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 325 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 326 | needToCallAfterHandlers = false; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 327 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | if (!isInvalidRequest) |
| 329 | { |
| 330 | res.completeRequestHandler = [] {}; |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 331 | res.isAliveHelper = [this]() -> bool { return isAlive(); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 332 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 333 | req->ioService = static_cast<decltype(req->ioService)>( |
| 334 | &adaptor.get_executor().context()); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 335 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 336 | if (!res.completed) |
| 337 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 338 | needToCallAfterHandlers = true; |
| 339 | res.completeRequestHandler = [self(shared_from_this())] { |
| 340 | self->completeRequest(); |
| 341 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 342 | if (req->isUpgrade() && |
| 343 | boost::iequals( |
| 344 | req->getHeaderValue(boost::beast::http::field::upgrade), |
| 345 | "websocket")) |
| 346 | { |
| 347 | handler->handleUpgrade(*req, res, std::move(adaptor)); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 348 | // delete lambda with self shared_ptr |
| 349 | // to enable connection destruction |
| 350 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 351 | return; |
| 352 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 353 | handler->handle(*req, res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 354 | } |
| 355 | else |
| 356 | { |
| 357 | completeRequest(); |
| 358 | } |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | completeRequest(); |
| 363 | } |
| 364 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 365 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 366 | bool isAlive() |
| 367 | { |
| 368 | |
| 369 | if constexpr (std::is_same_v<Adaptor, |
| 370 | boost::beast::ssl_stream< |
| 371 | boost::asio::ip::tcp::socket>>) |
| 372 | { |
| 373 | return adaptor.next_layer().is_open(); |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | return adaptor.is_open(); |
| 378 | } |
| 379 | } |
| 380 | void close() |
| 381 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 382 | if constexpr (std::is_same_v<Adaptor, |
| 383 | boost::beast::ssl_stream< |
| 384 | boost::asio::ip::tcp::socket>>) |
| 385 | { |
| 386 | adaptor.next_layer().close(); |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 387 | #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
| 388 | if (auto sp = session.lock()) |
| 389 | { |
Zbigniew Kurzynski | 009c2a4 | 2019-11-14 13:37:15 +0100 | [diff] [blame] | 390 | BMCWEB_LOG_DEBUG << this |
| 391 | << " Removing TLS session: " << sp->uniqueId; |
Kowalski, Kamil | 55e43f6 | 2019-07-10 13:12:57 +0200 | [diff] [blame] | 392 | persistent_data::SessionStore::getInstance().removeSession(sp); |
| 393 | } |
| 394 | #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 395 | } |
| 396 | else |
| 397 | { |
| 398 | adaptor.close(); |
| 399 | } |
| 400 | } |
| 401 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 402 | void completeRequest() |
| 403 | { |
| 404 | BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' |
| 405 | << res.resultInt() << " keepalive=" << req->keepAlive(); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 406 | |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 407 | addSecurityHeaders(res); |
| 408 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 409 | if (needToCallAfterHandlers) |
| 410 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 411 | crow::authorization::cleanupTempSession(*req); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 412 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 413 | |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 414 | if (!isAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 415 | { |
| 416 | // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << |
| 417 | // isReading |
| 418 | // << ' ' << isWriting; |
| 419 | // delete this; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 420 | |
| 421 | // delete lambda with self shared_ptr |
| 422 | // to enable connection destruction |
| 423 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | if (res.body().empty() && !res.jsonValue.empty()) |
| 427 | { |
| 428 | if (http_helpers::requestPrefersHtml(*req)) |
| 429 | { |
| 430 | prettyPrintJson(res); |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | res.jsonMode(); |
Jason M. Bills | 193ad2f | 2018-09-26 15:08:52 -0700 | [diff] [blame] | 435 | res.body() = res.jsonValue.dump(2, ' ', true); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 438 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 439 | if (res.resultInt() >= 400 && res.body().empty()) |
| 440 | { |
| 441 | res.body() = std::string(res.reason()); |
| 442 | } |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 443 | |
| 444 | if (res.result() == boost::beast::http::status::no_content) |
| 445 | { |
| 446 | // Boost beast throws if content is provided on a no-content |
| 447 | // response. Ideally, this would never happen, but in the case that |
| 448 | // it does, we don't want to throw. |
| 449 | BMCWEB_LOG_CRITICAL |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 450 | << this << " Response content provided but code was no-content"; |
Ed Tanous | 6295bec | 2019-09-03 10:11:01 -0700 | [diff] [blame] | 451 | res.body().clear(); |
| 452 | } |
| 453 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 454 | res.addHeader(boost::beast::http::field::date, getCachedDateStr()); |
| 455 | |
| 456 | res.keepAlive(req->keepAlive()); |
| 457 | |
| 458 | doWrite(); |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 459 | |
| 460 | // delete lambda with self shared_ptr |
| 461 | // to enable connection destruction |
| 462 | res.completeRequestHandler = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | private: |
| 466 | void doReadHeaders() |
| 467 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 468 | BMCWEB_LOG_DEBUG << this << " doReadHeaders"; |
| 469 | |
| 470 | // Clean up any previous Connection. |
| 471 | boost::beast::http::async_read_header( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 472 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 473 | [this, |
| 474 | self(shared_from_this())](const boost::system::error_code& ec, |
| 475 | std::size_t bytes_transferred) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 476 | BMCWEB_LOG_ERROR << this << " async_read_header " |
| 477 | << bytes_transferred << " Bytes"; |
| 478 | bool errorWhileReading = false; |
| 479 | if (ec) |
| 480 | { |
| 481 | errorWhileReading = true; |
| 482 | BMCWEB_LOG_ERROR |
| 483 | << this << " Error while reading: " << ec.message(); |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | // if the adaptor isn't open anymore, and wasn't handed to a |
| 488 | // websocket, treat as an error |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 489 | if (!isAlive() && !req->isUpgrade()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 490 | { |
| 491 | errorWhileReading = true; |
| 492 | } |
| 493 | } |
| 494 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 495 | cancelDeadlineTimer(); |
| 496 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 497 | if (errorWhileReading) |
| 498 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 499 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 500 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 504 | if (!req) |
| 505 | { |
| 506 | close(); |
| 507 | return; |
| 508 | } |
| 509 | |
Ed Tanous | dc7a793 | 2020-08-17 15:04:58 -0700 | [diff] [blame] | 510 | // Note, despite the bmcweb coding policy on use of exceptions |
| 511 | // for error handling, this one particular use of exceptions is |
| 512 | // deemed acceptible, as it solved a significant error handling |
| 513 | // problem that resulted in seg faults, the exact thing that the |
| 514 | // exceptions rule is trying to avoid. If at some point, |
| 515 | // boost::urls makes the parser object public (or we port it |
| 516 | // into bmcweb locally) this will be replaced with |
| 517 | // parser::parse, which returns a status code |
James Feist | 5a7e877 | 2020-07-22 09:08:38 -0700 | [diff] [blame] | 518 | |
Ed Tanous | dc7a793 | 2020-08-17 15:04:58 -0700 | [diff] [blame] | 519 | try |
| 520 | { |
| 521 | req->urlView = boost::urls::url_view(req->target()); |
| 522 | req->url = req->urlView.encoded_path(); |
| 523 | } |
Ed Tanous | 92ccb88 | 2020-08-18 10:36:33 -0700 | [diff] [blame] | 524 | catch (std::exception& p) |
Ed Tanous | dc7a793 | 2020-08-17 15:04:58 -0700 | [diff] [blame] | 525 | { |
Ed Tanous | 3bcc015 | 2020-08-25 07:47:29 -0700 | [diff] [blame] | 526 | BMCWEB_LOG_ERROR << p.what(); |
Ed Tanous | dc7a793 | 2020-08-17 15:04:58 -0700 | [diff] [blame] | 527 | } |
Ed Tanous | 92ccb88 | 2020-08-18 10:36:33 -0700 | [diff] [blame] | 528 | |
James Feist | 6964c98 | 2020-07-28 16:10:23 -0700 | [diff] [blame] | 529 | crow::authorization::authenticate(*req, res, session); |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 530 | |
| 531 | bool loggedIn = req && req->session; |
| 532 | if (loggedIn) |
| 533 | { |
| 534 | startDeadline(loggedInAttempts); |
| 535 | BMCWEB_LOG_DEBUG << "Starting slow deadline"; |
| 536 | |
James Feist | 5a7e877 | 2020-07-22 09:08:38 -0700 | [diff] [blame] | 537 | req->urlParams = req->urlView.params(); |
| 538 | |
| 539 | #ifdef BMCWEB_ENABLE_DEBUG |
| 540 | std::string paramList = ""; |
| 541 | for (const auto param : req->urlParams) |
| 542 | { |
| 543 | paramList += param->key() + " " + param->value() + " "; |
| 544 | } |
| 545 | BMCWEB_LOG_DEBUG << "QueryParams: " << paramList; |
| 546 | #endif |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 547 | } |
| 548 | else |
| 549 | { |
| 550 | const boost::optional<uint64_t> contentLength = |
| 551 | parser->content_length(); |
| 552 | if (contentLength && |
| 553 | *contentLength > loggedOutPostBodyLimit) |
| 554 | { |
| 555 | BMCWEB_LOG_DEBUG << "Content length greater than limit " |
| 556 | << *contentLength; |
| 557 | close(); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | startDeadline(loggedOutAttempts); |
| 562 | BMCWEB_LOG_DEBUG << "Starting quick deadline"; |
| 563 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 564 | doRead(); |
| 565 | }); |
| 566 | } |
| 567 | |
| 568 | void doRead() |
| 569 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 570 | BMCWEB_LOG_DEBUG << this << " doRead"; |
| 571 | |
| 572 | boost::beast::http::async_read( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 573 | adaptor, buffer, *parser, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 574 | [this, |
| 575 | self(shared_from_this())](const boost::system::error_code& ec, |
| 576 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 577 | BMCWEB_LOG_DEBUG << this << " async_read " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 578 | << " Bytes"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 579 | |
| 580 | bool errorWhileReading = false; |
| 581 | if (ec) |
| 582 | { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 583 | BMCWEB_LOG_ERROR |
| 584 | << this << " Error while reading: " << ec.message(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 585 | errorWhileReading = true; |
| 586 | } |
| 587 | else |
| 588 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 589 | if (isAlive()) |
| 590 | { |
| 591 | cancelDeadlineTimer(); |
| 592 | bool loggedIn = req && req->session; |
| 593 | if (loggedIn) |
| 594 | { |
| 595 | startDeadline(loggedInAttempts); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | startDeadline(loggedOutAttempts); |
| 600 | } |
| 601 | } |
| 602 | else |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 603 | { |
| 604 | errorWhileReading = true; |
| 605 | } |
| 606 | } |
| 607 | if (errorWhileReading) |
| 608 | { |
| 609 | cancelDeadlineTimer(); |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 610 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 611 | BMCWEB_LOG_DEBUG << this << " from read(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | handle(); |
| 615 | }); |
| 616 | } |
| 617 | |
| 618 | void doWrite() |
| 619 | { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 620 | bool loggedIn = req && req->session; |
| 621 | if (loggedIn) |
| 622 | { |
| 623 | startDeadline(loggedInAttempts); |
| 624 | } |
| 625 | else |
| 626 | { |
| 627 | startDeadline(loggedOutAttempts); |
| 628 | } |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 629 | BMCWEB_LOG_DEBUG << this << " doWrite"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 630 | res.preparePayload(); |
| 631 | serializer.emplace(*res.stringResponse); |
| 632 | boost::beast::http::async_write( |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 633 | adaptor, *serializer, |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 634 | [this, |
| 635 | self(shared_from_this())](const boost::system::error_code& ec, |
| 636 | std::size_t bytes_transferred) { |
Zbigniew Kurzynski | 2658d98 | 2019-11-19 18:01:08 +0100 | [diff] [blame] | 637 | BMCWEB_LOG_DEBUG << this << " async_write " << bytes_transferred |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 638 | << " bytes"; |
| 639 | |
James Feist | 54d8bb1 | 2020-07-20 13:28:59 -0700 | [diff] [blame] | 640 | cancelDeadlineTimer(); |
| 641 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 642 | if (ec) |
| 643 | { |
| 644 | BMCWEB_LOG_DEBUG << this << " from write(2)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 645 | return; |
| 646 | } |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 647 | if (!res.keepAlive()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 648 | { |
Ed Tanous | e278c18 | 2019-03-13 16:23:37 -0700 | [diff] [blame] | 649 | close(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 650 | BMCWEB_LOG_DEBUG << this << " from write(1)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
| 654 | serializer.reset(); |
| 655 | BMCWEB_LOG_DEBUG << this << " Clearing response"; |
| 656 | res.clear(); |
| 657 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Gunnar Mills | ded2a1e | 2020-07-24 09:46:33 -0500 | [diff] [blame] | 658 | parser->body_limit(httpReqBodyLimit); // reset body limit for |
| 659 | // newly created parser |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 660 | buffer.consume(buffer.size()); |
| 661 | |
| 662 | req.emplace(parser->get()); |
| 663 | doReadHeaders(); |
| 664 | }); |
| 665 | } |
| 666 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 667 | void cancelDeadlineTimer() |
| 668 | { |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 669 | if (timerCancelKey) |
| 670 | { |
| 671 | BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue |
| 672 | << ' ' << *timerCancelKey; |
| 673 | timerQueue.cancel(*timerCancelKey); |
| 674 | timerCancelKey.reset(); |
| 675 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 676 | } |
| 677 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 678 | void startDeadline(size_t timerIterations) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 679 | { |
| 680 | cancelDeadlineTimer(); |
| 681 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 682 | if (timerIterations) |
| 683 | { |
| 684 | timerIterations--; |
| 685 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 686 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 687 | timerCancelKey = |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 688 | timerQueue.add([self(shared_from_this()), timerIterations, |
| 689 | readCount{parser->get().body().size()}] { |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 690 | // Mark timer as not active to avoid canceling it during |
| 691 | // Connection destructor which leads to double free issue |
| 692 | self->timerCancelKey.reset(); |
| 693 | if (!self->isAlive()) |
| 694 | { |
| 695 | return; |
| 696 | } |
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 697 | |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 698 | bool loggedIn = self->req && self->req->session; |
| 699 | // allow slow uploads for logged in users |
| 700 | if (loggedIn && self->parser->get().body().size() > readCount) |
| 701 | { |
| 702 | BMCWEB_LOG_DEBUG << self.get() |
| 703 | << " restart timer - read in progress"; |
| 704 | self->startDeadline(timerIterations); |
| 705 | return; |
| 706 | } |
| 707 | |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 708 | // Threshold can be used to drop slow connections |
| 709 | // to protect against slow-rate DoS attack |
| 710 | if (timerIterations) |
| 711 | { |
James Feist | be5dfca | 2020-07-22 08:54:59 -0700 | [diff] [blame] | 712 | BMCWEB_LOG_DEBUG << self.get() << " restart timer"; |
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 713 | self->startDeadline(timerIterations); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | self->close(); |
| 718 | }); |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 719 | |
| 720 | if (!timerCancelKey) |
| 721 | { |
| 722 | close(); |
| 723 | return; |
| 724 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 725 | BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 726 | << *timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | private: |
| 730 | Adaptor adaptor; |
| 731 | Handler* handler; |
| 732 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 733 | // Making this a std::optional allows it to be efficiently destroyed and |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 734 | // re-created on Connection reset |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 735 | std::optional< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 736 | boost::beast::http::request_parser<boost::beast::http::string_body>> |
| 737 | parser; |
| 738 | |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 739 | boost::beast::flat_static_buffer<8192> buffer; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 740 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 741 | std::optional<boost::beast::http::response_serializer< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 742 | boost::beast::http::string_body>> |
| 743 | serializer; |
| 744 | |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 745 | std::optional<crow::Request> req; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 746 | crow::Response res; |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 747 | |
| 748 | std::weak_ptr<persistent_data::UserSession> session; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 749 | |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 750 | std::optional<size_t> timerCancelKey; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 751 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 752 | bool needToCallAfterHandlers{}; |
| 753 | bool needToStartReadAfterComplete{}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 754 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 755 | std::function<std::string()>& getCachedDateStr; |
| 756 | detail::TimerQueue& timerQueue; |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 757 | |
| 758 | using std::enable_shared_from_this< |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 759 | Connection<Adaptor, Handler>>::shared_from_this; |
Ed Tanous | 3112a14 | 2018-11-29 15:45:10 -0800 | [diff] [blame] | 760 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 761 | } // namespace crow |