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