diff options
author | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 20:55:44 -0700 |
---|---|---|
committer | Richard Walters <rwalters@digitalstirling.com> | 2018-06-30 20:55:44 -0700 |
commit | 58e2beb7717cf724ae37e03f2e5bf3afbfc23a35 (patch) | |
tree | 6d56372959fb135e1f37717d9e9ffcb24658efd7 | |
parent | a84a28c61ff44f714edf31e77500d796e3bd4ce4 (diff) |
Uri: fix mistakes from last session
* Parts of a path are called "segments", not "steps",
in the RFC.
* The RFC specifies that path separators are always
forward slashes, so don't support other separators.
-rw-r--r-- | include/Uri/Uri.hpp | 16 | ||||
-rw-r--r-- | src/Uri.cpp | 20 | ||||
-rw-r--r-- | test/src/UriTests.cpp | 32 |
3 files changed, 8 insertions, 60 deletions
diff --git a/include/Uri/Uri.hpp b/include/Uri/Uri.hpp index 6103bff..fb1e5c8 100644 --- a/include/Uri/Uri.hpp +++ b/include/Uri/Uri.hpp @@ -36,16 +36,6 @@ namespace Uri { Uri(); /** - * This method sets the character or character sequence - * that should be interpreted as a path delimiter. - * - * @param[in] newPathDelimiter - * This is the character or character sequence - * that should be interpreted as a path delimiter. - */ - void SetPathDelimiter(const std::string& newPathDelimiter); - - /** * This method builds the URI from the elements parsed * from the given string rendering of a URI. * @@ -82,15 +72,15 @@ namespace Uri { /** * This method returns the "path" element of the URI, - * as a sequence of steps. + * as a sequence of segments. * * @note - * If the first step of the path is an empty string, + * If the first segment of the path is an empty string, * then the URI has an absolute path. * * @return * The "path" element of the URI is returned - * as a sequence of steps. + * as a sequence of segments. */ std::vector< std::string > GetPath() const; diff --git a/src/Uri.cpp b/src/Uri.cpp index 287d3ba..c028443 100644 --- a/src/Uri.cpp +++ b/src/Uri.cpp @@ -16,12 +16,6 @@ namespace Uri { */ struct Uri::Impl { /** - * This is the character or character sequence - * that should be interpreted as a path delimiter. - */ - std::string pathDelimiter = "/"; - - /** * This is the "scheme" element of the URI. */ std::string scheme; @@ -33,7 +27,7 @@ namespace Uri { /** * This is the "path" element of the URI, - * as a sequence of steps. + * as a sequence of segments. */ std::vector< std::string > path; }; @@ -45,10 +39,6 @@ namespace Uri { { } - void Uri::SetPathDelimiter(const std::string& newPathDelimiter) { - impl_->pathDelimiter = newPathDelimiter; - } - bool Uri::ParseFromString(const std::string& uriString) { // First parse the scheme. const auto schemeEnd = uriString.find(':'); @@ -57,7 +47,7 @@ namespace Uri { // Next parse the host. if (rest.substr(0, 2) == "//") { - const auto authorityEnd = rest.find(impl_->pathDelimiter, 2); + const auto authorityEnd = rest.find('/', 2); impl_->host = rest.substr(2, authorityEnd - 2); rest = rest.substr(authorityEnd); } else { @@ -66,13 +56,13 @@ namespace Uri { // Finally, parse the path. impl_->path.clear(); - if (rest == impl_->pathDelimiter) { + if (rest == "/") { // Special case of a path that is empty but needs a single // empty-string element to indicate that it is absolute. impl_->path.push_back(""); } else if (!rest.empty()) { for(;;) { - auto pathDelimiter = rest.find(impl_->pathDelimiter); + auto pathDelimiter = rest.find('/'); if (pathDelimiter == std::string::npos) { impl_->path.push_back(rest); break; @@ -81,7 +71,7 @@ namespace Uri { rest.begin(), rest.begin() + pathDelimiter ); - rest = rest.substr(pathDelimiter + impl_->pathDelimiter.length()); + rest = rest.substr(pathDelimiter + 1); } } } diff --git a/test/src/UriTests.cpp b/test/src/UriTests.cpp index 73848a9..2f8f28e 100644 --- a/test/src/UriTests.cpp +++ b/test/src/UriTests.cpp @@ -38,38 +38,6 @@ TEST(UriTests, ParseFromStringUrnDefaultPathDelimiter) { ); } -TEST(UriTests, ParseFromStringUrnSingleCharacterPathDelimiter) { - Uri::Uri uri; - uri.SetPathDelimiter(":"); - ASSERT_TRUE(uri.ParseFromString("urn:book:fantasy:Hobbit")); - ASSERT_EQ("urn", uri.GetScheme()); - ASSERT_EQ("", uri.GetHost()); - ASSERT_EQ( - (std::vector< std::string >{ - "book", - "fantasy", - "Hobbit", - }), - uri.GetPath() - ); -} - -TEST(UriTests, ParseFromStringUrnMultiCharacterPathDelimiter) { - Uri::Uri uri; - uri.SetPathDelimiter("/-"); - ASSERT_TRUE(uri.ParseFromString("urn:bo-/ok/-fant/asy/-Hob-bit")); - ASSERT_EQ("urn", uri.GetScheme()); - ASSERT_EQ("", uri.GetHost()); - ASSERT_EQ( - (std::vector< std::string >{ - "bo-/ok", - "fant/asy", - "Hob-bit", - }), - uri.GetPath() - ); -} - TEST(UriTests, ParseFromStringPathCornerCases) { struct TestVector { std::string pathIn; |