blob: 8f03e3e1b28346cebac2af38af367a90295f567a [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>
Abiola Asojod23d6342025-06-18 20:15:24 +000044#include <boost/url/url_view.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045
Ed Tanousd7857202025-01-28 15:32:26 -080046#include <bit>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047#include <chrono>
Ed Tanousd7857202025-01-28 15:32:26 -080048#include <cstddef>
49#include <cstdint>
50#include <functional>
Jonathan Doman102a4cd2024-04-15 16:56:23 -070051#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080052#include <optional>
53#include <string>
54#include <string_view>
55#include <system_error>
56#include <type_traits>
57#include <utility>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050058
Ed Tanous1abe55e2018-09-05 08:30:59 -070059namespace crow
60{
Ed Tanous257f5792018-03-17 14:40:09 -070061
Ed Tanouscf9e4172022-12-21 09:30:16 -080062// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Ed Tanous6fbdbca2021-12-06 14:36:06 -080063static int connectionCount = 0;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070064
Ed Tanous25b54db2024-04-17 15:40:31 -070065// request body limit size set by the BMCWEB_HTTP_BODY_LIMIT option
66constexpr uint64_t httpReqBodyLimit = 1024UL * 1024UL * BMCWEB_HTTP_BODY_LIMIT;
Jennifer Leeacb7cfb2018-06-07 16:08:15 -070067
Ed Tanous1d1d7782024-04-09 12:54:08 -070068constexpr uint64_t loggedOutPostBodyLimit = 4096U;
James Feist3909dc82020-04-03 10:58:55 -070069
Ed Tanous1d1d7782024-04-09 12:54:08 -070070constexpr uint32_t httpHeaderLimit = 8192U;
James Feist3909dc82020-04-03 10:58:55 -070071
Ed Tanous52cc1122020-07-18 13:51:21 -070072template <typename Adaptor, typename Handler>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050073class Connection :
Ed Tanous52cc1122020-07-18 13:51:21 -070074 public std::enable_shared_from_this<Connection<Adaptor, Handler>>
Ed Tanous1abe55e2018-09-05 08:30:59 -070075{
Ed Tanous7c8e0642022-02-21 12:11:14 -080076 using self_type = Connection<Adaptor, Handler>;
77
Ed Tanous1abe55e2018-09-05 08:30:59 -070078 public:
Ed Tanous796ba932020-08-02 04:29:21 +000079 Connection(Handler* handlerIn, HttpType httpTypeIn,
80 boost::asio::steady_timer&& timerIn,
Ed Tanous81ce6092020-12-17 16:54:55 +000081 std::function<std::string()>& getCachedDateStrF,
Ed Tanous796ba932020-08-02 04:29:21 +000082 boost::asio::ssl::stream<Adaptor>&& adaptorIn) :
83 httpType(httpTypeIn), adaptor(std::move(adaptorIn)), handler(handlerIn),
Patrick Williamsbd79bce2024-08-16 15:22:20 -040084 timer(std::move(timerIn)), getCachedDateStr(getCachedDateStrF)
Ed Tanous1abe55e2018-09-05 08:30:59 -070085 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070086 initParser();
Kowalski, Kamil55e43f62019-07-10 13:12:57 +020087
Ed Tanous40aa0582021-07-14 13:24:40 -070088 connectionCount++;
Ed Tanous6fbdbca2021-12-06 14:36:06 -080089
Ed Tanous1d1d7782024-04-09 12:54:08 -070090 BMCWEB_LOG_DEBUG("{} Connection created, total {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -070091 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -070092 }
93
94 ~Connection()
95 {
Ed Tanous1d1d7782024-04-09 12:54:08 -070096 res.releaseCompleteRequestHandler();
Ed Tanous40aa0582021-07-14 13:24:40 -070097 cancelDeadlineTimer();
Ed Tanous6fbdbca2021-12-06 14:36:06 -080098
Ed Tanous40aa0582021-07-14 13:24:40 -070099 connectionCount--;
Ed Tanous62598e32023-07-17 17:06:25 -0700100 BMCWEB_LOG_DEBUG("{} Connection closed, total {}", logPtr(this),
101 connectionCount);
Ed Tanous40aa0582021-07-14 13:24:40 -0700102 }
103
Ed Tanousecd6a3a2022-01-07 09:18:40 -0800104 Connection(const Connection&) = delete;
105 Connection(Connection&&) = delete;
106 Connection& operator=(const Connection&) = delete;
107 Connection& operator=(Connection&&) = delete;
108
Ed Tanous7c8e0642022-02-21 12:11:14 -0800109 bool tlsVerifyCallback(bool preverified,
110 boost::asio::ssl::verify_context& ctx)
111 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700112 BMCWEB_LOG_DEBUG("{} tlsVerifyCallback called with preverified {}",
113 logPtr(this), preverified);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800114 if (preverified)
115 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700116 mtlsSession = verifyMtlsUser(ip, ctx);
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100117 if (mtlsSession)
Ed Tanous7c8e0642022-02-21 12:11:14 -0800118 {
Ed Tanous3281bcf2024-06-25 16:02:05 -0700119 BMCWEB_LOG_DEBUG("{} Generated TLS session: {}", logPtr(this),
Ed Tanous62598e32023-07-17 17:06:25 -0700120 mtlsSession->uniqueId);
Ed Tanous7c8e0642022-02-21 12:11:14 -0800121 }
122 }
Ed Tanous3281bcf2024-06-25 16:02:05 -0700123 const persistent_data::AuthConfigMethods& c =
124 persistent_data::SessionStore::getInstance().getAuthMethodsConfig();
125 if (c.tlsStrict)
126 {
Ed Tanous463a0e32024-10-14 11:21:48 -0700127 BMCWEB_LOG_DEBUG(
128 "{} TLS is in strict mode, returning preverified as is.",
129 logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700130 return preverified;
131 }
132 // If tls strict mode is disabled
133 // We always return true to allow full auth flow for resources that
134 // don't require auth
Ed Tanous7c8e0642022-02-21 12:11:14 -0800135 return true;
136 }
137
Ed Tanous3281bcf2024-06-25 16:02:05 -0700138 bool prepareMutualTls()
Ed Tanous40aa0582021-07-14 13:24:40 -0700139 {
Ed Tanous796ba932020-08-02 04:29:21 +0000140 BMCWEB_LOG_DEBUG("prepareMutualTls");
141
142 constexpr std::string_view id = "bmcweb";
143
144 const char* idPtr = id.data();
145 const auto* idCPtr = std::bit_cast<const unsigned char*>(idPtr);
146 auto idLen = static_cast<unsigned int>(id.length());
147 int ret =
148 SSL_set_session_id_context(adaptor.native_handle(), idCPtr, idLen);
149 if (ret == 0)
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100150 {
Ed Tanous796ba932020-08-02 04:29:21 +0000151 BMCWEB_LOG_ERROR("{} failed to set SSL id", logPtr(this));
152 return false;
153 }
Zbigniew Kurzynski009c2a42019-11-14 13:37:15 +0100154
Ed Tanous796ba932020-08-02 04:29:21 +0000155 BMCWEB_LOG_DEBUG("set_verify_callback");
Ed Tanous3281bcf2024-06-25 16:02:05 -0700156
Ed Tanous796ba932020-08-02 04:29:21 +0000157 boost::system::error_code ec;
158 adaptor.set_verify_callback(
159 std::bind_front(&self_type::tlsVerifyCallback, this), ec);
160 if (ec)
161 {
162 BMCWEB_LOG_ERROR("Failed to set verify callback {}", ec);
163 return false;
Ed Tanous3281bcf2024-06-25 16:02:05 -0700164 }
165
166 return true;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700167 }
168
Ed Tanous796ba932020-08-02 04:29:21 +0000169 void afterDetectSsl(const std::shared_ptr<self_type>& /*self*/,
170 boost::beast::error_code ec, bool isTls)
171 {
172 if (ec)
173 {
174 BMCWEB_LOG_ERROR("Couldn't detect ssl ", ec);
175 return;
176 }
177 BMCWEB_LOG_DEBUG("{} TLS was detected as {}", logPtr(this), isTls);
178 if (isTls)
179 {
180 if (httpType != HttpType::HTTPS && httpType != HttpType::BOTH)
181 {
182 BMCWEB_LOG_WARNING(
183 "{} Connection closed due to incompatible type",
184 logPtr(this));
185 return;
186 }
187 httpType = HttpType::HTTPS;
188 adaptor.async_handshake(
189 boost::asio::ssl::stream_base::server, buffer.data(),
190 std::bind_front(&self_type::afterSslHandshake, this,
191 shared_from_this()));
192 }
193 else
194 {
195 if (httpType != HttpType::HTTP && httpType != HttpType::BOTH)
196 {
197 BMCWEB_LOG_WARNING(
198 "{} Connection closed due to incompatible type",
199 logPtr(this));
200 return;
201 }
202
203 httpType = HttpType::HTTP;
204 BMCWEB_LOG_INFO("Starting non-SSL session");
205 doReadHeaders();
206 }
207 }
208
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 void start()
210 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700211 BMCWEB_LOG_DEBUG("{} Connection started, total {}", logPtr(this),
212 connectionCount);
Gunnar Mills4f63be02023-10-25 09:14:07 -0500213 if (connectionCount >= 200)
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800214 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700215 BMCWEB_LOG_CRITICAL("{} Max connection count exceeded.",
Ed Tanous62598e32023-07-17 17:06:25 -0700216 logPtr(this));
Ed Tanous6fbdbca2021-12-06 14:36:06 -0800217 return;
218 }
219
Ed Tanous3281bcf2024-06-25 16:02:05 -0700220 if constexpr (BMCWEB_MUTUAL_TLS_AUTH)
221 {
222 if (!prepareMutualTls())
223 {
224 BMCWEB_LOG_ERROR("{} Failed to prepare mTLS", logPtr(this));
225 return;
226 }
227 }
228
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800229 startDeadline();
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500230
Ed Tanous1d1d7782024-04-09 12:54:08 -0700231 readClientIp();
Ed Tanous796ba932020-08-02 04:29:21 +0000232 boost::beast::async_detect_ssl(
233 adaptor.next_layer(), buffer,
234 std::bind_front(&self_type::afterDetectSsl, this,
235 shared_from_this()));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700236 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700237
Ed Tanous796ba932020-08-02 04:29:21 +0000238 void afterSslHandshake(const std::shared_ptr<self_type>& /*self*/,
239 const boost::system::error_code& ec,
240 size_t bytesParsed)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800241 {
Ed Tanous796ba932020-08-02 04:29:21 +0000242 buffer.consume(bytesParsed);
243 if (ec)
244 {
245 BMCWEB_LOG_ERROR("{} SSL handshake failed", logPtr(this));
246 return;
247 }
248 BMCWEB_LOG_DEBUG("{} SSL handshake succeeded", logPtr(this));
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800249 // If http2 is enabled, negotiate the protocol
Ed Tanous39fe3af2025-02-17 11:34:12 -0800250 if constexpr (BMCWEB_HTTP2)
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800251 {
252 const unsigned char* alpn = nullptr;
253 unsigned int alpnlen = 0;
254 SSL_get0_alpn_selected(adaptor.native_handle(), &alpn, &alpnlen);
255 if (alpn != nullptr)
256 {
257 std::string_view selectedProtocol(
258 std::bit_cast<const char*>(alpn), alpnlen);
Ed Tanous62598e32023-07-17 17:06:25 -0700259 BMCWEB_LOG_DEBUG("ALPN selected protocol \"{}\" len: {}",
260 selectedProtocol, alpnlen);
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800261 if (selectedProtocol == "h2")
262 {
Ed Tanous796ba932020-08-02 04:29:21 +0000263 upgradeToHttp2();
Ed Tanousfca2cbe2021-01-28 14:49:59 -0800264 return;
265 }
266 }
267 }
268
269 doReadHeaders();
270 }
271
Ed Tanous1d1d7782024-04-09 12:54:08 -0700272 void initParser()
273 {
274 boost::beast::http::request_parser<bmcweb::HttpBody>& instance =
Ed Tanous38afdb92024-12-11 23:57:53 -0800275 parser.emplace();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700276
277 // reset header limit for newly created parser
278 instance.header_limit(httpHeaderLimit);
279
280 // Initially set no body limit. We don't yet know if the user is
281 // authenticated.
282 instance.body_limit(boost::none);
283 }
284
Ed Tanous796ba932020-08-02 04:29:21 +0000285 void upgradeToHttp2()
286 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800287 auto http2 = std::make_shared<HTTP2Connection<Adaptor, Handler>>(
Ed Tanous2e3cdf82025-08-01 09:49:35 -0700288 std::move(adaptor), handler, getCachedDateStr, httpType,
289 mtlsSession);
Ed Tanousebe4c572025-02-08 14:29:53 -0800290 if (http2settings.empty())
Ed Tanous796ba932020-08-02 04:29:21 +0000291 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800292 http2->start();
Ed Tanous796ba932020-08-02 04:29:21 +0000293 }
294 else
295 {
Ed Tanousebe4c572025-02-08 14:29:53 -0800296 http2->startFromSettings(http2settings);
Ed Tanous796ba932020-08-02 04:29:21 +0000297 }
298 }
299
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800300 // returns whether connection was upgraded
301 bool doUpgrade(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
302 {
303 using boost::beast::http::field;
304 using boost::beast::http::token_list;
305
306 bool isSse =
307 isContentTypeAllowed(req->getHeaderValue("Accept"),
308 http_helpers::ContentType::EventStream, false);
309
310 bool isWebsocket = false;
311 bool isH2c = false;
312 // Check connection header is upgrade
313 if (token_list{req->req[field::connection]}.exists("upgrade"))
314 {
315 BMCWEB_LOG_DEBUG("{} Connection: Upgrade header was present",
316 logPtr(this));
317 // Parse if upgrade is h2c or websocket
318 token_list upgrade{req->req[field::upgrade]};
319 isWebsocket = upgrade.exists("websocket");
320 isH2c = upgrade.exists("h2c");
321 BMCWEB_LOG_DEBUG("{} Upgrade isWebsocket: {} isH2c: {}",
322 logPtr(this), isWebsocket, isH2c);
323 }
324
Ed Tanous39fe3af2025-02-17 11:34:12 -0800325 if (BMCWEB_HTTP2 && isH2c)
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800326 {
327 std::string_view base64settings = req->req[field::http2_settings];
328 if (utility::base64Decode<true>(base64settings, http2settings))
329 {
330 res.result(boost::beast::http::status::switching_protocols);
331 res.addHeader(boost::beast::http::field::connection, "Upgrade");
332 res.addHeader(boost::beast::http::field::upgrade, "h2c");
333 }
334 }
335
336 // websocket and SSE are only allowed on GET
337 if (req->req.method() == boost::beast::http::verb::get)
338 {
339 if (isWebsocket || isSse)
340 {
341 asyncResp->res.setCompleteRequestHandler(
342 [self(shared_from_this())](crow::Response& thisRes) {
343 if (thisRes.result() != boost::beast::http::status::ok)
344 {
345 // When any error occurs before handle upgradation,
346 // the result in response will be set to respective
347 // error. By default the Result will be OK (200),
348 // which implies successful handle upgrade. Response
349 // needs to be sent over this connection only on
350 // failure.
351 self->completeRequest(thisRes);
352 return;
353 }
354 });
355 BMCWEB_LOG_INFO("{} Upgrading socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000356 if (httpType == HttpType::HTTP)
357 {
358 handler->handleUpgrade(req, asyncResp,
359 std::move(adaptor.next_layer()));
360 }
361 else
362 {
363 handler->handleUpgrade(req, asyncResp, std::move(adaptor));
364 }
365
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800366 return true;
367 }
368 }
369 return false;
370 }
371
Ed Tanous1abe55e2018-09-05 08:30:59 -0700372 void handle()
373 {
Ed Tanousf79b7a52021-09-22 19:04:29 -0700374 std::error_code reqEc;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700375 if (!parser)
376 {
377 return;
378 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700379 req = std::make_shared<crow::Request>(parser->release(), reqEc);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700380 if (reqEc)
381 {
Ed Tanous62598e32023-07-17 17:06:25 -0700382 BMCWEB_LOG_DEBUG("Request failed to construct{}", reqEc.message());
Gunnar Mills262f1152022-12-20 15:18:47 -0600383 res.result(boost::beast::http::status::bad_request);
384 completeRequest(res);
Ed Tanousf79b7a52021-09-22 19:04:29 -0700385 return;
386 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700387 req->session = userSession;
Ed Tanousb2539062024-03-12 16:58:35 -0700388 using boost::beast::http::field;
389 accept = req->getHeaderValue(field::accept);
390 acceptEncoding = req->getHeaderValue(field::accept_encoding);
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000391 // Fetch the client IP address
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700392 req->ipAddress = ip;
Ivan Mikhaylovf65b0be2021-04-19 10:05:30 +0000393
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 // Check for HTTP version 1.1.
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700395 if (req->version() == 11)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700396 {
Ed Tanousb2539062024-03-12 16:58:35 -0700397 if (req->getHeaderValue(field::host).empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700398 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700399 res.result(boost::beast::http::status::bad_request);
Nan Zhou72374eb2022-01-27 17:06:51 -0800400 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700401 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 }
403 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700404
Ed Tanous62598e32023-07-17 17:06:25 -0700405 BMCWEB_LOG_INFO("Request: {} HTTP/{}.{} {} {} {}", logPtr(this),
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700406 req->version() / 10, req->version() % 10,
407 req->methodString(), req->target(),
408 req->ipAddress.to_string());
Ed Tanousd32c4fa2021-09-14 13:16:51 -0700409
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700410 if (res.completed)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700411 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800412 completeRequest(res);
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700413 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414 }
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700415 keepAlive = req->keepAlive();
Ed Tanous796ba932020-08-02 04:29:21 +0000416
417 if (authenticationEnabled)
Ed Tanous1d3c14a2021-09-22 18:54:40 -0700418 {
Ed Tanous796ba932020-08-02 04:29:21 +0000419 if (!crow::authentication::isOnAllowlist(req->url().path(),
420 req->method()) &&
421 req->session == nullptr)
Ed Tanous4fa45df2023-09-01 14:20:50 -0700422 {
Ed Tanous796ba932020-08-02 04:29:21 +0000423 BMCWEB_LOG_WARNING("Authentication failed");
424 forward_unauthorized::sendUnauthorized(
425 req->url().encoded_path(),
426 req->getHeaderValue("X-Requested-With"),
427 req->getHeaderValue("Accept"), res);
428 completeRequest(res);
429 return;
Ed Tanous4fa45df2023-09-01 14:20:50 -0700430 }
Ed Tanous4fa45df2023-09-01 14:20:50 -0700431 }
Ed Tanous796ba932020-08-02 04:29:21 +0000432
Nan Zhou72374eb2022-01-27 17:06:51 -0800433 auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
Ed Tanous62598e32023-07-17 17:06:25 -0700434 BMCWEB_LOG_DEBUG("Setting completion handler");
Nan Zhou72374eb2022-01-27 17:06:51 -0800435 asyncResp->res.setCompleteRequestHandler(
436 [self(shared_from_this())](crow::Response& thisRes) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400437 self->completeRequest(thisRes);
438 });
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800439 if (doUpgrade(asyncResp))
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700440 {
Ed Tanous6c7f01d2021-08-25 13:42:35 -0700441 return;
442 }
Ed Tanous291d7092022-04-13 12:34:57 -0700443 std::string_view expected =
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700444 req->getHeaderValue(boost::beast::http::field::if_none_match);
Ed Tanous291d7092022-04-13 12:34:57 -0700445 if (!expected.empty())
446 {
Ed Tanous37b912f2025-03-20 18:24:30 -0700447 asyncResp->res.setExpectedHash(expected);
Ed Tanous291d7092022-04-13 12:34:57 -0700448 }
Ed Tanous52e31622024-01-23 16:31:11 -0800449 handler->handle(req, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700451
Ed Tanous1d1d7782024-04-09 12:54:08 -0700452 void hardClose()
Ed Tanouse278c182019-03-13 16:23:37 -0700453 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700454 BMCWEB_LOG_DEBUG("{} Closing socket", logPtr(this));
Ed Tanous796ba932020-08-02 04:29:21 +0000455 adaptor.next_layer().close();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700456 }
457
458 void tlsShutdownComplete(const std::shared_ptr<self_type>& self,
459 const boost::system::error_code& ec)
460 {
461 if (ec)
462 {
463 BMCWEB_LOG_WARNING("{} Failed to shut down TLS cleanly {}",
464 logPtr(self.get()), ec);
465 }
466 self->hardClose();
467 }
468
469 void gracefulClose()
470 {
471 BMCWEB_LOG_DEBUG("{} Socket close requested", logPtr(this));
Ed Tanous3281bcf2024-06-25 16:02:05 -0700472
Ed Tanous796ba932020-08-02 04:29:21 +0000473 if (httpType == HttpType::HTTPS)
Ed Tanouse278c182019-03-13 16:23:37 -0700474 {
Ed Tanous796ba932020-08-02 04:29:21 +0000475 if (mtlsSession != nullptr)
476 {
477 BMCWEB_LOG_DEBUG("{} Removing TLS session: {}", logPtr(this),
478 mtlsSession->uniqueId);
479 persistent_data::SessionStore::getInstance().removeSession(
480 mtlsSession);
481 }
482
Ed Tanous1d1d7782024-04-09 12:54:08 -0700483 adaptor.async_shutdown(std::bind_front(
484 &self_type::tlsShutdownComplete, this, shared_from_this()));
Ed Tanouse278c182019-03-13 16:23:37 -0700485 }
486 else
487 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700488 hardClose();
Ed Tanouse278c182019-03-13 16:23:37 -0700489 }
490 }
491
Nan Zhou72374eb2022-01-27 17:06:51 -0800492 void completeRequest(crow::Response& thisRes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700493 {
Nan Zhou72374eb2022-01-27 17:06:51 -0800494 res = std::move(thisRes);
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800495 res.keepAlive(keepAlive);
Ed Tanous5ae6f922023-01-09 10:45:53 -0800496
Ed Tanousb2539062024-03-12 16:58:35 -0700497 completeResponseFields(accept, acceptEncoding, res);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700498 res.addHeader(boost::beast::http::field::date, getCachedDateStr());
Ed Tanous291d7092022-04-13 12:34:57 -0700499
Ed Tanous52e31622024-01-23 16:31:11 -0800500 doWrite();
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000501
502 // delete lambda with self shared_ptr
503 // to enable connection destruction
John Edward Broadbent4147b8a2021-07-19 16:52:24 -0700504 res.setCompleteRequestHandler(nullptr);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700505 }
506
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500507 void readClientIp()
508 {
509 boost::system::error_code ec;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500510
Ed Tanous796ba932020-08-02 04:29:21 +0000511 boost::asio::ip::tcp::endpoint endpoint =
512 boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700513
Ed Tanous796ba932020-08-02 04:29:21 +0000514 if (ec)
515 {
516 // If remote endpoint fails keep going. "ClientOriginIPAddress"
517 // will be empty.
518 BMCWEB_LOG_ERROR("Failed to get the client's IP Address. ec : {}",
519 ec);
520 return;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500521 }
Ed Tanous796ba932020-08-02 04:29:21 +0000522 ip = endpoint.address();
523 }
524
525 void disableAuth()
526 {
527 authenticationEnabled = false;
Sunitha Harishc0ea7ae2020-10-30 02:37:30 -0500528 }
529
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 private:
Ed Tanous1d1d7782024-04-09 12:54:08 -0700531 uint64_t getContentLengthLimit()
532 {
Ed Tanous83328312024-05-09 15:48:09 -0700533 if constexpr (!BMCWEB_INSECURE_DISABLE_AUTH)
Ed Tanous1d1d7782024-04-09 12:54:08 -0700534 {
Ed Tanous83328312024-05-09 15:48:09 -0700535 if (userSession == nullptr)
536 {
537 return loggedOutPostBodyLimit;
538 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700539 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700540
541 return httpReqBodyLimit;
542 }
543
544 // Returns true if content length was within limits
545 // Returns false if content length error has been returned
546 bool handleContentLengthError()
547 {
548 if (!parser)
549 {
Manojkiran Edaefff2b52024-06-18 18:01:46 +0530550 BMCWEB_LOG_CRITICAL("Parser was null");
Ed Tanous1d1d7782024-04-09 12:54:08 -0700551 return false;
552 }
553 const boost::optional<uint64_t> contentLength =
554 parser->content_length();
555 if (!contentLength)
556 {
557 BMCWEB_LOG_DEBUG("{} No content length available", logPtr(this));
558 return true;
559 }
560
561 uint64_t maxAllowedContentLength = getContentLengthLimit();
562
563 if (*contentLength > maxAllowedContentLength)
564 {
565 // If the users content limit is between the logged in
566 // and logged out limits They probably just didn't log
567 // in
568 if (*contentLength > loggedOutPostBodyLimit &&
569 *contentLength < httpReqBodyLimit)
570 {
571 BMCWEB_LOG_DEBUG(
572 "{} Content length {} valid, but greater than logged out"
573 " limit of {}. Setting unauthorized",
574 logPtr(this), *contentLength, loggedOutPostBodyLimit);
575 res.result(boost::beast::http::status::unauthorized);
576 }
577 else
578 {
579 // Otherwise they're over both limits, so inform
580 // them
581 BMCWEB_LOG_DEBUG(
582 "{} Content length {} was greater than global limit {}."
583 " Setting payload too large",
584 logPtr(this), *contentLength, httpReqBodyLimit);
585 res.result(boost::beast::http::status::payload_too_large);
586 }
587
588 keepAlive = false;
589 doWrite();
590 return false;
591 }
592
593 return true;
594 }
595
Ed Tanous116370d2024-10-08 10:37:28 -0700596 void afterReadHeaders(const std::shared_ptr<self_type>& /*self*/,
597 const boost::system::error_code& ec,
598 std::size_t bytesTransferred)
599 {
600 BMCWEB_LOG_DEBUG("{} async_read_header {} Bytes", logPtr(this),
601 bytesTransferred);
602
603 if (ec)
604 {
605 cancelDeadlineTimer();
606
607 if (ec == boost::beast::http::error::header_limit)
608 {
609 BMCWEB_LOG_ERROR("{} Header field too large, closing",
610 logPtr(this), ec.message());
611
612 res.result(boost::beast::http::status::
613 request_header_fields_too_large);
614 keepAlive = false;
615 doWrite();
616 return;
617 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700618 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
619 ec);
620 hardClose();
Ed Tanous116370d2024-10-08 10:37:28 -0700621 return;
622 }
623
624 if (!parser)
625 {
626 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
627 return;
628 }
Ed Tanous3d158642025-05-12 14:20:49 -0700629 auto& parse = *parser;
630 const auto& value = parser->get();
Ed Tanous116370d2024-10-08 10:37:28 -0700631
Ed Tanous796ba932020-08-02 04:29:21 +0000632 if (authenticationEnabled)
Ed Tanous116370d2024-10-08 10:37:28 -0700633 {
Ed Tanous3d158642025-05-12 14:20:49 -0700634 boost::beast::http::verb method = value.method();
Ed Tanous796ba932020-08-02 04:29:21 +0000635 userSession = authentication::authenticate(
Ed Tanous3d158642025-05-12 14:20:49 -0700636 ip, res, method, value.base(), mtlsSession);
Ed Tanous116370d2024-10-08 10:37:28 -0700637 }
638
Ed Tanous3d158642025-05-12 14:20:49 -0700639 std::string_view expect = value[boost::beast::http::field::expect];
Ed Tanous116370d2024-10-08 10:37:28 -0700640 if (bmcweb::asciiIEquals(expect, "100-continue"))
641 {
642 res.result(boost::beast::http::status::continue_);
643 doWrite();
644 return;
645 }
646
647 if (!handleContentLengthError())
648 {
649 return;
650 }
651
Ed Tanous3d158642025-05-12 14:20:49 -0700652 parse.body_limit(getContentLengthLimit());
Ed Tanous116370d2024-10-08 10:37:28 -0700653
Ed Tanous3d158642025-05-12 14:20:49 -0700654 if (parse.is_done())
Ed Tanous116370d2024-10-08 10:37:28 -0700655 {
656 handle();
657 return;
658 }
659
660 doRead();
661 }
662
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663 void doReadHeaders()
664 {
Ed Tanous62598e32023-07-17 17:06:25 -0700665 BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700666 if (!parser)
667 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700668 BMCWEB_LOG_CRITICAL("Parser was not initialized.");
Ed Tanouse01d0c32023-06-30 13:21:32 -0700669 return;
670 }
Ed Tanous796ba932020-08-02 04:29:21 +0000671
672 if (httpType == HttpType::HTTP)
673 {
674 boost::beast::http::async_read_header(
675 adaptor.next_layer(), buffer, *parser,
676 std::bind_front(&self_type::afterReadHeaders, this,
677 shared_from_this()));
678 }
679 else
680 {
681 boost::beast::http::async_read_header(
682 adaptor, buffer, *parser,
683 std::bind_front(&self_type::afterReadHeaders, this,
684 shared_from_this()));
685 }
Ed Tanous116370d2024-10-08 10:37:28 -0700686 }
Ed Tanous52e31622024-01-23 16:31:11 -0800687
Ed Tanous116370d2024-10-08 10:37:28 -0700688 void afterRead(const std::shared_ptr<self_type>& /*self*/,
689 const boost::system::error_code& ec,
690 std::size_t bytesTransferred)
691 {
692 BMCWEB_LOG_DEBUG("{} async_read_some {} Bytes", logPtr(this),
693 bytesTransferred);
694
695 if (ec)
696 {
697 BMCWEB_LOG_ERROR("{} Error while reading: {}", logPtr(this),
698 ec.message());
699 if (ec == boost::beast::http::error::body_limit)
700 {
701 if (handleContentLengthError())
Ed Tanous1d1d7782024-04-09 12:54:08 -0700702 {
Ed Tanous116370d2024-10-08 10:37:28 -0700703 BMCWEB_LOG_CRITICAL("Body length limit reached, "
704 "but no content-length "
705 "available? Should never happen");
706 res.result(
707 boost::beast::http::status::internal_server_error);
708 keepAlive = false;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700709 doWrite();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700710 }
Ed Tanous116370d2024-10-08 10:37:28 -0700711 return;
712 }
Ed Tanous62b06bc2025-05-06 13:29:48 -0700713 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
714 ec);
715 hardClose();
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400716
Ed Tanous116370d2024-10-08 10:37:28 -0700717 return;
718 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700719
Ed Tanous116370d2024-10-08 10:37:28 -0700720 // If the user is logged in, allow them to send files
721 // incrementally one piece at a time. If authentication is
722 // disabled then there is no user session hence always allow to
723 // send one piece at a time.
724 if (userSession != nullptr)
725 {
726 cancelDeadlineTimer();
727 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700728
Ed Tanous116370d2024-10-08 10:37:28 -0700729 if (!parser)
730 {
731 BMCWEB_LOG_ERROR("Parser was unexpectedly null");
732 return;
733 }
734 if (!parser->is_done())
735 {
736 doRead();
737 return;
738 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700739
Ed Tanous116370d2024-10-08 10:37:28 -0700740 cancelDeadlineTimer();
741 handle();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700742 }
743
744 void doRead()
745 {
Ed Tanous62598e32023-07-17 17:06:25 -0700746 BMCWEB_LOG_DEBUG("{} doRead", logPtr(this));
Ed Tanouse01d0c32023-06-30 13:21:32 -0700747 if (!parser)
748 {
749 return;
750 }
Ed Tanous3d158642025-05-12 14:20:49 -0700751 auto& parse = *parser;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800752 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000753 if (httpType == HttpType::HTTP)
754 {
755 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700756 adaptor.next_layer(), buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000757 std::bind_front(&self_type::afterRead, this,
758 shared_from_this()));
759 }
760 else
761 {
762 boost::beast::http::async_read_some(
Ed Tanous3d158642025-05-12 14:20:49 -0700763 adaptor, buffer, parse,
Ed Tanous796ba932020-08-02 04:29:21 +0000764 std::bind_front(&self_type::afterRead, this,
765 shared_from_this()));
766 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700767 }
768
Ed Tanous27b0cf92023-08-07 12:02:40 -0700769 void afterDoWrite(const std::shared_ptr<self_type>& /*self*/,
770 const boost::system::error_code& ec,
771 std::size_t bytesTransferred)
772 {
Ed Tanous0242baf2024-05-16 19:52:47 -0700773 BMCWEB_LOG_DEBUG("{} async_write wrote {} bytes, ec={}", logPtr(this),
Ed Tanous1d1d7782024-04-09 12:54:08 -0700774 bytesTransferred, ec);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700775
776 cancelDeadlineTimer();
777
Ed Tanous0242baf2024-05-16 19:52:47 -0700778 if (ec == boost::system::errc::operation_would_block ||
779 ec == boost::system::errc::resource_unavailable_try_again)
780 {
781 doWrite();
782 return;
783 }
Chandramohan Harkude352ee0e2025-06-04 11:16:34 +0530784
785 if (ec == boost::beast::http::error::end_of_stream ||
786 ec == boost::asio::ssl::error::stream_truncated)
787 {
788 BMCWEB_LOG_WARNING("{} End of stream, closing {}", logPtr(this),
789 ec);
790 hardClose();
791 return;
792 }
793
Ed Tanous27b0cf92023-08-07 12:02:40 -0700794 if (ec)
795 {
796 BMCWEB_LOG_DEBUG("{} from write(2)", logPtr(this));
797 return;
798 }
Ed Tanous1d1d7782024-04-09 12:54:08 -0700799
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800800 if (res.result() == boost::beast::http::status::switching_protocols)
801 {
Ed Tanous796ba932020-08-02 04:29:21 +0000802 upgradeToHttp2();
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800803 return;
804 }
805
Ed Tanous1d1d7782024-04-09 12:54:08 -0700806 if (res.result() == boost::beast::http::status::continue_)
807 {
808 // Reset the result to ok
809 res.result(boost::beast::http::status::ok);
810 doRead();
811 return;
812 }
813
Ed Tanous27b0cf92023-08-07 12:02:40 -0700814 if (!keepAlive)
815 {
Ed Tanous1d1d7782024-04-09 12:54:08 -0700816 BMCWEB_LOG_DEBUG("{} keepalive not set. Closing socket",
817 logPtr(this));
818
819 gracefulClose();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700820 return;
821 }
822
823 BMCWEB_LOG_DEBUG("{} Clearing response", logPtr(this));
824 res.clear();
Ed Tanous1d1d7782024-04-09 12:54:08 -0700825 initParser();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700826
827 userSession = nullptr;
828
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700829 req->clear();
Ed Tanous27b0cf92023-08-07 12:02:40 -0700830 doReadHeaders();
831 }
832
Ed Tanous52e31622024-01-23 16:31:11 -0800833 void doWrite()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700834 {
Ed Tanous62598e32023-07-17 17:06:25 -0700835 BMCWEB_LOG_DEBUG("{} doWrite", logPtr(this));
Abiola Asojod23d6342025-06-18 20:15:24 +0000836
837 boost::urls::url_view urlView;
838 if (req != nullptr)
839 {
840 urlView = req->url();
841 }
842 res.preparePayload(urlView);
Ed Tanous27b0cf92023-08-07 12:02:40 -0700843
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800844 startDeadline();
Ed Tanous796ba932020-08-02 04:29:21 +0000845 if (httpType == HttpType::HTTP)
846 {
847 boost::beast::async_write(
848 adaptor.next_layer(),
849 boost::beast::http::message_generator(std::move(res.response)),
850 std::bind_front(&self_type::afterDoWrite, this,
851 shared_from_this()));
852 }
853 else
854 {
855 boost::beast::async_write(
856 adaptor,
857 boost::beast::http::message_generator(std::move(res.response)),
858 std::bind_front(&self_type::afterDoWrite, this,
859 shared_from_this()));
860 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700861 }
862
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 void cancelDeadlineTimer()
864 {
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800865 timer.cancel();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 }
867
Ed Tanous116370d2024-10-08 10:37:28 -0700868 void afterTimerWait(const std::weak_ptr<self_type>& weakSelf,
869 const boost::system::error_code& ec)
870 {
871 // Note, we are ignoring other types of errors here; If the timer
872 // failed for any reason, we should still close the connection
873 std::shared_ptr<Connection<Adaptor, Handler>> self = weakSelf.lock();
874 if (!self)
875 {
876 if (ec == boost::asio::error::operation_aborted)
877 {
878 BMCWEB_LOG_DEBUG(
879 "{} Timer canceled on connection being destroyed",
880 logPtr(self.get()));
881 }
882 else
883 {
884 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
885 logPtr(self.get()));
886 }
887 return;
888 }
889
890 self->timerStarted = false;
891
892 if (ec)
893 {
894 if (ec == boost::asio::error::operation_aborted)
895 {
896 BMCWEB_LOG_DEBUG("{} Timer canceled", logPtr(self.get()));
897 return;
898 }
899 BMCWEB_LOG_CRITICAL("{} Timer failed {}", logPtr(self.get()), ec);
900 }
901
902 BMCWEB_LOG_WARNING("{} Connection timed out, hard closing",
903 logPtr(self.get()));
904
905 self->hardClose();
906 }
907
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800908 void startDeadline()
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 {
Ed Tanous7d243eb2023-01-23 15:57:41 -0800910 // Timer is already started so no further action is required.
911 if (timerStarted)
912 {
913 return;
914 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800916 std::chrono::seconds timeout(15);
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800917
918 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this();
919 timer.expires_after(timeout);
Ed Tanous116370d2024-10-08 10:37:28 -0700920 timer.async_wait(std::bind_front(&self_type::afterTimerWait, this,
921 weak_from_this()));
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800922
Ed Tanous7d243eb2023-01-23 15:57:41 -0800923 timerStarted = true;
Ed Tanous62598e32023-07-17 17:06:25 -0700924 BMCWEB_LOG_DEBUG("{} timer started", logPtr(this));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700925 }
926
Ed Tanous796ba932020-08-02 04:29:21 +0000927 bool authenticationEnabled = !BMCWEB_INSECURE_DISABLE_AUTH;
928 HttpType httpType = HttpType::BOTH;
929
930 boost::asio::ssl::stream<Adaptor> adaptor;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700931 Handler* handler;
Ed Tanous1d1d7782024-04-09 12:54:08 -0700932
933 boost::asio::ip::address ip;
934
Ed Tanousa24526d2018-12-10 15:17:59 -0800935 // Making this a std::optional allows it to be efficiently destroyed and
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 // re-created on Connection reset
Ed Tanousb2896142024-01-31 15:25:47 -0800937 std::optional<boost::beast::http::request_parser<bmcweb::HttpBody>> parser;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938
Ed Tanous3112a142018-11-29 15:45:10 -0800939 boost::beast::flat_static_buffer<8192> buffer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700941 std::shared_ptr<crow::Request> req;
Ed Tanous89cda632024-04-16 08:45:54 -0700942 std::string accept;
Ed Tanouscd7dbb32025-02-01 12:37:56 -0800943 std::string http2settings;
Ed Tanousb2539062024-03-12 16:58:35 -0700944 std::string acceptEncoding;
945
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 crow::Response res;
Ed Tanous52cc1122020-07-18 13:51:21 -0700947
John Edward Broadbent59b98b22021-07-13 15:36:32 -0700948 std::shared_ptr<persistent_data::UserSession> userSession;
Boleslaw Ogonczyk Makowskib4963072023-02-06 09:59:58 +0100949 std::shared_ptr<persistent_data::UserSession> mtlsSession;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700950
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800951 boost::asio::steady_timer timer;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952
Ed Tanous4cdc2e82023-01-13 10:03:22 -0800953 bool keepAlive = true;
954
Ed Tanous7d243eb2023-01-23 15:57:41 -0800955 bool timerStarted = false;
956
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 std::function<std::string()>& getCachedDateStr;
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000958
959 using std::enable_shared_from_this<
Ed Tanous52cc1122020-07-18 13:51:21 -0700960 Connection<Adaptor, Handler>>::shared_from_this;
Ed Tanous5dfb5b22021-12-03 11:24:53 -0800961
962 using std::enable_shared_from_this<
963 Connection<Adaptor, Handler>>::weak_from_this;
Ed Tanous3112a142018-11-29 15:45:10 -0800964};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700965} // namespace crow