blob: 0c8ad0e4eeab410583a36bd445d2ac18d08fd19b [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 Tanous39fe3af2025-02-17 11:34:12 -0800249 if constexpr (BMCWEB_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
Ed Tanous39fe3af2025-02-17 11:34:12 -0800323 if (BMCWEB_HTTP2 && isH2c)
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800324 {
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 Tanousb2539062024-03-12 16:58:35 -0700386 using boost::beast::http::field;
387 accept = req->getHeaderValue(field::accept);
388 acceptEncoding = req->getHeaderValue(field::accept_encoding);
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000389 // Fetch the client IP address
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700390 req->ipAddress = ip;
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000391
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 // Check for HTTP version 1.1.
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700393 if (req->version() == 11)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 {
Ed Tanousb2539062024-03-12 16:58:35 -0700395 if (req->getHeaderValue(field::host).empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700397 res.result(boost::beast::http::status::bad_request);
Nan Zhou72374eb2022-01-27 17:06:51 -0800398 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700399 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400 }
401 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700402
Ed Tanous62598e32023-07-17 17:06:25 -0700403 BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this),
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700404 req->version() / 10, req->version() % 10,
405 req->methodString(), req->target(),
406 req->ipAddress.to_string());
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700407
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700408 if (res.completed)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700409 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800410 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700411 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700412 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700413 keepAlive = req->keepAlive();
Ed Tanous796ba932020-08-02 04:29:21 +0000414
415 if (authenticationEnabled)
Ed Tanous1d3c14a2021-09-22 18:54:40 -0700416 {
Ed Tanous796ba932020-08-02 04:29:21 +0000417 if (!crow::authentication::isOnAllowlist(req->url().path(),
418 req->method()) &&
419 req->session == nullptr)
Ed Tanous4fa45df2023-09-01 14:20:50 -0700420 {
Ed Tanous796ba932020-08-02 04:29:21 +0000421 BMCWEB_LOG_WARNING("Authentication failed");
422 forward_unauthorized::sendUnauthorized(
423 req->url().encoded_path(),
424 req->getHeaderValue("X-Requested-With"),
425 req->getHeaderValue("Accept"), res);
426 completeRequest(res);
427 return;
Ed Tanous4fa45df2023-09-01 14:20:50 -0700428 }
Ed Tanous4fa45df2023-09-01 14:20:50 -0700429 }
Ed Tanous796ba932020-08-02 04:29:21 +0000430
Nan Zhou72374eb2022-01-27 17:06:51 -0800431 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Ed Tanous62598e32023-07-17 17:06:25 -0700432 BMCWEB_LOG_DEBUG("Setting completion handler");
Nan Zhou72374eb2022-01-27 17:06:51 -0800433 asyncResp->res.setCompleteRequestHandler(
434 [self(shared_from_this())](crow::Response& thisRes) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400435 self->completeRequest(thisRes);
436 });
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800437 if (doUpgrade(asyncResp))
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700438 {
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700439 return;
440 }
Ed Tanous291d7092022-04-13 12:34:57 -0700441 std::string_view expected =
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700442 req->getHeaderValue(boost::beast::http::field::if_none_match);
Ed Tanous291d7092022-04-13 12:34:57 -0700443 if (!expected.empty())
444 {
Ed Tanous37b912f2025-03-20 18:24:30 -0700445 asyncResp->res.setExpectedHash(expected);
Ed Tanous291d7092022-04-13 12:34:57 -0700446 }
Ed Tanous52e31622024-01-23 16:31:11 -0800447 handler->handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700449
Ed Tanous1d1d7782024-04-09 12:54:08 -0700450 void hardClose()
Ed Tanouse278c182019-03-13 16:23:37 -0700451 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700452 BMCWEB_LOG_DEBUG("{} Closing socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000453 adaptor.next_layer().close();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700454 }
455
456 void tlsShutdownComplete(const std::shared_ptr<self_type>& self,
457 const boost::system::error_code& ec)
458 {
459 if (ec)
460 {
461 BMCWEB_LOG_WARNING("{} Failed to shut down TLS cleanly {}",
462 logPtr(self.get()), ec);
463 }
464 self->hardClose();
465 }
466
467 void gracefulClose()
468 {
469 BMCWEB_LOG_DEBUG("{} Socket close requested", logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700470
Ed Tanous796ba932020-08-02 04:29:21 +0000471 if (httpType == HttpType::HTTPS)
Ed Tanouse278c182019-03-13 16:23:37 -0700472 {
Ed Tanous796ba932020-08-02 04:29:21 +0000473 if (mtlsSession != nullptr)
474 {
475 BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this),
476 mtlsSession->uniqueId);
477 persistent_data::SessionStore::getInstance().removeSession(
478 mtlsSession);
479 }
480
Ed Tanous1d1d7782024-04-09 12:54:08 -0700481 adaptor.async_shutdown(std::bind_front(
482 &self_type::tlsShutdownComplete, this, shared_from_this()));
Ed Tanouse278c182019-03-13 16:23:37 -0700483 }
484 else
485 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700486 hardClose();
Ed Tanouse278c182019-03-13 16:23:37 -0700487 }
488 }
489
Nan Zhou72374eb2022-01-27 17:06:51 -0800490 void completeRequest(crow::Response& thisRes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700491 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800492 res = std::move(thisRes);
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800493 res.keepAlive(keepAlive);
Ed Tanous5ae6f922023-01-09 10:45:53 -0800494
Ed Tanousb2539062024-03-12 16:58:35 -0700495 completeResponseFields(accept, acceptEncoding, res);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700496 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
Ed Tanous291d7092022-04-13 12:34:57 -0700497
Ed Tanous52e31622024-01-23 16:31:11 -0800498 doWrite();
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000499
500 // delete lambda with self shared_ptr
501 // to enable connection destruction
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700502 res.setCompleteRequestHandler(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700503 }
504
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500505 void readClientIp()
506 {
507 boost::system::error_code ec;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500508
Ed Tanous796ba932020-08-02 04:29:21 +0000509 boost::asio::ip::tcp::endpoint endpoint =
510 boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700511
Ed Tanous796ba932020-08-02 04:29:21 +0000512 if (ec)
513 {
514 // If remote endpoint fails keep going. "ClientOriginIPAddress"
515 // will be empty.
516 BMCWEB_LOG_ERROR("Failed to get the client's IP Address. ec : {}",
517 ec);
518 return;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500519 }
Ed Tanous796ba932020-08-02 04:29:21 +0000520 ip = endpoint.address();
521 }
522
523 void disableAuth()
524 {
525 authenticationEnabled = false;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500526 }
527
Ed Tanous1abe55e2018-09-05 08:30:59 -0700528 private:
Ed Tanous1d1d7782024-04-09 12:54:08 -0700529 uint64_t getContentLengthLimit()
530 {
Ed Tanous83328312024-05-09 15:48:09 -0700531 if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH)
Ed Tanous1d1d7782024-04-09 12:54:08 -0700532 {
Ed Tanous83328312024-05-09 15:48:09 -0700533 if (userSession == nullptr)
534 {
535 return loggedOutPostBodyLimit;
536 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700537 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700538
539 return httpReqBodyLimit;
540 }
541
542 // Returns true if content length was within limits
543 // Returns false if content length error has been returned
544 bool handleContentLengthError()
545 {
546 if (!parser)
547 {
Manojkiran Edaefff2b52024-06-18 18:01:46 +0530548 BMCWEB_LOG_CRITICAL("Parser was null");
Ed Tanous1d1d7782024-04-09 12:54:08 -0700549 return false;
550 }
551 const boost::optional<uint64_t> contentLength =
552 parser->content_length();
553 if (!contentLength)
554 {
555 BMCWEB_LOG_DEBUG("{} No content length available", logPtr(this));
556 return true;
557 }
558
559 uint64_t maxAllowedContentLength = getContentLengthLimit();
560
561 if (*contentLength > maxAllowedContentLength)
562 {
563 // If the users content limit is between the logged in
564 // and logged out limits They probably just didn't log
565 // in
566 if (*contentLength > loggedOutPostBodyLimit &&
567 *contentLength < httpReqBodyLimit)
568 {
569 BMCWEB_LOG_DEBUG(
570 "{} Content length {} valid, but greater than logged out"
571 " limit of {}. Setting unauthorized",
572 logPtr(this), *contentLength, loggedOutPostBodyLimit);
573 res.result(boost::beast::http::status::unauthorized);
574 }
575 else
576 {
577 // Otherwise they're over both limits, so inform
578 // them
579 BMCWEB_LOG_DEBUG(
580 "{} Content length {} was greater than global limit {}."
581 " Setting payload too large",
582 logPtr(this), *contentLength, httpReqBodyLimit);
583 res.result(boost::beast::http::status::payload_too_large);
584 }
585
586 keepAlive = false;
587 doWrite();
588 return false;
589 }
590
591 return true;
592 }
593
Ed Tanous116370d2024-10-08 10:37:28 -0700594 void afterReadHeaders(const std::shared_ptr<self_type>& /*self*/,
595 const boost::system::error_code& ec,
596 std::size_t bytesTransferred)
597 {
598 BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this),
599 bytesTransferred);
600
601 if (ec)
602 {
603 cancelDeadlineTimer();
604
605 if (ec == boost::beast::http::error::header_limit)
606 {
607 BMCWEB_LOG_ERROR("{} Header field too large, closing",
608 logPtr(this), ec.message());
609
610 res.result(boost::beast::http::status::
611 request_header_fields_too_large);
612 keepAlive = false;
613 doWrite();
614 return;
615 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700616 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
617 ec);
618 hardClose();
Ed Tanous116370d2024-10-08 10:37:28 -0700619 return;
620 }
621
622 if (!parser)
623 {
624 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
625 return;
626 }
Ed Tanous3d158642025-05-12 14:20:49 -0700627 auto& parse = *parser;
628 const auto& value = parser->get();
Ed Tanous116370d2024-10-08 10:37:28 -0700629
Ed Tanous796ba932020-08-02 04:29:21 +0000630 if (authenticationEnabled)
Ed Tanous116370d2024-10-08 10:37:28 -0700631 {
Ed Tanous3d158642025-05-12 14:20:49 -0700632 boost::beast::http::verb method = value.method();
Ed Tanous796ba932020-08-02 04:29:21 +0000633 userSession = authentication::authenticate(
Ed Tanous3d158642025-05-12 14:20:49 -0700634 ip, res, method, value.base(), mtlsSession);
Ed Tanous116370d2024-10-08 10:37:28 -0700635 }
636
Ed Tanous3d158642025-05-12 14:20:49 -0700637 std::string_view expect = value[boost::beast::http::field::expect];
Ed Tanous116370d2024-10-08 10:37:28 -0700638 if (bmcweb::asciiIEquals(expect, "100-continue"))
639 {
640 res.result(boost::beast::http::status::continue_);
641 doWrite();
642 return;
643 }
644
645 if (!handleContentLengthError())
646 {
647 return;
648 }
649
Ed Tanous3d158642025-05-12 14:20:49 -0700650 parse.body_limit(getContentLengthLimit());
Ed Tanous116370d2024-10-08 10:37:28 -0700651
Ed Tanous3d158642025-05-12 14:20:49 -0700652 if (parse.is_done())
Ed Tanous116370d2024-10-08 10:37:28 -0700653 {
654 handle();
655 return;
656 }
657
658 doRead();
659 }
660
Ed Tanous1abe55e2018-09-05 08:30:59 -0700661 void doReadHeaders()
662 {
Ed Tanous62598e32023-07-17 17:06:25 -0700663 BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700664 if (!parser)
665 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700666 BMCWEB_LOG_CRITICAL("Parser was not initialized.");
Ed Tanouse01d0c32023-06-30 13:21:32 -0700667 return;
668 }
Ed Tanous796ba932020-08-02 04:29:21 +0000669
670 if (httpType == HttpType::HTTP)
671 {
672 boost::beast::http::async_read_header(
673 adaptor.next_layer(), buffer, *parser,
674 std::bind_front(&self_type::afterReadHeaders, this,
675 shared_from_this()));
676 }
677 else
678 {
679 boost::beast::http::async_read_header(
680 adaptor, buffer, *parser,
681 std::bind_front(&self_type::afterReadHeaders, this,
682 shared_from_this()));
683 }
Ed Tanous116370d2024-10-08 10:37:28 -0700684 }
Ed Tanous52e31622024-01-23 16:31:11 -0800685
Ed Tanous116370d2024-10-08 10:37:28 -0700686 void afterRead(const std::shared_ptr<self_type>& /*self*/,
687 const boost::system::error_code& ec,
688 std::size_t bytesTransferred)
689 {
690 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
691 bytesTransferred);
692
693 if (ec)
694 {
695 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
696 ec.message());
697 if (ec == boost::beast::http::error::body_limit)
698 {
699 if (handleContentLengthError())
Ed Tanous1d1d7782024-04-09 12:54:08 -0700700 {
Ed Tanous116370d2024-10-08 10:37:28 -0700701 BMCWEB_LOG_CRITICAL("Body length limit reached, "
702 "but no content-length "
703 "available? Should never happen");
704 res.result(
705 boost::beast::http::status::internal_server_error);
706 keepAlive = false;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700707 doWrite();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700708 }
Ed Tanous116370d2024-10-08 10:37:28 -0700709 return;
710 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700711 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
712 ec);
713 hardClose();
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400714
Ed Tanous116370d2024-10-08 10:37:28 -0700715 return;
716 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700717
Ed Tanous116370d2024-10-08 10:37:28 -0700718 // If the user is logged in, allow them to send files
719 // incrementally one piece at a time. If authentication is
720 // disabled then there is no user session hence always allow to
721 // send one piece at a time.
722 if (userSession != nullptr)
723 {
724 cancelDeadlineTimer();
725 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700726
Ed Tanous116370d2024-10-08 10:37:28 -0700727 if (!parser)
728 {
729 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
730 return;
731 }
732 if (!parser->is_done())
733 {
734 doRead();
735 return;
736 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700737
Ed Tanous116370d2024-10-08 10:37:28 -0700738 cancelDeadlineTimer();
739 handle();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700740 }
741
742 void doRead()
743 {
Ed Tanous62598e32023-07-17 17:06:25 -0700744 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700745 if (!parser)
746 {
747 return;
748 }
Ed Tanous3d158642025-05-12 14:20:49 -0700749 auto& parse = *parser;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800750 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000751 if (httpType == HttpType::HTTP)
752 {
753 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700754 adaptor.next_layer(), buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000755 std::bind_front(&self_type::afterRead, this,
756 shared_from_this()));
757 }
758 else
759 {
760 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700761 adaptor, buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000762 std::bind_front(&self_type::afterRead, this,
763 shared_from_this()));
764 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 }
766
Ed Tanous27b0cf92023-08-07 12:02:40 -0700767 void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
768 const boost::system::error_code& ec,
769 std::size_t bytesTransferred)
770 {
Ed Tanous0242baf2024-05-16 19:52:47 -0700771 BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this),
Ed Tanous1d1d7782024-04-09 12:54:08 -0700772 bytesTransferred, ec);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700773
774 cancelDeadlineTimer();
775
Ed Tanous0242baf2024-05-16 19:52:47 -0700776 if (ec == boost::system::errc::operation_would_block ||
777 ec == boost::system::errc::resource_unavailable_try_again)
778 {
779 doWrite();
780 return;
781 }
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530782
783 if (ec == boost::beast::http::error::end_of_stream ||
784 ec == boost::asio::ssl::error::stream_truncated)
785 {
786 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
787 ec);
788 hardClose();
789 return;
790 }
791
Ed Tanous27b0cf92023-08-07 12:02:40 -0700792 if (ec)
793 {
794 BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
795 return;
796 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700797
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800798 if (res.result() == boost::beast::http::status::switching_protocols)
799 {
Ed Tanous796ba932020-08-02 04:29:21 +0000800 upgradeToHttp2();
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800801 return;
802 }
803
Ed Tanous1d1d7782024-04-09 12:54:08 -0700804 if (res.result() == boost::beast::http::status::continue_)
805 {
806 // Reset the result to ok
807 res.result(boost::beast::http::status::ok);
808 doRead();
809 return;
810 }
811
Ed Tanous27b0cf92023-08-07 12:02:40 -0700812 if (!keepAlive)
813 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700814 BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket",
815 logPtr(this));
816
817 gracefulClose();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700818 return;
819 }
820
821 BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
822 res.clear();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700823 initParser();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700824
825 userSession = nullptr;
826
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700827 req->clear();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700828 doReadHeaders();
829 }
830
Ed Tanous52e31622024-01-23 16:31:11 -0800831 void doWrite()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700832 {
Ed Tanous62598e32023-07-17 17:06:25 -0700833 BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800834 res.preparePayload();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700835
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800836 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000837 if (httpType == HttpType::HTTP)
838 {
839 boost::beast::async_write(
840 adaptor.next_layer(),
841 boost::beast::http::message_generator(std::move(res.response)),
842 std::bind_front(&self_type::afterDoWrite, this,
843 shared_from_this()));
844 }
845 else
846 {
847 boost::beast::async_write(
848 adaptor,
849 boost::beast::http::message_generator(std::move(res.response)),
850 std::bind_front(&self_type::afterDoWrite, this,
851 shared_from_this()));
852 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 }
854
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 void cancelDeadlineTimer()
856 {
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800857 timer.cancel();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700858 }
859
Ed Tanous116370d2024-10-08 10:37:28 -0700860 void afterTimerWait(const std::weak_ptr<self_type>& weakSelf,
861 const boost::system::error_code& ec)
862 {
863 // Note, we are ignoring other types of errors here; If the timer
864 // failed for any reason, we should still close the connection
865 std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock();
866 if (!self)
867 {
868 if (ec == boost::asio::error::operation_aborted)
869 {
870 BMCWEB_LOG_DEBUG(
871 "{} Timer canceled on connection being destroyed",
872 logPtr(self.get()));
873 }
874 else
875 {
876 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
877 logPtr(self.get()));
878 }
879 return;
880 }
881
882 self->timerStarted = false;
883
884 if (ec)
885 {
886 if (ec == boost::asio::error::operation_aborted)
887 {
888 BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get()));
889 return;
890 }
891 BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec);
892 }
893
894 BMCWEB_LOG_WARNING("{} Connection timed out, hard closing",
895 logPtr(self.get()));
896
897 self->hardClose();
898 }
899
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800900 void startDeadline()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 {
Ed Tanous7d243eb2023-01-23 15:57:41 -0800902 // Timer is already started so no further action is required.
903 if (timerStarted)
904 {
905 return;
906 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800908 std::chrono::seconds timeout(15);
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800909
910 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this();
911 timer.expires_after(timeout);
Ed Tanous116370d2024-10-08 10:37:28 -0700912 timer.async_wait(std::bind_front(&self_type::afterTimerWait, this,
913 weak_from_this()));
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800914
Ed Tanous7d243eb2023-01-23 15:57:41 -0800915 timerStarted = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700916 BMCWEB_LOG_DEBUG("{} timer started", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 }
918
Ed Tanous796ba932020-08-02 04:29:21 +0000919 bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH;
920 HttpType httpType = HttpType::BOTH;
921
922 boost::asio::ssl::stream<Adaptor> adaptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923 Handler* handler;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700924
925 boost::asio::ip::address ip;
926
Ed Tanousa24526d2018-12-10 15:17:59 -0800927 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700928 // re-created on Connection reset
Ed Tanousb2896142024-01-31 15:25:47 -0800929 std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930
Ed Tanous3112a142018-11-29 15:45:10 -0800931 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700932
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700933 std::shared_ptr<crow::Request> req;
Ed Tanous89cda632024-04-16 08:45:54 -0700934 std::string accept;
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800935 std::string http2settings;
Ed Tanousb2539062024-03-12 16:58:35 -0700936 std::string acceptEncoding;
937
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 crow::Response res;
Ed Tanous52cc1122020-07-18 13:51:21 -0700939
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700940 std::shared_ptr<persistent_data::UserSession> userSession;
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100941 std::shared_ptr<persistent_data::UserSession> mtlsSession;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800943 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700944
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800945 bool keepAlive = true;
946
Ed Tanous7d243eb2023-01-23 15:57:41 -0800947 bool timerStarted = false;
948
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 std::function<std::string()>& getCachedDateStr;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000950
951 using std::enable_shared_from_this<
Ed Tanous52cc1122020-07-18 13:51:21 -0700952 Connection<Adaptor, Handler>>::shared_from_this;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800953
954 using std::enable_shared_from_this<
955 Connection<Adaptor, Handler>>::weak_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800956};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957} // namespace crow