summaryrefslogtreecommitdiff
path: root/http
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2023-08-01 21:35:53 +0300
committerEd Tanous <ed@tanous.net>2023-08-23 19:55:21 +0300
commita716aa74954617e32a1d2e691d184580402b3eaf (patch)
tree836d67ff00ddb6611e69e7ce51d9f32901ba34e6 /http
parent3544d2a719c8bb7b07f9a39f61a3770ec84b909f (diff)
downloadbmcweb-a716aa74954617e32a1d2e691d184580402b3eaf.tar.xz
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>
Diffstat (limited to 'http')
-rw-r--r--http/http_client.hpp161
-rw-r--r--http/logging.hpp13
-rw-r--r--http/utility.hpp78
3 files changed, 100 insertions, 152 deletions
diff --git a/http/http_client.hpp b/http/http_client.hpp
index e1c4d374eb..2cbdbbcef1 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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
{
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
// 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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
// 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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
{
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
{
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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
// 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 @@ class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
}
}
- 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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
{
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
// 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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
}
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
// 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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
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 @@ class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
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 @@ class HttpClient
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 @@ class HttpClient
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);
}
};
diff --git a/http/logging.hpp b/http/logging.hpp
index 1d8e67ffb6..3fdef7e744 100644
--- a/http/logging.hpp
+++ b/http/logging.hpp
@@ -47,6 +47,19 @@ struct std::formatter<boost::urls::pct_string_view>
};
template <>
+struct std::formatter<boost::urls::url_view>
+{
+ constexpr auto parse(std::format_parse_context& ctx)
+ {
+ return ctx.begin();
+ }
+ auto format(const boost::urls::url& msg, auto& ctx) const
+ {
+ return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer()));
+ }
+};
+
+template <>
struct std::formatter<boost::urls::url>
{
constexpr auto parse(std::format_parse_context& ctx)
diff --git a/http/utility.hpp b/http/utility.hpp
index 572caec710..dc7ea7f1b2 100644
--- a/http/utility.hpp
+++ b/http/utility.hpp
@@ -445,91 +445,55 @@ inline boost::urls::url replaceUrlSegment(boost::urls::url_view urlView,
return url;
}
-inline std::string setProtocolDefaults(boost::urls::url_view urlView)
+inline void setProtocolDefaults(boost::urls::url& url,
+ std::string_view protocol)
{
- if (urlView.scheme() == "https")
+ if (url.has_scheme())
{
- return "https";
+ return;
}
- if (urlView.scheme() == "http")
+ if (protocol == "Redfish" || protocol.empty())
{
- if (bmcwebInsecureEnableHttpPushStyleEventing)
+ if (url.port_number() == 443)
{
- return "http";
+ url.set_scheme("https");
+ }
+ if (url.port_number() == 80)
+ {
+ if (bmcwebInsecureEnableHttpPushStyleEventing)
+ {
+ url.set_scheme("http");
+ }
}
- return "";
}
- if (urlView.scheme() == "snmp")
+ else if (protocol == "SNMPv2c")
{
- return "snmp";
+ url.set_scheme("snmp");
}
- return "";
}
-inline uint16_t setPortDefaults(boost::urls::url_view url)
+inline void setPortDefaults(boost::urls::url& url)
{
uint16_t port = url.port_number();
if (port != 0)
{
- // user picked a port already.
- return port;
+ return;
}
// If the user hasn't explicitly stated a port, pick one explicitly for them
// based on the protocol defaults
if (url.scheme() == "http")
{
- return 80;
+ url.set_port_number(80);
}
if (url.scheme() == "https")
{
- return 443;
+ url.set_port_number(443);
}
if (url.scheme() == "snmp")
{
- return 162;
- }
- return 0;
-}
-
-inline bool validateAndSplitUrl(std::string_view destUrl, std::string& urlProto,
- std::string& host, uint16_t& port,
- std::string& path)
-{
- boost::urls::result<boost::urls::url_view> url =
- boost::urls::parse_uri(destUrl);
- if (!url)
- {
- return false;
- }
- urlProto = setProtocolDefaults(url.value());
- if (urlProto.empty())
- {
- return false;
+ url.set_port_number(162);
}
-
- port = setPortDefaults(url.value());
-
- host = url->encoded_host_address();
-
- path = url->encoded_path();
- if (path.empty())
- {
- path = "/";
- }
- if (url->has_fragment())
- {
- path += '#';
- path += url->encoded_fragment();
- }
-
- if (url->has_query())
- {
- path += '?';
- path += url->encoded_query();
- }
-
- return true;
}
} // namespace utility