AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 1 | /* |
Ed Tanous | 6be832e | 2024-09-10 11:44:48 -0700 | [diff] [blame] | 2 | Copyright (c) 2020 Intel Corporation |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 15 | */ |
| 16 | #pragma once |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 17 | |
| 18 | #include "async_resolve.hpp" |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 19 | #include "http_body.hpp" |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 20 | #include "http_response.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 21 | #include "logging.hpp" |
| 22 | #include "ssl_key_handler.hpp" |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 23 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 24 | #include <boost/asio/connect.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 25 | #include <boost/asio/io_context.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 26 | #include <boost/asio/ip/address.hpp> |
| 27 | #include <boost/asio/ip/basic_endpoint.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 28 | #include <boost/asio/ip/tcp.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 29 | #include <boost/asio/ssl/context.hpp> |
| 30 | #include <boost/asio/ssl/error.hpp> |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 31 | #include <boost/asio/ssl/stream.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 32 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 33 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 34 | #include <boost/beast/http/message.hpp> |
Ed Tanous | 4d69861 | 2024-02-06 14:57:24 -0800 | [diff] [blame] | 35 | #include <boost/beast/http/message_generator.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 36 | #include <boost/beast/http/parser.hpp> |
| 37 | #include <boost/beast/http/read.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 38 | #include <boost/beast/http/write.hpp> |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 39 | #include <boost/container/devector.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 40 | #include <boost/system/error_code.hpp> |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 41 | #include <boost/url/format.hpp> |
| 42 | #include <boost/url/url.hpp> |
Ed Tanous | 4a7fbef | 2024-04-06 16:03:49 -0700 | [diff] [blame] | 43 | #include <boost/url/url_view_base.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 44 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 45 | #include <cstdlib> |
| 46 | #include <functional> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 47 | #include <memory> |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 48 | #include <queue> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 49 | #include <string> |
| 50 | |
| 51 | namespace crow |
| 52 | { |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 53 | // With Redfish Aggregation it is assumed we will connect to another |
| 54 | // instance of BMCWeb which can handle 100 simultaneous connections. |
Carson Labrado | 66d90c2 | 2022-12-07 22:34:33 +0000 | [diff] [blame] | 55 | constexpr size_t maxPoolSize = 20; |
| 56 | constexpr size_t maxRequestQueueSize = 500; |
Carson Labrado | 17dcc31 | 2022-07-28 22:17:28 +0000 | [diff] [blame] | 57 | constexpr unsigned int httpReadBodyLimit = 131072; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 58 | constexpr unsigned int httpReadBufferSize = 4096; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 59 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 60 | enum class ConnState |
| 61 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 62 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 63 | resolveInProgress, |
| 64 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 65 | connectInProgress, |
| 66 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 67 | connected, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 68 | handshakeInProgress, |
| 69 | handshakeFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 70 | sendInProgress, |
| 71 | sendFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 72 | recvInProgress, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 73 | recvFailed, |
| 74 | idle, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 75 | closed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 76 | suspended, |
| 77 | terminated, |
| 78 | abortConnection, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 79 | sslInitFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 80 | retry |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 81 | }; |
| 82 | |
Ed Tanous | 4ff0f1f | 2024-09-04 17:27:37 -0700 | [diff] [blame] | 83 | inline boost::system::error_code defaultRetryHandler(unsigned int respCode) |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 84 | { |
| 85 | // As a default, assume 200X is alright |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_DEBUG("Using default check for response code validity"); |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 87 | if ((respCode < 200) || (respCode >= 300)) |
| 88 | { |
| 89 | return boost::system::errc::make_error_code( |
| 90 | boost::system::errc::result_out_of_range); |
| 91 | } |
| 92 | |
| 93 | // Return 0 if the response code is valid |
| 94 | return boost::system::errc::make_error_code(boost::system::errc::success); |
| 95 | }; |
| 96 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 97 | // We need to allow retry information to be set before a message has been |
| 98 | // sent and a connection pool has been created |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 99 | struct ConnectionPolicy |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 100 | { |
| 101 | uint32_t maxRetryAttempts = 5; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 102 | |
| 103 | // the max size of requests in bytes. 0 for unlimited |
| 104 | boost::optional<uint64_t> requestByteLimit = httpReadBodyLimit; |
| 105 | |
| 106 | size_t maxConnections = 1; |
| 107 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 108 | std::string retryPolicyAction = "TerminateAfterRetries"; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 109 | |
| 110 | std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0); |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 111 | std::function<boost::system::error_code(unsigned int respCode)> |
| 112 | invalidResp = defaultRetryHandler; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct PendingRequest |
| 116 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 117 | boost::beast::http::request<bmcweb::HttpBody> req; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 118 | std::function<void(bool, uint32_t, Response&)> callback; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 119 | PendingRequest( |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 120 | boost::beast::http::request<bmcweb::HttpBody>&& reqIn, |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 121 | const std::function<void(bool, uint32_t, Response&)>& callbackIn) : |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 122 | req(std::move(reqIn)), callback(callbackIn) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 123 | {} |
| 124 | }; |
| 125 | |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 126 | namespace http = boost::beast::http; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 127 | class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 128 | { |
| 129 | private: |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 130 | ConnState state = ConnState::initialized; |
| 131 | uint32_t retryCount = 0; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 132 | std::string subId; |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 133 | std::shared_ptr<ConnectionPolicy> connPolicy; |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 134 | boost::urls::url host; |
Ed Tanous | 19bb362 | 2024-07-05 10:07:40 -0500 | [diff] [blame] | 135 | ensuressl::VerifyCertificate verifyCert; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 136 | uint32_t connId; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 137 | // Data buffers |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 138 | http::request<bmcweb::HttpBody> req; |
| 139 | using parser_type = http::response_parser<bmcweb::HttpBody>; |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 140 | std::optional<parser_type> parser; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 141 | boost::beast::flat_static_buffer<httpReadBufferSize> buffer; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 142 | Response res; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 143 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 144 | // Ascync callables |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 145 | std::function<void(bool, uint32_t, Response&)> callback; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 146 | |
Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 147 | boost::asio::io_context& ioc; |
| 148 | |
Ed Tanous | 25b54db | 2024-04-17 15:40:31 -0700 | [diff] [blame] | 149 | using Resolver = std::conditional_t<BMCWEB_DNS_RESOLVER == "systemd-dbus", |
| 150 | async_resolve::Resolver, |
| 151 | boost::asio::ip::tcp::resolver>; |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 152 | Resolver resolver; |
| 153 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 154 | boost::asio::ip::tcp::socket conn; |
Ed Tanous | 003301a | 2024-04-16 09:59:19 -0700 | [diff] [blame] | 155 | std::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>> |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 156 | sslConn; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 157 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 158 | boost::asio::steady_timer timer; |
Ed Tanous | 84b3560 | 2021-09-08 20:06:32 -0700 | [diff] [blame] | 159 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 160 | friend class ConnectionPool; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 161 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 162 | void doResolve() |
| 163 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 164 | state = ConnState::resolveInProgress; |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 165 | BMCWEB_LOG_DEBUG("Trying to resolve: {}, id: {}", host, connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 166 | |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 167 | resolver.async_resolve(host.encoded_host_address(), host.port(), |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 168 | std::bind_front(&ConnectionInfo::afterResolve, |
| 169 | this, shared_from_this())); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Ed Tanous | f8ca6d7 | 2022-06-28 12:12:03 -0700 | [diff] [blame] | 172 | void afterResolve(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 173 | const boost::system::error_code& ec, |
| 174 | const Resolver::results_type& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 175 | { |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 176 | if (ec || (endpointList.empty())) |
| 177 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 178 | BMCWEB_LOG_ERROR("Resolve failed: {} {}", ec.message(), host); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 179 | state = ConnState::resolveFailed; |
| 180 | waitAndRetry(); |
| 181 | return; |
| 182 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 183 | BMCWEB_LOG_DEBUG("Resolved {}, id: {}", host, connId); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 184 | state = ConnState::connectInProgress; |
| 185 | |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 186 | BMCWEB_LOG_DEBUG("Trying to connect to: {}, id: {}", host, connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 187 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 188 | timer.expires_after(std::chrono::seconds(30)); |
| 189 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 190 | |
| 191 | boost::asio::async_connect( |
| 192 | conn, endpointList, |
| 193 | std::bind_front(&ConnectionInfo::afterConnect, this, |
| 194 | shared_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/, |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 198 | const boost::beast::error_code& ec, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 199 | const boost::asio::ip::tcp::endpoint& endpoint) |
| 200 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 201 | // The operation already timed out. We don't want do continue down |
| 202 | // this branch |
| 203 | if (ec && ec == boost::asio::error::operation_aborted) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 208 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 209 | if (ec) |
| 210 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 211 | BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}", |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 212 | endpoint.address().to_string(), endpoint.port(), |
| 213 | connId, ec.message()); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 214 | state = ConnState::connectFailed; |
| 215 | waitAndRetry(); |
| 216 | return; |
| 217 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 218 | BMCWEB_LOG_DEBUG("Connected to: {}:{}, id: {}", |
| 219 | endpoint.address().to_string(), endpoint.port(), |
| 220 | connId); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 221 | if (sslConn) |
| 222 | { |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 223 | doSslHandshake(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | state = ConnState::connected; |
| 227 | sendMessage(); |
| 228 | } |
| 229 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 230 | void doSslHandshake() |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 231 | { |
| 232 | if (!sslConn) |
| 233 | { |
| 234 | return; |
| 235 | } |
| 236 | state = ConnState::handshakeInProgress; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 237 | timer.expires_after(std::chrono::seconds(30)); |
| 238 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 239 | sslConn->async_handshake( |
| 240 | boost::asio::ssl::stream_base::client, |
| 241 | std::bind_front(&ConnectionInfo::afterSslHandshake, this, |
| 242 | shared_from_this())); |
| 243 | } |
| 244 | |
| 245 | void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/, |
Ed Tanous | 81c4e33 | 2023-05-18 10:30:34 -0700 | [diff] [blame] | 246 | const boost::beast::error_code& ec) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 247 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 248 | // The operation already timed out. We don't want do continue down |
| 249 | // this branch |
| 250 | if (ec && ec == boost::asio::error::operation_aborted) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 255 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 256 | if (ec) |
| 257 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 258 | BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}", connId, |
| 259 | ec.message()); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 260 | state = ConnState::handshakeFailed; |
| 261 | waitAndRetry(); |
| 262 | return; |
| 263 | } |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 264 | BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}", connId); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 265 | state = ConnState::connected; |
| 266 | sendMessage(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 267 | } |
| 268 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 269 | void sendMessage() |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 270 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 271 | state = ConnState::sendInProgress; |
| 272 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 273 | // Set a timeout on the operation |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 274 | timer.expires_after(std::chrono::seconds(30)); |
| 275 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 276 | // Send the HTTP request to the remote host |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 277 | if (sslConn) |
| 278 | { |
Ed Tanous | cd504a9 | 2024-08-19 11:46:20 -0700 | [diff] [blame] | 279 | boost::beast::http::async_write( |
| 280 | *sslConn, req, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 281 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 282 | shared_from_this())); |
| 283 | } |
| 284 | else |
| 285 | { |
Ed Tanous | cd504a9 | 2024-08-19 11:46:20 -0700 | [diff] [blame] | 286 | boost::beast::http::async_write( |
| 287 | conn, req, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 288 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 289 | shared_from_this())); |
| 290 | } |
| 291 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 292 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 293 | void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 294 | const boost::beast::error_code& ec, size_t bytesTransferred) |
| 295 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 296 | // The operation already timed out. We don't want do continue down |
| 297 | // this branch |
| 298 | if (ec && ec == boost::asio::error::operation_aborted) |
| 299 | { |
| 300 | return; |
| 301 | } |
| 302 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 303 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 304 | if (ec) |
| 305 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 306 | BMCWEB_LOG_ERROR("sendMessage() failed: {} {}", ec.message(), host); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 307 | state = ConnState::sendFailed; |
| 308 | waitAndRetry(); |
| 309 | return; |
| 310 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 311 | BMCWEB_LOG_DEBUG("sendMessage() bytes transferred: {}", |
| 312 | bytesTransferred); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 313 | |
| 314 | recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void recvMessage() |
| 318 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 319 | state = ConnState::recvInProgress; |
| 320 | |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 321 | parser_type& thisParser = |
| 322 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 323 | |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 324 | thisParser.body_limit(connPolicy->requestByteLimit); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 325 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 326 | timer.expires_after(std::chrono::seconds(30)); |
| 327 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 328 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 329 | // Receive the HTTP response |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 330 | if (sslConn) |
| 331 | { |
| 332 | boost::beast::http::async_read( |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 333 | *sslConn, buffer, thisParser, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 334 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 335 | shared_from_this())); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | boost::beast::http::async_read( |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 340 | conn, buffer, thisParser, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 341 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 342 | shared_from_this())); |
| 343 | } |
| 344 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 345 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 346 | void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 347 | const boost::beast::error_code& ec, |
| 348 | const std::size_t& bytesTransferred) |
| 349 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 350 | // The operation already timed out. We don't want do continue down |
| 351 | // this branch |
| 352 | if (ec && ec == boost::asio::error::operation_aborted) |
| 353 | { |
| 354 | return; |
| 355 | } |
| 356 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 357 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 358 | if (ec && ec != boost::asio::ssl::error::stream_truncated) |
| 359 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 360 | BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}", ec.message(), |
| 361 | host); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 362 | state = ConnState::recvFailed; |
| 363 | waitAndRetry(); |
| 364 | return; |
| 365 | } |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 366 | BMCWEB_LOG_DEBUG("recvMessage() bytes transferred: {}", |
| 367 | bytesTransferred); |
Ed Tanous | e01d0c3 | 2023-06-30 13:21:32 -0700 | [diff] [blame] | 368 | if (!parser) |
| 369 | { |
| 370 | return; |
| 371 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 372 | BMCWEB_LOG_DEBUG("recvMessage() data: {}", parser->get().body().str()); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 373 | |
| 374 | unsigned int respCode = parser->get().result_int(); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 375 | BMCWEB_LOG_DEBUG("recvMessage() Header Response Code: {}", respCode); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 376 | |
Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 377 | // Handle the case of stream_truncated. Some servers close the ssl |
| 378 | // connection uncleanly, so check to see if we got a full response |
| 379 | // before we handle this as an error. |
| 380 | if (!parser->is_done()) |
| 381 | { |
| 382 | state = ConnState::recvFailed; |
| 383 | waitAndRetry(); |
| 384 | return; |
| 385 | } |
| 386 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 387 | // Make sure the received response code is valid as defined by |
| 388 | // the associated retry policy |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 389 | if (connPolicy->invalidResp(respCode)) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 390 | { |
| 391 | // The listener failed to receive the Sent-Event |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 392 | BMCWEB_LOG_ERROR( |
| 393 | "recvMessage() Listener Failed to " |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 394 | "receive Sent-Event. Header Response Code: {} from {}", |
| 395 | respCode, host); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 396 | state = ConnState::recvFailed; |
| 397 | waitAndRetry(); |
| 398 | return; |
| 399 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 400 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 401 | // Send is successful |
| 402 | // Reset the counter just in case this was after retrying |
| 403 | retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 404 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 405 | // Keep the connection alive if server supports it |
| 406 | // Else close the connection |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 407 | BMCWEB_LOG_DEBUG("recvMessage() keepalive : {}", parser->keep_alive()); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 408 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 409 | // Copy the response into a Response object so that it can be |
| 410 | // processed by the callback function. |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 411 | res.response = parser->release(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 412 | callback(parser->keep_alive(), connId, res); |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 413 | res.clear(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 416 | static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 417 | const boost::system::error_code& ec) |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 418 | { |
| 419 | if (ec == boost::asio::error::operation_aborted) |
| 420 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 421 | BMCWEB_LOG_DEBUG( |
| 422 | "async_wait failed since the operation is aborted"); |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | if (ec) |
| 426 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 427 | BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message()); |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 428 | // If the timer fails, we need to close the socket anyway, same |
| 429 | // as if it expired. |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 430 | } |
| 431 | std::shared_ptr<ConnectionInfo> self = weakSelf.lock(); |
| 432 | if (self == nullptr) |
| 433 | { |
| 434 | return; |
| 435 | } |
| 436 | self->waitAndRetry(); |
| 437 | } |
| 438 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 439 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 440 | { |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 441 | if ((retryCount >= connPolicy->maxRetryAttempts) || |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 442 | (state == ConnState::sslInitFailed)) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 443 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 444 | BMCWEB_LOG_ERROR("Maximum number of retries reached. {}", host); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 445 | BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction); |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 446 | |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 447 | if (connPolicy->retryPolicyAction == "TerminateAfterRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 448 | { |
| 449 | // TODO: delete subscription |
| 450 | state = ConnState::terminated; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 451 | } |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 452 | if (connPolicy->retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 453 | { |
| 454 | state = ConnState::suspended; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 455 | } |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 456 | |
| 457 | // We want to return a 502 to indicate there was an error with |
| 458 | // the external server |
| 459 | res.result(boost::beast::http::status::bad_gateway); |
| 460 | callback(false, connId, res); |
| 461 | res.clear(); |
| 462 | |
Ed Tanous | 27b0cf9 | 2023-08-07 12:02:40 -0700 | [diff] [blame] | 463 | // Reset the retrycount to zero so that client can try |
| 464 | // connecting again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 465 | retryCount = 0; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 466 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 467 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 468 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 469 | retryCount++; |
| 470 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 471 | BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}", |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 472 | connPolicy->retryIntervalSecs.count(), retryCount); |
Carson Labrado | d14a48f | 2023-02-22 00:24:54 +0000 | [diff] [blame] | 473 | timer.expires_after(connPolicy->retryIntervalSecs); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 474 | timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this, |
| 475 | shared_from_this())); |
| 476 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 477 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 478 | void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 479 | const boost::system::error_code& ec) |
| 480 | { |
| 481 | if (ec == boost::asio::error::operation_aborted) |
| 482 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 483 | BMCWEB_LOG_DEBUG( |
| 484 | "async_wait failed since the operation is aborted{}", |
| 485 | ec.message()); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 486 | } |
| 487 | else if (ec) |
| 488 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 489 | BMCWEB_LOG_ERROR("async_wait failed: {}", ec.message()); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 490 | // Ignore the error and continue the retry loop to attempt |
| 491 | // sending the event as per the retry policy |
| 492 | } |
| 493 | |
| 494 | // Let's close the connection and restart from resolve. |
Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 495 | shutdownConn(true); |
| 496 | } |
| 497 | |
| 498 | void restartConnection() |
| 499 | { |
| 500 | BMCWEB_LOG_DEBUG("{}, id: {} restartConnection", host, |
| 501 | std::to_string(connId)); |
| 502 | initializeConnection(host.scheme() == "https"); |
| 503 | doResolve(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 504 | } |
| 505 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 506 | void shutdownConn(bool retry) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 507 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 508 | boost::beast::error_code ec; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 509 | conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 510 | conn.close(); |
| 511 | |
| 512 | // not_connected happens sometimes so don't bother reporting it. |
| 513 | if (ec && ec != boost::beast::errc::not_connected) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 514 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 515 | BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId, |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 516 | ec.message()); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 517 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 518 | else |
| 519 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 520 | BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId); |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 521 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 522 | |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 523 | if (retry) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 524 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 525 | // Now let's try to resend the data |
| 526 | state = ConnState::retry; |
Abhilash Raju | f3cb5df | 2023-11-30 03:54:11 -0600 | [diff] [blame] | 527 | restartConnection(); |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 528 | } |
| 529 | else |
| 530 | { |
| 531 | state = ConnState::closed; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 532 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 533 | } |
| 534 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 535 | void doClose(bool retry = false) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 536 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 537 | if (!sslConn) |
| 538 | { |
| 539 | shutdownConn(retry); |
| 540 | return; |
| 541 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 542 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 543 | sslConn->async_shutdown( |
| 544 | std::bind_front(&ConnectionInfo::afterSslShutdown, this, |
| 545 | shared_from_this(), retry)); |
| 546 | } |
| 547 | |
| 548 | void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 549 | bool retry, const boost::system::error_code& ec) |
| 550 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 551 | if (ec) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 552 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 553 | BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId, |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 554 | ec.message()); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 555 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 556 | else |
| 557 | { |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 558 | BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId); |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 559 | } |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 560 | shutdownConn(retry); |
| 561 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 562 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 563 | void setCipherSuiteTLSext() |
| 564 | { |
| 565 | if (!sslConn) |
| 566 | { |
| 567 | return; |
| 568 | } |
Ravi Teja | e7c2991 | 2023-07-31 09:39:32 -0500 | [diff] [blame] | 569 | |
| 570 | if (host.host_type() != boost::urls::host_type::name) |
| 571 | { |
| 572 | // Avoid setting SNI hostname if its IP address |
| 573 | return; |
| 574 | } |
| 575 | // Create a null terminated string for SSL |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 576 | std::string hostname(host.encoded_host_address()); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 577 | // NOTE: The SSL_set_tlsext_host_name is defined in tlsv1.h header |
| 578 | // file but its having old style casting (name is cast to void*). |
| 579 | // Since bmcweb compiler treats all old-style-cast as error, its |
| 580 | // causing the build failure. So replaced the same macro inline and |
| 581 | // did corrected the code by doing static_cast to viod*. This has to |
| 582 | // be fixed in openssl library in long run. Set SNI Hostname (many |
| 583 | // hosts need this to handshake successfully) |
| 584 | if (SSL_ctrl(sslConn->native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME, |
| 585 | TLSEXT_NAMETYPE_host_name, |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 586 | static_cast<void*>(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 |