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