blob: 21e0909de82c670a887d14c2a6e62932084f0060 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2020 Intel Corporation
AppaRao Pulibd030d02020-03-20 03:34:29 +05304#pragma once
Nan Zhou77665bd2022-10-12 20:28:58 +00005
Ed Tanousd7857202025-01-28 15:32:26 -08006#include "bmcweb_config.h"
7
Nan Zhou77665bd2022-10-12 20:28:58 +00008#include "async_resolve.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08009#include "boost_formatters.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -080010#include "http_body.hpp"
Nan Zhou77665bd2022-10-12 20:28:58 +000011#include "http_response.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "logging.hpp"
13#include "ssl_key_handler.hpp"
Nan Zhou77665bd2022-10-12 20:28:58 +000014
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <openssl/err.h>
16#include <openssl/ssl.h>
17#include <openssl/tls1.h>
18
Ed Tanous0d5f5cf2022-03-12 15:30:55 -080019#include <boost/asio/connect.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080020#include <boost/asio/error.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070021#include <boost/asio/io_context.hpp>
Sunitha Harish29a82b02021-02-18 15:54:16 +053022#include <boost/asio/ip/address.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070023#include <boost/asio/ip/tcp.hpp>
AppaRao Pulie38778a2022-06-27 23:09:03 +000024#include <boost/asio/ssl/context.hpp>
25#include <boost/asio/ssl/error.hpp>
Ed Tanous003301a2024-04-16 09:59:19 -070026#include <boost/asio/ssl/stream.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080027#include <boost/asio/ssl/stream_base.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070028#include <boost/asio/steady_timer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080029#include <boost/beast/core/error.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070030#include <boost/beast/core/flat_static_buffer.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080031#include <boost/beast/http/field.hpp>
32#include <boost/beast/http/fields.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070033#include <boost/beast/http/message.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070034#include <boost/beast/http/parser.hpp>
35#include <boost/beast/http/read.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080036#include <boost/beast/http/status.hpp>
37#include <boost/beast/http/verb.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070038#include <boost/beast/http/write.hpp>
Carson Labradof52c03c2022-03-23 18:50:15 +000039#include <boost/container/devector.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080040#include <boost/optional/optional.hpp>
41#include <boost/system/errc.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070042#include <boost/system/error_code.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080043#include <boost/url/host_type.hpp>
Ed Tanous27b0cf92023-08-07 12:02:40 -070044#include <boost/url/url.hpp>
Ed Tanous4a7fbef2024-04-06 16:03:49 -070045#include <boost/url/url_view_base.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050046
Ed Tanousd7857202025-01-28 15:32:26 -080047#include <chrono>
48#include <cstdint>
AppaRao Pulibd030d02020-03-20 03:34:29 +053049#include <cstdlib>
Ed Tanousd7857202025-01-28 15:32:26 -080050#include <format>
AppaRao Pulibd030d02020-03-20 03:34:29 +053051#include <functional>
AppaRao Pulibd030d02020-03-20 03:34:29 +053052#include <memory>
Ed Tanousd7857202025-01-28 15:32:26 -080053#include <optional>
AppaRao Pulibd030d02020-03-20 03:34:29 +053054#include <string>
Ed Tanousd7857202025-01-28 15:32:26 -080055#include <string_view>
56#include <type_traits>
57#include <unordered_map>
58#include <utility>
59#include <vector>
AppaRao Pulibd030d02020-03-20 03:34:29 +053060
61namespace crow
62{
Ed Tanous27b0cf92023-08-07 12:02:40 -070063// With Redfish Aggregation it is assumed we will connect to another
64// instance of BMCWeb which can handle 100 simultaneous connections.
Carson Labrado66d90c22022-12-07 22:34:33 +000065constexpr size_t maxPoolSize = 20;
66constexpr size_t maxRequestQueueSize = 500;
Carson Labrado17dcc312022-07-28 22:17:28 +000067constexpr unsigned int httpReadBodyLimit = 131072;
Carson Labrado4d942722022-06-22 22:16:10 +000068constexpr unsigned int httpReadBufferSize = 4096;
AppaRao Puli2a5689a2020-04-29 15:24:31 +053069
AppaRao Pulibd030d02020-03-20 03:34:29 +053070enum class ConnState
71{
AppaRao Puli2a5689a2020-04-29 15:24:31 +053072 initialized,
Sunitha Harish29a82b02021-02-18 15:54:16 +053073 resolveInProgress,
74 resolveFailed,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053075 connectInProgress,
76 connectFailed,
AppaRao Pulibd030d02020-03-20 03:34:29 +053077 connected,
AppaRao Pulie38778a2022-06-27 23:09:03 +000078 handshakeInProgress,
79 handshakeFailed,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053080 sendInProgress,
81 sendFailed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053082 recvInProgress,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053083 recvFailed,
84 idle,
Ayushi Smritife44eb02020-05-15 15:24:45 +053085 closed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053086 suspended,
87 terminated,
88 abortConnection,
AppaRao Pulie38778a2022-06-27 23:09:03 +000089 sslInitFailed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053090 retry
AppaRao Pulibd030d02020-03-20 03:34:29 +053091};
92
Ed Tanous4ff0f1f2024-09-04 17:27:37 -070093inline boost::system::error_code defaultRetryHandler(unsigned int respCode)
Carson Labradoa7a80292022-06-01 16:01:52 +000094{
95 // As a default, assume 200X is alright
Ed Tanous62598e32023-07-17 17:06:25 -070096 BMCWEB_LOG_DEBUG("Using default check for response code validity");
Carson Labradoa7a80292022-06-01 16:01:52 +000097 if ((respCode < 200) || (respCode >= 300))
98 {
99 return boost::system::errc::make_error_code(
100 boost::system::errc::result_out_of_range);
101 }
102
103 // Return 0 if the response code is valid
104 return boost::system::errc::make_error_code(boost::system::errc::success);
105};
106
Ed Tanous27b0cf92023-08-07 12:02:40 -0700107// We need to allow retry information to be set before a message has been
108// sent and a connection pool has been created
Carson Labradod14a48f2023-02-22 00:24:54 +0000109struct ConnectionPolicy
Carson Labradof52c03c2022-03-23 18:50:15 +0000110{
111 uint32_t maxRetryAttempts = 5;
Carson Labradod14a48f2023-02-22 00:24:54 +0000112
113 // the max size of requests in bytes. 0 for unlimited
114 boost::optional<uint64_t> requestByteLimit = httpReadBodyLimit;
115
116 size_t maxConnections = 1;
117
Carson Labradof52c03c2022-03-23 18:50:15 +0000118 std::string retryPolicyAction = "TerminateAfterRetries";
Carson Labradod14a48f2023-02-22 00:24:54 +0000119
120 std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0);
Carson Labradoa7a80292022-06-01 16:01:52 +0000121 std::function<boost::system::error_code(unsigned int respCode)>
122 invalidResp = defaultRetryHandler;
Carson Labradof52c03c2022-03-23 18:50:15 +0000123};
124
125struct PendingRequest
126{
Ed Tanousb2896142024-01-31 15:25:47 -0800127 boost::beast::http::request<bmcweb::HttpBody> req;
Carson Labrado039a47e2022-04-05 16:03:20 +0000128 std::function<void(bool, uint32_t, Response&)> callback;
Carson Labrado039a47e2022-04-05 16:03:20 +0000129 PendingRequest(
Ed Tanousb2896142024-01-31 15:25:47 -0800130 boost::beast::http::request<bmcweb::HttpBody>&& reqIn,
Carson Labradod14a48f2023-02-22 00:24:54 +0000131 const std::function<void(bool, uint32_t, Response&)>& callbackIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400132 req(std::move(reqIn)), callback(callbackIn)
Carson Labradof52c03c2022-03-23 18:50:15 +0000133 {}
134};
135
Ed Tanouse01d0c32023-06-30 13:21:32 -0700136namespace http = boost::beast::http;
Carson Labradof52c03c2022-03-23 18:50:15 +0000137class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
AppaRao Pulibd030d02020-03-20 03:34:29 +0530138{
139 private:
Carson Labradof52c03c2022-03-23 18:50:15 +0000140 ConnState state = ConnState::initialized;
141 uint32_t retryCount = 0;
Carson Labradof52c03c2022-03-23 18:50:15 +0000142 std::string subId;
Carson Labradod14a48f2023-02-22 00:24:54 +0000143 std::shared_ptr<ConnectionPolicy> connPolicy;
Ed Tanousa716aa72023-08-01 11:35:53 -0700144 boost::urls::url host;
Ed Tanous19bb3622024-07-05 10:07:40 -0500145 ensuressl::VerifyCertificate verifyCert;
Carson Labradof52c03c2022-03-23 18:50:15 +0000146 uint32_t connId;
Carson Labradof52c03c2022-03-23 18:50:15 +0000147 // Data buffers
Ed Tanousb2896142024-01-31 15:25:47 -0800148 http::request<bmcweb::HttpBody> req;
149 using parser_type = http::response_parser<bmcweb::HttpBody>;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700150 std::optional<parser_type> parser;
Carson Labrado4d942722022-06-22 22:16:10 +0000151 boost::beast::flat_static_buffer<httpReadBufferSize> buffer;
Carson Labrado039a47e2022-04-05 16:03:20 +0000152 Response res;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530153
Ed Tanousd0fd3e52025-06-10 09:19:33 -0700154 // Async callables
Carson Labrado039a47e2022-04-05 16:03:20 +0000155 std::function<void(bool, uint32_t, Response&)> callback;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700156
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600157 boost::asio::io_context& ioc;
158
Ed Tanous25b54db2024-04-17 15:40:31 -0700159 using Resolver = std::conditional_t<BMCWEB_DNS_RESOLVER == "systemd-dbus",
160 async_resolve::Resolver,
161 boost::asio::ip::tcp::resolver>;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700162 Resolver resolver;
163
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800164 boost::asio::ip::tcp::socket conn;
Ed Tanous003301a2024-04-16 09:59:19 -0700165 std::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>>
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800166 sslConn;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000167
Carson Labradof52c03c2022-03-23 18:50:15 +0000168 boost::asio::steady_timer timer;
Ed Tanous84b35602021-09-08 20:06:32 -0700169
Carson Labradof52c03c2022-03-23 18:50:15 +0000170 friend class ConnectionPool;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530171
Sunitha Harish29a82b02021-02-18 15:54:16 +0530172 void doResolve()
173 {
Sunitha Harish29a82b02021-02-18 15:54:16 +0530174 state = ConnState::resolveInProgress;
Ed Tanousa716aa72023-08-01 11:35:53 -0700175 BMCWEB_LOG_DEBUG("Trying to resolve: {}, id: {}", host, connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530176
Ed Tanousa716aa72023-08-01 11:35:53 -0700177 resolver.async_resolve(host.encoded_host_address(), host.port(),
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700178 std::bind_front(&ConnectionInfo::afterResolve,
179 this, shared_from_this()));
Sunitha Harish29a82b02021-02-18 15:54:16 +0530180 }
181
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700182 void afterResolve(const std::shared_ptr<ConnectionInfo>& /*self*/,
183 const boost::system::error_code& ec,
184 const Resolver::results_type& endpointList)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530185 {
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700186 if (ec || (endpointList.empty()))
187 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700188 BMCWEB_LOG_ERROR("Resolve failed: {} {}", ec.message(), host);
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700189 state = ConnState::resolveFailed;
190 waitAndRetry();
191 return;
192 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700193 BMCWEB_LOG_DEBUG("Resolved {}, id: {}", host, connId);
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530194 state = ConnState::connectInProgress;
195
Ed Tanousa716aa72023-08-01 11:35:53 -0700196 BMCWEB_LOG_DEBUG("Trying to connect to: {}, id: {}", host, connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530197
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800198 timer.expires_after(std::chrono::seconds(30));
199 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
200
201 boost::asio::async_connect(
202 conn, endpointList,
203 std::bind_front(&ConnectionInfo::afterConnect, this,
204 shared_from_this()));
AppaRao Pulie38778a2022-06-27 23:09:03 +0000205 }
206
207 void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/,
Ed Tanous81c4e332023-05-18 10:30:34 -0700208 const boost::beast::error_code& ec,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000209 const boost::asio::ip::tcp::endpoint& endpoint)
210 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000211 // The operation already timed out. We don't want do continue down
212 // this branch
213 if (ec && ec == boost::asio::error::operation_aborted)
214 {
215 return;
216 }
217
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800218 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000219 if (ec)
220 {
Ed Tanous62598e32023-07-17 17:06:25 -0700221 BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}",
abhilashraju079fbcf2025-04-24 05:24:56 +0000222 host.encoded_host_address(), host.port(), connId,
223 ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000224 state = ConnState::connectFailed;
225 waitAndRetry();
226 return;
227 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700228 BMCWEB_LOG_DEBUG("Connected to: {}:{}, id: {}",
229 endpoint.address().to_string(), endpoint.port(),
230 connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000231 if (sslConn)
232 {
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800233 doSslHandshake();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000234 return;
235 }
236 state = ConnState::connected;
237 sendMessage();
238 }
239
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800240 void doSslHandshake()
AppaRao Pulie38778a2022-06-27 23:09:03 +0000241 {
242 if (!sslConn)
243 {
244 return;
245 }
Ed Tanous3d158642025-05-12 14:20:49 -0700246 auto& ssl = *sslConn;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000247 state = ConnState::handshakeInProgress;
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800248 timer.expires_after(std::chrono::seconds(30));
249 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
Ed Tanous3d158642025-05-12 14:20:49 -0700250 ssl.async_handshake(boost::asio::ssl::stream_base::client,
251 std::bind_front(&ConnectionInfo::afterSslHandshake,
252 this, shared_from_this()));
AppaRao Pulie38778a2022-06-27 23:09:03 +0000253 }
254
255 void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/,
Ed Tanous81c4e332023-05-18 10:30:34 -0700256 const boost::beast::error_code& ec)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000257 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000258 // The operation already timed out. We don't want do continue down
259 // this branch
260 if (ec && ec == boost::asio::error::operation_aborted)
261 {
262 return;
263 }
264
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800265 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000266 if (ec)
267 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700268 BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}", connId,
269 ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000270 state = ConnState::handshakeFailed;
271 waitAndRetry();
272 return;
273 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700274 BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}", connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000275 state = ConnState::connected;
276 sendMessage();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530277 }
278
Carson Labradof52c03c2022-03-23 18:50:15 +0000279 void sendMessage()
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530280 {
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530281 state = ConnState::sendInProgress;
282
AppaRao Pulibd030d02020-03-20 03:34:29 +0530283 // Set a timeout on the operation
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800284 timer.expires_after(std::chrono::seconds(30));
285 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
AppaRao Pulibd030d02020-03-20 03:34:29 +0530286 // Send the HTTP request to the remote host
AppaRao Pulie38778a2022-06-27 23:09:03 +0000287 if (sslConn)
288 {
Ed Tanouscd504a92024-08-19 11:46:20 -0700289 boost::beast::http::async_write(
290 *sslConn, req,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000291 std::bind_front(&ConnectionInfo::afterWrite, this,
292 shared_from_this()));
293 }
294 else
295 {
Ed Tanouscd504a92024-08-19 11:46:20 -0700296 boost::beast::http::async_write(
297 conn, req,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000298 std::bind_front(&ConnectionInfo::afterWrite, this,
299 shared_from_this()));
300 }
301 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530302
AppaRao Pulie38778a2022-06-27 23:09:03 +0000303 void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/,
304 const boost::beast::error_code& ec, size_t bytesTransferred)
305 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000306 // The operation already timed out. We don't want do continue down
307 // this branch
308 if (ec && ec == boost::asio::error::operation_aborted)
309 {
310 return;
311 }
312
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800313 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000314 if (ec)
315 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700316 BMCWEB_LOG_ERROR("sendMessage() failed: {} {}", ec.message(), host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000317 state = ConnState::sendFailed;
318 waitAndRetry();
319 return;
320 }
Ed Tanous62598e32023-07-17 17:06:25 -0700321 BMCWEB_LOG_DEBUG("sendMessage() bytes transferred: {}",
322 bytesTransferred);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000323
324 recvMessage();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530325 }
326
327 void recvMessage()
328 {
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530329 state = ConnState::recvInProgress;
330
Ed Tanous38afdb92024-12-11 23:57:53 -0800331 parser_type& thisParser = parser.emplace();
Carson Labradod14a48f2023-02-22 00:24:54 +0000332
Ed Tanouse01d0c32023-06-30 13:21:32 -0700333 thisParser.body_limit(connPolicy->requestByteLimit);
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530334
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800335 timer.expires_after(std::chrono::seconds(30));
336 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
337
AppaRao Pulibd030d02020-03-20 03:34:29 +0530338 // Receive the HTTP response
AppaRao Pulie38778a2022-06-27 23:09:03 +0000339 if (sslConn)
340 {
341 boost::beast::http::async_read(
Ed Tanouse01d0c32023-06-30 13:21:32 -0700342 *sslConn, buffer, thisParser,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000343 std::bind_front(&ConnectionInfo::afterRead, this,
344 shared_from_this()));
345 }
346 else
347 {
348 boost::beast::http::async_read(
Ed Tanouse01d0c32023-06-30 13:21:32 -0700349 conn, buffer, thisParser,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000350 std::bind_front(&ConnectionInfo::afterRead, this,
351 shared_from_this()));
352 }
353 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530354
AppaRao Pulie38778a2022-06-27 23:09:03 +0000355 void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/,
356 const boost::beast::error_code& ec,
Ed Tanousd0fd3e52025-06-10 09:19:33 -0700357 const std::size_t bytesTransferred)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000358 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000359 // The operation already timed out. We don't want do continue down
360 // this branch
361 if (ec && ec == boost::asio::error::operation_aborted)
362 {
363 return;
364 }
365
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800366 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000367 if (ec && ec != boost::asio::ssl::error::stream_truncated)
368 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700369 BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}", ec.message(),
370 host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000371 state = ConnState::recvFailed;
372 waitAndRetry();
373 return;
374 }
Ed Tanous62598e32023-07-17 17:06:25 -0700375 BMCWEB_LOG_DEBUG("recvMessage() bytes transferred: {}",
376 bytesTransferred);
Ed Tanouse01d0c32023-06-30 13:21:32 -0700377 if (!parser)
378 {
379 return;
380 }
Ed Tanous52e31622024-01-23 16:31:11 -0800381 BMCWEB_LOG_DEBUG("recvMessage() data: {}", parser->get().body().str());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000382
383 unsigned int respCode = parser->get().result_int();
Ed Tanous62598e32023-07-17 17:06:25 -0700384 BMCWEB_LOG_DEBUG("recvMessage() Header Response Code: {}", respCode);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000385
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600386 // Handle the case of stream_truncated. Some servers close the ssl
387 // connection uncleanly, so check to see if we got a full response
388 // before we handle this as an error.
389 if (!parser->is_done())
390 {
391 state = ConnState::recvFailed;
392 waitAndRetry();
393 return;
394 }
395
AppaRao Pulie38778a2022-06-27 23:09:03 +0000396 // Make sure the received response code is valid as defined by
397 // the associated retry policy
Carson Labradod14a48f2023-02-22 00:24:54 +0000398 if (connPolicy->invalidResp(respCode))
AppaRao Pulie38778a2022-06-27 23:09:03 +0000399 {
400 // The listener failed to receive the Sent-Event
Ed Tanous62598e32023-07-17 17:06:25 -0700401 BMCWEB_LOG_ERROR(
402 "recvMessage() Listener Failed to "
Ed Tanousa716aa72023-08-01 11:35:53 -0700403 "receive Sent-Event. Header Response Code: {} from {}",
404 respCode, host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000405 state = ConnState::recvFailed;
406 waitAndRetry();
407 return;
408 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700409
AppaRao Pulie38778a2022-06-27 23:09:03 +0000410 // Send is successful
411 // Reset the counter just in case this was after retrying
412 retryCount = 0;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530413
AppaRao Pulie38778a2022-06-27 23:09:03 +0000414 // Keep the connection alive if server supports it
415 // Else close the connection
Ed Tanous62598e32023-07-17 17:06:25 -0700416 BMCWEB_LOG_DEBUG("recvMessage() keepalive : {}", parser->keep_alive());
AppaRao Pulibd030d02020-03-20 03:34:29 +0530417
AppaRao Pulie38778a2022-06-27 23:09:03 +0000418 // Copy the response into a Response object so that it can be
419 // processed by the callback function.
Ed Tanous27b0cf92023-08-07 12:02:40 -0700420 res.response = parser->release();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000421 callback(parser->keep_alive(), connId, res);
Carson Labrado513d1ff2022-07-19 00:38:15 +0000422 res.clear();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530423 }
424
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800425 static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800426 const boost::system::error_code& ec)
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800427 {
428 if (ec == boost::asio::error::operation_aborted)
429 {
Ed Tanous62598e32023-07-17 17:06:25 -0700430 BMCWEB_LOG_DEBUG(
431 "async_wait failed since the operation is aborted");
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800432 return;
433 }
434 if (ec)
435 {
Ed Tanous62598e32023-07-17 17:06:25 -0700436 BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
Ed Tanous27b0cf92023-08-07 12:02:40 -0700437 // If the timer fails, we need to close the socket anyway, same
438 // as if it expired.
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800439 }
440 std::shared_ptr<ConnectionInfo> self = weakSelf.lock();
441 if (self == nullptr)
442 {
443 return;
444 }
445 self->waitAndRetry();
446 }
447
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530448 void waitAndRetry()
AppaRao Pulibd030d02020-03-20 03:34:29 +0530449 {
Carson Labradod14a48f2023-02-22 00:24:54 +0000450 if ((retryCount >= connPolicy->maxRetryAttempts) ||
AppaRao Pulie38778a2022-06-27 23:09:03 +0000451 (state == ConnState::sslInitFailed))
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530452 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700453 BMCWEB_LOG_ERROR("Maximum number of retries reached. {}", host);
Ed Tanous62598e32023-07-17 17:06:25 -0700454 BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction);
Carson Labrado039a47e2022-04-05 16:03:20 +0000455
Carson Labradod14a48f2023-02-22 00:24:54 +0000456 if (connPolicy->retryPolicyAction == "TerminateAfterRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530457 {
458 // TODO: delete subscription
459 state = ConnState::terminated;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530460 }
Carson Labradod14a48f2023-02-22 00:24:54 +0000461 if (connPolicy->retryPolicyAction == "SuspendRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530462 {
463 state = ConnState::suspended;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530464 }
Carson Labrado513d1ff2022-07-19 00:38:15 +0000465
466 // We want to return a 502 to indicate there was an error with
467 // the external server
468 res.result(boost::beast::http::status::bad_gateway);
469 callback(false, connId, res);
470 res.clear();
471
Ed Tanous27b0cf92023-08-07 12:02:40 -0700472 // Reset the retrycount to zero so that client can try
473 // connecting again if needed
Ed Tanous3174e4d2020-10-07 11:41:22 -0700474 retryCount = 0;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530475 return;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530476 }
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530477
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530478 retryCount++;
479
Ed Tanous62598e32023-07-17 17:06:25 -0700480 BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700481 connPolicy->retryIntervalSecs.count(), retryCount);
Carson Labradod14a48f2023-02-22 00:24:54 +0000482 timer.expires_after(connPolicy->retryIntervalSecs);
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700483 timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this,
484 shared_from_this()));
485 }
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530486
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700487 void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/,
488 const boost::system::error_code& ec)
489 {
490 if (ec == boost::asio::error::operation_aborted)
491 {
Ed Tanous62598e32023-07-17 17:06:25 -0700492 BMCWEB_LOG_DEBUG(
493 "async_wait failed since the operation is aborted{}",
494 ec.message());
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700495 }
496 else if (ec)
497 {
Ed Tanous62598e32023-07-17 17:06:25 -0700498 BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700499 // Ignore the error and continue the retry loop to attempt
500 // sending the event as per the retry policy
501 }
502
503 // Let's close the connection and restart from resolve.
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600504 shutdownConn(true);
505 }
506
507 void restartConnection()
508 {
509 BMCWEB_LOG_DEBUG("{}, id: {} restartConnection", host,
510 std::to_string(connId));
511 initializeConnection(host.scheme() == "https");
512 doResolve();
Ayushi Smritife44eb02020-05-15 15:24:45 +0530513 }
514
AppaRao Pulie38778a2022-06-27 23:09:03 +0000515 void shutdownConn(bool retry)
Ayushi Smritife44eb02020-05-15 15:24:45 +0530516 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000517 boost::beast::error_code ec;
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800518 conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
Carson Labradof52c03c2022-03-23 18:50:15 +0000519 conn.close();
520
521 // not_connected happens sometimes so don't bother reporting it.
522 if (ec && ec != boost::beast::errc::not_connected)
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530523 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700524 BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
Ed Tanous62598e32023-07-17 17:06:25 -0700525 ec.message());
Carson Labradof52c03c2022-03-23 18:50:15 +0000526 }
Carson Labrado5cab68f2022-07-11 22:26:21 +0000527 else
528 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700529 BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
Carson Labrado5cab68f2022-07-11 22:26:21 +0000530 }
Ed Tanousca723762022-06-28 19:40:39 -0700531
Carson Labrado513d1ff2022-07-19 00:38:15 +0000532 if (retry)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000533 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000534 // Now let's try to resend the data
535 state = ConnState::retry;
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600536 restartConnection();
Carson Labrado513d1ff2022-07-19 00:38:15 +0000537 }
538 else
539 {
540 state = ConnState::closed;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000541 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000542 }
543
AppaRao Pulie38778a2022-06-27 23:09:03 +0000544 void doClose(bool retry = false)
Carson Labradof52c03c2022-03-23 18:50:15 +0000545 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000546 if (!sslConn)
547 {
548 shutdownConn(retry);
549 return;
550 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000551
AppaRao Pulie38778a2022-06-27 23:09:03 +0000552 sslConn->async_shutdown(
553 std::bind_front(&ConnectionInfo::afterSslShutdown, this,
554 shared_from_this(), retry));
555 }
556
557 void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/,
558 bool retry, const boost::system::error_code& ec)
559 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000560 if (ec)
Carson Labradof52c03c2022-03-23 18:50:15 +0000561 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700562 BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
Ed Tanous62598e32023-07-17 17:06:25 -0700563 ec.message());
Carson Labradof52c03c2022-03-23 18:50:15 +0000564 }
Carson Labrado5cab68f2022-07-11 22:26:21 +0000565 else
566 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700567 BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
Carson Labrado5cab68f2022-07-11 22:26:21 +0000568 }
AppaRao Pulie38778a2022-06-27 23:09:03 +0000569 shutdownConn(retry);
570 }
Ed Tanousca723762022-06-28 19:40:39 -0700571
AppaRao Pulie38778a2022-06-27 23:09:03 +0000572 void setCipherSuiteTLSext()
573 {
574 if (!sslConn)
575 {
576 return;
577 }
Ravi Tejae7c29912023-07-31 09:39:32 -0500578
579 if (host.host_type() != boost::urls::host_type::name)
580 {
581 // Avoid setting SNI hostname if its IP address
582 return;
583 }
584 // Create a null terminated string for SSL
Ed Tanousa716aa72023-08-01 11:35:53 -0700585 std::string hostname(host.encoded_host_address());
Ed Tanousd0fd3e52025-06-10 09:19:33 -0700586 if (SSL_set_tlsext_host_name(sslConn->native_handle(),
587 hostname.data()) == 0)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000588
589 {
590 boost::beast::error_code ec{static_cast<int>(::ERR_get_error()),
591 boost::asio::error::get_ssl_category()};
592
Ed Tanousa716aa72023-08-01 11:35:53 -0700593 BMCWEB_LOG_ERROR("SSL_set_tlsext_host_name {}, id: {} failed: {}",
594 host, connId, ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000595 // Set state as sslInit failed so that we close the connection
596 // and take appropriate action as per retry configuration.
597 state = ConnState::sslInitFailed;
598 waitAndRetry();
599 return;
600 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530601 }
602
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600603 void initializeConnection(bool ssl)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000604 {
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600605 conn = boost::asio::ip::tcp::socket(ioc);
606 if (ssl)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000607 {
608 std::optional<boost::asio::ssl::context> sslCtx =
Ed Tanous19bb3622024-07-05 10:07:40 -0500609 ensuressl::getSSLClientContext(verifyCert);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000610
611 if (!sslCtx)
612 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700613 BMCWEB_LOG_ERROR("prepareSSLContext failed - {}, id: {}", host,
614 connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000615 // Don't retry if failure occurs while preparing SSL context
Ed Tanous27b0cf92023-08-07 12:02:40 -0700616 // such as certificate is invalid or set cipher failure or
617 // set host name failure etc... Setting conn state to
618 // sslInitFailed and connection state will be transitioned
619 // to next state depending on retry policy set by
620 // subscription.
AppaRao Pulie38778a2022-06-27 23:09:03 +0000621 state = ConnState::sslInitFailed;
622 waitAndRetry();
623 return;
624 }
625 sslConn.emplace(conn, *sslCtx);
626 setCipherSuiteTLSext();
627 }
628 }
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600629
630 public:
631 explicit ConnectionInfo(
632 boost::asio::io_context& iocIn, const std::string& idIn,
633 const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
Ed Tanous19bb3622024-07-05 10:07:40 -0500634 const boost::urls::url_view_base& hostIn,
635 ensuressl::VerifyCertificate verifyCertIn, unsigned int connIdIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400636 subId(idIn), connPolicy(connPolicyIn), host(hostIn),
637 verifyCert(verifyCertIn), connId(connIdIn), ioc(iocIn), resolver(iocIn),
638 conn(iocIn), timer(iocIn)
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600639 {
640 initializeConnection(host.scheme() == "https");
641 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000642};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530643
Carson Labradof52c03c2022-03-23 18:50:15 +0000644class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
645{
646 private:
647 boost::asio::io_context& ioc;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000648 std::string id;
Carson Labradod14a48f2023-02-22 00:24:54 +0000649 std::shared_ptr<ConnectionPolicy> connPolicy;
Ed Tanousa716aa72023-08-01 11:35:53 -0700650 boost::urls::url destIP;
Carson Labradof52c03c2022-03-23 18:50:15 +0000651 std::vector<std::shared_ptr<ConnectionInfo>> connections;
652 boost::container::devector<PendingRequest> requestQueue;
Ed Tanous19bb3622024-07-05 10:07:40 -0500653 ensuressl::VerifyCertificate verifyCert;
Carson Labradof52c03c2022-03-23 18:50:15 +0000654
655 friend class HttpClient;
656
Carson Labrado244256c2022-04-27 17:16:32 +0000657 // Configure a connections's request, callback, and retry info in
658 // preparation to begin sending the request
Carson Labradof52c03c2022-03-23 18:50:15 +0000659 void setConnProps(ConnectionInfo& conn)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530660 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000661 if (requestQueue.empty())
AppaRao Pulibd030d02020-03-20 03:34:29 +0530662 {
Ed Tanous62598e32023-07-17 17:06:25 -0700663 BMCWEB_LOG_ERROR(
664 "setConnProps() should not have been called when requestQueue is empty");
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530665 return;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530666 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530667
Ed Tanous52e31622024-01-23 16:31:11 -0800668 PendingRequest& nextReq = requestQueue.front();
Carson Labrado244256c2022-04-27 17:16:32 +0000669 conn.req = std::move(nextReq.req);
670 conn.callback = std::move(nextReq.callback);
Carson Labradof52c03c2022-03-23 18:50:15 +0000671
Ed Tanousa716aa72023-08-01 11:35:53 -0700672 BMCWEB_LOG_DEBUG("Setting properties for connection {}, id: {}",
673 conn.host, conn.connId);
Carson Labradof52c03c2022-03-23 18:50:15 +0000674
675 // We can remove the request from the queue at this point
676 requestQueue.pop_front();
677 }
678
Carson Labradof52c03c2022-03-23 18:50:15 +0000679 // Gets called as part of callback after request is sent
680 // Reuses the connection if there are any requests waiting to be sent
681 // Otherwise closes the connection if it is not a keep-alive
682 void sendNext(bool keepAlive, uint32_t connId)
683 {
684 auto conn = connections[connId];
Carson Labrado46a81462022-04-27 21:11:37 +0000685
686 // Allow the connection's handler to be deleted
687 // This is needed because of Redfish Aggregation passing an
688 // AsyncResponse shared_ptr to this callback
689 conn->callback = nullptr;
690
Carson Labradof52c03c2022-03-23 18:50:15 +0000691 // Reuse the connection to send the next request in the queue
692 if (!requestQueue.empty())
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530693 {
Ed Tanous62598e32023-07-17 17:06:25 -0700694 BMCWEB_LOG_DEBUG(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800695 "{} requests remaining in queue for {}, reusing connection {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700696 requestQueue.size(), destIP, connId);
Carson Labradof52c03c2022-03-23 18:50:15 +0000697
698 setConnProps(*conn);
699
700 if (keepAlive)
701 {
702 conn->sendMessage();
703 }
704 else
705 {
706 // Server is not keep-alive enabled so we need to close the
707 // connection and then start over from resolve
708 conn->doClose();
Abhilash Raju2ecde742024-06-01 02:01:01 -0500709 conn->restartConnection();
Carson Labradof52c03c2022-03-23 18:50:15 +0000710 }
711 return;
712 }
713
714 // No more messages to send so close the connection if necessary
715 if (keepAlive)
716 {
717 conn->state = ConnState::idle;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530718 }
719 else
720 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000721 // Abort the connection since server is not keep-alive enabled
722 conn->state = ConnState::abortConnection;
723 conn->doClose();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530724 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530725 }
726
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700727 void sendData(std::string&& data, const boost::urls::url_view_base& destUri,
Carson Labrado244256c2022-04-27 17:16:32 +0000728 const boost::beast::http::fields& httpHeader,
729 const boost::beast::http::verb verb,
Ed Tanous6b3db602022-06-28 19:41:44 -0700730 const std::function<void(Response&)>& resHandler)
Ayushi Smritife44eb02020-05-15 15:24:45 +0530731 {
Carson Labrado244256c2022-04-27 17:16:32 +0000732 // Construct the request to be sent
Ed Tanousb2896142024-01-31 15:25:47 -0800733 boost::beast::http::request<bmcweb::HttpBody> thisReq(
Ed Tanousa716aa72023-08-01 11:35:53 -0700734 verb, destUri.encoded_target(), 11, "", httpHeader);
735 thisReq.set(boost::beast::http::field::host,
736 destUri.encoded_host_address());
Carson Labrado244256c2022-04-27 17:16:32 +0000737 thisReq.keep_alive(true);
Ed Tanous52e31622024-01-23 16:31:11 -0800738 thisReq.body().str() = std::move(data);
Carson Labrado244256c2022-04-27 17:16:32 +0000739 thisReq.prepare_payload();
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700740 auto cb = std::bind_front(&ConnectionPool::afterSendData,
741 weak_from_this(), resHandler);
Carson Labradof52c03c2022-03-23 18:50:15 +0000742 // Reuse an existing connection if one is available
743 for (unsigned int i = 0; i < connections.size(); i++)
744 {
745 auto conn = connections[i];
746 if ((conn->state == ConnState::idle) ||
747 (conn->state == ConnState::initialized) ||
748 (conn->state == ConnState::closed))
749 {
Carson Labrado244256c2022-04-27 17:16:32 +0000750 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000751 conn->callback = std::move(cb);
Ed Tanousa716aa72023-08-01 11:35:53 -0700752 std::string commonMsg = std::format("{} from pool {}", i, id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000753
754 if (conn->state == ConnState::idle)
755 {
Ed Tanous62598e32023-07-17 17:06:25 -0700756 BMCWEB_LOG_DEBUG("Grabbing idle connection {}", commonMsg);
Carson Labradof52c03c2022-03-23 18:50:15 +0000757 conn->sendMessage();
758 }
759 else
760 {
Ed Tanous62598e32023-07-17 17:06:25 -0700761 BMCWEB_LOG_DEBUG("Reusing existing connection {}",
762 commonMsg);
Abhilash Raju2ecde742024-06-01 02:01:01 -0500763 conn->restartConnection();
Carson Labradof52c03c2022-03-23 18:50:15 +0000764 }
765 return;
766 }
767 }
768
Ed Tanous27b0cf92023-08-07 12:02:40 -0700769 // All connections in use so create a new connection or add request
770 // to the queue
Carson Labradod14a48f2023-02-22 00:24:54 +0000771 if (connections.size() < connPolicy->maxConnections)
Carson Labradof52c03c2022-03-23 18:50:15 +0000772 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700773 BMCWEB_LOG_DEBUG("Adding new connection to pool {}", id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000774 auto conn = addConnection();
Carson Labrado244256c2022-04-27 17:16:32 +0000775 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000776 conn->callback = std::move(cb);
Carson Labradof52c03c2022-03-23 18:50:15 +0000777 conn->doResolve();
778 }
779 else if (requestQueue.size() < maxRequestQueueSize)
780 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700781 BMCWEB_LOG_DEBUG("Max pool size reached. Adding data to queue {}",
782 id);
Carson Labradod14a48f2023-02-22 00:24:54 +0000783 requestQueue.emplace_back(std::move(thisReq), std::move(cb));
Carson Labradof52c03c2022-03-23 18:50:15 +0000784 }
785 else
786 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700787 // If we can't buffer the request then we should let the
788 // callback handle a 429 Too Many Requests dummy response
Ed Tanous6ea90762024-04-07 08:38:44 -0700789 BMCWEB_LOG_ERROR("{} request queue full. Dropping request.", id);
Carson Labrado43e14d32022-11-09 00:25:20 +0000790 Response dummyRes;
791 dummyRes.result(boost::beast::http::status::too_many_requests);
792 resHandler(dummyRes);
Carson Labradof52c03c2022-03-23 18:50:15 +0000793 }
Ayushi Smritife44eb02020-05-15 15:24:45 +0530794 }
795
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700796 // Callback to be called once the request has been sent
797 static void afterSendData(const std::weak_ptr<ConnectionPool>& weakSelf,
798 const std::function<void(Response&)>& resHandler,
799 bool keepAlive, uint32_t connId, Response& res)
800 {
801 // Allow provided callback to perform additional processing of the
802 // request
803 resHandler(res);
804
805 // If requests remain in the queue then we want to reuse this
806 // connection to send the next request
807 std::shared_ptr<ConnectionPool> self = weakSelf.lock();
808 if (!self)
809 {
Ed Tanous62598e32023-07-17 17:06:25 -0700810 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
811 logPtr(self.get()));
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700812 return;
813 }
814
815 self->sendNext(keepAlive, connId);
816 }
817
Carson Labradof52c03c2022-03-23 18:50:15 +0000818 std::shared_ptr<ConnectionInfo>& addConnection()
Ayushi Smritife44eb02020-05-15 15:24:45 +0530819 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000820 unsigned int newId = static_cast<unsigned int>(connections.size());
821
AppaRao Pulie38778a2022-06-27 23:09:03 +0000822 auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>(
Ed Tanous19bb3622024-07-05 10:07:40 -0500823 ioc, id, connPolicy, destIP, verifyCert, newId));
Carson Labradof52c03c2022-03-23 18:50:15 +0000824
Ed Tanousa716aa72023-08-01 11:35:53 -0700825 BMCWEB_LOG_DEBUG("Added connection {} to pool {}",
826 connections.size() - 1, id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000827
828 return ret;
829 }
830
831 public:
Carson Labradod14a48f2023-02-22 00:24:54 +0000832 explicit ConnectionPool(
833 boost::asio::io_context& iocIn, const std::string& idIn,
834 const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
Ed Tanous19bb3622024-07-05 10:07:40 -0500835 const boost::urls::url_view_base& destIPIn,
836 ensuressl::VerifyCertificate verifyCertIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400837 ioc(iocIn), id(idIn), connPolicy(connPolicyIn), destIP(destIPIn),
Ed Tanous19bb3622024-07-05 10:07:40 -0500838 verifyCert(verifyCertIn)
Carson Labradof52c03c2022-03-23 18:50:15 +0000839 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700840 BMCWEB_LOG_DEBUG("Initializing connection pool for {}", id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000841
842 // Initialize the pool with a single connection
843 addConnection();
Ayushi Smritife44eb02020-05-15 15:24:45 +0530844 }
Myung Baea0969c72024-09-19 08:46:45 -0400845
846 // Check whether all connections are terminated
847 bool areAllConnectionsTerminated()
848 {
849 if (connections.empty())
850 {
851 BMCWEB_LOG_DEBUG("There are no connections for pool id:{}", id);
852 return false;
853 }
854 for (const auto& conn : connections)
855 {
856 if (conn != nullptr && conn->state != ConnState::terminated)
857 {
858 BMCWEB_LOG_DEBUG(
859 "Not all connections of pool id:{} are terminated", id);
860 return false;
861 }
862 }
863 BMCWEB_LOG_INFO("All connections of pool id:{} are terminated", id);
864 return true;
865 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530866};
867
Carson Labradof52c03c2022-03-23 18:50:15 +0000868class HttpClient
869{
870 private:
871 std::unordered_map<std::string, std::shared_ptr<ConnectionPool>>
872 connectionPools;
Ed Tanous4b712a22023-08-02 12:56:52 -0700873
874 // reference_wrapper here makes HttpClient movable
875 std::reference_wrapper<boost::asio::io_context> ioc;
Carson Labradod14a48f2023-02-22 00:24:54 +0000876 std::shared_ptr<ConnectionPolicy> connPolicy;
Carson Labradof52c03c2022-03-23 18:50:15 +0000877
Carson Labrado039a47e2022-04-05 16:03:20 +0000878 // Used as a dummy callback by sendData() in order to call
879 // sendDataWithCallback()
Ed Tanous02cad962022-06-30 16:50:15 -0700880 static void genericResHandler(const Response& res)
Carson Labrado039a47e2022-04-05 16:03:20 +0000881 {
Ed Tanous62598e32023-07-17 17:06:25 -0700882 BMCWEB_LOG_DEBUG("Response handled with return code: {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700883 res.resultInt());
Ed Tanous4ee8e212022-05-28 09:42:51 -0700884 }
Carson Labrado039a47e2022-04-05 16:03:20 +0000885
Carson Labradof52c03c2022-03-23 18:50:15 +0000886 public:
Carson Labradod14a48f2023-02-22 00:24:54 +0000887 HttpClient() = delete;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700888 explicit HttpClient(boost::asio::io_context& iocIn,
889 const std::shared_ptr<ConnectionPolicy>& connPolicyIn) :
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400890 ioc(iocIn), connPolicy(connPolicyIn)
Carson Labradod14a48f2023-02-22 00:24:54 +0000891 {}
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700892
Carson Labradof52c03c2022-03-23 18:50:15 +0000893 HttpClient(const HttpClient&) = delete;
894 HttpClient& operator=(const HttpClient&) = delete;
Ed Tanous4b712a22023-08-02 12:56:52 -0700895 HttpClient(HttpClient&& client) = default;
896 HttpClient& operator=(HttpClient&& client) = default;
Carson Labradof52c03c2022-03-23 18:50:15 +0000897 ~HttpClient() = default;
898
Ed Tanousa716aa72023-08-01 11:35:53 -0700899 // Send a request to destIP where additional processing of the
Carson Labrado039a47e2022-04-05 16:03:20 +0000900 // result is not required
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700901 void sendData(std::string&& data, const boost::urls::url_view_base& destUri,
Ed Tanous19bb3622024-07-05 10:07:40 -0500902 ensuressl::VerifyCertificate verifyCert,
Carson Labradof52c03c2022-03-23 18:50:15 +0000903 const boost::beast::http::fields& httpHeader,
Carson Labradod14a48f2023-02-22 00:24:54 +0000904 const boost::beast::http::verb verb)
Carson Labradof52c03c2022-03-23 18:50:15 +0000905 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000906 const std::function<void(Response&)> cb = genericResHandler;
Ed Tanous19bb3622024-07-05 10:07:40 -0500907 sendDataWithCallback(std::move(data), destUri, verifyCert, httpHeader,
908 verb, cb);
Carson Labrado039a47e2022-04-05 16:03:20 +0000909 }
910
Ed Tanousa716aa72023-08-01 11:35:53 -0700911 // Send request to destIP and use the provided callback to
Carson Labrado039a47e2022-04-05 16:03:20 +0000912 // handle the response
Ed Tanous4a7fbef2024-04-06 16:03:49 -0700913 void sendDataWithCallback(std::string&& data,
914 const boost::urls::url_view_base& destUrl,
Ed Tanous19bb3622024-07-05 10:07:40 -0500915 ensuressl::VerifyCertificate verifyCert,
Carson Labrado039a47e2022-04-05 16:03:20 +0000916 const boost::beast::http::fields& httpHeader,
Carson Labrado244256c2022-04-27 17:16:32 +0000917 const boost::beast::http::verb verb,
Ed Tanous6b3db602022-06-28 19:41:44 -0700918 const std::function<void(Response&)>& resHandler)
Carson Labrado039a47e2022-04-05 16:03:20 +0000919 {
Ed Tanous19bb3622024-07-05 10:07:40 -0500920 std::string_view verify = "ssl_verify";
921 if (verifyCert == ensuressl::VerifyCertificate::NoVerify)
922 {
923 verify = "ssl no verify";
924 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400925 std::string clientKey =
926 std::format("{}{}://{}", verify, destUrl.scheme(),
927 destUrl.encoded_host_and_port());
Carson Labradod14a48f2023-02-22 00:24:54 +0000928 auto pool = connectionPools.try_emplace(clientKey);
929 if (pool.first->second == nullptr)
Carson Labradof52c03c2022-03-23 18:50:15 +0000930 {
Carson Labradod14a48f2023-02-22 00:24:54 +0000931 pool.first->second = std::make_shared<ConnectionPool>(
Ed Tanous19bb3622024-07-05 10:07:40 -0500932 ioc, clientKey, connPolicy, destUrl, verifyCert);
Carson Labradof52c03c2022-03-23 18:50:15 +0000933 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700934 // Send the data using either the existing connection pool or the
935 // newly created connection pool
Ed Tanousa716aa72023-08-01 11:35:53 -0700936 pool.first->second->sendData(std::move(data), destUrl, httpHeader, verb,
Carson Labradod14a48f2023-02-22 00:24:54 +0000937 resHandler);
Carson Labradof52c03c2022-03-23 18:50:15 +0000938 }
Myung Baea0969c72024-09-19 08:46:45 -0400939
940 // Test whether all connections are terminated (after MaxRetryAttempts)
941 bool isTerminated()
942 {
943 for (const auto& pool : connectionPools)
944 {
945 if (pool.second != nullptr &&
946 !pool.second->areAllConnectionsTerminated())
947 {
948 BMCWEB_LOG_DEBUG(
949 "Not all of client connections are terminated");
950 return false;
951 }
952 }
953 BMCWEB_LOG_DEBUG("All client connections are terminated");
954 return true;
955 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000956};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530957} // namespace crow