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