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