blob: b3d803a842406009ed58215e560b45fd2e7d6051 [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 }
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530614 if (ec == boost::beast::http::error::end_of_stream ||
615 ec == boost::asio::ssl::error::stream_truncated)
Ed Tanous116370d2024-10-08 10:37:28 -0700616 {
617 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
618 ec);
619 hardClose();
620 return;
621 }
622
623 BMCWEB_LOG_DEBUG("{} Closing socket due to read error {}",
624 logPtr(this), ec.message());
625 gracefulClose();
626
627 return;
628 }
629
630 if (!parser)
631 {
632 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
633 return;
634 }
Ed Tanous3d158642025-05-12 14:20:49 -0700635 auto& parse = *parser;
636 const auto& value = parser->get();
Ed Tanous116370d2024-10-08 10:37:28 -0700637
Ed Tanous796ba932020-08-02 04:29:21 +0000638 if (authenticationEnabled)
Ed Tanous116370d2024-10-08 10:37:28 -0700639 {
Ed Tanous3d158642025-05-12 14:20:49 -0700640 boost::beast::http::verb method = value.method();
Ed Tanous796ba932020-08-02 04:29:21 +0000641 userSession = authentication::authenticate(
Ed Tanous3d158642025-05-12 14:20:49 -0700642 ip, res, method, value.base(), mtlsSession);
Ed Tanous116370d2024-10-08 10:37:28 -0700643 }
644
Ed Tanous3d158642025-05-12 14:20:49 -0700645 std::string_view expect = value[boost::beast::http::field::expect];
Ed Tanous116370d2024-10-08 10:37:28 -0700646 if (bmcweb::asciiIEquals(expect, "100-continue"))
647 {
648 res.result(boost::beast::http::status::continue_);
649 doWrite();
650 return;
651 }
652
653 if (!handleContentLengthError())
654 {
655 return;
656 }
657
Ed Tanous3d158642025-05-12 14:20:49 -0700658 parse.body_limit(getContentLengthLimit());
Ed Tanous116370d2024-10-08 10:37:28 -0700659
Ed Tanous3d158642025-05-12 14:20:49 -0700660 if (parse.is_done())
Ed Tanous116370d2024-10-08 10:37:28 -0700661 {
662 handle();
663 return;
664 }
665
666 doRead();
667 }
668
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669 void doReadHeaders()
670 {
Ed Tanous62598e32023-07-17 17:06:25 -0700671 BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700672 if (!parser)
673 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700674 BMCWEB_LOG_CRITICAL("Parser was not initialized.");
Ed Tanouse01d0c32023-06-30 13:21:32 -0700675 return;
676 }
Ed Tanous796ba932020-08-02 04:29:21 +0000677
678 if (httpType == HttpType::HTTP)
679 {
680 boost::beast::http::async_read_header(
681 adaptor.next_layer(), buffer, *parser,
682 std::bind_front(&self_type::afterReadHeaders, this,
683 shared_from_this()));
684 }
685 else
686 {
687 boost::beast::http::async_read_header(
688 adaptor, buffer, *parser,
689 std::bind_front(&self_type::afterReadHeaders, this,
690 shared_from_this()));
691 }
Ed Tanous116370d2024-10-08 10:37:28 -0700692 }
Ed Tanous52e31622024-01-23 16:31:11 -0800693
Ed Tanous116370d2024-10-08 10:37:28 -0700694 void afterRead(const std::shared_ptr<self_type>& /*self*/,
695 const boost::system::error_code& ec,
696 std::size_t bytesTransferred)
697 {
698 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
699 bytesTransferred);
700
701 if (ec)
702 {
703 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
704 ec.message());
705 if (ec == boost::beast::http::error::body_limit)
706 {
707 if (handleContentLengthError())
Ed Tanous1d1d7782024-04-09 12:54:08 -0700708 {
Ed Tanous116370d2024-10-08 10:37:28 -0700709 BMCWEB_LOG_CRITICAL("Body length limit reached, "
710 "but no content-length "
711 "available? Should never happen");
712 res.result(
713 boost::beast::http::status::internal_server_error);
714 keepAlive = false;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700715 doWrite();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700716 }
Ed Tanous116370d2024-10-08 10:37:28 -0700717 return;
718 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400719
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530720 if (ec == boost::beast::http::error::end_of_stream ||
721 ec == boost::asio::ssl::error::stream_truncated)
722 {
723 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
724 ec);
725 hardClose();
726 return;
727 }
728
Ed Tanous116370d2024-10-08 10:37:28 -0700729 gracefulClose();
730 return;
731 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700732
Ed Tanous116370d2024-10-08 10:37:28 -0700733 // If the user is logged in, allow them to send files
734 // incrementally one piece at a time. If authentication is
735 // disabled then there is no user session hence always allow to
736 // send one piece at a time.
737 if (userSession != nullptr)
738 {
739 cancelDeadlineTimer();
740 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700741
Ed Tanous116370d2024-10-08 10:37:28 -0700742 if (!parser)
743 {
744 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
745 return;
746 }
747 if (!parser->is_done())
748 {
749 doRead();
750 return;
751 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700752
Ed Tanous116370d2024-10-08 10:37:28 -0700753 cancelDeadlineTimer();
754 handle();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755 }
756
757 void doRead()
758 {
Ed Tanous62598e32023-07-17 17:06:25 -0700759 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700760 if (!parser)
761 {
762 return;
763 }
Ed Tanous3d158642025-05-12 14:20:49 -0700764 auto& parse = *parser;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800765 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000766 if (httpType == HttpType::HTTP)
767 {
768 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700769 adaptor.next_layer(), buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000770 std::bind_front(&self_type::afterRead, this,
771 shared_from_this()));
772 }
773 else
774 {
775 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700776 adaptor, buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000777 std::bind_front(&self_type::afterRead, this,
778 shared_from_this()));
779 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700780 }
781
Ed Tanous27b0cf92023-08-07 12:02:40 -0700782 void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
783 const boost::system::error_code& ec,
784 std::size_t bytesTransferred)
785 {
Ed Tanous0242baf2024-05-16 19:52:47 -0700786 BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this),
Ed Tanous1d1d7782024-04-09 12:54:08 -0700787 bytesTransferred, ec);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700788
789 cancelDeadlineTimer();
790
Ed Tanous0242baf2024-05-16 19:52:47 -0700791 if (ec == boost::system::errc::operation_would_block ||
792 ec == boost::system::errc::resource_unavailable_try_again)
793 {
794 doWrite();
795 return;
796 }
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530797
798 if (ec == boost::beast::http::error::end_of_stream ||
799 ec == boost::asio::ssl::error::stream_truncated)
800 {
801 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
802 ec);
803 hardClose();
804 return;
805 }
806
Ed Tanous27b0cf92023-08-07 12:02:40 -0700807 if (ec)
808 {
809 BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
810 return;
811 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700812
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800813 if (res.result() == boost::beast::http::status::switching_protocols)
814 {
Ed Tanous796ba932020-08-02 04:29:21 +0000815 upgradeToHttp2();
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800816 return;
817 }
818
Ed Tanous1d1d7782024-04-09 12:54:08 -0700819 if (res.result() == boost::beast::http::status::continue_)
820 {
821 // Reset the result to ok
822 res.result(boost::beast::http::status::ok);
823 doRead();
824 return;
825 }
826
Ed Tanous27b0cf92023-08-07 12:02:40 -0700827 if (!keepAlive)
828 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700829 BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket",
830 logPtr(this));
831
832 gracefulClose();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700833 return;
834 }
835
836 BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
837 res.clear();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700838 initParser();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700839
840 userSession = nullptr;
841
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700842 req->clear();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700843 doReadHeaders();
844 }
845
Ed Tanous52e31622024-01-23 16:31:11 -0800846 void doWrite()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 {
Ed Tanous62598e32023-07-17 17:06:25 -0700848 BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
Ed Tanous52e31622024-01-23 16:31:11 -0800849 res.preparePayload();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700850
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800851 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000852 if (httpType == HttpType::HTTP)
853 {
854 boost::beast::async_write(
855 adaptor.next_layer(),
856 boost::beast::http::message_generator(std::move(res.response)),
857 std::bind_front(&self_type::afterDoWrite, this,
858 shared_from_this()));
859 }
860 else
861 {
862 boost::beast::async_write(
863 adaptor,
864 boost::beast::http::message_generator(std::move(res.response)),
865 std::bind_front(&self_type::afterDoWrite, this,
866 shared_from_this()));
867 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700868 }
869
Ed Tanous1abe55e2018-09-05 08:30:59 -0700870 void cancelDeadlineTimer()
871 {
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800872 timer.cancel();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 }
874
Ed Tanous116370d2024-10-08 10:37:28 -0700875 void afterTimerWait(const std::weak_ptr<self_type>& weakSelf,
876 const boost::system::error_code& ec)
877 {
878 // Note, we are ignoring other types of errors here; If the timer
879 // failed for any reason, we should still close the connection
880 std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock();
881 if (!self)
882 {
883 if (ec == boost::asio::error::operation_aborted)
884 {
885 BMCWEB_LOG_DEBUG(
886 "{} Timer canceled on connection being destroyed",
887 logPtr(self.get()));
888 }
889 else
890 {
891 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
892 logPtr(self.get()));
893 }
894 return;
895 }
896
897 self->timerStarted = false;
898
899 if (ec)
900 {
901 if (ec == boost::asio::error::operation_aborted)
902 {
903 BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get()));
904 return;
905 }
906 BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec);
907 }
908
909 BMCWEB_LOG_WARNING("{} Connection timed out, hard closing",
910 logPtr(self.get()));
911
912 self->hardClose();
913 }
914
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800915 void startDeadline()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanous7d243eb2023-01-23 15:57:41 -0800917 // Timer is already started so no further action is required.
918 if (timerStarted)
919 {
920 return;
921 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800923 std::chrono::seconds timeout(15);
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800924
925 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this();
926 timer.expires_after(timeout);
Ed Tanous116370d2024-10-08 10:37:28 -0700927 timer.async_wait(std::bind_front(&self_type::afterTimerWait, this,
928 weak_from_this()));
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800929
Ed Tanous7d243eb2023-01-23 15:57:41 -0800930 timerStarted = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700931 BMCWEB_LOG_DEBUG("{} timer started", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700932 }
933
Ed Tanous796ba932020-08-02 04:29:21 +0000934 bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH;
935 HttpType httpType = HttpType::BOTH;
936
937 boost::asio::ssl::stream<Adaptor> adaptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 Handler* handler;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700939
940 boost::asio::ip::address ip;
941
Ed Tanousa24526d2018-12-10 15:17:59 -0800942 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 // re-created on Connection reset
Ed Tanousb2896142024-01-31 15:25:47 -0800944 std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945
Ed Tanous3112a142018-11-29 15:45:10 -0800946 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700948 std::shared_ptr<crow::Request> req;
Ed Tanous89cda632024-04-16 08:45:54 -0700949 std::string accept;
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800950 std::string http2settings;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 crow::Response res;
Ed Tanous52cc1122020-07-18 13:51:21 -0700952
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700953 std::shared_ptr<persistent_data::UserSession> userSession;
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100954 std::shared_ptr<persistent_data::UserSession> mtlsSession;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700955
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800956 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800958 bool keepAlive = true;
959
Ed Tanous7d243eb2023-01-23 15:57:41 -0800960 bool timerStarted = false;
961
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962 std::function<std::string()>& getCachedDateStr;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000963
964 using std::enable_shared_from_this<
Ed Tanous52cc1122020-07-18 13:51:21 -0700965 Connection<Adaptor, Handler>>::shared_from_this;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800966
967 using std::enable_shared_from_this<
968 Connection<Adaptor, Handler>>::weak_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800969};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700970} // namespace crow