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