Boost uri update
Update to the latest version of boost::uri
The newest version of boost uri makes some breaking changes that we need
to account for. At the same time, we take the opportunity to move to
the error code based parse methods that don't rely on exceptions.
The biggest changes are:
The standalone build is no longer present. A discussion with the
boost::url maintainers shows that our best option is to do a simple
copy of the headers, and compile boost/url/src.hpp in a separate file.
This is intended to allow people to pull the library in "standalone" and
not have to rely on the build machinery in boost-url, which we don't
really need. Interestingly, this file doesn't have a newline at the
end, which clang correctly flags. OpenBMC doesn't really need that
warning, as we rely on clang-format to do that, so we add
-Wno-newline-eof clang to get the code to compile there.
All url parsers are moved to the parse_uri, or parse_relative_uri
equivalents. This slightly tightens the requirements around what URLs
are accepted, but in no ways that should break anything. (Ie,
"/redfish/v1" is no longer accepted for a virtual media endpoint.
boost::urls::url_view::params_type has been renamed to
query_params_type, and the relevant methods have been updated.
Because of the missing standalone mode, we now need to use
boost::string_view which doesn't implicitly construct from
std::string_view. Some discussion on the boost list shows that this is
coming soon, so that cruft can eventually be cleaned up, but for now we
need the construction.
Tested:
Loaded in qemu, and ran some URLs (/redfish/v1 and /redfish/v1/Chassis)
to ensure that the url handler functions as intended.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I5843776d4ec01b4d92af2ee3a9cf1ebb1d920ae7
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index ae0477a..3720d9d 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -16,6 +16,7 @@
#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/ssl/ssl_stream.hpp>
#include <boost/beast/websocket.hpp>
+#include <boost/url/url_view.hpp>
#include <json_html_serializer.hpp>
#include <security_headers.hpp>
#include <ssl_key_handler.hpp>
@@ -336,15 +337,18 @@
<< "." << thisReq.version() % 10 << ' '
<< thisReq.methodString() << " " << thisReq.target()
<< " " << thisReq.ipAddress;
- try
+
+ boost::urls::error_code ec;
+ req->urlView = boost::urls::parse_relative_ref(
+ boost::urls::string_view(req->target().data(),
+ req->target().size()),
+ ec);
+ if (ec)
{
- thisReq.urlView = boost::urls::url_view(thisReq.target());
- thisReq.url = thisReq.urlView.encoded_path();
+ return;
}
- catch (std::exception& p)
- {
- BMCWEB_LOG_ERROR << p.what();
- }
+ req->url = std::string_view(req->urlView.encoded_path().data(),
+ req->urlView.encoded_path().size());
res.setCompleteRequestHandler(nullptr);
res.isAliveHelper = [this]() -> bool { return isAlive(); };
@@ -547,16 +551,21 @@
boost::beast::http::verb method = parser->get().method();
readClientIp();
- try
+ boost::urls::error_code uriEc;
+ boost::urls::string_view uriStringView(
+ parser->get().target().data(),
+ parser->get().target().size());
+ BMCWEB_LOG_DEBUG << "Parsing URI: " << uriStringView;
+ req->urlView =
+ boost::urls::parse_relative_ref(uriStringView, uriEc);
+ if (uriEc)
{
- req->urlView =
- boost::urls::url_view(parser->get().target());
- req->url = req->urlView.encoded_path();
+ BMCWEB_LOG_ERROR << "Failed to parse URI "
+ << uriEc.message();
+ return;
}
- catch (std::exception& p)
- {
- BMCWEB_LOG_ERROR << p.what();
- }
+ req->url = std::string_view(req->urlView.encoded_path().data(),
+ req->urlView.encoded_path().size());
boost::asio::ip::address ip;
if (getClientIp(ip))
@@ -573,7 +582,7 @@
startDeadline(loggedInAttempts);
BMCWEB_LOG_DEBUG << "Starting slow deadline";
- req->urlParams = req->urlView.params();
+ req->urlParams = req->urlView.query_params();
#ifdef BMCWEB_ENABLE_DEBUG
std::string paramList = "";
diff --git a/http/http_request.hpp b/http/http_request.hpp
index fecb9de..da3d4e0 100644
--- a/http/http_request.hpp
+++ b/http/http_request.hpp
@@ -22,7 +22,7 @@
boost::beast::http::fields& fields;
std::string_view url{};
boost::urls::url_view urlView{};
- boost::urls::url_view::params_type urlParams{};
+ boost::urls::query_params_view urlParams{};
bool isSecure{false};
const std::string& body;
diff --git a/meson.build b/meson.build
index 2bbc0d9..650a5ec 100644
--- a/meson.build
+++ b/meson.build
@@ -151,6 +151,7 @@
'-Wswitch-enum',
'-Wnull-dereference',
'-Wdouble-promotion',
+ '-Wno-newline-eof',
'-Wformat=2',
]),
language:'cpp')
@@ -363,7 +364,8 @@
srcfiles_bmcweb = [
'src/webserver_main.cpp',
'redfish-core/src/error_messages.cpp',
- 'redfish-core/src/utils/json_utils.cpp'
+ 'redfish-core/src/utils/json_utils.cpp',
+ 'src/boost_url.cpp'
]
srcfiles_unittest = [
@@ -433,7 +435,9 @@
if(get_option('tests').enabled())
foreach src_test : srcfiles_unittest
testname = src_test.split('/')[-1].split('.')[0]
- test(testname,executable(testname,src_test,
+ test(testname,executable(testname,
+ [src_test,
+ 'src/boost_url.cpp'],
include_directories : incdir,
install_dir: bindir,
dependencies: [
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index 12ec64a..2c9ae3a 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -188,8 +188,7 @@
static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const crow::Request& req, uint64_t& skip)
{
- boost::urls::url_view::params_type::iterator it =
- req.urlParams.find("$skip");
+ boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
if (it != req.urlParams.end())
{
std::string skipParam = it->value();
@@ -210,8 +209,7 @@
static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
const crow::Request& req, uint64_t& top)
{
- boost::urls::url_view::params_type::iterator it =
- req.urlParams.find("$top");
+ boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
if (it != req.urlParams.end())
{
std::string topParam = it->value();
diff --git a/redfish-core/lib/virtual_media.hpp b/redfish-core/lib/virtual_media.hpp
index 6e69f20..7383477 100644
--- a/redfish-core/lib/virtual_media.hpp
+++ b/redfish-core/lib/virtual_media.hpp
@@ -33,22 +33,23 @@
*/
inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
{
- try
+ boost::urls::error_code ec;
+ boost::urls::url_view url =
+ boost::urls::parse_uri(boost::string_view(imageUri), ec);
+ if (ec)
{
- std::string_view scheme = boost::urls::url_view(imageUri).scheme();
- if (scheme == "smb")
- {
- return "CIFS";
- }
- if (scheme == "https")
- {
- return "HTTPS";
- }
+ return "None";
}
- catch (std::exception& p)
+ boost::string_view scheme = url.scheme();
+ if (scheme == "smb")
{
- BMCWEB_LOG_ERROR << p.what();
+ return "CIFS";
}
+ if (scheme == "https")
+ {
+ return "HTTPS";
+ }
+
return "None";
}
@@ -325,25 +326,26 @@
inline std::optional<TransferProtocol>
getTransferProtocolFromUri(const std::string& imageUri)
{
- try
+ boost::urls::error_code ec;
+ boost::urls::url_view url =
+ boost::urls::parse_uri(boost::string_view(imageUri), ec);
+ if (ec)
{
- std::string_view scheme = boost::urls::url_view(imageUri).scheme();
- if (scheme == "smb")
- {
- return TransferProtocol::smb;
- }
- if (scheme == "https")
- {
- return TransferProtocol::https;
- }
- if (!scheme.empty())
- {
- return TransferProtocol::invalid;
- }
+ return {};
}
- catch (std::exception& p)
+
+ boost::string_view scheme = url.scheme();
+ if (scheme == "smb")
{
- BMCWEB_LOG_ERROR << p.what();
+ return TransferProtocol::smb;
+ }
+ if (scheme == "https")
+ {
+ return TransferProtocol::https;
+ }
+ if (!scheme.empty())
+ {
+ return TransferProtocol::invalid;
}
return {};
diff --git a/src/boost_url.cpp b/src/boost_url.cpp
new file mode 100644
index 0000000..ce55e9b
--- /dev/null
+++ b/src/boost_url.cpp
@@ -0,0 +1,2 @@
+
+#include <boost/url/src.hpp>
\ No newline at end of file
diff --git a/subprojects/boost-url.wrap b/subprojects/boost-url.wrap
index c41d2d1..3d00dcf 100644
--- a/subprojects/boost-url.wrap
+++ b/subprojects/boost-url.wrap
@@ -1,3 +1,3 @@
[wrap-git]
-revision = a56ae0df6d3078319755fbaa67822b4fa7fd352b
+revision = 4f712ed69a04a344957d22efa5dc111b415b3aff
url = https://github.com/CPPAlliance/url.git
\ No newline at end of file