blob: 955cb3473feb1717035b6588f2ca6fbc021380e3 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
Ed Tanous75312982021-02-11 14:26:02 -08004#include "bmcweb_config.h"
Adriana Kobylak0e1cf262019-12-05 13:57:57 -06005
Ed Tanousd093c992023-01-19 19:01:49 -08006#include "async_resp.hpp"
Nan Zhoud055a342022-05-25 01:15:34 +00007#include "authentication.hpp"
Ed Tanoused5f8952023-06-22 14:06:22 -07008#include "complete_response_fields.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "forward_unauthorized.hpp"
Ed Tanousfca2cbe2021-01-28 14:49:59 -080010#include "http2_connection.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -080011#include "http_body.hpp"
Ed Tanous796ba932020-08-02 04:29:21 +000012#include "http_connect_types.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080013#include "http_request.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070014#include "http_response.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070015#include "http_utility.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070016#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080017#include "mutual_tls.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080018#include "sessions.hpp"
Ed Tanous18f8f602023-07-18 10:07:23 -070019#include "str_utility.hpp"
Ed Tanouscd7dbb32025-02-01 12:37:56 -080020#include "utility.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070021
Ed Tanousd7857202025-01-28 15:32:26 -080022#include <boost/asio/error.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -080023#include <boost/asio/ip/tcp.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070024#include <boost/asio/ssl/stream.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080025#include <boost/asio/ssl/stream_base.hpp>
26#include <boost/asio/ssl/verify_context.hpp>
Ed Tanous5dfb5b22021-12-03 11:24:53 -080027#include <boost/asio/steady_timer.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070028#include <boost/beast/_experimental/test/stream.hpp>
Ed Tanous4d698612024-02-06 14:57:24 -080029#include <boost/beast/core/buffers_generator.hpp>
Ed Tanous796ba932020-08-02 04:29:21 +000030#include <boost/beast/core/detect_ssl.hpp>
31#include <boost/beast/core/error.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -080032#include <boost/beast/core/flat_static_buffer.hpp>
Myung Baea4326fe2023-01-10 14:29:24 -060033#include <boost/beast/http/error.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080034#include <boost/beast/http/field.hpp>
Ed Tanous4d698612024-02-06 14:57:24 -080035#include <boost/beast/http/message_generator.hpp>
Ed Tanous918ef252022-05-25 10:40:41 -070036#include <boost/beast/http/parser.hpp>
37#include <boost/beast/http/read.hpp>
Ed Tanouscd7dbb32025-02-01 12:37:56 -080038#include <boost/beast/http/rfc7230.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080039#include <boost/beast/http/status.hpp>
40#include <boost/beast/http/verb.hpp>
41#include <boost/none.hpp>
42#include <boost/optional/optional.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050043
Ed Tanousd7857202025-01-28 15:32:26 -080044#include <bit>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045#include <chrono>
Ed Tanousd7857202025-01-28 15:32:26 -080046#include <cstddef>
47#include <cstdint>
48#include <functional>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070049#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080050#include <optional>
51#include <string>
52#include <string_view>
53#include <system_error>
54#include <type_traits>
55#include <utility>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057namespace crow
58{
Ed Tanous257f5792018-03-17 14:40:09 -070059
Ed Tanouscf9e4172022-12-21 09:30:16 -080060// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous6fbdbca2021-12-06 14:36:06 -080061static int connectionCount = 0;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070062
Ed Tanous25b54db2024-04-17 15:40:31 -070063// request body limit size set by the BMCWEB_HTTP_BODY_LIMIT option
64constexpr uint64_t httpReqBodyLimit = 1024UL * 1024UL * BMCWEB_HTTP_BODY_LIMIT;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070065
Ed Tanous1d1d7782024-04-09 12:54:08 -070066constexpr uint64_t loggedOutPostBodyLimit = 4096U;
James Feist3909dc82020-04-03 10:58:55 -070067
Ed Tanous1d1d7782024-04-09 12:54:08 -070068constexpr uint32_t httpHeaderLimit = 8192U;
James Feist3909dc82020-04-03 10:58:55 -070069
Ed Tanous52cc1122020-07-18 13:51:21 -070070template <typename Adaptor, typename Handler>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071class Connection :
Ed Tanous52cc1122020-07-18 13:51:21 -070072 public std::enable_shared_from_this<Connection<Adaptor, Handler>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070073{
Ed Tanous7c8e0642022-02-21 12:11:14 -080074 using self_type = Connection<Adaptor, Handler>;
75
Ed Tanous1abe55e2018-09-05 08:30:59 -070076 public:
Ed Tanous796ba932020-08-02 04:29:21 +000077 Connection(Handler* handlerIn, HttpType httpTypeIn,
78 boost::asio::steady_timer&& timerIn,
Ed Tanous81ce6092020-12-17 16:54:55 +000079 std::function<std::string()>& getCachedDateStrF,
Ed Tanous796ba932020-08-02 04:29:21 +000080 boost::asio::ssl::stream<Adaptor>&& adaptorIn) :
81 httpType(httpTypeIn), adaptor(std::move(adaptorIn)), handler(handlerIn),
Patrick Williamsbd79bce2024-08-16 15:22:20 -040082 timer(std::move(timerIn)), getCachedDateStr(getCachedDateStrF)
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070084 initParser();
Kowalski, Kamil55e43f62019-07-10 13:12:57 +020085
Ed Tanous40aa0582021-07-14 13:24:40 -070086 connectionCount++;
Ed Tanous6fbdbca2021-12-06 14:36:06 -080087
Ed Tanous1d1d7782024-04-09 12:54:08 -070088 BMCWEB_LOG_DEBUG("{} Connection created, total {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -070089 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -070090 }
91
92 ~Connection()
93 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070094 res.releaseCompleteRequestHandler();
Ed Tanous40aa0582021-07-14 13:24:40 -070095 cancelDeadlineTimer();
Ed Tanous6fbdbca2021-12-06 14:36:06 -080096
Ed Tanous40aa0582021-07-14 13:24:40 -070097 connectionCount--;
Ed Tanous62598e32023-07-17 17:06:25 -070098 BMCWEB_LOG_DEBUG("{} Connection closed, total {}", logPtr(this),
99 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -0700100 }
101
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800102 Connection(const Connection&) = delete;
103 Connection(Connection&&) = delete;
104 Connection& operator=(const Connection&) = delete;
105 Connection& operator=(Connection&&) = delete;
106
Ed Tanous7c8e0642022-02-21 12:11:14 -0800107 bool tlsVerifyCallback(bool preverified,
108 boost::asio::ssl::verify_context& ctx)
109 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700110 BMCWEB_LOG_DEBUG("{} tlsVerifyCallback called with preverified {}",
111 logPtr(this), preverified);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800112 if (preverified)
113 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700114 mtlsSession = verifyMtlsUser(ip, ctx);
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100115 if (mtlsSession)
Ed Tanous7c8e0642022-02-21 12:11:14 -0800116 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700117 BMCWEB_LOG_DEBUG("{} Generated TLS session: {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -0700118 mtlsSession->uniqueId);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800119 }
120 }
Ed Tanous3281bcf2024-06-25 16:02:05 -0700121 const persistent_data::AuthConfigMethods& c =
122 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
123 if (c.tlsStrict)
124 {
Ed Tanous463a0e32024-10-14 11:21:48 -0700125 BMCWEB_LOG_DEBUG(
126 "{} TLS is in strict mode, returning preverified as is.",
127 logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700128 return preverified;
129 }
130 // If tls strict mode is disabled
131 // We always return true to allow full auth flow for resources that
132 // don't require auth
Ed Tanous7c8e0642022-02-21 12:11:14 -0800133 return true;
134 }
135
Ed Tanous3281bcf2024-06-25 16:02:05 -0700136 bool prepareMutualTls()
Ed Tanous40aa0582021-07-14 13:24:40 -0700137 {
Ed Tanous796ba932020-08-02 04:29:21 +0000138 BMCWEB_LOG_DEBUG("prepareMutualTls");
139
140 constexpr std::string_view id = "bmcweb";
141
142 const char* idPtr = id.data();
143 const auto* idCPtr = std::bit_cast<const unsigned char*>(idPtr);
144 auto idLen = static_cast<unsigned int>(id.length());
145 int ret =
146 SSL_set_session_id_context(adaptor.native_handle(), idCPtr, idLen);
147 if (ret == 0)
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100148 {
Ed Tanous796ba932020-08-02 04:29:21 +0000149 BMCWEB_LOG_ERROR("{} failed to set SSL id", logPtr(this));
150 return false;
151 }
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100152
Ed Tanous796ba932020-08-02 04:29:21 +0000153 BMCWEB_LOG_DEBUG("set_verify_callback");
Ed Tanous3281bcf2024-06-25 16:02:05 -0700154
Ed Tanous796ba932020-08-02 04:29:21 +0000155 boost::system::error_code ec;
156 adaptor.set_verify_callback(
157 std::bind_front(&self_type::tlsVerifyCallback, this), ec);
158 if (ec)
159 {
160 BMCWEB_LOG_ERROR("Failed to set verify callback {}", ec);
161 return false;
Ed Tanous3281bcf2024-06-25 16:02:05 -0700162 }
163
164 return true;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700165 }
166
Ed Tanous796ba932020-08-02 04:29:21 +0000167 void afterDetectSsl(const std::shared_ptr<self_type>& /*self*/,
168 boost::beast::error_code ec, bool isTls)
169 {
170 if (ec)
171 {
172 BMCWEB_LOG_ERROR("Couldn't detect ssl ", ec);
173 return;
174 }
175 BMCWEB_LOG_DEBUG("{} TLS was detected as {}", logPtr(this), isTls);
176 if (isTls)
177 {
178 if (httpType != HttpType::HTTPS && httpType != HttpType::BOTH)
179 {
180 BMCWEB_LOG_WARNING(
181 "{} Connection closed due to incompatible type",
182 logPtr(this));
183 return;
184 }
185 httpType = HttpType::HTTPS;
186 adaptor.async_handshake(
187 boost::asio::ssl::stream_base::server, buffer.data(),
188 std::bind_front(&self_type::afterSslHandshake, this,
189 shared_from_this()));
190 }
191 else
192 {
193 if (httpType != HttpType::HTTP && httpType != HttpType::BOTH)
194 {
195 BMCWEB_LOG_WARNING(
196 "{} Connection closed due to incompatible type",
197 logPtr(this));
198 return;
199 }
200
201 httpType = HttpType::HTTP;
202 BMCWEB_LOG_INFO("Starting non-SSL session");
203 doReadHeaders();
204 }
205 }
206
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 void start()
208 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700209 BMCWEB_LOG_DEBUG("{} Connection started, total {}", logPtr(this),
210 connectionCount);
Gunnar Mills4f63be02023-10-25 09:14:07 -0500211 if (connectionCount >= 200)
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800212 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700213 BMCWEB_LOG_CRITICAL("{} Max connection count exceeded.",
Ed Tanous62598e32023-07-17 17:06:25 -0700214 logPtr(this));
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800215 return;
216 }
217
Ed Tanous3281bcf2024-06-25 16:02:05 -0700218 if constexpr (BMCWEB_MUTUAL_TLS_AUTH)
219 {
220 if (!prepareMutualTls())
221 {
222 BMCWEB_LOG_ERROR("{} Failed to prepare mTLS", logPtr(this));
223 return;
224 }
225 }
226
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800227 startDeadline();
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500228
Ed Tanous1d1d7782024-04-09 12:54:08 -0700229 readClientIp();
Ed Tanous796ba932020-08-02 04:29:21 +0000230 boost::beast::async_detect_ssl(
231 adaptor.next_layer(), buffer,
232 std::bind_front(&self_type::afterDetectSsl, this,
233 shared_from_this()));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700234 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700235
Ed Tanous796ba932020-08-02 04:29:21 +0000236 void afterSslHandshake(const std::shared_ptr<self_type>& /*self*/,
237 const boost::system::error_code& ec,
238 size_t bytesParsed)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800239 {
Ed Tanous796ba932020-08-02 04:29:21 +0000240 buffer.consume(bytesParsed);
241 if (ec)
242 {
243 BMCWEB_LOG_ERROR("{} SSL handshake failed", logPtr(this));
244 return;
245 }
246 BMCWEB_LOG_DEBUG("{} SSL handshake succeeded", logPtr(this));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800247 // If http2 is enabled, negotiate the protocol
Ed Tanous25b54db2024-04-17 15:40:31 -0700248 if constexpr (BMCWEB_EXPERIMENTAL_HTTP2)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800249 {
250 const unsigned char* alpn = nullptr;
251 unsigned int alpnlen = 0;
252 SSL_get0_alpn_selected(adaptor.native_handle(), &alpn, &alpnlen);
253 if (alpn != nullptr)
254 {
255 std::string_view selectedProtocol(
256 std::bit_cast<const char*>(alpn), alpnlen);
Ed Tanous62598e32023-07-17 17:06:25 -0700257 BMCWEB_LOG_DEBUG("ALPN selected protocol \"{}\" len: {}",
258 selectedProtocol, alpnlen);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800259 if (selectedProtocol == "h2")
260 {
Ed Tanous796ba932020-08-02 04:29:21 +0000261 upgradeToHttp2();
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800262 return;
263 }
264 }
265 }
266
267 doReadHeaders();
268 }
269
Ed Tanous1d1d7782024-04-09 12:54:08 -0700270 void initParser()
271 {
272 boost::beast::http::request_parser<bmcweb::HttpBody>& instance =
Ed Tanous38afdb92024-12-11 23:57:53 -0800273 parser.emplace();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700274
275 // reset header limit for newly created parser
276 instance.header_limit(httpHeaderLimit);
277
278 // Initially set no body limit. We don't yet know if the user is
279 // authenticated.
280 instance.body_limit(boost::none);
281 }
282
Ed Tanous796ba932020-08-02 04:29:21 +0000283 void upgradeToHttp2()
284 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800285 auto http2 = std::make_shared<HTTP2Connection<Adaptor, Handler>>(
286 std::move(adaptor), handler, getCachedDateStr, httpType);
287 if (http2settings.empty())
Ed Tanous796ba932020-08-02 04:29:21 +0000288 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800289 http2->start();
Ed Tanous796ba932020-08-02 04:29:21 +0000290 }
291 else
292 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800293 http2->startFromSettings(http2settings);
Ed Tanous796ba932020-08-02 04:29:21 +0000294 }
295 }
296
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800297 // returns whether connection was upgraded
298 bool doUpgrade(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
299 {
300 using boost::beast::http::field;
301 using boost::beast::http::token_list;
302
303 bool isSse =
304 isContentTypeAllowed(req->getHeaderValue("Accept"),
305 http_helpers::ContentType::EventStream, false);
306
307 bool isWebsocket = false;
308 bool isH2c = false;
309 // Check connection header is upgrade
310 if (token_list{req->req[field::connection]}.exists("upgrade"))
311 {
312 BMCWEB_LOG_DEBUG("{} Connection: Upgrade header was present",
313 logPtr(this));
314 // Parse if upgrade is h2c or websocket
315 token_list upgrade{req->req[field::upgrade]};
316 isWebsocket = upgrade.exists("websocket");
317 isH2c = upgrade.exists("h2c");
318 BMCWEB_LOG_DEBUG("{} Upgrade isWebsocket: {} isH2c: {}",
319 logPtr(this), isWebsocket, isH2c);
320 }
321
322 if (BMCWEB_EXPERIMENTAL_HTTP2 && isH2c)
323 {
324 std::string_view base64settings = req->req[field::http2_settings];
325 if (utility::base64Decode<true>(base64settings, http2settings))
326 {
327 res.result(boost::beast::http::status::switching_protocols);
328 res.addHeader(boost::beast::http::field::connection, "Upgrade");
329 res.addHeader(boost::beast::http::field::upgrade, "h2c");
330 }
331 }
332
333 // websocket and SSE are only allowed on GET
334 if (req->req.method() == boost::beast::http::verb::get)
335 {
336 if (isWebsocket || isSse)
337 {
338 asyncResp->res.setCompleteRequestHandler(
339 [self(shared_from_this())](crow::Response& thisRes) {
340 if (thisRes.result() != boost::beast::http::status::ok)
341 {
342 // When any error occurs before handle upgradation,
343 // the result in response will be set to respective
344 // error. By default the Result will be OK (200),
345 // which implies successful handle upgrade. Response
346 // needs to be sent over this connection only on
347 // failure.
348 self->completeRequest(thisRes);
349 return;
350 }
351 });
352 BMCWEB_LOG_INFO("{} Upgrading socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000353 if (httpType == HttpType::HTTP)
354 {
355 handler->handleUpgrade(req, asyncResp,
356 std::move(adaptor.next_layer()));
357 }
358 else
359 {
360 handler->handleUpgrade(req, asyncResp, std::move(adaptor));
361 }
362
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800363 return true;
364 }
365 }
366 return false;
367 }
368
Ed Tanous1abe55e2018-09-05 08:30:59 -0700369 void handle()
370 {
Ed Tanousf79b7a52021-09-22 19:04:29 -0700371 std::error_code reqEc;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700372 if (!parser)
373 {
374 return;
375 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700376 req = std::make_shared<crow::Request>(parser->release(), reqEc);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700377 if (reqEc)
378 {
Ed Tanous62598e32023-07-17 17:06:25 -0700379 BMCWEB_LOG_DEBUG("Request failed to construct{}", reqEc.message());
Gunnar Mills262f1152022-12-20 15:18:47 -0600380 res.result(boost::beast::http::status::bad_request);
381 completeRequest(res);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700382 return;
383 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700384 req->session = userSession;
Ed Tanous89cda632024-04-16 08:45:54 -0700385 accept = req->getHeaderValue("Accept");
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000386 // Fetch the client IP address
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700387 req->ipAddress = ip;
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000388
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 // Check for HTTP version 1.1.
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700390 if (req->version() == 11)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 {
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700392 if (req->getHeaderValue(boost::beast::http::field::host).empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700393 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700394 res.result(boost::beast::http::status::bad_request);
Nan Zhou72374eb2022-01-27 17:06:51 -0800395 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700396 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 }
398 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700399
Ed Tanous62598e32023-07-17 17:06:25 -0700400 BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this),
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700401 req->version() / 10, req->version() % 10,
402 req->methodString(), req->target(),
403 req->ipAddress.to_string());
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700404
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700405 if (res.completed)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800407 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700408 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700409 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700410 keepAlive = req->keepAlive();
Ed Tanous796ba932020-08-02 04:29:21 +0000411
412 if (authenticationEnabled)
Ed Tanous1d3c14a2021-09-22 18:54:40 -0700413 {
Ed Tanous796ba932020-08-02 04:29:21 +0000414 if (!crow::authentication::isOnAllowlist(req->url().path(),
415 req->method()) &&
416 req->session == nullptr)
Ed Tanous4fa45df2023-09-01 14:20:50 -0700417 {
Ed Tanous796ba932020-08-02 04:29:21 +0000418 BMCWEB_LOG_WARNING("Authentication failed");
419 forward_unauthorized::sendUnauthorized(
420 req->url().encoded_path(),
421 req->getHeaderValue("X-Requested-With"),
422 req->getHeaderValue("Accept"), res);
423 completeRequest(res);
424 return;
Ed Tanous4fa45df2023-09-01 14:20:50 -0700425 }
Ed Tanous4fa45df2023-09-01 14:20:50 -0700426 }
Ed Tanous796ba932020-08-02 04:29:21 +0000427
Nan Zhou72374eb2022-01-27 17:06:51 -0800428 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Ed Tanous62598e32023-07-17 17:06:25 -0700429 BMCWEB_LOG_DEBUG("Setting completion handler");
Nan Zhou72374eb2022-01-27 17:06:51 -0800430 asyncResp->res.setCompleteRequestHandler(
431 [self(shared_from_this())](crow::Response& thisRes) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400432 self->completeRequest(thisRes);
433 });
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800434 if (doUpgrade(asyncResp))
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700435 {
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700436 return;
437 }
Ed Tanous291d7092022-04-13 12:34:57 -0700438 std::string_view expected =
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700439 req->getHeaderValue(boost::beast::http::field::if_none_match);
Ed Tanous291d7092022-04-13 12:34:57 -0700440 if (!expected.empty())
441 {
Ed Tanous37b912f2025-03-20 18:24:30 -0700442 asyncResp->res.setExpectedHash(expected);
Ed Tanous291d7092022-04-13 12:34:57 -0700443 }
Ed Tanous52e31622024-01-23 16:31:11 -0800444 handler->handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700445 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700446
Ed Tanous1d1d7782024-04-09 12:54:08 -0700447 void hardClose()
Ed Tanouse278c182019-03-13 16:23:37 -0700448 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700449 BMCWEB_LOG_DEBUG("{} Closing socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000450 adaptor.next_layer().close();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700451 }
452
453 void tlsShutdownComplete(const std::shared_ptr<self_type>& self,
454 const boost::system::error_code& ec)
455 {
456 if (ec)
457 {
458 BMCWEB_LOG_WARNING("{} Failed to shut down TLS cleanly {}",
459 logPtr(self.get()), ec);
460 }
461 self->hardClose();
462 }
463
464 void gracefulClose()
465 {
466 BMCWEB_LOG_DEBUG("{} Socket close requested", logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700467
Ed Tanous796ba932020-08-02 04:29:21 +0000468 if (httpType == HttpType::HTTPS)
Ed Tanouse278c182019-03-13 16:23:37 -0700469 {
Ed Tanous796ba932020-08-02 04:29:21 +0000470 if (mtlsSession != nullptr)
471 {
472 BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this),
473 mtlsSession->uniqueId);
474 persistent_data::SessionStore::getInstance().removeSession(
475 mtlsSession);
476 }
477
Ed Tanous1d1d7782024-04-09 12:54:08 -0700478 adaptor.async_shutdown(std::bind_front(
479 &self_type::tlsShutdownComplete, this, shared_from_this()));
Ed Tanouse278c182019-03-13 16:23:37 -0700480 }
481 else
482 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700483 hardClose();
Ed Tanouse278c182019-03-13 16:23:37 -0700484 }
485 }
486
Nan Zhou72374eb2022-01-27 17:06:51 -0800487 void completeRequest(crow::Response& thisRes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700488 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800489 res = std::move(thisRes);
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800490 res.keepAlive(keepAlive);
Ed Tanous5ae6f922023-01-09 10:45:53 -0800491
Ed Tanous89cda632024-04-16 08:45:54 -0700492 completeResponseFields(accept, res);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700493 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
Ed Tanous291d7092022-04-13 12:34:57 -0700494
Ed Tanous52e31622024-01-23 16:31:11 -0800495 doWrite();
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000496
497 // delete lambda with self shared_ptr
498 // to enable connection destruction
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700499 res.setCompleteRequestHandler(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700500 }
501
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500502 void readClientIp()
503 {
504 boost::system::error_code ec;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500505
Ed Tanous796ba932020-08-02 04:29:21 +0000506 boost::asio::ip::tcp::endpoint endpoint =
507 boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700508
Ed Tanous796ba932020-08-02 04:29:21 +0000509 if (ec)
510 {
511 // If remote endpoint fails keep going. "ClientOriginIPAddress"
512 // will be empty.
513 BMCWEB_LOG_ERROR("Failed to get the client's IP Address. ec : {}",
514 ec);
515 return;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500516 }
Ed Tanous796ba932020-08-02 04:29:21 +0000517 ip = endpoint.address();
518 }
519
520 void disableAuth()
521 {
522 authenticationEnabled = false;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500523 }
524
Ed Tanous1abe55e2018-09-05 08:30:59 -0700525 private:
Ed Tanous1d1d7782024-04-09 12:54:08 -0700526 uint64_t getContentLengthLimit()
527 {
Ed Tanous83328312024-05-09 15:48:09 -0700528 if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH)
Ed Tanous1d1d7782024-04-09 12:54:08 -0700529 {
Ed Tanous83328312024-05-09 15:48:09 -0700530 if (userSession == nullptr)
531 {
532 return loggedOutPostBodyLimit;
533 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700534 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700535
536 return httpReqBodyLimit;
537 }
538
539 // Returns true if content length was within limits
540 // Returns false if content length error has been returned
541 bool handleContentLengthError()
542 {
543 if (!parser)
544 {
Manojkiran Edaefff2b52024-06-18 18:01:46 +0530545 BMCWEB_LOG_CRITICAL("Parser was null");
Ed Tanous1d1d7782024-04-09 12:54:08 -0700546 return false;
547 }
548 const boost::optional<uint64_t> contentLength =
549 parser->content_length();
550 if (!contentLength)
551 {
552 BMCWEB_LOG_DEBUG("{} No content length available", logPtr(this));
553 return true;
554 }
555
556 uint64_t maxAllowedContentLength = getContentLengthLimit();
557
558 if (*contentLength > maxAllowedContentLength)
559 {
560 // If the users content limit is between the logged in
561 // and logged out limits They probably just didn't log
562 // in
563 if (*contentLength > loggedOutPostBodyLimit &&
564 *contentLength < httpReqBodyLimit)
565 {
566 BMCWEB_LOG_DEBUG(
567 "{} Content length {} valid, but greater than logged out"
568 " limit of {}. Setting unauthorized",
569 logPtr(this), *contentLength, loggedOutPostBodyLimit);
570 res.result(boost::beast::http::status::unauthorized);
571 }
572 else
573 {
574 // Otherwise they're over both limits, so inform
575 // them
576 BMCWEB_LOG_DEBUG(
577 "{} Content length {} was greater than global limit {}."
578 " Setting payload too large",
579 logPtr(this), *contentLength, httpReqBodyLimit);
580 res.result(boost::beast::http::status::payload_too_large);
581 }
582
583 keepAlive = false;
584 doWrite();
585 return false;
586 }
587
588 return true;
589 }
590
Ed Tanous116370d2024-10-08 10:37:28 -0700591 void afterReadHeaders(const std::shared_ptr<self_type>& /*self*/,
592 const boost::system::error_code& ec,
593 std::size_t bytesTransferred)
594 {
595 BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this),
596 bytesTransferred);
597
598 if (ec)
599 {
600 cancelDeadlineTimer();
601
602 if (ec == boost::beast::http::error::header_limit)
603 {
604 BMCWEB_LOG_ERROR("{} Header field too large, closing",
605 logPtr(this), ec.message());
606
607 res.result(boost::beast::http::status::
608 request_header_fields_too_large);
609 keepAlive = false;
610 doWrite();
611 return;
612 }
613 if (ec == boost::beast::http::error::end_of_stream)
614 {
615 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
616 ec);
617 hardClose();
618 return;
619 }
620
621 BMCWEB_LOG_DEBUG("{} Closing socket due to read error {}",
622 logPtr(this), ec.message());
623 gracefulClose();
624
625 return;
626 }
627
628 if (!parser)
629 {
630 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
631 return;
632 }
Ed Tanous3d158642025-05-12 14:20:49 -0700633 auto& parse = *parser;
634 const auto& value = parser->get();
Ed Tanous116370d2024-10-08 10:37:28 -0700635
Ed Tanous796ba932020-08-02 04:29:21 +0000636 if (authenticationEnabled)
Ed Tanous116370d2024-10-08 10:37:28 -0700637 {
Ed Tanous3d158642025-05-12 14:20:49 -0700638 boost::beast::http::verb method = value.method();
Ed Tanous796ba932020-08-02 04:29:21 +0000639 userSession = authentication::authenticate(
Ed Tanous3d158642025-05-12 14:20:49 -0700640 ip, res, method, value.base(), mtlsSession);
Ed Tanous116370d2024-10-08 10:37:28 -0700641 }
642
Ed Tanous3d158642025-05-12 14:20:49 -0700643 std::string_view expect = value[boost::beast::http::field::expect];
Ed Tanous116370d2024-10-08 10:37:28 -0700644 if (bmcweb::asciiIEquals(expect, "100-continue"))
645 {
646 res.result(boost::beast::http::status::continue_);
647 doWrite();
648 return;
649 }
650
651 if (!handleContentLengthError())
652 {
653 return;
654 }
655
Ed Tanous3d158642025-05-12 14:20:49 -0700656 parse.body_limit(getContentLengthLimit());
Ed Tanous116370d2024-10-08 10:37:28 -0700657
Ed Tanous3d158642025-05-12 14:20:49 -0700658 if (parse.is_done())
Ed Tanous116370d2024-10-08 10:37:28 -0700659 {
660 handle();
661 return;
662 }
663
664 doRead();
665 }
666
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 void doReadHeaders()
668 {
Ed Tanous62598e32023-07-17 17:06:25 -0700669 BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700670 if (!parser)
671 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700672 BMCWEB_LOG_CRITICAL("Parser was not initialized.");
Ed Tanouse01d0c32023-06-30 13:21:32 -0700673 return;
674 }
Ed Tanous796ba932020-08-02 04:29:21 +0000675
676 if (httpType == HttpType::HTTP)
677 {
678 boost::beast::http::async_read_header(
679 adaptor.next_layer(), buffer, *parser,
680 std::bind_front(&self_type::afterReadHeaders, this,
681 shared_from_this()));
682 }
683 else
684 {
685 boost::beast::http::async_read_header(
686 adaptor, buffer, *parser,
687 std::bind_front(&self_type::afterReadHeaders, this,
688 shared_from_this()));
689 }
Ed Tanous116370d2024-10-08 10:37:28 -0700690 }
Ed Tanous52e31622024-01-23 16:31:11 -0800691
Ed Tanous116370d2024-10-08 10:37:28 -0700692 void afterRead(const std::shared_ptr<self_type>& /*self*/,
693 const boost::system::error_code& ec,
694 std::size_t bytesTransferred)
695 {
696 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
697 bytesTransferred);
698
699 if (ec)
700 {
701 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
702 ec.message());
703 if (ec == boost::beast::http::error::body_limit)
704 {
705 if (handleContentLengthError())
Ed Tanous1d1d7782024-04-09 12:54:08 -0700706 {
Ed Tanous116370d2024-10-08 10:37:28 -0700707 BMCWEB_LOG_CRITICAL("Body length limit reached, "
708 "but no content-length "
709 "available? Should never happen");
710 res.result(
711 boost::beast::http::status::internal_server_error);
712 keepAlive = false;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700713 doWrite();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700714 }
Ed Tanous116370d2024-10-08 10:37:28 -0700715 return;
716 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400717
Ed Tanous116370d2024-10-08 10:37:28 -0700718 gracefulClose();
719 return;
720 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700721
Ed Tanous116370d2024-10-08 10:37:28 -0700722 // If the user is logged in, allow them to send files
723 // incrementally one piece at a time. If authentication is
724 // disabled then there is no user session hence always allow to
725 // send one piece at a time.
726 if (userSession != nullptr)
727 {
728 cancelDeadlineTimer();
729 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700730
Ed Tanous116370d2024-10-08 10:37:28 -0700731 if (!parser)
732 {
733 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
734 return;
735 }
736 if (!parser->is_done())
737 {
738 doRead();
739 return;
740 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700741
Ed Tanous116370d2024-10-08 10:37:28 -0700742 cancelDeadlineTimer();
743 handle();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700744 }
745
746 void doRead()
747 {
Ed Tanous62598e32023-07-17 17:06:25 -0700748 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700749 if (!parser)
750 {
751 return;
752 }
Ed Tanous3d158642025-05-12 14:20:49 -0700753 auto& parse = *parser;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800754 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000755 if (httpType == HttpType::HTTP)
756 {
757 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700758 adaptor.next_layer(), buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000759 std::bind_front(&self_type::afterRead, this,
760 shared_from_this()));
761 }
762 else
763 {
764 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700765 adaptor, buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000766 std::bind_front(&self_type::afterRead, this,
767 shared_from_this()));
768 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 }
770
Ed Tanous27b0cf92023-08-07 12:02:40 -0700771 void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
772 const boost::system::error_code& ec,
773 std::size_t bytesTransferred)
774 {
Ed Tanous0242baf2024-05-16 19:52:47 -0700775 BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this),
Ed Tanous1d1d7782024-04-09 12:54:08 -0700776 bytesTransferred, ec);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700777
778 cancelDeadlineTimer();
779
Ed Tanous0242baf2024-05-16 19:52:47 -0700780 if (ec == boost::system::errc::operation_would_block ||
781 ec == boost::system::errc::resource_unavailable_try_again)
782 {
783 doWrite();
784 return;
785 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700786 if (ec)
787 {
788 BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
789 return;
790 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700791
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800792 if (res.result() == boost::beast::http::status::switching_protocols)
793 {
Ed Tanous796ba932020-08-02 04:29:21 +0000794 upgradeToHttp2();
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800795 return;
796 }
797
Ed Tanous1d1d7782024-04-09 12:54:08 -0700798 if (res.result() == boost::beast::http::status::continue_)
799 {
800 // Reset the result to ok
801 res.result(boost::beast::http::status::ok);
802 doRead();
803 return;
804 }
805
Ed Tanous27b0cf92023-08-07 12:02:40 -0700806 if (!keepAlive)
807 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700808 BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket",
809 logPtr(this));
810
811 gracefulClose();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700812 return;
813 }
814
815 BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
816 res.clear();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700817 initParser();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700818
819 userSession = nullptr;
820
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700821 req->clear();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700822 doReadHeaders();
823 }
824
Ed Tanous52e31622024-01-23 16:31:11 -0800825 void doWrite()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700826 {
Ed Tanous62598e32023-07-17 17:06:25 -0700827 BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800828 res.preparePayload();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700829
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800830 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000831 if (httpType == HttpType::HTTP)
832 {
833 boost::beast::async_write(
834 adaptor.next_layer(),
835 boost::beast::http::message_generator(std::move(res.response)),
836 std::bind_front(&self_type::afterDoWrite, this,
837 shared_from_this()));
838 }
839 else
840 {
841 boost::beast::async_write(
842 adaptor,
843 boost::beast::http::message_generator(std::move(res.response)),
844 std::bind_front(&self_type::afterDoWrite, this,
845 shared_from_this()));
846 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 }
848
Ed Tanous1abe55e2018-09-05 08:30:59 -0700849 void cancelDeadlineTimer()
850 {
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800851 timer.cancel();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 }
853
Ed Tanous116370d2024-10-08 10:37:28 -0700854 void afterTimerWait(const std::weak_ptr<self_type>& weakSelf,
855 const boost::system::error_code& ec)
856 {
857 // Note, we are ignoring other types of errors here; If the timer
858 // failed for any reason, we should still close the connection
859 std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock();
860 if (!self)
861 {
862 if (ec == boost::asio::error::operation_aborted)
863 {
864 BMCWEB_LOG_DEBUG(
865 "{} Timer canceled on connection being destroyed",
866 logPtr(self.get()));
867 }
868 else
869 {
870 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
871 logPtr(self.get()));
872 }
873 return;
874 }
875
876 self->timerStarted = false;
877
878 if (ec)
879 {
880 if (ec == boost::asio::error::operation_aborted)
881 {
882 BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get()));
883 return;
884 }
885 BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec);
886 }
887
888 BMCWEB_LOG_WARNING("{} Connection timed out, hard closing",
889 logPtr(self.get()));
890
891 self->hardClose();
892 }
893
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800894 void startDeadline()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895 {
Ed Tanous7d243eb2023-01-23 15:57:41 -0800896 // Timer is already started so no further action is required.
897 if (timerStarted)
898 {
899 return;
900 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800902 std::chrono::seconds timeout(15);
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800903
904 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this();
905 timer.expires_after(timeout);
Ed Tanous116370d2024-10-08 10:37:28 -0700906 timer.async_wait(std::bind_front(&self_type::afterTimerWait, this,
907 weak_from_this()));
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800908
Ed Tanous7d243eb2023-01-23 15:57:41 -0800909 timerStarted = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700910 BMCWEB_LOG_DEBUG("{} timer started", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911 }
912
Ed Tanous796ba932020-08-02 04:29:21 +0000913 bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH;
914 HttpType httpType = HttpType::BOTH;
915
916 boost::asio::ssl::stream<Adaptor> adaptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 Handler* handler;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700918
919 boost::asio::ip::address ip;
920
Ed Tanousa24526d2018-12-10 15:17:59 -0800921 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922 // re-created on Connection reset
Ed Tanousb2896142024-01-31 15:25:47 -0800923 std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924
Ed Tanous3112a142018-11-29 15:45:10 -0800925 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700926
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700927 std::shared_ptr<crow::Request> req;
Ed Tanous89cda632024-04-16 08:45:54 -0700928 std::string accept;
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800929 std::string http2settings;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930 crow::Response res;
Ed Tanous52cc1122020-07-18 13:51:21 -0700931
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700932 std::shared_ptr<persistent_data::UserSession> userSession;
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100933 std::shared_ptr<persistent_data::UserSession> mtlsSession;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800935 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800937 bool keepAlive = true;
938
Ed Tanous7d243eb2023-01-23 15:57:41 -0800939 bool timerStarted = false;
940
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 std::function<std::string()>& getCachedDateStr;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000942
943 using std::enable_shared_from_this<
Ed Tanous52cc1122020-07-18 13:51:21 -0700944 Connection<Adaptor, Handler>>::shared_from_this;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800945
946 using std::enable_shared_from_this<
947 Connection<Adaptor, Handler>>::weak_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800948};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949} // namespace crow