Move http client to URL
Type safety is a good thing. In:
https://gerrit.openbmc.org/c/openbmc/bmcweb/+/65606
It was found that splitting out the URI into encoded pieces in the early
phase removed some information we needed, namely whether or not a URI
was ipv6. This commit changes http client such that it passes all the
information through, with the correct type, rather than passing in
hostname, port, path, and ssl separately.
Opportunistically, because a number of log lines are changing, this uses
the opportunity to remove a number of calls to std::to_string, and rely
on std::format instead.
Now that we no longer use custom URI splitting code, the
ValidateAndSplitUrl() method can be removed, given that our validation
now happens in the URI class.
Tested: Aggregation works properly when satellite URIs are queried.
Change-Id: I9f605863179af54c5af2719bc5ce9d29cbfffab7
Signed-off-by: Ed Tanous <edtanous@google.com>
diff --git a/http/http_client.hpp b/http/http_client.hpp
index e1c4d37..2cbdbbc 100644
--- a/http/http_client.hpp
+++ b/http/http_client.hpp
@@ -39,6 +39,7 @@
#include <boost/beast/version.hpp>
#include <boost/container/devector.hpp>
#include <boost/system/error_code.hpp>
+#include <boost/url/url_view.hpp>
#include <cstdlib>
#include <functional>
@@ -133,8 +134,7 @@
uint32_t retryCount = 0;
std::string subId;
std::shared_ptr<ConnectionPolicy> connPolicy;
- std::string host;
- uint16_t port;
+ boost::urls::url host;
uint32_t connId;
// Data buffers
@@ -165,10 +165,9 @@
void doResolve()
{
state = ConnState::resolveInProgress;
- BMCWEB_LOG_DEBUG("Trying to resolve: {}:{}, id: {}", host,
- std::to_string(port), std::to_string(connId));
+ BMCWEB_LOG_DEBUG("Trying to resolve: {}, id: {}", host, connId);
- resolver.async_resolve(host, std::to_string(port),
+ resolver.async_resolve(host.encoded_host_address(), host.port(),
std::bind_front(&ConnectionInfo::afterResolve,
this, shared_from_this()));
}
@@ -179,18 +178,15 @@
{
if (ec || (endpointList.empty()))
{
- BMCWEB_LOG_ERROR("Resolve failed: {} {}:{}", ec.message(), host,
- std::to_string(port));
+ BMCWEB_LOG_ERROR("Resolve failed: {} {}", ec.message(), host);
state = ConnState::resolveFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG("Resolved {}:{}, id: {}", host, std::to_string(port),
- std::to_string(connId));
+ BMCWEB_LOG_DEBUG("Resolved {}, id: {}", host, connId);
state = ConnState::connectInProgress;
- BMCWEB_LOG_DEBUG("Trying to connect to: {}:{}, id: {}", host,
- std::to_string(port), std::to_string(connId));
+ BMCWEB_LOG_DEBUG("Trying to connect to: {}, id: {}", host, connId);
timer.expires_after(std::chrono::seconds(30));
timer.async_wait(std::bind_front(onTimeout, weak_from_this()));
@@ -216,16 +212,15 @@
if (ec)
{
BMCWEB_LOG_ERROR("Connect {}:{}, id: {} failed: {}",
- endpoint.address().to_string(),
- std::to_string(endpoint.port()),
- std::to_string(connId), ec.message());
+ endpoint.address().to_string(), endpoint.port(),
+ connId, ec.message());
state = ConnState::connectFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG(
- "Connected to: {}:{}, id: {}", endpoint.address().to_string(),
- std::to_string(endpoint.port()), std::to_string(connId));
+ BMCWEB_LOG_DEBUG("Connected to: {}:{}, id: {}",
+ endpoint.address().to_string(), endpoint.port(),
+ connId);
if (sslConn)
{
doSslHandshake();
@@ -263,14 +258,13 @@
timer.cancel();
if (ec)
{
- BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}",
- std::to_string(connId), ec.message());
+ BMCWEB_LOG_ERROR("SSL Handshake failed - id: {} error: {}", connId,
+ ec.message());
state = ConnState::handshakeFailed;
waitAndRetry();
return;
}
- BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}",
- std::to_string(connId));
+ BMCWEB_LOG_DEBUG("SSL Handshake successful - id: {}", connId);
state = ConnState::connected;
sendMessage();
}
@@ -313,8 +307,7 @@
timer.cancel();
if (ec)
{
- BMCWEB_LOG_ERROR("sendMessage() failed: {} {}:{}", ec.message(),
- host, std::to_string(port));
+ BMCWEB_LOG_ERROR("sendMessage() failed: {} {}", ec.message(), host);
state = ConnState::sendFailed;
waitAndRetry();
return;
@@ -368,8 +361,8 @@
timer.cancel();
if (ec && ec != boost::asio::ssl::error::stream_truncated)
{
- BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}:{}",
- ec.message(), host, std::to_string(port));
+ BMCWEB_LOG_ERROR("recvMessage() failed: {} from {}", ec.message(),
+ host);
state = ConnState::recvFailed;
waitAndRetry();
return;
@@ -392,8 +385,8 @@
// The listener failed to receive the Sent-Event
BMCWEB_LOG_ERROR(
"recvMessage() Listener Failed to "
- "receive Sent-Event. Header Response Code: {} from {}:{}",
- respCode, host, std::to_string(port));
+ "receive Sent-Event. Header Response Code: {} from {}",
+ respCode, host);
state = ConnState::recvFailed;
waitAndRetry();
return;
@@ -442,8 +435,7 @@
if ((retryCount >= connPolicy->maxRetryAttempts) ||
(state == ConnState::sslInitFailed))
{
- BMCWEB_LOG_ERROR("Maximum number of retries reached. {}:{}", host,
- std::to_string(port));
+ BMCWEB_LOG_ERROR("Maximum number of retries reached. {}", host);
BMCWEB_LOG_DEBUG("Retry policy: {}", connPolicy->retryPolicyAction);
if (connPolicy->retryPolicyAction == "TerminateAfterRetries")
@@ -471,8 +463,7 @@
retryCount++;
BMCWEB_LOG_DEBUG("Attempt retry after {} seconds. RetryCount = {}",
- std::to_string(connPolicy->retryIntervalSecs.count()),
- retryCount);
+ connPolicy->retryIntervalSecs.count(), retryCount);
timer.expires_after(connPolicy->retryIntervalSecs);
timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this,
shared_from_this()));
@@ -507,14 +498,12 @@
// not_connected happens sometimes so don't bother reporting it.
if (ec && ec != boost::beast::errc::not_connected)
{
- BMCWEB_LOG_ERROR("{}:{}, id: {} shutdown failed: {}", host,
- std::to_string(port), std::to_string(connId),
+ BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
ec.message());
}
else
{
- BMCWEB_LOG_DEBUG("{}:{}, id: {} closed gracefully", host,
- std::to_string(port), std::to_string(connId));
+ BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
}
if (retry)
@@ -547,14 +536,12 @@
{
if (ec)
{
- BMCWEB_LOG_ERROR("{}:{}, id: {} shutdown failed: {}", host,
- std::to_string(port), std::to_string(connId),
+ BMCWEB_LOG_ERROR("{}, id: {} shutdown failed: {}", host, connId,
ec.message());
}
else
{
- BMCWEB_LOG_DEBUG("{}:{}, id: {} closed gracefully", host,
- std::to_string(port), std::to_string(connId));
+ BMCWEB_LOG_DEBUG("{}, id: {} closed gracefully", host, connId);
}
shutdownConn(retry);
}
@@ -565,6 +552,7 @@
{
return;
}
+ std::string hostname(host.encoded_host_address());
// NOTE: The SSL_set_tlsext_host_name is defined in tlsv1.h header
// file but its having old style casting (name is cast to void*).
// Since bmcweb compiler treats all old-style-cast as error, its
@@ -574,15 +562,14 @@
// hosts need this to handshake successfully)
if (SSL_ctrl(sslConn->native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME,
TLSEXT_NAMETYPE_host_name,
- static_cast<void*>(&host.front())) == 0)
+ static_cast<void*>(hostname.data())) == 0)
{
boost::beast::error_code ec{static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category()};
- BMCWEB_LOG_ERROR(
- "SSL_set_tlsext_host_name {}:{}, id: {} failed: {}", host, port,
- std::to_string(connId), ec.message());
+ BMCWEB_LOG_ERROR("SSL_set_tlsext_host_name {}, id: {} failed: {}",
+ host, connId, ec.message());
// Set state as sslInit failed so that we close the connection
// and take appropriate action as per retry configuration.
state = ConnState::sslInitFailed;
@@ -595,21 +582,20 @@
explicit ConnectionInfo(
boost::asio::io_context& iocIn, const std::string& idIn,
const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
- const std::string& destIPIn, uint16_t destPortIn, bool useSSL,
- unsigned int connIdIn) :
+ boost::urls::url_view hostIn, unsigned int connIdIn) :
subId(idIn),
- connPolicy(connPolicyIn), host(destIPIn), port(destPortIn),
- connId(connIdIn), resolver(iocIn), conn(iocIn), timer(iocIn)
+ connPolicy(connPolicyIn), host(hostIn), connId(connIdIn),
+ resolver(iocIn), conn(iocIn), timer(iocIn)
{
- if (useSSL)
+ if (host.scheme() == "https")
{
std::optional<boost::asio::ssl::context> sslCtx =
ensuressl::getSSLClientContext();
if (!sslCtx)
{
- BMCWEB_LOG_ERROR("prepareSSLContext failed - {}:{}, id: {}",
- host, port, std::to_string(connId));
+ BMCWEB_LOG_ERROR("prepareSSLContext failed - {}, id: {}", host,
+ connId);
// Don't retry if failure occurs while preparing SSL context
// such as certificate is invalid or set cipher failure or set
// host name failure etc... Setting conn state to sslInitFailed
@@ -631,9 +617,7 @@
boost::asio::io_context& ioc;
std::string id;
std::shared_ptr<ConnectionPolicy> connPolicy;
- std::string destIP;
- uint16_t destPort;
- bool useSSL;
+ boost::urls::url destIP;
std::vector<std::shared_ptr<ConnectionInfo>> connections;
boost::container::devector<PendingRequest> requestQueue;
@@ -654,9 +638,8 @@
conn.req = std::move(nextReq.req);
conn.callback = std::move(nextReq.callback);
- BMCWEB_LOG_DEBUG("Setting properties for connection {}:{}, id: {}",
- conn.host, std::to_string(conn.port),
- std::to_string(conn.connId));
+ BMCWEB_LOG_DEBUG("Setting properties for connection {}, id: {}",
+ conn.host, conn.connId);
// We can remove the request from the queue at this point
requestQueue.pop_front();
@@ -678,9 +661,8 @@
if (!requestQueue.empty())
{
BMCWEB_LOG_DEBUG(
- "{} requests remaining in queue for {}:{}, reusing connnection {}",
- std::to_string(requestQueue.size()), destIP,
- std::to_string(destPort), std::to_string(connId));
+ "{} requests remaining in queue for {}, reusing connnection {}",
+ requestQueue.size(), destIP, connId);
setConnProps(*conn);
@@ -711,15 +693,16 @@
}
}
- void sendData(std::string&& data, const std::string& destUri,
+ void sendData(std::string&& data, boost::urls::url_view destUri,
const boost::beast::http::fields& httpHeader,
const boost::beast::http::verb verb,
const std::function<void(Response&)>& resHandler)
{
// Construct the request to be sent
boost::beast::http::request<boost::beast::http::string_body> thisReq(
- verb, destUri, 11, "", httpHeader);
- thisReq.set(boost::beast::http::field::host, destIP);
+ verb, destUri.encoded_target(), 11, "", httpHeader);
+ thisReq.set(boost::beast::http::field::host,
+ destUri.encoded_host_address());
thisReq.keep_alive(true);
thisReq.body() = std::move(data);
thisReq.prepare_payload();
@@ -735,8 +718,7 @@
{
conn->req = std::move(thisReq);
conn->callback = std::move(cb);
- std::string commonMsg = std::to_string(i) + " from pool " +
- destIP + ":" + std::to_string(destPort);
+ std::string commonMsg = std::format("{} from pool {}", i, id);
if (conn->state == ConnState::idle)
{
@@ -757,8 +739,7 @@
// the queue
if (connections.size() < connPolicy->maxConnections)
{
- BMCWEB_LOG_DEBUG("Adding new connection to pool {}:{}", destIP,
- std::to_string(destPort));
+ BMCWEB_LOG_DEBUG("Adding new connection to pool {}", id);
auto conn = addConnection();
conn->req = std::move(thisReq);
conn->callback = std::move(cb);
@@ -766,9 +747,8 @@
}
else if (requestQueue.size() < maxRequestQueueSize)
{
- BMCWEB_LOG_DEBUG(
- "Max pool size reached. Adding data to queue.{}:{}", destIP,
- std::to_string(destPort));
+ BMCWEB_LOG_DEBUG("Max pool size reached. Adding data to queue {}",
+ id);
requestQueue.emplace_back(std::move(thisReq), std::move(cb));
}
else
@@ -776,7 +756,7 @@
// If we can't buffer the request then we should let the callback
// handle a 429 Too Many Requests dummy response
BMCWEB_LOG_ERROR("{}:{} request queue full. Dropping request.",
- destIP, std::to_string(destPort));
+ id);
Response dummyRes;
dummyRes.result(boost::beast::http::status::too_many_requests);
resHandler(dummyRes);
@@ -810,11 +790,10 @@
unsigned int newId = static_cast<unsigned int>(connections.size());
auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>(
- ioc, id, connPolicy, destIP, destPort, useSSL, newId));
+ ioc, id, connPolicy, destIP, newId));
- BMCWEB_LOG_DEBUG("Added connection {} to pool {}:{}",
- std::to_string(connections.size() - 1), destIP,
- std::to_string(destPort));
+ BMCWEB_LOG_DEBUG("Added connection {} to pool {}",
+ connections.size() - 1, id);
return ret;
}
@@ -823,13 +802,11 @@
explicit ConnectionPool(
boost::asio::io_context& iocIn, const std::string& idIn,
const std::shared_ptr<ConnectionPolicy>& connPolicyIn,
- const std::string& destIPIn, uint16_t destPortIn, bool useSSLIn) :
+ boost::urls::url_view destIPIn) :
ioc(iocIn),
- id(idIn), connPolicy(connPolicyIn), destIP(destIPIn),
- destPort(destPortIn), useSSL(useSSLIn)
+ id(idIn), connPolicy(connPolicyIn), destIP(destIPIn)
{
- BMCWEB_LOG_DEBUG("Initializing connection pool for {}:{}", destIP,
- std::to_string(destPort));
+ BMCWEB_LOG_DEBUG("Initializing connection pool for {}", id);
// Initialize the pool with a single connection
addConnection();
@@ -849,7 +826,7 @@
static void genericResHandler(const Response& res)
{
BMCWEB_LOG_DEBUG("Response handled with return code: {}",
- std::to_string(res.resultInt()));
+ res.resultInt());
}
public:
@@ -866,40 +843,34 @@
HttpClient& operator=(HttpClient&&) = delete;
~HttpClient() = default;
- // Send a request to destIP:destPort where additional processing of the
+ // Send a request to destIP where additional processing of the
// result is not required
- void sendData(std::string&& data, const std::string& destIP,
- uint16_t destPort, const std::string& destUri, bool useSSL,
+ void sendData(std::string&& data, boost::urls::url_view destUri,
const boost::beast::http::fields& httpHeader,
const boost::beast::http::verb verb)
{
const std::function<void(Response&)> cb = genericResHandler;
- sendDataWithCallback(std::move(data), destIP, destPort, destUri, useSSL,
- httpHeader, verb, cb);
+ sendDataWithCallback(std::move(data), destUri, httpHeader, verb, cb);
}
- // Send request to destIP:destPort and use the provided callback to
+ // Send request to destIP and use the provided callback to
// handle the response
- void sendDataWithCallback(std::string&& data, const std::string& destIP,
- uint16_t destPort, const std::string& destUri,
- bool useSSL,
+ void sendDataWithCallback(std::string&& data, boost::urls::url_view destUrl,
const boost::beast::http::fields& httpHeader,
const boost::beast::http::verb verb,
const std::function<void(Response&)>& resHandler)
{
- std::string clientKey = useSSL ? "https" : "http";
- clientKey += destIP;
- clientKey += ":";
- clientKey += std::to_string(destPort);
+ std::string clientKey = std::format("{}://{}", destUrl.scheme(),
+ destUrl.encoded_host_and_port());
auto pool = connectionPools.try_emplace(clientKey);
if (pool.first->second == nullptr)
{
pool.first->second = std::make_shared<ConnectionPool>(
- ioc, clientKey, connPolicy, destIP, destPort, useSSL);
+ ioc, clientKey, connPolicy, destUrl);
}
// Send the data using either the existing connection pool or the newly
// created connection pool
- pool.first->second->sendData(std::move(data), destUri, httpHeader, verb,
+ pool.first->second->sendData(std::move(data), destUrl, httpHeader, verb,
resHandler);
}
};