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