| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors | 
|  | 3 | // SPDX-FileCopyrightText: Copyright 2020 Intel Corporation | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 4 | #pragma once | 
| Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 5 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "bmcweb_config.h" | 
|  | 7 |  | 
| Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 8 | #include "async_resolve.hpp" | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "boost_formatters.hpp" | 
| Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 10 | #include "http_body.hpp" | 
| Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 11 | #include "http_response.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 12 | #include "logging.hpp" | 
|  | 13 | #include "ssl_key_handler.hpp" | 
| Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 14 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 15 | #include <openssl/err.h> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 16 | #include <openssl/tls1.h> | 
|  | 17 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 18 | #include <boost/asio/connect.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 19 | #include <boost/asio/error.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 20 | #include <boost/asio/io_context.hpp> | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 21 | #include <boost/asio/ip/address.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 22 | #include <boost/asio/ip/tcp.hpp> | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 23 | #include <boost/asio/ssl/context.hpp> | 
|  | 24 | #include <boost/asio/ssl/error.hpp> | 
| Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 25 | #include <boost/asio/ssl/stream.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 26 | #include <boost/asio/ssl/stream_base.hpp> | 
| Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 27 | #include <boost/asio/steady_timer.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 28 | #include <boost/beast/core/error.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 29 | #include <boost/beast/core/flat_static_buffer.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 30 | #include <boost/beast/http/field.hpp> | 
|  | 31 | #include <boost/beast/http/fields.hpp> | 
| Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 32 | #include <boost/beast/http/message.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 33 | #include <boost/beast/http/parser.hpp> | 
|  | 34 | #include <boost/beast/http/read.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 35 | #include <boost/beast/http/status.hpp> | 
|  | 36 | #include <boost/beast/http/verb.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 37 | #include <boost/beast/http/write.hpp> | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 38 | #include <boost/container/devector.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 39 | #include <boost/optional/optional.hpp> | 
|  | 40 | #include <boost/system/errc.hpp> | 
| Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 41 | #include <boost/system/error_code.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 42 | #include <boost/url/host_type.hpp> | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 43 | #include <boost/url/url.hpp> | 
| Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 44 | #include <boost/url/url_view_base.hpp> | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 45 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 46 | #include <chrono> | 
|  | 47 | #include <cstdint> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 48 | #include <cstdlib> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 49 | #include <format> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 50 | #include <functional> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 51 | #include <memory> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 52 | #include <optional> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 53 | #include <string> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 54 | #include <string_view> | 
|  | 55 | #include <type_traits> | 
|  | 56 | #include <unordered_map> | 
|  | 57 | #include <utility> | 
|  | 58 | #include <vector> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 59 |  | 
|  | 60 | namespace crow | 
|  | 61 | { | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 62 | // With Redfish Aggregation it is assumed we will connect to another | 
|  | 63 | // instance of BMCWeb which can handle 100 simultaneous connections. | 
| Carson Labrado | 66d90c2 | 2022-12-07 22:34:33 +0000 | [diff] [blame] | 64 | constexpr size_t maxPoolSize = 20; | 
|  | 65 | constexpr size_t maxRequestQueueSize = 500; | 
| Carson Labrado | 17dcc31 | 2022-07-28 22:17:28 +0000 | [diff] [blame] | 66 | constexpr unsigned int httpReadBodyLimit = 131072; | 
| Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 67 | constexpr unsigned int httpReadBufferSize = 4096; | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 68 |  | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 69 | enum class ConnState | 
|  | 70 | { | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 71 | initialized, | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 72 | resolveInProgress, | 
|  | 73 | resolveFailed, | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 74 | connectInProgress, | 
|  | 75 | connectFailed, | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 76 | connected, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 77 | handshakeInProgress, | 
|  | 78 | handshakeFailed, | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 79 | sendInProgress, | 
|  | 80 | sendFailed, | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 81 | recvInProgress, | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 82 | recvFailed, | 
|  | 83 | idle, | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 84 | closed, | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 85 | suspended, | 
|  | 86 | terminated, | 
|  | 87 | abortConnection, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 88 | sslInitFailed, | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 89 | retry | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 90 | }; | 
|  | 91 |  | 
| Ed Tanous | 4ff0f1f | 2024-09-04 17:27:37 -0700 | [diff] [blame] | 92 | inline boost::system::error_code defaultRetryHandler(unsigned int respCode) | 
| Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 93 | { | 
|  | 94 | // As a default, assume 200X is alright | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 95 | BMCWEB_LOG_DEBUG("Using default check for response code validity"); | 
| Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 96 | if ((respCode < 200) || (respCode >= 300)) | 
|  | 97 | { | 
|  | 98 | return boost::system::errc::make_error_code( | 
|  | 99 | boost::system::errc::result_out_of_range); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | // Return 0 if the response code is valid | 
|  | 103 | return boost::system::errc::make_error_code(boost::system::errc::success); | 
|  | 104 | }; | 
|  | 105 |  | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 106 | // We need to allow retry information to be set before a message has been | 
|  | 107 | // sent and a connection pool has been created | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 108 | struct ConnectionPolicy | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 109 | { | 
|  | 110 | uint32_t maxRetryAttempts = 5; | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 111 |  | 
|  | 112 | // the max size of requests in bytes.  0 for unlimited | 
|  | 113 | boost::optional<uint64_t> requestByteLimit = httpReadBodyLimit; | 
|  | 114 |  | 
|  | 115 | size_t maxConnections = 1; | 
|  | 116 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 117 | std::string retryPolicyAction = "TerminateAfterRetries"; | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 118 |  | 
|  | 119 | std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0); | 
| Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 120 | std::function<boost::system::error_code(unsigned int respCode)> | 
|  | 121 | invalidResp = defaultRetryHandler; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 122 | }; | 
|  | 123 |  | 
|  | 124 | struct PendingRequest | 
|  | 125 | { | 
| Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 126 | boost::beast::http::request<bmcweb::HttpBody> req; | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 127 | std::function<void(bool, uint32_t, Response&)> callback; | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 128 | PendingRequest( | 
| Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 129 | boost::beast::http::request<bmcweb::HttpBody>&& reqIn, | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 130 | const std::function<void(bool, uint32_t, Response&)>& callbackIn) : | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 131 | req(std::move(reqIn)), callback(callbackIn) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 132 | {} | 
|  | 133 | }; | 
|  | 134 |  | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 135 | namespace http = boost::beast::http; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 136 | class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo> | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 137 | { | 
|  | 138 | private: | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 139 | ConnState state = ConnState::initialized; | 
|  | 140 | uint32_t retryCount = 0; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 141 | std::string subId; | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 142 | std::shared_ptr<ConnectionPolicy> connPolicy; | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 143 | boost::urls::url host; | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 144 | ensuressl::VerifyCertificate verifyCert; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 145 | uint32_t connId; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 146 | // Data buffers | 
| Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 147 | http::request<bmcweb::HttpBody> req; | 
|  | 148 | using parser_type = http::response_parser<bmcweb::HttpBody>; | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 149 | std::optional<parser_type> parser; | 
| Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 150 | boost::beast::flat_static_buffer<httpReadBufferSize> buffer; | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 151 | Response res; | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 152 |  | 
| Ed Tanous | d0fd3e5 | 2025-06-10 09:19:33 -0700 | [diff] [blame] | 153 | // Async callables | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 154 | std::function<void(bool, uint32_t, Response&)> callback; | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 155 |  | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 156 | boost::asio::io_context& ioc; | 
|  | 157 |  | 
| Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 158 | using Resolver = std::conditional_t<BMCWEB_DNS_RESOLVER == "systemd-dbus", | 
|  | 159 | async_resolve::Resolver, | 
|  | 160 | boost::asio::ip::tcp::resolver>; | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 161 | Resolver resolver; | 
|  | 162 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 163 | boost::asio::ip::tcp::socket conn; | 
| Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 164 | std::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>> | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 165 | sslConn; | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 166 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 167 | boost::asio::steady_timer timer; | 
| Ed Tanous | 84b3560 | 2021-09-08 20:06:32 -0700 | [diff] [blame] | 168 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 169 | friend class ConnectionPool; | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 170 |  | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 171 | void doResolve() | 
|  | 172 | { | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 173 | state = ConnState::resolveInProgress; | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 174 | BMCWEB_LOG_DEBUG("Trying to resolve: {}, id: {}", host, connId); | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 175 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 176 | resolver.async_resolve(host.encoded_host_address(), host.port(), | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 177 | std::bind_front(&ConnectionInfo::afterResolve, | 
|  | 178 | this, shared_from_this())); | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 181 | void afterResolve(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
|  | 182 | const boost::system::error_code& ec, | 
|  | 183 | const Resolver::results_type& endpointList) | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 184 | { | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 185 | if (ec || (endpointList.empty())) | 
|  | 186 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 187 | BMCWEB_LOG_ERROR("Resolve failed: {} {}", ec.message(), host); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 188 | state = ConnState::resolveFailed; | 
|  | 189 | waitAndRetry(); | 
|  | 190 | return; | 
|  | 191 | } | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 192 | BMCWEB_LOG_DEBUG("Resolved {}, id: {}", host, connId); | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 193 | state = ConnState::connectInProgress; | 
|  | 194 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 195 | BMCWEB_LOG_DEBUG("Trying to connect to: {}, id: {}", host, connId); | 
| Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 196 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 197 | timer.expires_after(std::chrono::seconds(30)); | 
|  | 198 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); | 
|  | 199 |  | 
|  | 200 | boost::asio::async_connect( | 
|  | 201 | conn, endpointList, | 
|  | 202 | std::bind_front(&ConnectionInfo::afterConnect, this, | 
|  | 203 | shared_from_this())); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
| Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 207 | const boost::beast::error_code& ec, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 208 | const boost::asio::ip::tcp::endpoint& endpoint) | 
|  | 209 | { | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 210 | // The operation already timed out.  We don't want do continue down | 
|  | 211 | // this branch | 
|  | 212 | if (ec && ec == boost::asio::error::operation_aborted) | 
|  | 213 | { | 
|  | 214 | return; | 
|  | 215 | } | 
|  | 216 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 217 | timer.cancel(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 218 | if (ec) | 
|  | 219 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 220 | BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}", | 
| abhilashraju | 079fbcf | 2025-04-24 05:24:56 +0000 | [diff] [blame] | 221 | host.encoded_host_address(), host.port(), connId, | 
|  | 222 | ec.message()); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 223 | state = ConnState::connectFailed; | 
|  | 224 | waitAndRetry(); | 
|  | 225 | return; | 
|  | 226 | } | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 227 | BMCWEB_LOG_DEBUG("Connected to: {}:{}, id: {}", | 
|  | 228 | endpoint.address().to_string(), endpoint.port(), | 
|  | 229 | connId); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 230 | if (sslConn) | 
|  | 231 | { | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 232 | doSslHandshake(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 233 | return; | 
|  | 234 | } | 
|  | 235 | state = ConnState::connected; | 
|  | 236 | sendMessage(); | 
|  | 237 | } | 
|  | 238 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 239 | void doSslHandshake() | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 240 | { | 
|  | 241 | if (!sslConn) | 
|  | 242 | { | 
|  | 243 | return; | 
|  | 244 | } | 
| Ed Tanous | 3d15864 | 2025-05-12 14:20:49 -0700 | [diff] [blame] | 245 | auto& ssl = *sslConn; | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 246 | state = ConnState::handshakeInProgress; | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 247 | timer.expires_after(std::chrono::seconds(30)); | 
|  | 248 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); | 
| Ed Tanous | 3d15864 | 2025-05-12 14:20:49 -0700 | [diff] [blame] | 249 | ssl.async_handshake(boost::asio::ssl::stream_base::client, | 
|  | 250 | std::bind_front(&ConnectionInfo::afterSslHandshake, | 
|  | 251 | this, shared_from_this())); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
| Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 255 | const boost::beast::error_code& ec) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 256 | { | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 257 | // The operation already timed out.  We don't want do continue down | 
|  | 258 | // this branch | 
|  | 259 | if (ec && ec == boost::asio::error::operation_aborted) | 
|  | 260 | { | 
|  | 261 | return; | 
|  | 262 | } | 
|  | 263 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 264 | timer.cancel(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 265 | if (ec) | 
|  | 266 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 267 | BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}", connId, | 
|  | 268 | ec.message()); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 269 | state = ConnState::handshakeFailed; | 
|  | 270 | waitAndRetry(); | 
|  | 271 | return; | 
|  | 272 | } | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 273 | BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}", connId); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 274 | state = ConnState::connected; | 
|  | 275 | sendMessage(); | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 278 | void sendMessage() | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 279 | { | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 280 | state = ConnState::sendInProgress; | 
|  | 281 |  | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 282 | // Set a timeout on the operation | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 283 | timer.expires_after(std::chrono::seconds(30)); | 
|  | 284 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 285 | // Send the HTTP request to the remote host | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 286 | if (sslConn) | 
|  | 287 | { | 
| Ed Tanous | cd504a9 | 2024-08-19 11:46:20 -0700 | [diff] [blame] | 288 | boost::beast::http::async_write( | 
|  | 289 | *sslConn, req, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 290 | std::bind_front(&ConnectionInfo::afterWrite, this, | 
|  | 291 | shared_from_this())); | 
|  | 292 | } | 
|  | 293 | else | 
|  | 294 | { | 
| Ed Tanous | cd504a9 | 2024-08-19 11:46:20 -0700 | [diff] [blame] | 295 | boost::beast::http::async_write( | 
|  | 296 | conn, req, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 297 | std::bind_front(&ConnectionInfo::afterWrite, this, | 
|  | 298 | shared_from_this())); | 
|  | 299 | } | 
|  | 300 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 301 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 302 | void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
|  | 303 | const boost::beast::error_code& ec, size_t bytesTransferred) | 
|  | 304 | { | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 305 | // The operation already timed out.  We don't want do continue down | 
|  | 306 | // this branch | 
|  | 307 | if (ec && ec == boost::asio::error::operation_aborted) | 
|  | 308 | { | 
|  | 309 | return; | 
|  | 310 | } | 
|  | 311 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 312 | timer.cancel(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 313 | if (ec) | 
|  | 314 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 315 | BMCWEB_LOG_ERROR("sendMessage() failed: {} {}", ec.message(), host); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 316 | state = ConnState::sendFailed; | 
|  | 317 | waitAndRetry(); | 
|  | 318 | return; | 
|  | 319 | } | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 320 | BMCWEB_LOG_DEBUG("sendMessage() bytes transferred: {}", | 
|  | 321 | bytesTransferred); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | recvMessage(); | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | void recvMessage() | 
|  | 327 | { | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 328 | state = ConnState::recvInProgress; | 
|  | 329 |  | 
| Ed Tanous | 38afdb9 | 2024-12-11 23:57:53 -0800 | [diff] [blame] | 330 | parser_type& thisParser = parser.emplace(); | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 331 |  | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 332 | thisParser.body_limit(connPolicy->requestByteLimit); | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 333 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 334 | timer.expires_after(std::chrono::seconds(30)); | 
|  | 335 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); | 
|  | 336 |  | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 337 | // Receive the HTTP response | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 338 | if (sslConn) | 
|  | 339 | { | 
|  | 340 | boost::beast::http::async_read( | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 341 | *sslConn, buffer, thisParser, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 342 | std::bind_front(&ConnectionInfo::afterRead, this, | 
|  | 343 | shared_from_this())); | 
|  | 344 | } | 
|  | 345 | else | 
|  | 346 | { | 
|  | 347 | boost::beast::http::async_read( | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 348 | conn, buffer, thisParser, | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 349 | std::bind_front(&ConnectionInfo::afterRead, this, | 
|  | 350 | shared_from_this())); | 
|  | 351 | } | 
|  | 352 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 353 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 354 | void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
|  | 355 | const boost::beast::error_code& ec, | 
| Ed Tanous | d0fd3e5 | 2025-06-10 09:19:33 -0700 | [diff] [blame] | 356 | const std::size_t bytesTransferred) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 357 | { | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 358 | // The operation already timed out.  We don't want do continue down | 
|  | 359 | // this branch | 
|  | 360 | if (ec && ec == boost::asio::error::operation_aborted) | 
|  | 361 | { | 
|  | 362 | return; | 
|  | 363 | } | 
|  | 364 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 365 | timer.cancel(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 366 | if (ec && ec != boost::asio::ssl::error::stream_truncated) | 
|  | 367 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 368 | BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}", ec.message(), | 
|  | 369 | host); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 370 | state = ConnState::recvFailed; | 
|  | 371 | waitAndRetry(); | 
|  | 372 | return; | 
|  | 373 | } | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 374 | BMCWEB_LOG_DEBUG("recvMessage() bytes transferred: {}", | 
|  | 375 | bytesTransferred); | 
| Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 376 | if (!parser) | 
|  | 377 | { | 
|  | 378 | return; | 
|  | 379 | } | 
| Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 380 | BMCWEB_LOG_DEBUG("recvMessage() data: {}", parser->get().body().str()); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 381 |  | 
|  | 382 | unsigned int respCode = parser->get().result_int(); | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 383 | BMCWEB_LOG_DEBUG("recvMessage() Header Response Code: {}", respCode); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 384 |  | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 385 | // Handle the case of stream_truncated.  Some servers close the ssl | 
|  | 386 | // connection uncleanly, so check to see if we got a full response | 
|  | 387 | // before we handle this as an error. | 
|  | 388 | if (!parser->is_done()) | 
|  | 389 | { | 
|  | 390 | state = ConnState::recvFailed; | 
|  | 391 | waitAndRetry(); | 
|  | 392 | return; | 
|  | 393 | } | 
|  | 394 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 395 | // Make sure the received response code is valid as defined by | 
|  | 396 | // the associated retry policy | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 397 | if (connPolicy->invalidResp(respCode)) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 398 | { | 
|  | 399 | // The listener failed to receive the Sent-Event | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 400 | BMCWEB_LOG_ERROR( | 
|  | 401 | "recvMessage() Listener Failed to " | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 402 | "receive Sent-Event. Header Response Code: {} from {}", | 
|  | 403 | respCode, host); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 404 | state = ConnState::recvFailed; | 
|  | 405 | waitAndRetry(); | 
|  | 406 | return; | 
|  | 407 | } | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 408 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 409 | // Send is successful | 
|  | 410 | // Reset the counter just in case this was after retrying | 
|  | 411 | retryCount = 0; | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 412 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 413 | // Keep the connection alive if server supports it | 
|  | 414 | // Else close the connection | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 415 | BMCWEB_LOG_DEBUG("recvMessage() keepalive : {}", parser->keep_alive()); | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 416 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 417 | // Copy the response into a Response object so that it can be | 
|  | 418 | // processed by the callback function. | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 419 | res.response = parser->release(); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 420 | callback(parser->keep_alive(), connId, res); | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 421 | res.clear(); | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 422 | } | 
|  | 423 |  | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 424 | static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf, | 
| Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 425 | const boost::system::error_code& ec) | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 426 | { | 
|  | 427 | if (ec == boost::asio::error::operation_aborted) | 
|  | 428 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 429 | BMCWEB_LOG_DEBUG( | 
|  | 430 | "async_wait failed since the operation is aborted"); | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 431 | return; | 
|  | 432 | } | 
|  | 433 | if (ec) | 
|  | 434 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 435 | BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message()); | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 436 | // If the timer fails, we need to close the socket anyway, same | 
|  | 437 | // as if it expired. | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 438 | } | 
|  | 439 | std::shared_ptr<ConnectionInfo> self = weakSelf.lock(); | 
|  | 440 | if (self == nullptr) | 
|  | 441 | { | 
|  | 442 | return; | 
|  | 443 | } | 
|  | 444 | self->waitAndRetry(); | 
|  | 445 | } | 
|  | 446 |  | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 447 | void waitAndRetry() | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 448 | { | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 449 | if ((retryCount >= connPolicy->maxRetryAttempts) || | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 450 | (state == ConnState::sslInitFailed)) | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 451 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 452 | BMCWEB_LOG_ERROR("Maximum number of retries reached. {}", host); | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 453 | BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction); | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 454 |  | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 455 | if (connPolicy->retryPolicyAction == "TerminateAfterRetries") | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 456 | { | 
|  | 457 | // TODO: delete subscription | 
|  | 458 | state = ConnState::terminated; | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 459 | } | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 460 | if (connPolicy->retryPolicyAction == "SuspendRetries") | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 461 | { | 
|  | 462 | state = ConnState::suspended; | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 463 | } | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 464 |  | 
|  | 465 | // We want to return a 502 to indicate there was an error with | 
|  | 466 | // the external server | 
|  | 467 | res.result(boost::beast::http::status::bad_gateway); | 
|  | 468 | callback(false, connId, res); | 
|  | 469 | res.clear(); | 
|  | 470 |  | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 471 | // Reset the retrycount to zero so that client can try | 
|  | 472 | // connecting again if needed | 
| Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 473 | retryCount = 0; | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 474 | return; | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 475 | } | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 476 |  | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 477 | retryCount++; | 
|  | 478 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 479 | BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}", | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 480 | connPolicy->retryIntervalSecs.count(), retryCount); | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 481 | timer.expires_after(connPolicy->retryIntervalSecs); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 482 | timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this, | 
|  | 483 | shared_from_this())); | 
|  | 484 | } | 
| Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 485 |  | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 486 | void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
|  | 487 | const boost::system::error_code& ec) | 
|  | 488 | { | 
|  | 489 | if (ec == boost::asio::error::operation_aborted) | 
|  | 490 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 491 | BMCWEB_LOG_DEBUG( | 
|  | 492 | "async_wait failed since the operation is aborted{}", | 
|  | 493 | ec.message()); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 494 | } | 
|  | 495 | else if (ec) | 
|  | 496 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 497 | BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message()); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 498 | // Ignore the error and continue the retry loop to attempt | 
|  | 499 | // sending the event as per the retry policy | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | // Let's close the connection and restart from resolve. | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 503 | shutdownConn(true); | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | void restartConnection() | 
|  | 507 | { | 
|  | 508 | BMCWEB_LOG_DEBUG("{}, id: {}  restartConnection", host, | 
|  | 509 | std::to_string(connId)); | 
|  | 510 | initializeConnection(host.scheme() == "https"); | 
|  | 511 | doResolve(); | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 512 | } | 
|  | 513 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 514 | void shutdownConn(bool retry) | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 515 | { | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 516 | boost::beast::error_code ec; | 
| Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 517 | conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 518 | conn.close(); | 
|  | 519 |  | 
|  | 520 | // not_connected happens sometimes so don't bother reporting it. | 
|  | 521 | if (ec && ec != boost::beast::errc::not_connected) | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 522 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 523 | BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId, | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 524 | ec.message()); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 525 | } | 
| Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 526 | else | 
|  | 527 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 528 | BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId); | 
| Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 529 | } | 
| Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 530 |  | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 531 | if (retry) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 532 | { | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 533 | // Now let's try to resend the data | 
|  | 534 | state = ConnState::retry; | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 535 | restartConnection(); | 
| Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 536 | } | 
|  | 537 | else | 
|  | 538 | { | 
|  | 539 | state = ConnState::closed; | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 540 | } | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 541 | } | 
|  | 542 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 543 | void doClose(bool retry = false) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 544 | { | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 545 | if (!sslConn) | 
|  | 546 | { | 
|  | 547 | shutdownConn(retry); | 
|  | 548 | return; | 
|  | 549 | } | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 550 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 551 | sslConn->async_shutdown( | 
|  | 552 | std::bind_front(&ConnectionInfo::afterSslShutdown, this, | 
|  | 553 | shared_from_this(), retry)); | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/, | 
|  | 557 | bool retry, const boost::system::error_code& ec) | 
|  | 558 | { | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 559 | if (ec) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 560 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 561 | BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId, | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 562 | ec.message()); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 563 | } | 
| Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 564 | else | 
|  | 565 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 566 | BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId); | 
| Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 567 | } | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 568 | shutdownConn(retry); | 
|  | 569 | } | 
| Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 570 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 571 | void setCipherSuiteTLSext() | 
|  | 572 | { | 
|  | 573 | if (!sslConn) | 
|  | 574 | { | 
|  | 575 | return; | 
|  | 576 | } | 
| Ravi Teja | e7c2991 | 2023-07-31 09:39:32 -0500 | [diff] [blame] | 577 |  | 
|  | 578 | if (host.host_type() != boost::urls::host_type::name) | 
|  | 579 | { | 
|  | 580 | // Avoid setting SNI hostname if its IP address | 
|  | 581 | return; | 
|  | 582 | } | 
|  | 583 | // Create a null terminated string for SSL | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 584 | std::string hostname(host.encoded_host_address()); | 
| Ed Tanous | d0fd3e5 | 2025-06-10 09:19:33 -0700 | [diff] [blame] | 585 | if (SSL_set_tlsext_host_name(sslConn->native_handle(), | 
|  | 586 | hostname.data()) == 0) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 587 |  | 
|  | 588 | { | 
|  | 589 | boost::beast::error_code ec{static_cast<int>(::ERR_get_error()), | 
|  | 590 | boost::asio::error::get_ssl_category()}; | 
|  | 591 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 592 | BMCWEB_LOG_ERROR("SSL_set_tlsext_host_name {}, id: {} failed: {}", | 
|  | 593 | host, connId, ec.message()); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 594 | // Set state as sslInit failed so that we close the connection | 
|  | 595 | // and take appropriate action as per retry configuration. | 
|  | 596 | state = ConnState::sslInitFailed; | 
|  | 597 | waitAndRetry(); | 
|  | 598 | return; | 
|  | 599 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 600 | } | 
|  | 601 |  | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 602 | void initializeConnection(bool ssl) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 603 | { | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 604 | conn = boost::asio::ip::tcp::socket(ioc); | 
|  | 605 | if (ssl) | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 606 | { | 
|  | 607 | std::optional<boost::asio::ssl::context> sslCtx = | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 608 | ensuressl::getSSLClientContext(verifyCert); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 609 |  | 
|  | 610 | if (!sslCtx) | 
|  | 611 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 612 | BMCWEB_LOG_ERROR("prepareSSLContext failed - {}, id: {}", host, | 
|  | 613 | connId); | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 614 | // Don't retry if failure occurs while preparing SSL context | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 615 | // such as certificate is invalid or set cipher failure or | 
|  | 616 | // set host name failure etc... Setting conn state to | 
|  | 617 | // sslInitFailed and connection state will be transitioned | 
|  | 618 | // to next state depending on retry policy set by | 
|  | 619 | // subscription. | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 620 | state = ConnState::sslInitFailed; | 
|  | 621 | waitAndRetry(); | 
|  | 622 | return; | 
|  | 623 | } | 
|  | 624 | sslConn.emplace(conn, *sslCtx); | 
|  | 625 | setCipherSuiteTLSext(); | 
|  | 626 | } | 
|  | 627 | } | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 628 |  | 
|  | 629 | public: | 
|  | 630 | explicit ConnectionInfo( | 
|  | 631 | boost::asio::io_context& iocIn, const std::string& idIn, | 
|  | 632 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn, | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 633 | const boost::urls::url_view_base& hostIn, | 
|  | 634 | ensuressl::VerifyCertificate verifyCertIn, unsigned int connIdIn) : | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 635 | subId(idIn), connPolicy(connPolicyIn), host(hostIn), | 
|  | 636 | verifyCert(verifyCertIn), connId(connIdIn), ioc(iocIn), resolver(iocIn), | 
|  | 637 | conn(iocIn), timer(iocIn) | 
| Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 638 | { | 
|  | 639 | initializeConnection(host.scheme() == "https"); | 
|  | 640 | } | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 641 | }; | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 642 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 643 | class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> | 
|  | 644 | { | 
|  | 645 | private: | 
|  | 646 | boost::asio::io_context& ioc; | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 647 | std::string id; | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 648 | std::shared_ptr<ConnectionPolicy> connPolicy; | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 649 | boost::urls::url destIP; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 650 | std::vector<std::shared_ptr<ConnectionInfo>> connections; | 
|  | 651 | boost::container::devector<PendingRequest> requestQueue; | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 652 | ensuressl::VerifyCertificate verifyCert; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 653 |  | 
|  | 654 | friend class HttpClient; | 
|  | 655 |  | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 656 | // Configure a connections's request, callback, and retry info in | 
|  | 657 | // preparation to begin sending the request | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 658 | void setConnProps(ConnectionInfo& conn) | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 659 | { | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 660 | if (requestQueue.empty()) | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 661 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 662 | BMCWEB_LOG_ERROR( | 
|  | 663 | "setConnProps() should not have been called when requestQueue is empty"); | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 664 | return; | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 665 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 666 |  | 
| Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 667 | PendingRequest& nextReq = requestQueue.front(); | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 668 | conn.req = std::move(nextReq.req); | 
|  | 669 | conn.callback = std::move(nextReq.callback); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 670 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 671 | BMCWEB_LOG_DEBUG("Setting properties for connection {}, id: {}", | 
|  | 672 | conn.host, conn.connId); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 673 |  | 
|  | 674 | // We can remove the request from the queue at this point | 
|  | 675 | requestQueue.pop_front(); | 
|  | 676 | } | 
|  | 677 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 678 | // Gets called as part of callback after request is sent | 
|  | 679 | // Reuses the connection if there are any requests waiting to be sent | 
|  | 680 | // Otherwise closes the connection if it is not a keep-alive | 
|  | 681 | void sendNext(bool keepAlive, uint32_t connId) | 
|  | 682 | { | 
|  | 683 | auto conn = connections[connId]; | 
| Carson Labrado | 46a8146 | 2022-04-27 21:11:37 +0000 | [diff] [blame] | 684 |  | 
|  | 685 | // Allow the connection's handler to be deleted | 
|  | 686 | // This is needed because of Redfish Aggregation passing an | 
|  | 687 | // AsyncResponse shared_ptr to this callback | 
|  | 688 | conn->callback = nullptr; | 
|  | 689 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 690 | // Reuse the connection to send the next request in the queue | 
|  | 691 | if (!requestQueue.empty()) | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 692 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 693 | BMCWEB_LOG_DEBUG( | 
| Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 694 | "{} requests remaining in queue for {}, reusing connection {}", | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 695 | requestQueue.size(), destIP, connId); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 696 |  | 
|  | 697 | setConnProps(*conn); | 
|  | 698 |  | 
|  | 699 | if (keepAlive) | 
|  | 700 | { | 
|  | 701 | conn->sendMessage(); | 
|  | 702 | } | 
|  | 703 | else | 
|  | 704 | { | 
|  | 705 | // Server is not keep-alive enabled so we need to close the | 
|  | 706 | // connection and then start over from resolve | 
|  | 707 | conn->doClose(); | 
| Abhilash Raju | 2ecde74 | 2024-06-01 02:01:01 -0500 | [diff] [blame] | 708 | conn->restartConnection(); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 709 | } | 
|  | 710 | return; | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | // No more messages to send so close the connection if necessary | 
|  | 714 | if (keepAlive) | 
|  | 715 | { | 
|  | 716 | conn->state = ConnState::idle; | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 717 | } | 
|  | 718 | else | 
|  | 719 | { | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 720 | // Abort the connection since server is not keep-alive enabled | 
|  | 721 | conn->state = ConnState::abortConnection; | 
|  | 722 | conn->doClose(); | 
| AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 723 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 724 | } | 
|  | 725 |  | 
| Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 726 | void sendData(std::string&& data, const boost::urls::url_view_base& destUri, | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 727 | const boost::beast::http::fields& httpHeader, | 
|  | 728 | const boost::beast::http::verb verb, | 
| Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 729 | const std::function<void(Response&)>& resHandler) | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 730 | { | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 731 | // Construct the request to be sent | 
| Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 732 | boost::beast::http::request<bmcweb::HttpBody> thisReq( | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 733 | verb, destUri.encoded_target(), 11, "", httpHeader); | 
|  | 734 | thisReq.set(boost::beast::http::field::host, | 
|  | 735 | destUri.encoded_host_address()); | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 736 | thisReq.keep_alive(true); | 
| Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 737 | thisReq.body().str() = std::move(data); | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 738 | thisReq.prepare_payload(); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 739 | auto cb = std::bind_front(&ConnectionPool::afterSendData, | 
|  | 740 | weak_from_this(), resHandler); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 741 | // Reuse an existing connection if one is available | 
|  | 742 | for (unsigned int i = 0; i < connections.size(); i++) | 
|  | 743 | { | 
|  | 744 | auto conn = connections[i]; | 
|  | 745 | if ((conn->state == ConnState::idle) || | 
|  | 746 | (conn->state == ConnState::initialized) || | 
|  | 747 | (conn->state == ConnState::closed)) | 
|  | 748 | { | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 749 | conn->req = std::move(thisReq); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 750 | conn->callback = std::move(cb); | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 751 | std::string commonMsg = std::format("{} from pool {}", i, id); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 752 |  | 
|  | 753 | if (conn->state == ConnState::idle) | 
|  | 754 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 755 | BMCWEB_LOG_DEBUG("Grabbing idle connection {}", commonMsg); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 756 | conn->sendMessage(); | 
|  | 757 | } | 
|  | 758 | else | 
|  | 759 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 760 | BMCWEB_LOG_DEBUG("Reusing existing connection {}", | 
|  | 761 | commonMsg); | 
| Abhilash Raju | 2ecde74 | 2024-06-01 02:01:01 -0500 | [diff] [blame] | 762 | conn->restartConnection(); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 763 | } | 
|  | 764 | return; | 
|  | 765 | } | 
|  | 766 | } | 
|  | 767 |  | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 768 | // All connections in use so create a new connection or add request | 
|  | 769 | // to the queue | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 770 | if (connections.size() < connPolicy->maxConnections) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 771 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 772 | BMCWEB_LOG_DEBUG("Adding new connection to pool {}", id); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 773 | auto conn = addConnection(); | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 774 | conn->req = std::move(thisReq); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 775 | conn->callback = std::move(cb); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 776 | conn->doResolve(); | 
|  | 777 | } | 
|  | 778 | else if (requestQueue.size() < maxRequestQueueSize) | 
|  | 779 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 780 | BMCWEB_LOG_DEBUG("Max pool size reached. Adding data to queue {}", | 
|  | 781 | id); | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 782 | requestQueue.emplace_back(std::move(thisReq), std::move(cb)); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 783 | } | 
|  | 784 | else | 
|  | 785 | { | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 786 | // If we can't buffer the request then we should let the | 
|  | 787 | // callback handle a 429 Too Many Requests dummy response | 
| Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 788 | BMCWEB_LOG_ERROR("{} request queue full.  Dropping request.", id); | 
| Carson Labrado | 43e14d3 | 2022-11-09 00:25:20 +0000 | [diff] [blame] | 789 | Response dummyRes; | 
|  | 790 | dummyRes.result(boost::beast::http::status::too_many_requests); | 
|  | 791 | resHandler(dummyRes); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 792 | } | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 793 | } | 
|  | 794 |  | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 795 | // Callback to be called once the request has been sent | 
|  | 796 | static void afterSendData(const std::weak_ptr<ConnectionPool>& weakSelf, | 
|  | 797 | const std::function<void(Response&)>& resHandler, | 
|  | 798 | bool keepAlive, uint32_t connId, Response& res) | 
|  | 799 | { | 
|  | 800 | // Allow provided callback to perform additional processing of the | 
|  | 801 | // request | 
|  | 802 | resHandler(res); | 
|  | 803 |  | 
|  | 804 | // If requests remain in the queue then we want to reuse this | 
|  | 805 | // connection to send the next request | 
|  | 806 | std::shared_ptr<ConnectionPool> self = weakSelf.lock(); | 
|  | 807 | if (!self) | 
|  | 808 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 809 | BMCWEB_LOG_CRITICAL("{} Failed to capture connection", | 
|  | 810 | logPtr(self.get())); | 
| Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 811 | return; | 
|  | 812 | } | 
|  | 813 |  | 
|  | 814 | self->sendNext(keepAlive, connId); | 
|  | 815 | } | 
|  | 816 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 817 | std::shared_ptr<ConnectionInfo>& addConnection() | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 818 | { | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 819 | unsigned int newId = static_cast<unsigned int>(connections.size()); | 
|  | 820 |  | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 821 | auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>( | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 822 | ioc, id, connPolicy, destIP, verifyCert, newId)); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 823 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 824 | BMCWEB_LOG_DEBUG("Added connection {} to pool {}", | 
|  | 825 | connections.size() - 1, id); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 826 |  | 
|  | 827 | return ret; | 
|  | 828 | } | 
|  | 829 |  | 
|  | 830 | public: | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 831 | explicit ConnectionPool( | 
|  | 832 | boost::asio::io_context& iocIn, const std::string& idIn, | 
|  | 833 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn, | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 834 | const boost::urls::url_view_base& destIPIn, | 
|  | 835 | ensuressl::VerifyCertificate verifyCertIn) : | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 836 | ioc(iocIn), id(idIn), connPolicy(connPolicyIn), destIP(destIPIn), | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 837 | verifyCert(verifyCertIn) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 838 | { | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 839 | BMCWEB_LOG_DEBUG("Initializing connection pool for {}", id); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 840 |  | 
|  | 841 | // Initialize the pool with a single connection | 
|  | 842 | addConnection(); | 
| Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 843 | } | 
| Myung Bae | a0969c7 | 2024-09-19 08:46:45 -0400 | [diff] [blame] | 844 |  | 
|  | 845 | // Check whether all connections are terminated | 
|  | 846 | bool areAllConnectionsTerminated() | 
|  | 847 | { | 
|  | 848 | if (connections.empty()) | 
|  | 849 | { | 
|  | 850 | BMCWEB_LOG_DEBUG("There are no connections for pool id:{}", id); | 
|  | 851 | return false; | 
|  | 852 | } | 
|  | 853 | for (const auto& conn : connections) | 
|  | 854 | { | 
|  | 855 | if (conn != nullptr && conn->state != ConnState::terminated) | 
|  | 856 | { | 
|  | 857 | BMCWEB_LOG_DEBUG( | 
|  | 858 | "Not all connections of pool id:{} are terminated", id); | 
|  | 859 | return false; | 
|  | 860 | } | 
|  | 861 | } | 
|  | 862 | BMCWEB_LOG_INFO("All connections of pool id:{} are terminated", id); | 
|  | 863 | return true; | 
|  | 864 | } | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 865 | }; | 
|  | 866 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 867 | class HttpClient | 
|  | 868 | { | 
|  | 869 | private: | 
|  | 870 | std::unordered_map<std::string, std::shared_ptr<ConnectionPool>> | 
|  | 871 | connectionPools; | 
| Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 872 |  | 
|  | 873 | // reference_wrapper here makes HttpClient movable | 
|  | 874 | std::reference_wrapper<boost::asio::io_context> ioc; | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 875 | std::shared_ptr<ConnectionPolicy> connPolicy; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 876 |  | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 877 | // Used as a dummy callback by sendData() in order to call | 
|  | 878 | // sendDataWithCallback() | 
| Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 879 | static void genericResHandler(const Response& res) | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 880 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 881 | BMCWEB_LOG_DEBUG("Response handled with return code: {}", | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 882 | res.resultInt()); | 
| Ed Tanous | 4ee8e21 | 2022-05-28 09:42:51 -0700 | [diff] [blame] | 883 | } | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 884 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 885 | public: | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 886 | HttpClient() = delete; | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 887 | explicit HttpClient(boost::asio::io_context& iocIn, | 
|  | 888 | const std::shared_ptr<ConnectionPolicy>& connPolicyIn) : | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 889 | ioc(iocIn), connPolicy(connPolicyIn) | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 890 | {} | 
| Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 891 |  | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 892 | HttpClient(const HttpClient&) = delete; | 
|  | 893 | HttpClient& operator=(const HttpClient&) = delete; | 
| Ed Tanous | 4b712a2 | 2023-08-02 12:56:52 -0700 | [diff] [blame] | 894 | HttpClient(HttpClient&& client) = default; | 
|  | 895 | HttpClient& operator=(HttpClient&& client) = default; | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 896 | ~HttpClient() = default; | 
|  | 897 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 898 | // Send a request to destIP where additional processing of the | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 899 | // result is not required | 
| Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 900 | void sendData(std::string&& data, const boost::urls::url_view_base& destUri, | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 901 | ensuressl::VerifyCertificate verifyCert, | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 902 | const boost::beast::http::fields& httpHeader, | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 903 | const boost::beast::http::verb verb) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 904 | { | 
| AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 905 | const std::function<void(Response&)> cb = genericResHandler; | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 906 | sendDataWithCallback(std::move(data), destUri, verifyCert, httpHeader, | 
|  | 907 | verb, cb); | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 908 | } | 
|  | 909 |  | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 910 | // Send request to destIP and use the provided callback to | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 911 | // handle the response | 
| Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 912 | void sendDataWithCallback(std::string&& data, | 
|  | 913 | const boost::urls::url_view_base& destUrl, | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 914 | ensuressl::VerifyCertificate verifyCert, | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 915 | const boost::beast::http::fields& httpHeader, | 
| Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 916 | const boost::beast::http::verb verb, | 
| Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 917 | const std::function<void(Response&)>& resHandler) | 
| Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 918 | { | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 919 | std::string_view verify = "ssl_verify"; | 
|  | 920 | if (verifyCert == ensuressl::VerifyCertificate::NoVerify) | 
|  | 921 | { | 
|  | 922 | verify = "ssl no verify"; | 
|  | 923 | } | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 924 | std::string clientKey = | 
|  | 925 | std::format("{}{}://{}", verify, destUrl.scheme(), | 
|  | 926 | destUrl.encoded_host_and_port()); | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 927 | auto pool = connectionPools.try_emplace(clientKey); | 
|  | 928 | if (pool.first->second == nullptr) | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 929 | { | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 930 | pool.first->second = std::make_shared<ConnectionPool>( | 
| Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 931 | ioc, clientKey, connPolicy, destUrl, verifyCert); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 932 | } | 
| Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 933 | // Send the data using either the existing connection pool or the | 
|  | 934 | // newly created connection pool | 
| Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 935 | pool.first->second->sendData(std::move(data), destUrl, httpHeader, verb, | 
| Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 936 | resHandler); | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 937 | } | 
| Myung Bae | a0969c7 | 2024-09-19 08:46:45 -0400 | [diff] [blame] | 938 |  | 
|  | 939 | // Test whether all connections are terminated (after MaxRetryAttempts) | 
|  | 940 | bool isTerminated() | 
|  | 941 | { | 
|  | 942 | for (const auto& pool : connectionPools) | 
|  | 943 | { | 
|  | 944 | if (pool.second != nullptr && | 
|  | 945 | !pool.second->areAllConnectionsTerminated()) | 
|  | 946 | { | 
|  | 947 | BMCWEB_LOG_DEBUG( | 
|  | 948 | "Not all of client connections are terminated"); | 
|  | 949 | return false; | 
|  | 950 | } | 
|  | 951 | } | 
|  | 952 | BMCWEB_LOG_DEBUG("All client connections are terminated"); | 
|  | 953 | return true; | 
|  | 954 | } | 
| Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 955 | }; | 
| AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 956 | } // namespace crow |