blob: 6f42f3ef6e74cbcd0bf551164d5c52fa5bfc076a [file] [log] [blame]
AppaRao Pulibd030d02020-03-20 03:34:29 +05301/*
2// Copyright (c) 2020 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
Nan Zhou77665bd2022-10-12 20:28:58 +000017
18#include "async_resolve.hpp"
19#include "http_response.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "logging.hpp"
21#include "ssl_key_handler.hpp"
Nan Zhou77665bd2022-10-12 20:28:58 +000022
Ed Tanous0d5f5cf2022-03-12 15:30:55 -080023#include <boost/asio/connect.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070024#include <boost/asio/io_context.hpp>
Sunitha Harish29a82b02021-02-18 15:54:16 +053025#include <boost/asio/ip/address.hpp>
26#include <boost/asio/ip/basic_endpoint.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070027#include <boost/asio/ip/tcp.hpp>
AppaRao Pulie38778a2022-06-27 23:09:03 +000028#include <boost/asio/ssl/context.hpp>
29#include <boost/asio/ssl/error.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070030#include <boost/asio/steady_timer.hpp>
31#include <boost/beast/core/flat_buffer.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070032#include <boost/beast/core/flat_static_buffer.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 Tanousbb49eb52022-06-28 12:02:42 -070036#include <boost/beast/http/write.hpp>
AppaRao Pulie38778a2022-06-27 23:09:03 +000037#include <boost/beast/ssl/ssl_stream.hpp>
AppaRao Pulibd030d02020-03-20 03:34:29 +053038#include <boost/beast/version.hpp>
Carson Labradof52c03c2022-03-23 18:50:15 +000039#include <boost/container/devector.hpp>
Ed Tanousbb49eb52022-06-28 12:02:42 -070040#include <boost/system/error_code.hpp>
Ed Tanous27b0cf92023-08-07 12:02:40 -070041#include <boost/url/format.hpp>
42#include <boost/url/url.hpp>
Ed Tanousa716aa72023-08-01 11:35:53 -070043#include <boost/url/url_view.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050044
AppaRao Pulibd030d02020-03-20 03:34:29 +053045#include <cstdlib>
46#include <functional>
47#include <iostream>
48#include <memory>
AppaRao Puli2a5689a2020-04-29 15:24:31 +053049#include <queue>
AppaRao Pulibd030d02020-03-20 03:34:29 +053050#include <string>
51
52namespace crow
53{
Ed Tanous27b0cf92023-08-07 12:02:40 -070054// With Redfish Aggregation it is assumed we will connect to another
55// instance of BMCWeb which can handle 100 simultaneous connections.
Carson Labrado66d90c22022-12-07 22:34:33 +000056constexpr size_t maxPoolSize = 20;
57constexpr size_t maxRequestQueueSize = 500;
Carson Labrado17dcc312022-07-28 22:17:28 +000058constexpr unsigned int httpReadBodyLimit = 131072;
Carson Labrado4d942722022-06-22 22:16:10 +000059constexpr unsigned int httpReadBufferSize = 4096;
AppaRao Puli2a5689a2020-04-29 15:24:31 +053060
AppaRao Pulibd030d02020-03-20 03:34:29 +053061enum class ConnState
62{
AppaRao Puli2a5689a2020-04-29 15:24:31 +053063 initialized,
Sunitha Harish29a82b02021-02-18 15:54:16 +053064 resolveInProgress,
65 resolveFailed,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053066 connectInProgress,
67 connectFailed,
AppaRao Pulibd030d02020-03-20 03:34:29 +053068 connected,
AppaRao Pulie38778a2022-06-27 23:09:03 +000069 handshakeInProgress,
70 handshakeFailed,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053071 sendInProgress,
72 sendFailed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053073 recvInProgress,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053074 recvFailed,
75 idle,
Ayushi Smritife44eb02020-05-15 15:24:45 +053076 closed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053077 suspended,
78 terminated,
79 abortConnection,
AppaRao Pulie38778a2022-06-27 23:09:03 +000080 sslInitFailed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053081 retry
AppaRao Pulibd030d02020-03-20 03:34:29 +053082};
83
Carson Labradoa7a80292022-06-01 16:01:52 +000084static inline boost::system::error_code
85 defaultRetryHandler(unsigned int respCode)
86{
87 // As a default, assume 200X is alright
Ed Tanous62598e32023-07-17 17:06:25 -070088 BMCWEB_LOG_DEBUG("Using default check for response code validity");
Carson Labradoa7a80292022-06-01 16:01:52 +000089 if ((respCode < 200) || (respCode >= 300))
90 {
91 return boost::system::errc::make_error_code(
92 boost::system::errc::result_out_of_range);
93 }
94
95 // Return 0 if the response code is valid
96 return boost::system::errc::make_error_code(boost::system::errc::success);
97};
98
Ed Tanous27b0cf92023-08-07 12:02:40 -070099// We need to allow retry information to be set before a message has been
100// sent and a connection pool has been created
Carson Labradod14a48f2023-02-22 00:24:54 +0000101struct ConnectionPolicy
Carson Labradof52c03c2022-03-23 18:50:15 +0000102{
103 uint32_t maxRetryAttempts = 5;
Carson Labradod14a48f2023-02-22 00:24:54 +0000104
105 // the max size of requests in bytes. 0 for unlimited
106 boost::optional<uint64_t> requestByteLimit = httpReadBodyLimit;
107
108 size_t maxConnections = 1;
109
Carson Labradof52c03c2022-03-23 18:50:15 +0000110 std::string retryPolicyAction = "TerminateAfterRetries";
Carson Labradod14a48f2023-02-22 00:24:54 +0000111
112 std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0);
Carson Labradoa7a80292022-06-01 16:01:52 +0000113 std::function<boost::system::error_code(unsigned int respCode)>
114 invalidResp = defaultRetryHandler;
Carson Labradof52c03c2022-03-23 18:50:15 +0000115};
116
117struct PendingRequest
118{
Ed Tanous52e31622024-01-23 16:31:11 -0800119 boost::beast::http::request<bmcweb::FileBody> req;
Carson Labrado039a47e2022-04-05 16:03:20 +0000120 std::function<void(bool, uint32_t, Response&)> callback;
Carson Labrado039a47e2022-04-05 16:03:20 +0000121 PendingRequest(
Ed Tanous52e31622024-01-23 16:31:11 -0800122 boost::beast::http::request<bmcweb::FileBody>&& reqIn,
Carson Labradod14a48f2023-02-22 00:24:54 +0000123 const std::function<void(bool, uint32_t, Response&)>& callbackIn) :
Ed Tanous8a592812022-06-04 09:06:59 -0700124 req(std::move(reqIn)),
Carson Labradod14a48f2023-02-22 00:24:54 +0000125 callback(callbackIn)
Carson Labradof52c03c2022-03-23 18:50:15 +0000126 {}
127};
128
Ed Tanouse01d0c32023-06-30 13:21:32 -0700129namespace http = boost::beast::http;
Carson Labradof52c03c2022-03-23 18:50:15 +0000130class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
AppaRao Pulibd030d02020-03-20 03:34:29 +0530131{
132 private:
Carson Labradof52c03c2022-03-23 18:50:15 +0000133 ConnState state = ConnState::initialized;
134 uint32_t retryCount = 0;
Carson Labradof52c03c2022-03-23 18:50:15 +0000135 std::string subId;
Carson Labradod14a48f2023-02-22 00:24:54 +0000136 std::shared_ptr<ConnectionPolicy> connPolicy;
Ed Tanousa716aa72023-08-01 11:35:53 -0700137 boost::urls::url host;
Carson Labradof52c03c2022-03-23 18:50:15 +0000138 uint32_t connId;
139
Carson Labradof52c03c2022-03-23 18:50:15 +0000140 // Data buffers
Ed Tanous52e31622024-01-23 16:31:11 -0800141 http::request<bmcweb::FileBody> req;
142 using parser_type = http::response_parser<bmcweb::FileBody>;
Ed Tanouse01d0c32023-06-30 13:21:32 -0700143 std::optional<parser_type> parser;
Carson Labrado4d942722022-06-22 22:16:10 +0000144 boost::beast::flat_static_buffer<httpReadBufferSize> buffer;
Carson Labrado039a47e2022-04-05 16:03:20 +0000145 Response res;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530146
Carson Labradof52c03c2022-03-23 18:50:15 +0000147 // Ascync callables
Carson Labrado039a47e2022-04-05 16:03:20 +0000148 std::function<void(bool, uint32_t, Response&)> callback;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700149
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600150 boost::asio::io_context& ioc;
151
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700152#ifdef BMCWEB_DBUS_DNS_RESOLVER
Ed Tanouse1452be2021-10-04 17:03:52 -0700153 using Resolver = async_resolve::Resolver;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700154#else
155 using Resolver = boost::asio::ip::tcp::resolver;
156#endif
157 Resolver resolver;
158
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800159 boost::asio::ip::tcp::socket conn;
160 std::optional<boost::beast::ssl_stream<boost::asio::ip::tcp::socket&>>
161 sslConn;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000162
Carson Labradof52c03c2022-03-23 18:50:15 +0000163 boost::asio::steady_timer timer;
Ed Tanous84b35602021-09-08 20:06:32 -0700164
Carson Labradof52c03c2022-03-23 18:50:15 +0000165 friend class ConnectionPool;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530166
Sunitha Harish29a82b02021-02-18 15:54:16 +0530167 void doResolve()
168 {
Sunitha Harish29a82b02021-02-18 15:54:16 +0530169 state = ConnState::resolveInProgress;
Ed Tanousa716aa72023-08-01 11:35:53 -0700170 BMCWEB_LOG_DEBUG("Trying to resolve: {}, id: {}", host, connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530171
Ed Tanousa716aa72023-08-01 11:35:53 -0700172 resolver.async_resolve(host.encoded_host_address(), host.port(),
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700173 std::bind_front(&ConnectionInfo::afterResolve,
174 this, shared_from_this()));
Sunitha Harish29a82b02021-02-18 15:54:16 +0530175 }
176
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700177 void afterResolve(const std::shared_ptr<ConnectionInfo>& /*self*/,
178 const boost::system::error_code& ec,
179 const Resolver::results_type& endpointList)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530180 {
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700181 if (ec || (endpointList.empty()))
182 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700183 BMCWEB_LOG_ERROR("Resolve failed: {} {}", ec.message(), host);
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700184 state = ConnState::resolveFailed;
185 waitAndRetry();
186 return;
187 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700188 BMCWEB_LOG_DEBUG("Resolved {}, id: {}", host, connId);
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530189 state = ConnState::connectInProgress;
190
Ed Tanousa716aa72023-08-01 11:35:53 -0700191 BMCWEB_LOG_DEBUG("Trying to connect to: {}, id: {}", host, connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530192
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800193 timer.expires_after(std::chrono::seconds(30));
194 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
195
196 boost::asio::async_connect(
197 conn, endpointList,
198 std::bind_front(&ConnectionInfo::afterConnect, this,
199 shared_from_this()));
AppaRao Pulie38778a2022-06-27 23:09:03 +0000200 }
201
202 void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/,
Ed Tanous81c4e332023-05-18 10:30:34 -0700203 const boost::beast::error_code& ec,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000204 const boost::asio::ip::tcp::endpoint& endpoint)
205 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000206 // The operation already timed out. We don't want do continue down
207 // this branch
208 if (ec && ec == boost::asio::error::operation_aborted)
209 {
210 return;
211 }
212
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800213 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000214 if (ec)
215 {
Ed Tanous62598e32023-07-17 17:06:25 -0700216 BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700217 endpoint.address().to_string(), endpoint.port(),
218 connId, ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000219 state = ConnState::connectFailed;
220 waitAndRetry();
221 return;
222 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700223 BMCWEB_LOG_DEBUG("Connected to: {}:{}, id: {}",
224 endpoint.address().to_string(), endpoint.port(),
225 connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000226 if (sslConn)
227 {
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800228 doSslHandshake();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000229 return;
230 }
231 state = ConnState::connected;
232 sendMessage();
233 }
234
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800235 void doSslHandshake()
AppaRao Pulie38778a2022-06-27 23:09:03 +0000236 {
237 if (!sslConn)
238 {
239 return;
240 }
241 state = ConnState::handshakeInProgress;
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800242 timer.expires_after(std::chrono::seconds(30));
243 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
AppaRao Pulie38778a2022-06-27 23:09:03 +0000244 sslConn->async_handshake(
245 boost::asio::ssl::stream_base::client,
246 std::bind_front(&ConnectionInfo::afterSslHandshake, this,
247 shared_from_this()));
248 }
249
250 void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/,
Ed Tanous81c4e332023-05-18 10:30:34 -0700251 const boost::beast::error_code& ec)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000252 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000253 // The operation already timed out. We don't want do continue down
254 // this branch
255 if (ec && ec == boost::asio::error::operation_aborted)
256 {
257 return;
258 }
259
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800260 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000261 if (ec)
262 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700263 BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}", connId,
264 ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000265 state = ConnState::handshakeFailed;
266 waitAndRetry();
267 return;
268 }
Ed Tanousa716aa72023-08-01 11:35:53 -0700269 BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}", connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000270 state = ConnState::connected;
271 sendMessage();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530272 }
273
Carson Labradof52c03c2022-03-23 18:50:15 +0000274 void sendMessage()
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530275 {
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530276 state = ConnState::sendInProgress;
277
AppaRao Pulibd030d02020-03-20 03:34:29 +0530278 // Set a timeout on the operation
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800279 timer.expires_after(std::chrono::seconds(30));
280 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
AppaRao Pulibd030d02020-03-20 03:34:29 +0530281
282 // Send the HTTP request to the remote host
AppaRao Pulie38778a2022-06-27 23:09:03 +0000283 if (sslConn)
284 {
285 boost::beast::http::async_write(
286 *sslConn, req,
287 std::bind_front(&ConnectionInfo::afterWrite, this,
288 shared_from_this()));
289 }
290 else
291 {
292 boost::beast::http::async_write(
293 conn, req,
294 std::bind_front(&ConnectionInfo::afterWrite, this,
295 shared_from_this()));
296 }
297 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530298
AppaRao Pulie38778a2022-06-27 23:09:03 +0000299 void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/,
300 const boost::beast::error_code& ec, size_t bytesTransferred)
301 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000302 // The operation already timed out. We don't want do continue down
303 // this branch
304 if (ec && ec == boost::asio::error::operation_aborted)
305 {
306 return;
307 }
308
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800309 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000310 if (ec)
311 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700312 BMCWEB_LOG_ERROR("sendMessage() failed: {} {}", ec.message(), host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000313 state = ConnState::sendFailed;
314 waitAndRetry();
315 return;
316 }
Ed Tanous62598e32023-07-17 17:06:25 -0700317 BMCWEB_LOG_DEBUG("sendMessage() bytes transferred: {}",
318 bytesTransferred);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000319
320 recvMessage();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530321 }
322
323 void recvMessage()
324 {
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530325 state = ConnState::recvInProgress;
326
Ed Tanouse01d0c32023-06-30 13:21:32 -0700327 parser_type& thisParser = parser.emplace(std::piecewise_construct,
328 std::make_tuple());
Carson Labradod14a48f2023-02-22 00:24:54 +0000329
Ed Tanouse01d0c32023-06-30 13:21:32 -0700330 thisParser.body_limit(connPolicy->requestByteLimit);
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530331
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800332 timer.expires_after(std::chrono::seconds(30));
333 timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
334
AppaRao Pulibd030d02020-03-20 03:34:29 +0530335 // Receive the HTTP response
AppaRao Pulie38778a2022-06-27 23:09:03 +0000336 if (sslConn)
337 {
338 boost::beast::http::async_read(
Ed Tanouse01d0c32023-06-30 13:21:32 -0700339 *sslConn, buffer, thisParser,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000340 std::bind_front(&ConnectionInfo::afterRead, this,
341 shared_from_this()));
342 }
343 else
344 {
345 boost::beast::http::async_read(
Ed Tanouse01d0c32023-06-30 13:21:32 -0700346 conn, buffer, thisParser,
AppaRao Pulie38778a2022-06-27 23:09:03 +0000347 std::bind_front(&ConnectionInfo::afterRead, this,
348 shared_from_this()));
349 }
350 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530351
AppaRao Pulie38778a2022-06-27 23:09:03 +0000352 void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/,
353 const boost::beast::error_code& ec,
354 const std::size_t& bytesTransferred)
355 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000356 // The operation already timed out. We don't want do continue down
357 // this branch
358 if (ec && ec == boost::asio::error::operation_aborted)
359 {
360 return;
361 }
362
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800363 timer.cancel();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000364 if (ec && ec != boost::asio::ssl::error::stream_truncated)
365 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700366 BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}", ec.message(),
367 host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000368 state = ConnState::recvFailed;
369 waitAndRetry();
370 return;
371 }
Ed Tanous62598e32023-07-17 17:06:25 -0700372 BMCWEB_LOG_DEBUG("recvMessage() bytes transferred: {}",
373 bytesTransferred);
Ed Tanouse01d0c32023-06-30 13:21:32 -0700374 if (!parser)
375 {
376 return;
377 }
Ed Tanous52e31622024-01-23 16:31:11 -0800378 BMCWEB_LOG_DEBUG("recvMessage() data: {}", parser->get().body().str());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000379
380 unsigned int respCode = parser->get().result_int();
Ed Tanous62598e32023-07-17 17:06:25 -0700381 BMCWEB_LOG_DEBUG("recvMessage() Header Response Code: {}", respCode);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000382
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600383 // Handle the case of stream_truncated. Some servers close the ssl
384 // connection uncleanly, so check to see if we got a full response
385 // before we handle this as an error.
386 if (!parser->is_done())
387 {
388 state = ConnState::recvFailed;
389 waitAndRetry();
390 return;
391 }
392
AppaRao Pulie38778a2022-06-27 23:09:03 +0000393 // Make sure the received response code is valid as defined by
394 // the associated retry policy
Carson Labradod14a48f2023-02-22 00:24:54 +0000395 if (connPolicy->invalidResp(respCode))
AppaRao Pulie38778a2022-06-27 23:09:03 +0000396 {
397 // The listener failed to receive the Sent-Event
Ed Tanous62598e32023-07-17 17:06:25 -0700398 BMCWEB_LOG_ERROR(
399 "recvMessage() Listener Failed to "
Ed Tanousa716aa72023-08-01 11:35:53 -0700400 "receive Sent-Event. Header Response Code: {} from {}",
401 respCode, host);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000402 state = ConnState::recvFailed;
403 waitAndRetry();
404 return;
405 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700406
AppaRao Pulie38778a2022-06-27 23:09:03 +0000407 // Send is successful
408 // Reset the counter just in case this was after retrying
409 retryCount = 0;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530410
AppaRao Pulie38778a2022-06-27 23:09:03 +0000411 // Keep the connection alive if server supports it
412 // Else close the connection
Ed Tanous62598e32023-07-17 17:06:25 -0700413 BMCWEB_LOG_DEBUG("recvMessage() keepalive : {}", parser->keep_alive());
AppaRao Pulibd030d02020-03-20 03:34:29 +0530414
AppaRao Pulie38778a2022-06-27 23:09:03 +0000415 // Copy the response into a Response object so that it can be
416 // processed by the callback function.
Ed Tanous27b0cf92023-08-07 12:02:40 -0700417 res.response = parser->release();
AppaRao Pulie38778a2022-06-27 23:09:03 +0000418 callback(parser->keep_alive(), connId, res);
Carson Labrado513d1ff2022-07-19 00:38:15 +0000419 res.clear();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530420 }
421
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800422 static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800423 const boost::system::error_code& ec)
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800424 {
425 if (ec == boost::asio::error::operation_aborted)
426 {
Ed Tanous62598e32023-07-17 17:06:25 -0700427 BMCWEB_LOG_DEBUG(
428 "async_wait failed since the operation is aborted");
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800429 return;
430 }
431 if (ec)
432 {
Ed Tanous62598e32023-07-17 17:06:25 -0700433 BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
Ed Tanous27b0cf92023-08-07 12:02:40 -0700434 // If the timer fails, we need to close the socket anyway, same
435 // as if it expired.
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800436 }
437 std::shared_ptr<ConnectionInfo> self = weakSelf.lock();
438 if (self == nullptr)
439 {
440 return;
441 }
442 self->waitAndRetry();
443 }
444
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530445 void waitAndRetry()
AppaRao Pulibd030d02020-03-20 03:34:29 +0530446 {
Carson Labradod14a48f2023-02-22 00:24:54 +0000447 if ((retryCount >= connPolicy->maxRetryAttempts) ||
AppaRao Pulie38778a2022-06-27 23:09:03 +0000448 (state == ConnState::sslInitFailed))
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530449 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700450 BMCWEB_LOG_ERROR("Maximum number of retries reached. {}", host);
Ed Tanous62598e32023-07-17 17:06:25 -0700451 BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction);
Carson Labrado039a47e2022-04-05 16:03:20 +0000452
Carson Labradod14a48f2023-02-22 00:24:54 +0000453 if (connPolicy->retryPolicyAction == "TerminateAfterRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530454 {
455 // TODO: delete subscription
456 state = ConnState::terminated;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530457 }
Carson Labradod14a48f2023-02-22 00:24:54 +0000458 if (connPolicy->retryPolicyAction == "SuspendRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530459 {
460 state = ConnState::suspended;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530461 }
Carson Labrado513d1ff2022-07-19 00:38:15 +0000462
463 // We want to return a 502 to indicate there was an error with
464 // the external server
465 res.result(boost::beast::http::status::bad_gateway);
466 callback(false, connId, res);
467 res.clear();
468
Ed Tanous27b0cf92023-08-07 12:02:40 -0700469 // Reset the retrycount to zero so that client can try
470 // connecting again if needed
Ed Tanous3174e4d2020-10-07 11:41:22 -0700471 retryCount = 0;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530472 return;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530473 }
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530474
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530475 retryCount++;
476
Ed Tanous62598e32023-07-17 17:06:25 -0700477 BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700478 connPolicy->retryIntervalSecs.count(), retryCount);
Carson Labradod14a48f2023-02-22 00:24:54 +0000479 timer.expires_after(connPolicy->retryIntervalSecs);
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700480 timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this,
481 shared_from_this()));
482 }
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530483
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700484 void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/,
485 const boost::system::error_code& ec)
486 {
487 if (ec == boost::asio::error::operation_aborted)
488 {
Ed Tanous62598e32023-07-17 17:06:25 -0700489 BMCWEB_LOG_DEBUG(
490 "async_wait failed since the operation is aborted{}",
491 ec.message());
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700492 }
493 else if (ec)
494 {
Ed Tanous62598e32023-07-17 17:06:25 -0700495 BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message());
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700496 // Ignore the error and continue the retry loop to attempt
497 // sending the event as per the retry policy
498 }
499
500 // Let's close the connection and restart from resolve.
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600501 shutdownConn(true);
502 }
503
504 void restartConnection()
505 {
506 BMCWEB_LOG_DEBUG("{}, id: {} restartConnection", host,
507 std::to_string(connId));
508 initializeConnection(host.scheme() == "https");
509 doResolve();
Ayushi Smritife44eb02020-05-15 15:24:45 +0530510 }
511
AppaRao Pulie38778a2022-06-27 23:09:03 +0000512 void shutdownConn(bool retry)
Ayushi Smritife44eb02020-05-15 15:24:45 +0530513 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000514 boost::beast::error_code ec;
Ed Tanous0d5f5cf2022-03-12 15:30:55 -0800515 conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
Carson Labradof52c03c2022-03-23 18:50:15 +0000516 conn.close();
517
518 // not_connected happens sometimes so don't bother reporting it.
519 if (ec && ec != boost::beast::errc::not_connected)
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530520 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700521 BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
Ed Tanous62598e32023-07-17 17:06:25 -0700522 ec.message());
Carson Labradof52c03c2022-03-23 18:50:15 +0000523 }
Carson Labrado5cab68f2022-07-11 22:26:21 +0000524 else
525 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700526 BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
Carson Labrado5cab68f2022-07-11 22:26:21 +0000527 }
Ed Tanousca723762022-06-28 19:40:39 -0700528
Carson Labrado513d1ff2022-07-19 00:38:15 +0000529 if (retry)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000530 {
Carson Labrado513d1ff2022-07-19 00:38:15 +0000531 // Now let's try to resend the data
532 state = ConnState::retry;
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600533 restartConnection();
Carson Labrado513d1ff2022-07-19 00:38:15 +0000534 }
535 else
536 {
537 state = ConnState::closed;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000538 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000539 }
540
AppaRao Pulie38778a2022-06-27 23:09:03 +0000541 void doClose(bool retry = false)
Carson Labradof52c03c2022-03-23 18:50:15 +0000542 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000543 if (!sslConn)
544 {
545 shutdownConn(retry);
546 return;
547 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000548
AppaRao Pulie38778a2022-06-27 23:09:03 +0000549 sslConn->async_shutdown(
550 std::bind_front(&ConnectionInfo::afterSslShutdown, this,
551 shared_from_this(), retry));
552 }
553
554 void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/,
555 bool retry, const boost::system::error_code& ec)
556 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000557 if (ec)
Carson Labradof52c03c2022-03-23 18:50:15 +0000558 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700559 BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
Ed Tanous62598e32023-07-17 17:06:25 -0700560 ec.message());
Carson Labradof52c03c2022-03-23 18:50:15 +0000561 }
Carson Labrado5cab68f2022-07-11 22:26:21 +0000562 else
563 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700564 BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
Carson Labrado5cab68f2022-07-11 22:26:21 +0000565 }
AppaRao Pulie38778a2022-06-27 23:09:03 +0000566 shutdownConn(retry);
567 }
Ed Tanousca723762022-06-28 19:40:39 -0700568
AppaRao Pulie38778a2022-06-27 23:09:03 +0000569 void setCipherSuiteTLSext()
570 {
571 if (!sslConn)
572 {
573 return;
574 }
Ravi Tejae7c29912023-07-31 09:39:32 -0500575
576 if (host.host_type() != boost::urls::host_type::name)
577 {
578 // Avoid setting SNI hostname if its IP address
579 return;
580 }
581 // Create a null terminated string for SSL
Ed Tanousa716aa72023-08-01 11:35:53 -0700582 std::string hostname(host.encoded_host_address());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000583 // NOTE: The SSL_set_tlsext_host_name is defined in tlsv1.h header
584 // file but its having old style casting (name is cast to void*).
585 // Since bmcweb compiler treats all old-style-cast as error, its
586 // causing the build failure. So replaced the same macro inline and
587 // did corrected the code by doing static_cast to viod*. This has to
588 // be fixed in openssl library in long run. Set SNI Hostname (many
589 // hosts need this to handshake successfully)
590 if (SSL_ctrl(sslConn->native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME,
591 TLSEXT_NAMETYPE_host_name,
Ed Tanousa716aa72023-08-01 11:35:53 -0700592 static_cast<void*>(hostname.data())) == 0)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000593
594 {
595 boost::beast::error_code ec{static_cast<int>(::ERR_get_error()),
596 boost::asio::error::get_ssl_category()};
597
Ed Tanousa716aa72023-08-01 11:35:53 -0700598 BMCWEB_LOG_ERROR("SSL_set_tlsext_host_name {}, id: {} failed: {}",
599 host, connId, ec.message());
AppaRao Pulie38778a2022-06-27 23:09:03 +0000600 // Set state as sslInit failed so that we close the connection
601 // and take appropriate action as per retry configuration.
602 state = ConnState::sslInitFailed;
603 waitAndRetry();
604 return;
605 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530606 }
607
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600608 void initializeConnection(bool ssl)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000609 {
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600610 conn = boost::asio::ip::tcp::socket(ioc);
611 if (ssl)
AppaRao Pulie38778a2022-06-27 23:09:03 +0000612 {
613 std::optional<boost::asio::ssl::context> sslCtx =
614 ensuressl::getSSLClientContext();
615
616 if (!sslCtx)
617 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700618 BMCWEB_LOG_ERROR("prepareSSLContext failed - {}, id: {}", host,
619 connId);
AppaRao Pulie38778a2022-06-27 23:09:03 +0000620 // Don't retry if failure occurs while preparing SSL context
Ed Tanous27b0cf92023-08-07 12:02:40 -0700621 // such as certificate is invalid or set cipher failure or
622 // set host name failure etc... Setting conn state to
623 // sslInitFailed and connection state will be transitioned
624 // to next state depending on retry policy set by
625 // subscription.
AppaRao Pulie38778a2022-06-27 23:09:03 +0000626 state = ConnState::sslInitFailed;
627 waitAndRetry();
628 return;
629 }
630 sslConn.emplace(conn, *sslCtx);
631 setCipherSuiteTLSext();
632 }
633 }
Abhilash Rajuf3cb5df2023-11-30 03:54:11 -0600634
635 public:
636 explicit ConnectionInfo(
637 boost::asio::io_context& iocIn, const std::string& idIn,
638 const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
639 boost::urls::url_view hostIn, unsigned int connIdIn) :
640 subId(idIn),
641 connPolicy(connPolicyIn), host(hostIn), connId(connIdIn), ioc(iocIn),
642 resolver(iocIn), conn(iocIn), timer(iocIn)
643 {
644 initializeConnection(host.scheme() == "https");
645 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000646};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530647
Carson Labradof52c03c2022-03-23 18:50:15 +0000648class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
649{
650 private:
651 boost::asio::io_context& ioc;
AppaRao Pulie38778a2022-06-27 23:09:03 +0000652 std::string id;
Carson Labradod14a48f2023-02-22 00:24:54 +0000653 std::shared_ptr<ConnectionPolicy> connPolicy;
Ed Tanousa716aa72023-08-01 11:35:53 -0700654 boost::urls::url destIP;
Carson Labradof52c03c2022-03-23 18:50:15 +0000655 std::vector<std::shared_ptr<ConnectionInfo>> connections;
656 boost::container::devector<PendingRequest> requestQueue;
657
658 friend class HttpClient;
659
Carson Labrado244256c2022-04-27 17:16:32 +0000660 // Configure a connections's request, callback, and retry info in
661 // preparation to begin sending the request
Carson Labradof52c03c2022-03-23 18:50:15 +0000662 void setConnProps(ConnectionInfo& conn)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530663 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000664 if (requestQueue.empty())
AppaRao Pulibd030d02020-03-20 03:34:29 +0530665 {
Ed Tanous62598e32023-07-17 17:06:25 -0700666 BMCWEB_LOG_ERROR(
667 "setConnProps() should not have been called when requestQueue is empty");
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530668 return;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530669 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530670
Ed Tanous52e31622024-01-23 16:31:11 -0800671 PendingRequest& nextReq = requestQueue.front();
Carson Labrado244256c2022-04-27 17:16:32 +0000672 conn.req = std::move(nextReq.req);
673 conn.callback = std::move(nextReq.callback);
Carson Labradof52c03c2022-03-23 18:50:15 +0000674
Ed Tanousa716aa72023-08-01 11:35:53 -0700675 BMCWEB_LOG_DEBUG("Setting properties for connection {}, id: {}",
676 conn.host, conn.connId);
Carson Labradof52c03c2022-03-23 18:50:15 +0000677
678 // We can remove the request from the queue at this point
679 requestQueue.pop_front();
680 }
681
Carson Labradof52c03c2022-03-23 18:50:15 +0000682 // Gets called as part of callback after request is sent
683 // Reuses the connection if there are any requests waiting to be sent
684 // Otherwise closes the connection if it is not a keep-alive
685 void sendNext(bool keepAlive, uint32_t connId)
686 {
687 auto conn = connections[connId];
Carson Labrado46a81462022-04-27 21:11:37 +0000688
689 // Allow the connection's handler to be deleted
690 // This is needed because of Redfish Aggregation passing an
691 // AsyncResponse shared_ptr to this callback
692 conn->callback = nullptr;
693
Carson Labradof52c03c2022-03-23 18:50:15 +0000694 // Reuse the connection to send the next request in the queue
695 if (!requestQueue.empty())
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530696 {
Ed Tanous62598e32023-07-17 17:06:25 -0700697 BMCWEB_LOG_DEBUG(
Ed Tanous8ece0e42024-01-02 13:16:50 -0800698 "{} requests remaining in queue for {}, reusing connection {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700699 requestQueue.size(), destIP, connId);
Carson Labradof52c03c2022-03-23 18:50:15 +0000700
701 setConnProps(*conn);
702
703 if (keepAlive)
704 {
705 conn->sendMessage();
706 }
707 else
708 {
709 // Server is not keep-alive enabled so we need to close the
710 // connection and then start over from resolve
711 conn->doClose();
712 conn->doResolve();
713 }
714 return;
715 }
716
717 // No more messages to send so close the connection if necessary
718 if (keepAlive)
719 {
720 conn->state = ConnState::idle;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530721 }
722 else
723 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000724 // Abort the connection since server is not keep-alive enabled
725 conn->state = ConnState::abortConnection;
726 conn->doClose();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530727 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530728 }
729
Ed Tanousa716aa72023-08-01 11:35:53 -0700730 void sendData(std::string&& data, boost::urls::url_view destUri,
Carson Labrado244256c2022-04-27 17:16:32 +0000731 const boost::beast::http::fields& httpHeader,
732 const boost::beast::http::verb verb,
Ed Tanous6b3db602022-06-28 19:41:44 -0700733 const std::function<void(Response&)>& resHandler)
Ayushi Smritife44eb02020-05-15 15:24:45 +0530734 {
Carson Labrado244256c2022-04-27 17:16:32 +0000735 // Construct the request to be sent
Ed Tanous52e31622024-01-23 16:31:11 -0800736 boost::beast::http::request<bmcweb::FileBody> thisReq(
Ed Tanousa716aa72023-08-01 11:35:53 -0700737 verb, destUri.encoded_target(), 11, "", httpHeader);
738 thisReq.set(boost::beast::http::field::host,
739 destUri.encoded_host_address());
Carson Labrado244256c2022-04-27 17:16:32 +0000740 thisReq.keep_alive(true);
Ed Tanous52e31622024-01-23 16:31:11 -0800741 thisReq.body().str() = std::move(data);
Carson Labrado244256c2022-04-27 17:16:32 +0000742 thisReq.prepare_payload();
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700743 auto cb = std::bind_front(&ConnectionPool::afterSendData,
744 weak_from_this(), resHandler);
Carson Labradof52c03c2022-03-23 18:50:15 +0000745 // Reuse an existing connection if one is available
746 for (unsigned int i = 0; i < connections.size(); i++)
747 {
748 auto conn = connections[i];
749 if ((conn->state == ConnState::idle) ||
750 (conn->state == ConnState::initialized) ||
751 (conn->state == ConnState::closed))
752 {
Carson Labrado244256c2022-04-27 17:16:32 +0000753 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000754 conn->callback = std::move(cb);
Ed Tanousa716aa72023-08-01 11:35:53 -0700755 std::string commonMsg = std::format("{} from pool {}", i, id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000756
757 if (conn->state == ConnState::idle)
758 {
Ed Tanous62598e32023-07-17 17:06:25 -0700759 BMCWEB_LOG_DEBUG("Grabbing idle connection {}", commonMsg);
Carson Labradof52c03c2022-03-23 18:50:15 +0000760 conn->sendMessage();
761 }
762 else
763 {
Ed Tanous62598e32023-07-17 17:06:25 -0700764 BMCWEB_LOG_DEBUG("Reusing existing connection {}",
765 commonMsg);
Carson Labradof52c03c2022-03-23 18:50:15 +0000766 conn->doResolve();
767 }
768 return;
769 }
770 }
771
Ed Tanous27b0cf92023-08-07 12:02:40 -0700772 // All connections in use so create a new connection or add request
773 // to the queue
Carson Labradod14a48f2023-02-22 00:24:54 +0000774 if (connections.size() < connPolicy->maxConnections)
Carson Labradof52c03c2022-03-23 18:50:15 +0000775 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700776 BMCWEB_LOG_DEBUG("Adding new connection to pool {}", id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000777 auto conn = addConnection();
Carson Labrado244256c2022-04-27 17:16:32 +0000778 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000779 conn->callback = std::move(cb);
Carson Labradof52c03c2022-03-23 18:50:15 +0000780 conn->doResolve();
781 }
782 else if (requestQueue.size() < maxRequestQueueSize)
783 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700784 BMCWEB_LOG_DEBUG("Max pool size reached. Adding data to queue {}",
785 id);
Carson Labradod14a48f2023-02-22 00:24:54 +0000786 requestQueue.emplace_back(std::move(thisReq), std::move(cb));
Carson Labradof52c03c2022-03-23 18:50:15 +0000787 }
788 else
789 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700790 // If we can't buffer the request then we should let the
791 // callback handle a 429 Too Many Requests dummy response
Ed Tanous62598e32023-07-17 17:06:25 -0700792 BMCWEB_LOG_ERROR("{}:{} request queue full. Dropping request.",
Ed Tanousa716aa72023-08-01 11:35:53 -0700793 id);
Carson Labrado43e14d32022-11-09 00:25:20 +0000794 Response dummyRes;
795 dummyRes.result(boost::beast::http::status::too_many_requests);
796 resHandler(dummyRes);
Carson Labradof52c03c2022-03-23 18:50:15 +0000797 }
Ayushi Smritife44eb02020-05-15 15:24:45 +0530798 }
799
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700800 // Callback to be called once the request has been sent
801 static void afterSendData(const std::weak_ptr<ConnectionPool>& weakSelf,
802 const std::function<void(Response&)>& resHandler,
803 bool keepAlive, uint32_t connId, Response& res)
804 {
805 // Allow provided callback to perform additional processing of the
806 // request
807 resHandler(res);
808
809 // If requests remain in the queue then we want to reuse this
810 // connection to send the next request
811 std::shared_ptr<ConnectionPool> self = weakSelf.lock();
812 if (!self)
813 {
Ed Tanous62598e32023-07-17 17:06:25 -0700814 BMCWEB_LOG_CRITICAL("{} Failed to capture connection",
815 logPtr(self.get()));
Ed Tanous3d36e3a2022-08-19 15:54:04 -0700816 return;
817 }
818
819 self->sendNext(keepAlive, connId);
820 }
821
Carson Labradof52c03c2022-03-23 18:50:15 +0000822 std::shared_ptr<ConnectionInfo>& addConnection()
Ayushi Smritife44eb02020-05-15 15:24:45 +0530823 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000824 unsigned int newId = static_cast<unsigned int>(connections.size());
825
AppaRao Pulie38778a2022-06-27 23:09:03 +0000826 auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>(
Ed Tanousa716aa72023-08-01 11:35:53 -0700827 ioc, id, connPolicy, destIP, newId));
Carson Labradof52c03c2022-03-23 18:50:15 +0000828
Ed Tanousa716aa72023-08-01 11:35:53 -0700829 BMCWEB_LOG_DEBUG("Added connection {} to pool {}",
830 connections.size() - 1, id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000831
832 return ret;
833 }
834
835 public:
Carson Labradod14a48f2023-02-22 00:24:54 +0000836 explicit ConnectionPool(
837 boost::asio::io_context& iocIn, const std::string& idIn,
838 const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
Ed Tanousa716aa72023-08-01 11:35:53 -0700839 boost::urls::url_view destIPIn) :
Ed Tanous8a592812022-06-04 09:06:59 -0700840 ioc(iocIn),
Ed Tanousa716aa72023-08-01 11:35:53 -0700841 id(idIn), connPolicy(connPolicyIn), destIP(destIPIn)
Carson Labradof52c03c2022-03-23 18:50:15 +0000842 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700843 BMCWEB_LOG_DEBUG("Initializing connection pool for {}", id);
Carson Labradof52c03c2022-03-23 18:50:15 +0000844
845 // Initialize the pool with a single connection
846 addConnection();
Ayushi Smritife44eb02020-05-15 15:24:45 +0530847 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530848};
849
Carson Labradof52c03c2022-03-23 18:50:15 +0000850class HttpClient
851{
852 private:
853 std::unordered_map<std::string, std::shared_ptr<ConnectionPool>>
854 connectionPools;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700855 boost::asio::io_context& ioc;
Carson Labradod14a48f2023-02-22 00:24:54 +0000856 std::shared_ptr<ConnectionPolicy> connPolicy;
Carson Labradof52c03c2022-03-23 18:50:15 +0000857
Carson Labrado039a47e2022-04-05 16:03:20 +0000858 // Used as a dummy callback by sendData() in order to call
859 // sendDataWithCallback()
Ed Tanous02cad962022-06-30 16:50:15 -0700860 static void genericResHandler(const Response& res)
Carson Labrado039a47e2022-04-05 16:03:20 +0000861 {
Ed Tanous62598e32023-07-17 17:06:25 -0700862 BMCWEB_LOG_DEBUG("Response handled with return code: {}",
Ed Tanousa716aa72023-08-01 11:35:53 -0700863 res.resultInt());
Ed Tanous4ee8e212022-05-28 09:42:51 -0700864 }
Carson Labrado039a47e2022-04-05 16:03:20 +0000865
Carson Labradof52c03c2022-03-23 18:50:15 +0000866 public:
Carson Labradod14a48f2023-02-22 00:24:54 +0000867 HttpClient() = delete;
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700868 explicit HttpClient(boost::asio::io_context& iocIn,
869 const std::shared_ptr<ConnectionPolicy>& connPolicyIn) :
870 ioc(iocIn),
Carson Labradod14a48f2023-02-22 00:24:54 +0000871 connPolicy(connPolicyIn)
872 {}
Ed Tanousf8ca6d72022-06-28 12:12:03 -0700873
Carson Labradof52c03c2022-03-23 18:50:15 +0000874 HttpClient(const HttpClient&) = delete;
875 HttpClient& operator=(const HttpClient&) = delete;
876 HttpClient(HttpClient&&) = delete;
877 HttpClient& operator=(HttpClient&&) = delete;
878 ~HttpClient() = default;
879
Ed Tanousa716aa72023-08-01 11:35:53 -0700880 // Send a request to destIP where additional processing of the
Carson Labrado039a47e2022-04-05 16:03:20 +0000881 // result is not required
Ed Tanousa716aa72023-08-01 11:35:53 -0700882 void sendData(std::string&& data, boost::urls::url_view destUri,
Carson Labradof52c03c2022-03-23 18:50:15 +0000883 const boost::beast::http::fields& httpHeader,
Carson Labradod14a48f2023-02-22 00:24:54 +0000884 const boost::beast::http::verb verb)
Carson Labradof52c03c2022-03-23 18:50:15 +0000885 {
AppaRao Pulie38778a2022-06-27 23:09:03 +0000886 const std::function<void(Response&)> cb = genericResHandler;
Ed Tanousa716aa72023-08-01 11:35:53 -0700887 sendDataWithCallback(std::move(data), destUri, httpHeader, verb, cb);
Carson Labrado039a47e2022-04-05 16:03:20 +0000888 }
889
Ed Tanousa716aa72023-08-01 11:35:53 -0700890 // Send request to destIP and use the provided callback to
Carson Labrado039a47e2022-04-05 16:03:20 +0000891 // handle the response
Ed Tanousa716aa72023-08-01 11:35:53 -0700892 void sendDataWithCallback(std::string&& data, boost::urls::url_view destUrl,
Carson Labrado039a47e2022-04-05 16:03:20 +0000893 const boost::beast::http::fields& httpHeader,
Carson Labrado244256c2022-04-27 17:16:32 +0000894 const boost::beast::http::verb verb,
Ed Tanous6b3db602022-06-28 19:41:44 -0700895 const std::function<void(Response&)>& resHandler)
Carson Labrado039a47e2022-04-05 16:03:20 +0000896 {
Ed Tanousa716aa72023-08-01 11:35:53 -0700897 std::string clientKey = std::format("{}://{}", destUrl.scheme(),
898 destUrl.encoded_host_and_port());
Carson Labradod14a48f2023-02-22 00:24:54 +0000899 auto pool = connectionPools.try_emplace(clientKey);
900 if (pool.first->second == nullptr)
Carson Labradof52c03c2022-03-23 18:50:15 +0000901 {
Carson Labradod14a48f2023-02-22 00:24:54 +0000902 pool.first->second = std::make_shared<ConnectionPool>(
Ed Tanousa716aa72023-08-01 11:35:53 -0700903 ioc, clientKey, connPolicy, destUrl);
Carson Labradof52c03c2022-03-23 18:50:15 +0000904 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700905 // Send the data using either the existing connection pool or the
906 // newly created connection pool
Ed Tanousa716aa72023-08-01 11:35:53 -0700907 pool.first->second->sendData(std::move(data), destUrl, httpHeader, verb,
Carson Labradod14a48f2023-02-22 00:24:54 +0000908 resHandler);
Carson Labradof52c03c2022-03-23 18:50:15 +0000909 }
910};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530911} // namespace crow