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