Add UrlFromPieces helper function
This commit attempts to improve our ability to encode URIs from pieces
of a string. In the past, we've used std::string::operator+= for this,
which has problems in that bad characters are not encoded correctly into
a URI. As an example, if we got a dbus path with _2F (ascii /) in it,
our current code would push that directly into the uri and break the
redfish tree.
Examples of use are provided in the unit tests.
Tested:
Unit tests pass, no functional changes yet.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I5801d2146a5c948396d4766ac96f1f2b25205a0f
diff --git a/http/ut/utility_test.cpp b/http/ut/utility_test.cpp
index 5dafdce..eecd1f6 100644
--- a/http/ut/utility_test.cpp
+++ b/http/ut/utility_test.cpp
@@ -107,5 +107,21 @@
EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
"1970-01-01T00:00:00+00:00");
}
+
+TEST(Utility, UrlFromPieces)
+{
+ using crow::utility::urlFromPieces;
+ boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
+
+ url = urlFromPieces("/", "badString");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString");
+
+ url = urlFromPieces("bad?tring");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring");
+
+ url = urlFromPieces("/", "bad&tring");
+ EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring");
+}
} // namespace
} // namespace crow::utility