blob: 35bf99c8c07a6241be209154d6037847ac768bba [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
Adriana Kobylak0e1cf262019-12-05 13:57:57 -06002#include "config.h"
3
Manojkiran Eda44250442020-06-16 12:51:38 +05304#include "http_response.h"
5#include "logging.h"
6#include "middleware_context.h"
7#include "timer_queue.h"
8#include "utility.h"
9
James Feist3909dc82020-04-03 10:58:55 -070010#include "authorization.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include "http_utility.hpp"
12
Ed Tanouse0d918b2018-03-27 17:41:04 -070013#include <boost/algorithm/string.hpp>
Ed Tanous257f5792018-03-17 14:40:09 -070014#include <boost/algorithm/string/predicate.hpp>
Ed Tanous8f626352018-12-19 14:51:54 -080015#include <boost/asio/io_context.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -080016#include <boost/asio/ip/tcp.hpp>
Ed Tanous2f1ebcd2019-02-13 19:39:07 -080017#include <boost/asio/ssl.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -080018#include <boost/beast/core/flat_static_buffer.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050019#include <boost/beast/http.hpp>
Manojkiran Eda44250442020-06-16 12:51:38 +053020#include <boost/beast/ssl/ssl_stream.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021#include <boost/beast/websocket.hpp>
22#include <ssl_key_handler.hpp>
23
Manojkiran Eda44250442020-06-16 12:51:38 +053024#include <atomic>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050025#include <chrono>
26#include <vector>
27
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace crow
29{
Ed Tanous257f5792018-03-17 14:40:09 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031inline void prettyPrintJson(crow::Response& res)
32{
Jason M. Bills193ad2f2018-09-26 15:08:52 -070033 std::string value = res.jsonValue.dump(4, ' ', true);
Ed Tanousa29c9972018-11-29 15:54:32 -080034 utility::escapeHtml(value);
35 utility::convertToLinks(value);
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 res.body() = "<html>\n"
37 "<head>\n"
38 "<title>Redfish API</title>\n"
39 "<link rel=\"stylesheet\" type=\"text/css\" "
40 "href=\"/styles/default.css\">\n"
41 "<script src=\"/highlight.pack.js\"></script>"
42 "<script>hljs.initHighlightingOnLoad();</script>"
43 "</head>\n"
44 "<body>\n"
45 "<div style=\"max-width: 576px;margin:0 auto;\">\n"
46 "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" "
47 "height=\"406px\" "
48 "width=\"576px\">\n"
49 "<br>\n"
50 "<pre>\n"
51 "<code class=\"json\">" +
52 value +
53 "</code>\n"
54 "</pre>\n"
55 "</div>\n"
56 "</body>\n"
57 "</html>\n";
Ed Tanous93ef5802019-01-03 10:15:41 -080058 res.addHeader("Content-Type", "text/html;charset=UTF-8");
Ed Tanous257f5792018-03-17 14:40:09 -070059}
60
Ed Tanous7045c8d2017-04-03 10:04:37 -070061using namespace boost;
62using tcp = asio::ip::tcp;
63
Ed Tanous1abe55e2018-09-05 08:30:59 -070064namespace detail
65{
Gunnar Mills1214b7e2020-06-04 10:11:30 -050066template <typename MW>
67struct CheckBeforeHandleArity3Const
Ed Tanous1abe55e2018-09-05 08:30:59 -070068{
69 template <typename T,
70 void (T::*)(Request&, Response&, typename MW::Context&) const =
71 &T::beforeHandle>
72 struct Get
Gunnar Mills1214b7e2020-06-04 10:11:30 -050073 {};
Ed Tanous7045c8d2017-04-03 10:04:37 -070074};
75
Gunnar Mills1214b7e2020-06-04 10:11:30 -050076template <typename MW>
77struct CheckBeforeHandleArity3
Ed Tanous1abe55e2018-09-05 08:30:59 -070078{
79 template <typename T, void (T::*)(Request&, Response&,
80 typename MW::Context&) = &T::beforeHandle>
81 struct Get
Gunnar Mills1214b7e2020-06-04 10:11:30 -050082 {};
Ed Tanous7045c8d2017-04-03 10:04:37 -070083};
84
Gunnar Mills1214b7e2020-06-04 10:11:30 -050085template <typename MW>
86struct CheckAfterHandleArity3Const
Ed Tanous1abe55e2018-09-05 08:30:59 -070087{
88 template <typename T,
89 void (T::*)(Request&, Response&, typename MW::Context&) const =
90 &T::afterHandle>
91 struct Get
Gunnar Mills1214b7e2020-06-04 10:11:30 -050092 {};
Ed Tanous7045c8d2017-04-03 10:04:37 -070093};
94
Gunnar Mills1214b7e2020-06-04 10:11:30 -050095template <typename MW>
96struct CheckAfterHandleArity3
Ed Tanous1abe55e2018-09-05 08:30:59 -070097{
98 template <typename T, void (T::*)(Request&, Response&,
99 typename MW::Context&) = &T::afterHandle>
100 struct Get
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500101 {};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102};
103
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500104template <typename T>
105struct IsBeforeHandleArity3Impl
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106{
107 template <typename C>
108 static std::true_type
109 f(typename CheckBeforeHandleArity3Const<T>::template Get<C>*);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 template <typename C>
112 static std::true_type
113 f(typename CheckBeforeHandleArity3<T>::template Get<C>*);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700114
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500115 template <typename C>
116 static std::false_type f(...);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700117
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 public:
Ed Tanous0c838cf2019-10-24 10:01:46 -0700119 static constexpr bool value = decltype(f<T>(nullptr))::value;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700120};
121
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500122template <typename T>
123struct IsAfterHandleArity3Impl
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124{
125 template <typename C>
126 static std::true_type
127 f(typename CheckAfterHandleArity3Const<T>::template Get<C>*);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700128
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 template <typename C>
130 static std::true_type
131 f(typename CheckAfterHandleArity3<T>::template Get<C>*);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700132
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500133 template <typename C>
134 static std::false_type f(...);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700135
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 public:
Ed Tanous0c838cf2019-10-24 10:01:46 -0700137 static constexpr bool value = decltype(f<T>(nullptr))::value;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700138};
139
140template <typename MW, typename Context, typename ParentContext>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700141typename std::enable_if<!IsBeforeHandleArity3Impl<MW>::value>::type
Ed Tanous1abe55e2018-09-05 08:30:59 -0700142 beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx,
143 ParentContext& /*parent_ctx*/)
144{
145 mw.beforeHandle(req, res, ctx.template get<MW>(), ctx);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700146}
147
148template <typename MW, typename Context, typename ParentContext>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700149typename std::enable_if<IsBeforeHandleArity3Impl<MW>::value>::type
Ed Tanous1abe55e2018-09-05 08:30:59 -0700150 beforeHandlerCall(MW& mw, Request& req, Response& res, Context& ctx,
151 ParentContext& /*parent_ctx*/)
152{
153 mw.beforeHandle(req, res, ctx.template get<MW>());
Ed Tanous7045c8d2017-04-03 10:04:37 -0700154}
155
156template <typename MW, typename Context, typename ParentContext>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700157typename std::enable_if<!IsAfterHandleArity3Impl<MW>::value>::type
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx,
159 ParentContext& /*parent_ctx*/)
160{
161 mw.afterHandle(req, res, ctx.template get<MW>(), ctx);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700162}
163
164template <typename MW, typename Context, typename ParentContext>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700165typename std::enable_if<IsAfterHandleArity3Impl<MW>::value>::type
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 afterHandlerCall(MW& mw, Request& req, Response& res, Context& ctx,
167 ParentContext& /*parent_ctx*/)
168{
169 mw.afterHandle(req, res, ctx.template get<MW>());
Ed Tanous7045c8d2017-04-03 10:04:37 -0700170}
171
Ed Tanous271584a2019-07-09 16:24:22 -0700172template <size_t N, typename Context, typename Container, typename CurrentMW,
Ed Tanous7045c8d2017-04-03 10:04:37 -0700173 typename... Middlewares>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700174bool middlewareCallHelper(Container& middlewares, Request& req, Response& res,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 Context& ctx)
176{
177 using parent_context_t = typename Context::template partial<N - 1>;
178 beforeHandlerCall<CurrentMW, Context, parent_context_t>(
Ed Tanous7045c8d2017-04-03 10:04:37 -0700179 std::get<N>(middlewares), req, res, ctx,
180 static_cast<parent_context_t&>(ctx));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700181
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182 if (res.isCompleted())
183 {
184 afterHandlerCall<CurrentMW, Context, parent_context_t>(
185 std::get<N>(middlewares), req, res, ctx,
186 static_cast<parent_context_t&>(ctx));
187 return true;
188 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700189
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 if (middlewareCallHelper<N + 1, Context, Container, Middlewares...>(
191 middlewares, req, res, ctx))
192 {
193 afterHandlerCall<CurrentMW, Context, parent_context_t>(
194 std::get<N>(middlewares), req, res, ctx,
195 static_cast<parent_context_t&>(ctx));
196 return true;
197 }
198
199 return false;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700200}
201
Ed Tanous271584a2019-07-09 16:24:22 -0700202template <size_t N, typename Context, typename Container>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700203bool middlewareCallHelper(Container& /*middlewares*/, Request& /*req*/,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 Response& /*res*/, Context& /*ctx*/)
205{
206 return false;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700207}
208
Ed Tanous271584a2019-07-09 16:24:22 -0700209template <size_t N, typename Context, typename Container>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210typename std::enable_if<(N < 0)>::type
211 afterHandlersCallHelper(Container& /*middlewares*/, Context& /*Context*/,
212 Request& /*req*/, Response& /*res*/)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213{}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700214
Ed Tanous271584a2019-07-09 16:24:22 -0700215template <size_t N, typename Context, typename Container>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216typename std::enable_if<(N == 0)>::type
217 afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req,
218 Response& res)
219{
220 using parent_context_t = typename Context::template partial<N - 1>;
221 using CurrentMW = typename std::tuple_element<
222 N, typename std::remove_reference<Container>::type>::type;
223 afterHandlerCall<CurrentMW, Context, parent_context_t>(
224 std::get<N>(middlewares), req, res, ctx,
225 static_cast<parent_context_t&>(ctx));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700226}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227
Ed Tanous271584a2019-07-09 16:24:22 -0700228template <size_t N, typename Context, typename Container>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229typename std::enable_if<(N > 0)>::type
230 afterHandlersCallHelper(Container& middlewares, Context& ctx, Request& req,
231 Response& res)
232{
233 using parent_context_t = typename Context::template partial<N - 1>;
234 using CurrentMW = typename std::tuple_element<
235 N, typename std::remove_reference<Container>::type>::type;
236 afterHandlerCall<CurrentMW, Context, parent_context_t>(
237 std::get<N>(middlewares), req, res, ctx,
238 static_cast<parent_context_t&>(ctx));
239 afterHandlersCallHelper<N - 1, Context, Container>(middlewares, ctx, req,
240 res);
241}
242} // namespace detail
Ed Tanous7045c8d2017-04-03 10:04:37 -0700243
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700244#ifdef BMCWEB_ENABLE_DEBUG
Ed Tanouse0d918b2018-03-27 17:41:04 -0700245static std::atomic<int> connectionCount;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700246#endif
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700247
Adriana Kobylak0e1cf262019-12-05 13:57:57 -0600248// request body limit size set by the BMCWEB_HTTP_REQ_BODY_LIMIT_MB option
249constexpr unsigned int httpReqBodyLimit =
250 1024 * 1024 * BMCWEB_HTTP_REQ_BODY_LIMIT_MB;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -0700251
James Feist3909dc82020-04-03 10:58:55 -0700252constexpr uint64_t loggedOutPostBodyLimit = 4096;
253
254constexpr uint32_t httpHeaderLimit = 8192;
255
256// drop all connections after 1 minute, this time limit was chosen
257// arbitrarily and can be adjusted later if needed
258static constexpr const size_t loggedInAttempts =
259 (60 / timerQueueTimeoutSeconds);
260
261static constexpr const size_t loggedOutAttempts =
262 (15 / timerQueueTimeoutSeconds);
263
Ed Tanous7045c8d2017-04-03 10:04:37 -0700264template <typename Adaptor, typename Handler, typename... Middlewares>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500265class Connection :
266 public std::enable_shared_from_this<
267 Connection<Adaptor, Handler, Middlewares...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268{
269 public:
Ed Tanous271584a2019-07-09 16:24:22 -0700270 Connection(boost::asio::io_context& ioService, Handler* handlerIn,
271 const std::string& ServerNameIn,
272 std::tuple<Middlewares...>* middlewaresIn,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 std::function<std::string()>& get_cached_date_str_f,
Ed Tanous271584a2019-07-09 16:24:22 -0700274 detail::TimerQueue& timerQueueIn, Adaptor adaptorIn) :
Ed Tanousceac6f72018-12-02 11:58:47 -0800275 adaptor(std::move(adaptorIn)),
Ed Tanous271584a2019-07-09 16:24:22 -0700276 handler(handlerIn), serverName(ServerNameIn),
277 middlewares(middlewaresIn), getCachedDateStr(get_cached_date_str_f),
278 timerQueue(timerQueueIn)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 {
280 parser.emplace(std::piecewise_construct, std::make_tuple());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700281 parser->body_limit(httpReqBodyLimit);
James Feist3909dc82020-04-03 10:58:55 -0700282 parser->header_limit(httpHeaderLimit);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700283 req.emplace(parser->get());
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200284
285#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100286 auto ca_available = !std::filesystem::is_empty(
287 std::filesystem::path(ensuressl::trustStorePath));
288 if (ca_available && crow::persistent_data::SessionStore::getInstance()
289 .getAuthMethodsConfig()
290 .tls)
291 {
292 adaptor.set_verify_mode(boost::asio::ssl::verify_peer);
293 SSL_set_session_id_context(
294 adaptor.native_handle(),
295 reinterpret_cast<const unsigned char*>(serverName.c_str()),
Zbigniew Kurzynskicac94c52019-11-07 12:55:04 +0100296 static_cast<unsigned int>(serverName.length()));
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100297 BMCWEB_LOG_DEBUG << this << " TLS is enabled on this connection.";
298 }
299
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100300 adaptor.set_verify_callback([this](
301 bool preverified,
302 boost::asio::ssl::verify_context& ctx) {
303 // do nothing if TLS is disabled
304 if (!crow::persistent_data::SessionStore::getInstance()
305 .getAuthMethodsConfig()
306 .tls)
307 {
308 BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled";
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200309 return true;
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100310 }
311
312 // We always return true to allow full auth flow
313 if (!preverified)
314 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100315 BMCWEB_LOG_DEBUG << this << " TLS preverification failed.";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100316 return true;
317 }
318
319 X509_STORE_CTX* cts = ctx.native_handle();
320 if (cts == nullptr)
321 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100322 BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle.";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100323 return true;
324 }
325
326 // Get certificate
327 X509* peerCert =
328 X509_STORE_CTX_get_current_cert(ctx.native_handle());
329 if (peerCert == nullptr)
330 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100331 BMCWEB_LOG_DEBUG << this
332 << " Cannot get current TLS certificate.";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100333 return true;
334 }
335
336 // Check if certificate is OK
337 int error = X509_STORE_CTX_get_error(cts);
338 if (error != X509_V_OK)
339 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100340 BMCWEB_LOG_INFO << this << " Last TLS error is: " << error;
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100341 return true;
342 }
343 // Check that we have reached final certificate in chain
344 int32_t depth = X509_STORE_CTX_get_error_depth(cts);
345 if (depth != 0)
346
347 {
348 BMCWEB_LOG_DEBUG
349 << this << " Certificate verification in progress (depth "
350 << depth << "), waiting to reach final depth";
351 return true;
352 }
353
354 BMCWEB_LOG_DEBUG << this
355 << " Certificate verification of final depth";
356
357 // Verify KeyUsage
358 bool isKeyUsageDigitalSignature = false;
359 bool isKeyUsageKeyAgreement = false;
360
361 ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>(
362 X509_get_ext_d2i(peerCert, NID_key_usage, NULL, NULL));
363
364 if (usage == nullptr)
365 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100366 BMCWEB_LOG_DEBUG << this << " TLS usage is null";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100367 return true;
368 }
369
370 for (int i = 0; i < usage->length; i++)
371 {
372 if (KU_DIGITAL_SIGNATURE & usage->data[i])
373 {
374 isKeyUsageDigitalSignature = true;
375 }
376 if (KU_KEY_AGREEMENT & usage->data[i])
377 {
378 isKeyUsageKeyAgreement = true;
379 }
380 }
381
382 if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement)
383 {
384 BMCWEB_LOG_DEBUG << this
385 << " Certificate ExtendedKeyUsage does "
386 "not allow provided certificate to "
387 "be used for user authentication";
388 return true;
389 }
Zbigniew Kurzynski09d02f82020-03-30 13:41:42 +0200390 ASN1_BIT_STRING_free(usage);
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100391
392 // Determine that ExtendedKeyUsage includes Client Auth
393
394 stack_st_ASN1_OBJECT* extUsage = static_cast<stack_st_ASN1_OBJECT*>(
395 X509_get_ext_d2i(peerCert, NID_ext_key_usage, NULL, NULL));
396
397 if (extUsage == nullptr)
398 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100399 BMCWEB_LOG_DEBUG << this << " TLS extUsage is null";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100400 return true;
401 }
402
403 bool isExKeyUsageClientAuth = false;
404 for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++)
405 {
406 if (NID_client_auth ==
407 OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i)))
408 {
409 isExKeyUsageClientAuth = true;
410 break;
411 }
412 }
Zbigniew Kurzynski09d02f82020-03-30 13:41:42 +0200413 sk_ASN1_OBJECT_free(extUsage);
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100414
415 // Certificate has to have proper key usages set
416 if (!isExKeyUsageClientAuth)
417 {
418 BMCWEB_LOG_DEBUG << this
419 << " Certificate ExtendedKeyUsage does "
420 "not allow provided certificate to "
421 "be used for user authentication";
422 return true;
423 }
424 std::string sslUser;
425 // Extract username contained in CommonName
426 sslUser.resize(256, '\0');
427
428 int status = X509_NAME_get_text_by_NID(
429 X509_get_subject_name(peerCert), NID_commonName, sslUser.data(),
430 static_cast<int>(sslUser.size()));
431
432 if (status == -1)
433 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100434 BMCWEB_LOG_DEBUG
435 << this << " TLS cannot get username to create session";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100436 return true;
437 }
438
439 size_t lastChar = sslUser.find('\0');
440 if (lastChar == std::string::npos || lastChar == 0)
441 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100442 BMCWEB_LOG_DEBUG << this << " Invalid TLS user name";
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100443 return true;
444 }
445 sslUser.resize(lastChar);
446
447 session = persistent_data::SessionStore::getInstance()
448 .generateUserSession(
449 sslUser,
450 crow::persistent_data::PersistenceType::TIMEOUT);
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100451 if (auto sp = session.lock())
452 {
453 BMCWEB_LOG_DEBUG << this
454 << " Generating TLS session: " << sp->uniqueId;
455 }
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100456 return true;
457 });
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200458#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
459
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700460#ifdef BMCWEB_ENABLE_DEBUG
Ed Tanous1abe55e2018-09-05 08:30:59 -0700461 connectionCount++;
462 BMCWEB_LOG_DEBUG << this << " Connection open, total "
463 << connectionCount;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700464#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -0700465 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700466
Ed Tanous1abe55e2018-09-05 08:30:59 -0700467 ~Connection()
468 {
469 res.completeRequestHandler = nullptr;
470 cancelDeadlineTimer();
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700471#ifdef BMCWEB_ENABLE_DEBUG
Ed Tanous1abe55e2018-09-05 08:30:59 -0700472 connectionCount--;
473 BMCWEB_LOG_DEBUG << this << " Connection closed, total "
474 << connectionCount;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700475#endif
Ed Tanous7045c8d2017-04-03 10:04:37 -0700476 }
477
Ed Tanousceac6f72018-12-02 11:58:47 -0800478 Adaptor& socket()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700479 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800480 return adaptor;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700481 }
482
Ed Tanous1abe55e2018-09-05 08:30:59 -0700483 void start()
484 {
Ed Tanous7045c8d2017-04-03 10:04:37 -0700485
James Feist3909dc82020-04-03 10:58:55 -0700486 startDeadline(0);
Ed Tanousceac6f72018-12-02 11:58:47 -0800487 // TODO(ed) Abstract this to a more clever class with the idea of an
488 // asynchronous "start"
489 if constexpr (std::is_same_v<Adaptor,
490 boost::beast::ssl_stream<
491 boost::asio::ip::tcp::socket>>)
492 {
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000493 adaptor.async_handshake(boost::asio::ssl::stream_base::server,
494 [this, self(shared_from_this())](
495 const boost::system::error_code& ec) {
496 if (ec)
497 {
498 return;
499 }
500 doReadHeaders();
501 });
Ed Tanousceac6f72018-12-02 11:58:47 -0800502 }
503 else
504 {
505 doReadHeaders();
506 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700507 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700508
Ed Tanous1abe55e2018-09-05 08:30:59 -0700509 void handle()
510 {
511 cancelDeadlineTimer();
James Feist3909dc82020-04-03 10:58:55 -0700512
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513 bool isInvalidRequest = false;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700514
Ed Tanous1abe55e2018-09-05 08:30:59 -0700515 // Check for HTTP version 1.1.
516 if (req->version() == 11)
517 {
518 if (req->getHeaderValue(boost::beast::http::field::host).empty())
519 {
520 isInvalidRequest = true;
Ed Tanousde5c9f32019-03-26 09:17:55 -0700521 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700522 }
523 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700524
Ed Tanouse278c182019-03-13 16:23:37 -0700525 BMCWEB_LOG_INFO << "Request: "
526 << " " << this << " HTTP/" << req->version() / 10 << "."
527 << req->version() % 10 << ' ' << req->methodString()
528 << " " << req->target();
Ed Tanous7045c8d2017-04-03 10:04:37 -0700529
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 needToCallAfterHandlers = false;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700531
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 if (!isInvalidRequest)
533 {
raviteja-b4722efe2020-02-03 12:23:18 -0600534 req->socket = [this, self = shared_from_this()]() -> Adaptor& {
535 return self->socket();
536 };
537
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 res.completeRequestHandler = [] {};
Ed Tanouse278c182019-03-13 16:23:37 -0700539 res.isAliveHelper = [this]() -> bool { return isAlive(); };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700540
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 ctx = detail::Context<Middlewares...>();
Ed Tanouse278c182019-03-13 16:23:37 -0700542 req->middlewareContext = static_cast<void*>(&ctx);
543 req->ioService = static_cast<decltype(req->ioService)>(
544 &adaptor.get_executor().context());
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200545
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 detail::middlewareCallHelper<
Ed Tanous271584a2019-07-09 16:24:22 -0700547 0U, decltype(ctx), decltype(*middlewares), Middlewares...>(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700548 *middlewares, *req, res, ctx);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700549
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 if (!res.completed)
551 {
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000552 needToCallAfterHandlers = true;
553 res.completeRequestHandler = [self(shared_from_this())] {
554 self->completeRequest();
555 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 if (req->isUpgrade() &&
557 boost::iequals(
558 req->getHeaderValue(boost::beast::http::field::upgrade),
559 "websocket"))
560 {
561 handler->handleUpgrade(*req, res, std::move(adaptor));
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000562 // delete lambda with self shared_ptr
563 // to enable connection destruction
564 res.completeRequestHandler = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 return;
566 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700567 handler->handle(*req, res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700568 }
569 else
570 {
571 completeRequest();
572 }
573 }
574 else
575 {
576 completeRequest();
577 }
578 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700579
Ed Tanouse278c182019-03-13 16:23:37 -0700580 bool isAlive()
581 {
582
583 if constexpr (std::is_same_v<Adaptor,
584 boost::beast::ssl_stream<
585 boost::asio::ip::tcp::socket>>)
586 {
587 return adaptor.next_layer().is_open();
588 }
589 else
590 {
591 return adaptor.is_open();
592 }
593 }
594 void close()
595 {
Ed Tanouse278c182019-03-13 16:23:37 -0700596 if constexpr (std::is_same_v<Adaptor,
597 boost::beast::ssl_stream<
598 boost::asio::ip::tcp::socket>>)
599 {
600 adaptor.next_layer().close();
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200601#ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
602 if (auto sp = session.lock())
603 {
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100604 BMCWEB_LOG_DEBUG << this
605 << " Removing TLS session: " << sp->uniqueId;
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200606 persistent_data::SessionStore::getInstance().removeSession(sp);
607 }
608#endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION
Ed Tanouse278c182019-03-13 16:23:37 -0700609 }
610 else
611 {
612 adaptor.close();
613 }
614 }
615
Ed Tanous1abe55e2018-09-05 08:30:59 -0700616 void completeRequest()
617 {
618 BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' '
619 << res.resultInt() << " keepalive=" << req->keepAlive();
Ed Tanous7045c8d2017-04-03 10:04:37 -0700620
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 if (needToCallAfterHandlers)
622 {
623 needToCallAfterHandlers = false;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700624
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 // call all afterHandler of middlewares
Ed Tanous271584a2019-07-09 16:24:22 -0700626 detail::afterHandlersCallHelper<sizeof...(Middlewares) - 1,
Ed Tanousb01bf292019-03-25 19:25:26 +0000627 decltype(ctx),
628 decltype(*middlewares)>(
629 *middlewares, ctx, *req, res);
James Feist3909dc82020-04-03 10:58:55 -0700630
631 crow::authorization::cleanupTempSession(*req);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700633
Ed Tanouse278c182019-03-13 16:23:37 -0700634 if (!isAlive())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700635 {
636 // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " <<
637 // isReading
638 // << ' ' << isWriting;
639 // delete this;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000640
641 // delete lambda with self shared_ptr
642 // to enable connection destruction
643 res.completeRequestHandler = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 return;
645 }
646 if (res.body().empty() && !res.jsonValue.empty())
647 {
648 if (http_helpers::requestPrefersHtml(*req))
649 {
650 prettyPrintJson(res);
651 }
652 else
653 {
654 res.jsonMode();
Jason M. Bills193ad2f2018-09-26 15:08:52 -0700655 res.body() = res.jsonValue.dump(2, ' ', true);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 }
657 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700658
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 if (res.resultInt() >= 400 && res.body().empty())
660 {
661 res.body() = std::string(res.reason());
662 }
Ed Tanous6295bec2019-09-03 10:11:01 -0700663
664 if (res.result() == boost::beast::http::status::no_content)
665 {
666 // Boost beast throws if content is provided on a no-content
667 // response. Ideally, this would never happen, but in the case that
668 // it does, we don't want to throw.
669 BMCWEB_LOG_CRITICAL
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100670 << this << " Response content provided but code was no-content";
Ed Tanous6295bec2019-09-03 10:11:01 -0700671 res.body().clear();
672 }
673
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 res.addHeader(boost::beast::http::field::server, serverName);
675 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
676
677 res.keepAlive(req->keepAlive());
678
679 doWrite();
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000680
681 // delete lambda with self shared_ptr
682 // to enable connection destruction
683 res.completeRequestHandler = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 }
685
686 private:
687 void doReadHeaders()
688 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 BMCWEB_LOG_DEBUG << this << " doReadHeaders";
690
691 // Clean up any previous Connection.
692 boost::beast::http::async_read_header(
Ed Tanousceac6f72018-12-02 11:58:47 -0800693 adaptor, buffer, *parser,
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000694 [this,
695 self(shared_from_this())](const boost::system::error_code& ec,
696 std::size_t bytes_transferred) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 BMCWEB_LOG_ERROR << this << " async_read_header "
698 << bytes_transferred << " Bytes";
699 bool errorWhileReading = false;
700 if (ec)
701 {
702 errorWhileReading = true;
703 BMCWEB_LOG_ERROR
704 << this << " Error while reading: " << ec.message();
705 }
706 else
707 {
708 // if the adaptor isn't open anymore, and wasn't handed to a
709 // websocket, treat as an error
Ed Tanouse278c182019-03-13 16:23:37 -0700710 if (!isAlive() && !req->isUpgrade())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700711 {
712 errorWhileReading = true;
713 }
714 }
715
James Feist3909dc82020-04-03 10:58:55 -0700716 cancelDeadlineTimer();
717
Ed Tanous1abe55e2018-09-05 08:30:59 -0700718 if (errorWhileReading)
719 {
Ed Tanouse278c182019-03-13 16:23:37 -0700720 close();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700721 BMCWEB_LOG_DEBUG << this << " from read(1)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722 return;
723 }
724
James Feist3909dc82020-04-03 10:58:55 -0700725 if (!req)
726 {
727 close();
728 return;
729 }
730
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731 // Compute the url parameters for the request
732 req->url = req->target();
733 std::size_t index = req->url.find("?");
Ed Tanous39e77502019-03-04 17:35:53 -0800734 if (index != std::string_view::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700735 {
Jason M. Bills43fcbe52018-10-16 15:19:20 -0700736 req->url = req->url.substr(0, index);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700737 }
James Feist6964c982020-07-28 16:10:23 -0700738 crow::authorization::authenticate(*req, res, session);
James Feist3909dc82020-04-03 10:58:55 -0700739
740 bool loggedIn = req && req->session;
741 if (loggedIn)
742 {
743 startDeadline(loggedInAttempts);
744 BMCWEB_LOG_DEBUG << "Starting slow deadline";
745
746 req->urlParams = QueryString(std::string(req->target()));
747 }
748 else
749 {
750 const boost::optional<uint64_t> contentLength =
751 parser->content_length();
752 if (contentLength &&
753 *contentLength > loggedOutPostBodyLimit)
754 {
755 BMCWEB_LOG_DEBUG << "Content length greater than limit "
756 << *contentLength;
757 close();
758 return;
759 }
760
761 startDeadline(loggedOutAttempts);
762 BMCWEB_LOG_DEBUG << "Starting quick deadline";
763 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700764 doRead();
765 });
766 }
767
768 void doRead()
769 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700770 BMCWEB_LOG_DEBUG << this << " doRead";
771
772 boost::beast::http::async_read(
Ed Tanousceac6f72018-12-02 11:58:47 -0800773 adaptor, buffer, *parser,
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000774 [this,
775 self(shared_from_this())](const boost::system::error_code& ec,
776 std::size_t bytes_transferred) {
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100777 BMCWEB_LOG_DEBUG << this << " async_read " << bytes_transferred
Ed Tanous1abe55e2018-09-05 08:30:59 -0700778 << " Bytes";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700779
780 bool errorWhileReading = false;
781 if (ec)
782 {
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100783 BMCWEB_LOG_ERROR
784 << this << " Error while reading: " << ec.message();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700785 errorWhileReading = true;
786 }
787 else
788 {
James Feist3909dc82020-04-03 10:58:55 -0700789 if (isAlive())
790 {
791 cancelDeadlineTimer();
792 bool loggedIn = req && req->session;
793 if (loggedIn)
794 {
795 startDeadline(loggedInAttempts);
796 }
797 else
798 {
799 startDeadline(loggedOutAttempts);
800 }
801 }
802 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 {
804 errorWhileReading = true;
805 }
806 }
807 if (errorWhileReading)
808 {
809 cancelDeadlineTimer();
Ed Tanouse278c182019-03-13 16:23:37 -0700810 close();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 BMCWEB_LOG_DEBUG << this << " from read(1)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700812 return;
813 }
814 handle();
815 });
816 }
817
818 void doWrite()
819 {
James Feist3909dc82020-04-03 10:58:55 -0700820 bool loggedIn = req && req->session;
821 if (loggedIn)
822 {
823 startDeadline(loggedInAttempts);
824 }
825 else
826 {
827 startDeadline(loggedOutAttempts);
828 }
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100829 BMCWEB_LOG_DEBUG << this << " doWrite";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 res.preparePayload();
831 serializer.emplace(*res.stringResponse);
832 boost::beast::http::async_write(
Ed Tanousceac6f72018-12-02 11:58:47 -0800833 adaptor, *serializer,
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000834 [this,
835 self(shared_from_this())](const boost::system::error_code& ec,
836 std::size_t bytes_transferred) {
Zbigniew Kurzynski2658d982019-11-19 18:01:08 +0100837 BMCWEB_LOG_DEBUG << this << " async_write " << bytes_transferred
Ed Tanous1abe55e2018-09-05 08:30:59 -0700838 << " bytes";
839
James Feist54d8bb12020-07-20 13:28:59 -0700840 cancelDeadlineTimer();
841
Ed Tanous1abe55e2018-09-05 08:30:59 -0700842 if (ec)
843 {
844 BMCWEB_LOG_DEBUG << this << " from write(2)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 return;
846 }
Ed Tanousceac6f72018-12-02 11:58:47 -0800847 if (!res.keepAlive())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 {
Ed Tanouse278c182019-03-13 16:23:37 -0700849 close();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850 BMCWEB_LOG_DEBUG << this << " from write(1)";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 return;
852 }
853
854 serializer.reset();
855 BMCWEB_LOG_DEBUG << this << " Clearing response";
856 res.clear();
857 parser.emplace(std::piecewise_construct, std::make_tuple());
Gunnar Millsded2a1e2020-07-24 09:46:33 -0500858 parser->body_limit(httpReqBodyLimit); // reset body limit for
859 // newly created parser
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 buffer.consume(buffer.size());
861
862 req.emplace(parser->get());
863 doReadHeaders();
864 });
865 }
866
Ed Tanous1abe55e2018-09-05 08:30:59 -0700867 void cancelDeadlineTimer()
868 {
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000869 if (timerCancelKey)
870 {
871 BMCWEB_LOG_DEBUG << this << " timer cancelled: " << &timerQueue
872 << ' ' << *timerCancelKey;
873 timerQueue.cancel(*timerCancelKey);
874 timerCancelKey.reset();
875 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 }
877
James Feist3909dc82020-04-03 10:58:55 -0700878 void startDeadline(size_t timerIterations)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700879 {
880 cancelDeadlineTimer();
881
James Feist3909dc82020-04-03 10:58:55 -0700882 if (timerIterations)
883 {
884 timerIterations--;
885 }
Jan Sowinski2b5e08e2020-01-09 17:16:02 +0100886
James Feist3909dc82020-04-03 10:58:55 -0700887 timerCancelKey =
James Feistbe5dfca2020-07-22 08:54:59 -0700888 timerQueue.add([self(shared_from_this()), timerIterations,
889 readCount{parser->get().body().size()}] {
James Feist3909dc82020-04-03 10:58:55 -0700890 // Mark timer as not active to avoid canceling it during
891 // Connection destructor which leads to double free issue
892 self->timerCancelKey.reset();
893 if (!self->isAlive())
894 {
895 return;
896 }
Jan Sowinski2b5e08e2020-01-09 17:16:02 +0100897
James Feistbe5dfca2020-07-22 08:54:59 -0700898 bool loggedIn = self->req && self->req->session;
899 // allow slow uploads for logged in users
900 if (loggedIn && self->parser->get().body().size() > readCount)
901 {
902 BMCWEB_LOG_DEBUG << self.get()
903 << " restart timer - read in progress";
904 self->startDeadline(timerIterations);
905 return;
906 }
907
James Feist3909dc82020-04-03 10:58:55 -0700908 // Threshold can be used to drop slow connections
909 // to protect against slow-rate DoS attack
910 if (timerIterations)
911 {
James Feistbe5dfca2020-07-22 08:54:59 -0700912 BMCWEB_LOG_DEBUG << self.get() << " restart timer";
James Feist3909dc82020-04-03 10:58:55 -0700913 self->startDeadline(timerIterations);
914 return;
915 }
916
917 self->close();
918 });
James Feistcb6cb492020-04-03 13:36:17 -0700919
920 if (!timerCancelKey)
921 {
922 close();
923 return;
924 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700925 BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' '
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000926 << *timerCancelKey;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927 }
928
929 private:
930 Adaptor adaptor;
931 Handler* handler;
932
Ed Tanousa24526d2018-12-10 15:17:59 -0800933 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 // re-created on Connection reset
Ed Tanousa24526d2018-12-10 15:17:59 -0800935 std::optional<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 boost::beast::http::request_parser<boost::beast::http::string_body>>
937 parser;
938
Ed Tanous3112a142018-11-29 15:45:10 -0800939 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940
Ed Tanousa24526d2018-12-10 15:17:59 -0800941 std::optional<boost::beast::http::response_serializer<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 boost::beast::http::string_body>>
943 serializer;
944
Ed Tanousa24526d2018-12-10 15:17:59 -0800945 std::optional<crow::Request> req;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 crow::Response res;
Kowalski, Kamil55e43f62019-07-10 13:12:57 +0200947 std::weak_ptr<crow::persistent_data::UserSession> session;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700948
949 const std::string& serverName;
950
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000951 std::optional<size_t> timerCancelKey;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952
Ed Tanous1abe55e2018-09-05 08:30:59 -0700953 bool needToCallAfterHandlers{};
954 bool needToStartReadAfterComplete{};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700955
956 std::tuple<Middlewares...>* middlewares;
957 detail::Context<Middlewares...> ctx;
958
959 std::function<std::string()>& getCachedDateStr;
960 detail::TimerQueue& timerQueue;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000961
962 using std::enable_shared_from_this<
963 Connection<Adaptor, Handler, Middlewares...>>::shared_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800964};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700965} // namespace crow