blob: 3f0ba4044e49394c398ade012b6e2cca7876003d [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>
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +053024#include <boost/asio/ssl/error.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070025#include <boost/asio/ssl/stream.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080026#include <boost/asio/ssl/stream_base.hpp>
27#include <boost/asio/ssl/verify_context.hpp>
Ed Tanous5dfb5b22021-12-03 11:24:53 -080028#include <boost/asio/steady_timer.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070029#include <boost/beast/_experimental/test/stream.hpp>
Ed Tanous4d698612024-02-06 14:57:24 -080030#include <boost/beast/core/buffers_generator.hpp>
Ed Tanous796ba932020-08-02 04:29:21 +000031#include <boost/beast/core/detect_ssl.hpp>
32#include <boost/beast/core/error.hpp>
Ed Tanous3112a142018-11-29 15:45:10 -080033#include <boost/beast/core/flat_static_buffer.hpp>
Myung Baea4326fe2023-01-10 14:29:24 -060034#include <boost/beast/http/error.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080035#include <boost/beast/http/field.hpp>
Ed Tanous4d698612024-02-06 14:57:24 -080036#include <boost/beast/http/message_generator.hpp>
Ed Tanous918ef252022-05-25 10:40:41 -070037#include <boost/beast/http/parser.hpp>
38#include <boost/beast/http/read.hpp>
Ed Tanouscd7dbb32025-02-01 12:37:56 -080039#include <boost/beast/http/rfc7230.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080040#include <boost/beast/http/status.hpp>
41#include <boost/beast/http/verb.hpp>
42#include <boost/none.hpp>
43#include <boost/optional/optional.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050044
Ed Tanousd7857202025-01-28 15:32:26 -080045#include <bit>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046#include <chrono>
Ed Tanousd7857202025-01-28 15:32:26 -080047#include <cstddef>
48#include <cstdint>
49#include <functional>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070050#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080051#include <optional>
52#include <string>
53#include <string_view>
54#include <system_error>
55#include <type_traits>
56#include <utility>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050057
Ed Tanous1abe55e2018-09-05 08:30:59 -070058namespace crow
59{
Ed Tanous257f5792018-03-17 14:40:09 -070060
Ed Tanouscf9e4172022-12-21 09:30:16 -080061// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous6fbdbca2021-12-06 14:36:06 -080062static int connectionCount = 0;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070063
Ed Tanous25b54db2024-04-17 15:40:31 -070064// request body limit size set by the BMCWEB_HTTP_BODY_LIMIT option
65constexpr uint64_t httpReqBodyLimit = 1024UL * 1024UL * BMCWEB_HTTP_BODY_LIMIT;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070066
Ed Tanous1d1d7782024-04-09 12:54:08 -070067constexpr uint64_t loggedOutPostBodyLimit = 4096U;
James Feist3909dc82020-04-03 10:58:55 -070068
Ed Tanous1d1d7782024-04-09 12:54:08 -070069constexpr uint32_t httpHeaderLimit = 8192U;
James Feist3909dc82020-04-03 10:58:55 -070070
Ed Tanous52cc1122020-07-18 13:51:21 -070071template <typename Adaptor, typename Handler>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050072class Connection :
Ed Tanous52cc1122020-07-18 13:51:21 -070073 public std::enable_shared_from_this<Connection<Adaptor, Handler>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070074{
Ed Tanous7c8e0642022-02-21 12:11:14 -080075 using self_type = Connection<Adaptor, Handler>;
76
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 public:
Ed Tanous796ba932020-08-02 04:29:21 +000078 Connection(Handler* handlerIn, HttpType httpTypeIn,
79 boost::asio::steady_timer&& timerIn,
Ed Tanous81ce6092020-12-17 16:54:55 +000080 std::function<std::string()>& getCachedDateStrF,
Ed Tanous796ba932020-08-02 04:29:21 +000081 boost::asio::ssl::stream<Adaptor>&& adaptorIn) :
82 httpType(httpTypeIn), adaptor(std::move(adaptorIn)), handler(handlerIn),
Patrick Williamsbd79bce2024-08-16 15:22:20 -040083 timer(std::move(timerIn)), getCachedDateStr(getCachedDateStrF)
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070085 initParser();
Kowalski, Kamil55e43f62019-07-10 13:12:57 +020086
Ed Tanous40aa0582021-07-14 13:24:40 -070087 connectionCount++;
Ed Tanous6fbdbca2021-12-06 14:36:06 -080088
Ed Tanous1d1d7782024-04-09 12:54:08 -070089 BMCWEB_LOG_DEBUG("{} Connection created, total {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -070090 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -070091 }
92
93 ~Connection()
94 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070095 res.releaseCompleteRequestHandler();
Ed Tanous40aa0582021-07-14 13:24:40 -070096 cancelDeadlineTimer();
Ed Tanous6fbdbca2021-12-06 14:36:06 -080097
Ed Tanous40aa0582021-07-14 13:24:40 -070098 connectionCount--;
Ed Tanous62598e32023-07-17 17:06:25 -070099 BMCWEB_LOG_DEBUG("{} Connection closed, total {}", logPtr(this),
100 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -0700101 }
102
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800103 Connection(const Connection&) = delete;
104 Connection(Connection&&) = delete;
105 Connection& operator=(const Connection&) = delete;
106 Connection& operator=(Connection&&) = delete;
107
Ed Tanous7c8e0642022-02-21 12:11:14 -0800108 bool tlsVerifyCallback(bool preverified,
109 boost::asio::ssl::verify_context& ctx)
110 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700111 BMCWEB_LOG_DEBUG("{} tlsVerifyCallback called with preverified {}",
112 logPtr(this), preverified);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800113 if (preverified)
114 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700115 mtlsSession = verifyMtlsUser(ip, ctx);
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100116 if (mtlsSession)
Ed Tanous7c8e0642022-02-21 12:11:14 -0800117 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700118 BMCWEB_LOG_DEBUG("{} Generated TLS session: {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -0700119 mtlsSession->uniqueId);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800120 }
121 }
Ed Tanous3281bcf2024-06-25 16:02:05 -0700122 const persistent_data::AuthConfigMethods& c =
123 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
124 if (c.tlsStrict)
125 {
Ed Tanous463a0e32024-10-14 11:21:48 -0700126 BMCWEB_LOG_DEBUG(
127 "{} TLS is in strict mode, returning preverified as is.",
128 logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700129 return preverified;
130 }
131 // If tls strict mode is disabled
132 // We always return true to allow full auth flow for resources that
133 // don't require auth
Ed Tanous7c8e0642022-02-21 12:11:14 -0800134 return true;
135 }
136
Ed Tanous3281bcf2024-06-25 16:02:05 -0700137 bool prepareMutualTls()
Ed Tanous40aa0582021-07-14 13:24:40 -0700138 {
Ed Tanous796ba932020-08-02 04:29:21 +0000139 BMCWEB_LOG_DEBUG("prepareMutualTls");
140
141 constexpr std::string_view id = "bmcweb";
142
143 const char* idPtr = id.data();
144 const auto* idCPtr = std::bit_cast<const unsigned char*>(idPtr);
145 auto idLen = static_cast<unsigned int>(id.length());
146 int ret =
147 SSL_set_session_id_context(adaptor.native_handle(), idCPtr, idLen);
148 if (ret == 0)
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100149 {
Ed Tanous796ba932020-08-02 04:29:21 +0000150 BMCWEB_LOG_ERROR("{} failed to set SSL id", logPtr(this));
151 return false;
152 }
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100153
Ed Tanous796ba932020-08-02 04:29:21 +0000154 BMCWEB_LOG_DEBUG("set_verify_callback");
Ed Tanous3281bcf2024-06-25 16:02:05 -0700155
Ed Tanous796ba932020-08-02 04:29:21 +0000156 boost::system::error_code ec;
157 adaptor.set_verify_callback(
158 std::bind_front(&self_type::tlsVerifyCallback, this), ec);
159 if (ec)
160 {
161 BMCWEB_LOG_ERROR("Failed to set verify callback {}", ec);
162 return false;
Ed Tanous3281bcf2024-06-25 16:02:05 -0700163 }
164
165 return true;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700166 }
167
Ed Tanous796ba932020-08-02 04:29:21 +0000168 void afterDetectSsl(const std::shared_ptr<self_type>& /*self*/,
169 boost::beast::error_code ec, bool isTls)
170 {
171 if (ec)
172 {
173 BMCWEB_LOG_ERROR("Couldn't detect ssl ", ec);
174 return;
175 }
176 BMCWEB_LOG_DEBUG("{} TLS was detected as {}", logPtr(this), isTls);
177 if (isTls)
178 {
179 if (httpType != HttpType::HTTPS && httpType != HttpType::BOTH)
180 {
181 BMCWEB_LOG_WARNING(
182 "{} Connection closed due to incompatible type",
183 logPtr(this));
184 return;
185 }
186 httpType = HttpType::HTTPS;
187 adaptor.async_handshake(
188 boost::asio::ssl::stream_base::server, buffer.data(),
189 std::bind_front(&self_type::afterSslHandshake, this,
190 shared_from_this()));
191 }
192 else
193 {
194 if (httpType != HttpType::HTTP && httpType != HttpType::BOTH)
195 {
196 BMCWEB_LOG_WARNING(
197 "{} Connection closed due to incompatible type",
198 logPtr(this));
199 return;
200 }
201
202 httpType = HttpType::HTTP;
203 BMCWEB_LOG_INFO("Starting non-SSL session");
204 doReadHeaders();
205 }
206 }
207
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 void start()
209 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700210 BMCWEB_LOG_DEBUG("{} Connection started, total {}", logPtr(this),
211 connectionCount);
Gunnar Mills4f63be02023-10-25 09:14:07 -0500212 if (connectionCount >= 200)
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800213 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700214 BMCWEB_LOG_CRITICAL("{} Max connection count exceeded.",
Ed Tanous62598e32023-07-17 17:06:25 -0700215 logPtr(this));
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800216 return;
217 }
218
Ed Tanous3281bcf2024-06-25 16:02:05 -0700219 if constexpr (BMCWEB_MUTUAL_TLS_AUTH)
220 {
221 if (!prepareMutualTls())
222 {
223 BMCWEB_LOG_ERROR("{} Failed to prepare mTLS", logPtr(this));
224 return;
225 }
226 }
227
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800228 startDeadline();
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500229
Ed Tanous1d1d7782024-04-09 12:54:08 -0700230 readClientIp();
Ed Tanous796ba932020-08-02 04:29:21 +0000231 boost::beast::async_detect_ssl(
232 adaptor.next_layer(), buffer,
233 std::bind_front(&self_type::afterDetectSsl, this,
234 shared_from_this()));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700235 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700236
Ed Tanous796ba932020-08-02 04:29:21 +0000237 void afterSslHandshake(const std::shared_ptr<self_type>& /*self*/,
238 const boost::system::error_code& ec,
239 size_t bytesParsed)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800240 {
Ed Tanous796ba932020-08-02 04:29:21 +0000241 buffer.consume(bytesParsed);
242 if (ec)
243 {
244 BMCWEB_LOG_ERROR("{} SSL handshake failed", logPtr(this));
245 return;
246 }
247 BMCWEB_LOG_DEBUG("{} SSL handshake succeeded", logPtr(this));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800248 // If http2 is enabled, negotiate the protocol
Ed Tanous25b54db2024-04-17 15:40:31 -0700249 if constexpr (BMCWEB_EXPERIMENTAL_HTTP2)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800250 {
251 const unsigned char* alpn = nullptr;
252 unsigned int alpnlen = 0;
253 SSL_get0_alpn_selected(adaptor.native_handle(), &alpn, &alpnlen);
254 if (alpn != nullptr)
255 {
256 std::string_view selectedProtocol(
257 std::bit_cast<const char*>(alpn), alpnlen);
Ed Tanous62598e32023-07-17 17:06:25 -0700258 BMCWEB_LOG_DEBUG("ALPN selected protocol \"{}\" len: {}",
259 selectedProtocol, alpnlen);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800260 if (selectedProtocol == "h2")
261 {
Ed Tanous796ba932020-08-02 04:29:21 +0000262 upgradeToHttp2();
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800263 return;
264 }
265 }
266 }
267
268 doReadHeaders();
269 }
270
Ed Tanous1d1d7782024-04-09 12:54:08 -0700271 void initParser()
272 {
273 boost::beast::http::request_parser<bmcweb::HttpBody>& instance =
Ed Tanous38afdb92024-12-11 23:57:53 -0800274 parser.emplace();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700275
276 // reset header limit for newly created parser
277 instance.header_limit(httpHeaderLimit);
278
279 // Initially set no body limit. We don't yet know if the user is
280 // authenticated.
281 instance.body_limit(boost::none);
282 }
283
Ed Tanous796ba932020-08-02 04:29:21 +0000284 void upgradeToHttp2()
285 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800286 auto http2 = std::make_shared<HTTP2Connection<Adaptor, Handler>>(
287 std::move(adaptor), handler, getCachedDateStr, httpType);
288 if (http2settings.empty())
Ed Tanous796ba932020-08-02 04:29:21 +0000289 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800290 http2->start();
Ed Tanous796ba932020-08-02 04:29:21 +0000291 }
292 else
293 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800294 http2->startFromSettings(http2settings);
Ed Tanous796ba932020-08-02 04:29:21 +0000295 }
296 }
297
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800298 // returns whether connection was upgraded
299 bool doUpgrade(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
300 {
301 using boost::beast::http::field;
302 using boost::beast::http::token_list;
303
304 bool isSse =
305 isContentTypeAllowed(req->getHeaderValue("Accept"),
306 http_helpers::ContentType::EventStream, false);
307
308 bool isWebsocket = false;
309 bool isH2c = false;
310 // Check connection header is upgrade
311 if (token_list{req->req[field::connection]}.exists("upgrade"))
312 {
313 BMCWEB_LOG_DEBUG("{} Connection: Upgrade header was present",
314 logPtr(this));
315 // Parse if upgrade is h2c or websocket
316 token_list upgrade{req->req[field::upgrade]};
317 isWebsocket = upgrade.exists("websocket");
318 isH2c = upgrade.exists("h2c");
319 BMCWEB_LOG_DEBUG("{} Upgrade isWebsocket: {} isH2c: {}",
320 logPtr(this), isWebsocket, isH2c);
321 }
322
323 if (BMCWEB_EXPERIMENTAL_HTTP2 && isH2c)
324 {
325 std::string_view base64settings = req->req[field::http2_settings];
326 if (utility::base64Decode<true>(base64settings, http2settings))
327 {
328 res.result(boost::beast::http::status::switching_protocols);
329 res.addHeader(boost::beast::http::field::connection, "Upgrade");
330 res.addHeader(boost::beast::http::field::upgrade, "h2c");
331 }
332 }
333
334 // websocket and SSE are only allowed on GET
335 if (req->req.method() == boost::beast::http::verb::get)
336 {
337 if (isWebsocket || isSse)
338 {
339 asyncResp->res.setCompleteRequestHandler(
340 [self(shared_from_this())](crow::Response& thisRes) {
341 if (thisRes.result() != boost::beast::http::status::ok)
342 {
343 // When any error occurs before handle upgradation,
344 // the result in response will be set to respective
345 // error. By default the Result will be OK (200),
346 // which implies successful handle upgrade. Response
347 // needs to be sent over this connection only on
348 // failure.
349 self->completeRequest(thisRes);
350 return;
351 }
352 });
353 BMCWEB_LOG_INFO("{} Upgrading socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000354 if (httpType == HttpType::HTTP)
355 {
356 handler->handleUpgrade(req, asyncResp,
357 std::move(adaptor.next_layer()));
358 }
359 else
360 {
361 handler->handleUpgrade(req, asyncResp, std::move(adaptor));
362 }
363
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800364 return true;
365 }
366 }
367 return false;
368 }
369
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 void handle()
371 {
Ed Tanousf79b7a52021-09-22 19:04:29 -0700372 std::error_code reqEc;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700373 if (!parser)
374 {
375 return;
376 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700377 req = std::make_shared<crow::Request>(parser->release(), reqEc);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700378 if (reqEc)
379 {
Ed Tanous62598e32023-07-17 17:06:25 -0700380 BMCWEB_LOG_DEBUG("Request failed to construct{}", reqEc.message());
Gunnar Mills262f1152022-12-20 15:18:47 -0600381 res.result(boost::beast::http::status::bad_request);
382 completeRequest(res);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700383 return;
384 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700385 req->session = userSession;
Ed Tanous89cda632024-04-16 08:45:54 -0700386 accept = req->getHeaderValue("Accept");
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000387 // Fetch the client IP address
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700388 req->ipAddress = ip;
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000389
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 // Check for HTTP version 1.1.
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700391 if (req->version() == 11)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700393 if (req->getHeaderValue(boost::beast::http::field::host).empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700395 res.result(boost::beast::http::status::bad_request);
Nan Zhou72374eb2022-01-27 17:06:51 -0800396 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700397 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 }
399 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700400
Ed Tanous62598e32023-07-17 17:06:25 -0700401 BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this),
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700402 req->version() / 10, req->version() % 10,
403 req->methodString(), req->target(),
404 req->ipAddress.to_string());
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700405
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700406 if (res.completed)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800408 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700409 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700411 keepAlive = req->keepAlive();
Ed Tanous796ba932020-08-02 04:29:21 +0000412
413 if (authenticationEnabled)
Ed Tanous1d3c14a2021-09-22 18:54:40 -0700414 {
Ed Tanous796ba932020-08-02 04:29:21 +0000415 if (!crow::authentication::isOnAllowlist(req->url().path(),
416 req->method()) &&
417 req->session == nullptr)
Ed Tanous4fa45df2023-09-01 14:20:50 -0700418 {
Ed Tanous796ba932020-08-02 04:29:21 +0000419 BMCWEB_LOG_WARNING("Authentication failed");
420 forward_unauthorized::sendUnauthorized(
421 req->url().encoded_path(),
422 req->getHeaderValue("X-Requested-With"),
423 req->getHeaderValue("Accept"), res);
424 completeRequest(res);
425 return;
Ed Tanous4fa45df2023-09-01 14:20:50 -0700426 }
Ed Tanous4fa45df2023-09-01 14:20:50 -0700427 }
Ed Tanous796ba932020-08-02 04:29:21 +0000428
Nan Zhou72374eb2022-01-27 17:06:51 -0800429 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Ed Tanous62598e32023-07-17 17:06:25 -0700430 BMCWEB_LOG_DEBUG("Setting completion handler");
Nan Zhou72374eb2022-01-27 17:06:51 -0800431 asyncResp->res.setCompleteRequestHandler(
432 [self(shared_from_this())](crow::Response& thisRes) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400433 self->completeRequest(thisRes);
434 });
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800435 if (doUpgrade(asyncResp))
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700436 {
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700437 return;
438 }
Ed Tanous291d7092022-04-13 12:34:57 -0700439 std::string_view expected =
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700440 req->getHeaderValue(boost::beast::http::field::if_none_match);
Ed Tanous291d7092022-04-13 12:34:57 -0700441 if (!expected.empty())
442 {
Ed Tanous37b912f2025-03-20 18:24:30 -0700443 asyncResp->res.setExpectedHash(expected);
Ed Tanous291d7092022-04-13 12:34:57 -0700444 }
Ed Tanous52e31622024-01-23 16:31:11 -0800445 handler->handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700446 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700447
Ed Tanous1d1d7782024-04-09 12:54:08 -0700448 void hardClose()
Ed Tanouse278c182019-03-13 16:23:37 -0700449 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700450 BMCWEB_LOG_DEBUG("{} Closing socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000451 adaptor.next_layer().close();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700452 }
453
454 void tlsShutdownComplete(const std::shared_ptr<self_type>& self,
455 const boost::system::error_code& ec)
456 {
457 if (ec)
458 {
459 BMCWEB_LOG_WARNING("{} Failed to shut down TLS cleanly {}",
460 logPtr(self.get()), ec);
461 }
462 self->hardClose();
463 }
464
465 void gracefulClose()
466 {
467 BMCWEB_LOG_DEBUG("{} Socket close requested", logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700468
Ed Tanous796ba932020-08-02 04:29:21 +0000469 if (httpType == HttpType::HTTPS)
Ed Tanouse278c182019-03-13 16:23:37 -0700470 {
Ed Tanous796ba932020-08-02 04:29:21 +0000471 if (mtlsSession != nullptr)
472 {
473 BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this),
474 mtlsSession->uniqueId);
475 persistent_data::SessionStore::getInstance().removeSession(
476 mtlsSession);
477 }
478
Ed Tanous1d1d7782024-04-09 12:54:08 -0700479 adaptor.async_shutdown(std::bind_front(
480 &self_type::tlsShutdownComplete, this, shared_from_this()));
Ed Tanouse278c182019-03-13 16:23:37 -0700481 }
482 else
483 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700484 hardClose();
Ed Tanouse278c182019-03-13 16:23:37 -0700485 }
486 }
487
Nan Zhou72374eb2022-01-27 17:06:51 -0800488 void completeRequest(crow::Response& thisRes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700489 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800490 res = std::move(thisRes);
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800491 res.keepAlive(keepAlive);
Ed Tanous5ae6f922023-01-09 10:45:53 -0800492
Ed Tanous89cda632024-04-16 08:45:54 -0700493 completeResponseFields(accept, res);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700494 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
Ed Tanous291d7092022-04-13 12:34:57 -0700495
Ed Tanous52e31622024-01-23 16:31:11 -0800496 doWrite();
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000497
498 // delete lambda with self shared_ptr
499 // to enable connection destruction
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700500 res.setCompleteRequestHandler(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700501 }
502
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500503 void readClientIp()
504 {
505 boost::system::error_code ec;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500506
Ed Tanous796ba932020-08-02 04:29:21 +0000507 boost::asio::ip::tcp::endpoint endpoint =
508 boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700509
Ed Tanous796ba932020-08-02 04:29:21 +0000510 if (ec)
511 {
512 // If remote endpoint fails keep going. "ClientOriginIPAddress"
513 // will be empty.
514 BMCWEB_LOG_ERROR("Failed to get the client's IP Address. ec : {}",
515 ec);
516 return;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500517 }
Ed Tanous796ba932020-08-02 04:29:21 +0000518 ip = endpoint.address();
519 }
520
521 void disableAuth()
522 {
523 authenticationEnabled = false;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500524 }
525
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 private:
Ed Tanous1d1d7782024-04-09 12:54:08 -0700527 uint64_t getContentLengthLimit()
528 {
Ed Tanous83328312024-05-09 15:48:09 -0700529 if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH)
Ed Tanous1d1d7782024-04-09 12:54:08 -0700530 {
Ed Tanous83328312024-05-09 15:48:09 -0700531 if (userSession == nullptr)
532 {
533 return loggedOutPostBodyLimit;
534 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700535 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700536
537 return httpReqBodyLimit;
538 }
539
540 // Returns true if content length was within limits
541 // Returns false if content length error has been returned
542 bool handleContentLengthError()
543 {
544 if (!parser)
545 {
Manojkiran Edaefff2b52024-06-18 18:01:46 +0530546 BMCWEB_LOG_CRITICAL("Parser was null");
Ed Tanous1d1d7782024-04-09 12:54:08 -0700547 return false;
548 }
549 const boost::optional<uint64_t> contentLength =
550 parser->content_length();
551 if (!contentLength)
552 {
553 BMCWEB_LOG_DEBUG("{} No content length available", logPtr(this));
554 return true;
555 }
556
557 uint64_t maxAllowedContentLength = getContentLengthLimit();
558
559 if (*contentLength > maxAllowedContentLength)
560 {
561 // If the users content limit is between the logged in
562 // and logged out limits They probably just didn't log
563 // in
564 if (*contentLength > loggedOutPostBodyLimit &&
565 *contentLength < httpReqBodyLimit)
566 {
567 BMCWEB_LOG_DEBUG(
568 "{} Content length {} valid, but greater than logged out"
569 " limit of {}. Setting unauthorized",
570 logPtr(this), *contentLength, loggedOutPostBodyLimit);
571 res.result(boost::beast::http::status::unauthorized);
572 }
573 else
574 {
575 // Otherwise they're over both limits, so inform
576 // them
577 BMCWEB_LOG_DEBUG(
578 "{} Content length {} was greater than global limit {}."
579 " Setting payload too large",
580 logPtr(this), *contentLength, httpReqBodyLimit);
581 res.result(boost::beast::http::status::payload_too_large);
582 }
583
584 keepAlive = false;
585 doWrite();
586 return false;
587 }
588
589 return true;
590 }
591
Ed Tanous116370d2024-10-08 10:37:28 -0700592 void afterReadHeaders(const std::shared_ptr<self_type>& /*self*/,
593 const boost::system::error_code& ec,
594 std::size_t bytesTransferred)
595 {
596 BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this),
597 bytesTransferred);
598
599 if (ec)
600 {
601 cancelDeadlineTimer();
602
603 if (ec == boost::beast::http::error::header_limit)
604 {
605 BMCWEB_LOG_ERROR("{} Header field too large, closing",
606 logPtr(this), ec.message());
607
608 res.result(boost::beast::http::status::
609 request_header_fields_too_large);
610 keepAlive = false;
611 doWrite();
612 return;
613 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700614 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
615 ec);
616 hardClose();
Ed Tanous116370d2024-10-08 10:37:28 -0700617 return;
618 }
619
620 if (!parser)
621 {
622 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
623 return;
624 }
Ed Tanous3d158642025-05-12 14:20:49 -0700625 auto& parse = *parser;
626 const auto& value = parser->get();
Ed Tanous116370d2024-10-08 10:37:28 -0700627
Ed Tanous796ba932020-08-02 04:29:21 +0000628 if (authenticationEnabled)
Ed Tanous116370d2024-10-08 10:37:28 -0700629 {
Ed Tanous3d158642025-05-12 14:20:49 -0700630 boost::beast::http::verb method = value.method();
Ed Tanous796ba932020-08-02 04:29:21 +0000631 userSession = authentication::authenticate(
Ed Tanous3d158642025-05-12 14:20:49 -0700632 ip, res, method, value.base(), mtlsSession);
Ed Tanous116370d2024-10-08 10:37:28 -0700633 }
634
Ed Tanous3d158642025-05-12 14:20:49 -0700635 std::string_view expect = value[boost::beast::http::field::expect];
Ed Tanous116370d2024-10-08 10:37:28 -0700636 if (bmcweb::asciiIEquals(expect, "100-continue"))
637 {
638 res.result(boost::beast::http::status::continue_);
639 doWrite();
640 return;
641 }
642
643 if (!handleContentLengthError())
644 {
645 return;
646 }
647
Ed Tanous3d158642025-05-12 14:20:49 -0700648 parse.body_limit(getContentLengthLimit());
Ed Tanous116370d2024-10-08 10:37:28 -0700649
Ed Tanous3d158642025-05-12 14:20:49 -0700650 if (parse.is_done())
Ed Tanous116370d2024-10-08 10:37:28 -0700651 {
652 handle();
653 return;
654 }
655
656 doRead();
657 }
658
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 void doReadHeaders()
660 {
Ed Tanous62598e32023-07-17 17:06:25 -0700661 BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700662 if (!parser)
663 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700664 BMCWEB_LOG_CRITICAL("Parser was not initialized.");
Ed Tanouse01d0c32023-06-30 13:21:32 -0700665 return;
666 }
Ed Tanous796ba932020-08-02 04:29:21 +0000667
668 if (httpType == HttpType::HTTP)
669 {
670 boost::beast::http::async_read_header(
671 adaptor.next_layer(), buffer, *parser,
672 std::bind_front(&self_type::afterReadHeaders, this,
673 shared_from_this()));
674 }
675 else
676 {
677 boost::beast::http::async_read_header(
678 adaptor, buffer, *parser,
679 std::bind_front(&self_type::afterReadHeaders, this,
680 shared_from_this()));
681 }
Ed Tanous116370d2024-10-08 10:37:28 -0700682 }
Ed Tanous52e31622024-01-23 16:31:11 -0800683
Ed Tanous116370d2024-10-08 10:37:28 -0700684 void afterRead(const std::shared_ptr<self_type>& /*self*/,
685 const boost::system::error_code& ec,
686 std::size_t bytesTransferred)
687 {
688 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
689 bytesTransferred);
690
691 if (ec)
692 {
693 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
694 ec.message());
695 if (ec == boost::beast::http::error::body_limit)
696 {
697 if (handleContentLengthError())
Ed Tanous1d1d7782024-04-09 12:54:08 -0700698 {
Ed Tanous116370d2024-10-08 10:37:28 -0700699 BMCWEB_LOG_CRITICAL("Body length limit reached, "
700 "but no content-length "
701 "available? Should never happen");
702 res.result(
703 boost::beast::http::status::internal_server_error);
704 keepAlive = false;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700705 doWrite();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700706 }
Ed Tanous116370d2024-10-08 10:37:28 -0700707 return;
708 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700709 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
710 ec);
711 hardClose();
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400712
Ed Tanous116370d2024-10-08 10:37:28 -0700713 return;
714 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700715
Ed Tanous116370d2024-10-08 10:37:28 -0700716 // If the user is logged in, allow them to send files
717 // incrementally one piece at a time. If authentication is
718 // disabled then there is no user session hence always allow to
719 // send one piece at a time.
720 if (userSession != nullptr)
721 {
722 cancelDeadlineTimer();
723 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700724
Ed Tanous116370d2024-10-08 10:37:28 -0700725 if (!parser)
726 {
727 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
728 return;
729 }
730 if (!parser->is_done())
731 {
732 doRead();
733 return;
734 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700735
Ed Tanous116370d2024-10-08 10:37:28 -0700736 cancelDeadlineTimer();
737 handle();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700738 }
739
740 void doRead()
741 {
Ed Tanous62598e32023-07-17 17:06:25 -0700742 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700743 if (!parser)
744 {
745 return;
746 }
Ed Tanous3d158642025-05-12 14:20:49 -0700747 auto& parse = *parser;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800748 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000749 if (httpType == HttpType::HTTP)
750 {
751 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700752 adaptor.next_layer(), buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000753 std::bind_front(&self_type::afterRead, this,
754 shared_from_this()));
755 }
756 else
757 {
758 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700759 adaptor, buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000760 std::bind_front(&self_type::afterRead, this,
761 shared_from_this()));
762 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700763 }
764
Ed Tanous27b0cf92023-08-07 12:02:40 -0700765 void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
766 const boost::system::error_code& ec,
767 std::size_t bytesTransferred)
768 {
Ed Tanous0242baf2024-05-16 19:52:47 -0700769 BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this),
Ed Tanous1d1d7782024-04-09 12:54:08 -0700770 bytesTransferred, ec);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700771
772 cancelDeadlineTimer();
773
Ed Tanous0242baf2024-05-16 19:52:47 -0700774 if (ec == boost::system::errc::operation_would_block ||
775 ec == boost::system::errc::resource_unavailable_try_again)
776 {
777 doWrite();
778 return;
779 }
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530780
781 if (ec == boost::beast::http::error::end_of_stream ||
782 ec == boost::asio::ssl::error::stream_truncated)
783 {
784 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
785 ec);
786 hardClose();
787 return;
788 }
789
Ed Tanous27b0cf92023-08-07 12:02:40 -0700790 if (ec)
791 {
792 BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
793 return;
794 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700795
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800796 if (res.result() == boost::beast::http::status::switching_protocols)
797 {
Ed Tanous796ba932020-08-02 04:29:21 +0000798 upgradeToHttp2();
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800799 return;
800 }
801
Ed Tanous1d1d7782024-04-09 12:54:08 -0700802 if (res.result() == boost::beast::http::status::continue_)
803 {
804 // Reset the result to ok
805 res.result(boost::beast::http::status::ok);
806 doRead();
807 return;
808 }
809
Ed Tanous27b0cf92023-08-07 12:02:40 -0700810 if (!keepAlive)
811 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700812 BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket",
813 logPtr(this));
814
815 gracefulClose();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700816 return;
817 }
818
819 BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
820 res.clear();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700821 initParser();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700822
823 userSession = nullptr;
824
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700825 req->clear();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700826 doReadHeaders();
827 }
828
Ed Tanous52e31622024-01-23 16:31:11 -0800829 void doWrite()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 {
Ed Tanous62598e32023-07-17 17:06:25 -0700831 BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800832 res.preparePayload();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700833
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800834 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000835 if (httpType == HttpType::HTTP)
836 {
837 boost::beast::async_write(
838 adaptor.next_layer(),
839 boost::beast::http::message_generator(std::move(res.response)),
840 std::bind_front(&self_type::afterDoWrite, this,
841 shared_from_this()));
842 }
843 else
844 {
845 boost::beast::async_write(
846 adaptor,
847 boost::beast::http::message_generator(std::move(res.response)),
848 std::bind_front(&self_type::afterDoWrite, this,
849 shared_from_this()));
850 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 }
852
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 void cancelDeadlineTimer()
854 {
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800855 timer.cancel();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700856 }
857
Ed Tanous116370d2024-10-08 10:37:28 -0700858 void afterTimerWait(const std::weak_ptr<self_type>& weakSelf,
859 const boost::system::error_code& ec)
860 {
861 // Note, we are ignoring other types of errors here; If the timer
862 // failed for any reason, we should still close the connection
863 std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock();
864 if (!self)
865 {
866 if (ec == boost::asio::error::operation_aborted)
867 {
868 BMCWEB_LOG_DEBUG(
869 "{} Timer canceled on connection being destroyed",
870 logPtr(self.get()));
871 }
872 else
873 {
874 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
875 logPtr(self.get()));
876 }
877 return;
878 }
879
880 self->timerStarted = false;
881
882 if (ec)
883 {
884 if (ec == boost::asio::error::operation_aborted)
885 {
886 BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get()));
887 return;
888 }
889 BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec);
890 }
891
892 BMCWEB_LOG_WARNING("{} Connection timed out, hard closing",
893 logPtr(self.get()));
894
895 self->hardClose();
896 }
897
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800898 void startDeadline()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 {
Ed Tanous7d243eb2023-01-23 15:57:41 -0800900 // Timer is already started so no further action is required.
901 if (timerStarted)
902 {
903 return;
904 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800906 std::chrono::seconds timeout(15);
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800907
908 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this();
909 timer.expires_after(timeout);
Ed Tanous116370d2024-10-08 10:37:28 -0700910 timer.async_wait(std::bind_front(&self_type::afterTimerWait, this,
911 weak_from_this()));
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800912
Ed Tanous7d243eb2023-01-23 15:57:41 -0800913 timerStarted = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700914 BMCWEB_LOG_DEBUG("{} timer started", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915 }
916
Ed Tanous796ba932020-08-02 04:29:21 +0000917 bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH;
918 HttpType httpType = HttpType::BOTH;
919
920 boost::asio::ssl::stream<Adaptor> adaptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 Handler* handler;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700922
923 boost::asio::ip::address ip;
924
Ed Tanousa24526d2018-12-10 15:17:59 -0800925 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700926 // re-created on Connection reset
Ed Tanousb2896142024-01-31 15:25:47 -0800927 std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928
Ed Tanous3112a142018-11-29 15:45:10 -0800929 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700931 std::shared_ptr<crow::Request> req;
Ed Tanous89cda632024-04-16 08:45:54 -0700932 std::string accept;
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800933 std::string http2settings;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 crow::Response res;
Ed Tanous52cc1122020-07-18 13:51:21 -0700935
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700936 std::shared_ptr<persistent_data::UserSession> userSession;
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100937 std::shared_ptr<persistent_data::UserSession> mtlsSession;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800939 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800941 bool keepAlive = true;
942
Ed Tanous7d243eb2023-01-23 15:57:41 -0800943 bool timerStarted = false;
944
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945 std::function<std::string()>& getCachedDateStr;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000946
947 using std::enable_shared_from_this<
Ed Tanous52cc1122020-07-18 13:51:21 -0700948 Connection<Adaptor, Handler>>::shared_from_this;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800949
950 using std::enable_shared_from_this<
951 Connection<Adaptor, Handler>>::weak_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800952};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700953} // namespace crow