diff options
author | Ed Tanous <edtanous@google.com> | 2021-09-14 23:16:51 +0300 |
---|---|---|
committer | Ed Tanous <edtanous@google.com> | 2021-10-05 20:51:01 +0300 |
commit | d32c4fa936f9a88fc1dd124201ed11a6a90a18c5 (patch) | |
tree | fad1030fb5244223fa81ae84b0e4cea8b0bdbd44 | |
parent | b7ff344535c42af074c60bfb272ef66a2ba157b4 (diff) | |
download | bmcweb-d32c4fa936f9a88fc1dd124201ed11a6a90a18c5.tar.xz |
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
-rw-r--r-- | http/http_connection.hpp | 41 | ||||
-rw-r--r-- | http/http_request.hpp | 2 | ||||
-rw-r--r-- | meson.build | 8 | ||||
-rw-r--r-- | redfish-core/lib/log_services.hpp | 6 | ||||
-rw-r--r-- | redfish-core/lib/virtual_media.hpp | 58 | ||||
-rw-r--r-- | src/boost_url.cpp | 2 | ||||
-rw-r--r-- | subprojects/boost-url.wrap | 2 |
7 files changed, 67 insertions, 52 deletions
diff --git a/http/http_connection.hpp b/http/http_connection.hpp index ae0477ab32..3720d9d355 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 @@ class Connection : << "." << thisReq.version() % 10 << ' ' << thisReq.methodString() << " " << thisReq.target() << " " << thisReq.ipAddress; - try - { - thisReq.urlView = boost::urls::url_view(thisReq.target()); - thisReq.url = thisReq.urlView.encoded_path(); - } - catch (std::exception& p) + + 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) { - BMCWEB_LOG_ERROR << p.what(); + return; } + 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 @@ class Connection : boost::beast::http::verb method = parser->get().method(); readClientIp(); - try - { - req->urlView = - boost::urls::url_view(parser->get().target()); - req->url = req->urlView.encoded_path(); - } - catch (std::exception& p) + 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) { - BMCWEB_LOG_ERROR << p.what(); + BMCWEB_LOG_ERROR << "Failed to parse URI " + << uriEc.message(); + return; } + 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 @@ class Connection : 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 fecb9de3fb..da3d4e0047 100644 --- a/http/http_request.hpp +++ b/http/http_request.hpp @@ -22,7 +22,7 @@ struct Request 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 2bbc0d9dab..650a5ec688 100644 --- a/meson.build +++ b/meson.build @@ -151,6 +151,7 @@ add_project_arguments( '-Wswitch-enum', '-Wnull-dereference', '-Wdouble-promotion', + '-Wno-newline-eof', '-Wformat=2', ]), language:'cpp') @@ -363,7 +364,8 @@ endif 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 @@ executable('bmcweb',srcfiles_bmcweb, 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 12ec64a880..2c9ae3a9ce 100644 --- a/redfish-core/lib/log_services.hpp +++ b/redfish-core/lib/log_services.hpp @@ -188,8 +188,7 @@ inline static bool getEntryTimestamp(sd_journal* journal, 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 constexpr const uint64_t maxEntriesPerPage = 1000; 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 6e69f20eac..7383477b3f 100644 --- a/redfish-core/lib/virtual_media.hpp +++ b/redfish-core/lib/virtual_media.hpp @@ -33,22 +33,23 @@ namespace redfish */ 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"; + } + boost::string_view scheme = url.scheme(); + if (scheme == "smb") + { + return "CIFS"; } - catch (std::exception& p) + if (scheme == "https") { - BMCWEB_LOG_ERROR << p.what(); + return "HTTPS"; } + return "None"; } @@ -325,25 +326,26 @@ enum class TransferProtocol 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 {}; + } + + boost::string_view scheme = url.scheme(); + if (scheme == "smb") + { + return TransferProtocol::smb; + } + if (scheme == "https") + { + return TransferProtocol::https; } - catch (std::exception& p) + if (!scheme.empty()) { - BMCWEB_LOG_ERROR << p.what(); + return TransferProtocol::invalid; } return {}; diff --git a/src/boost_url.cpp b/src/boost_url.cpp new file mode 100644 index 0000000000..ce55e9b9c3 --- /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 c41d2d119e..3d00dcf8cc 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 |