diff --git a/include/upa/url.h b/include/upa/url.h index accfcd9..52d2a2b 100644 --- a/include/upa/url.h +++ b/include/upa/url.h @@ -171,11 +171,21 @@ class url { /// @param[in] base pointer to base URL, may be nullptr /// @return error code (@a validation_errc::ok on success) template = 0> - validation_errc parse(T&& str_url, const url* base) { + validation_errc parse(T&& str_url, const url* base = nullptr) { const auto inp = make_str_arg(std::forward(str_url)); return do_parse(inp.begin(), inp.end(), base); } + /// @brief Parses given URL string against base URL. + /// + /// @param[in] str_url URL string to parse + /// @param[in] base base URL + /// @return error code (@a validation_errc::ok on success) + template = 0> + validation_errc parse(T&& str_url, const url& base) { + return parse(std::forward(str_url), &base); + } + /// @brief Checks if a given URL string can be successfully parsed /// /// If @a pbase is not nullptr, then try to parse against *pbase URL. @@ -1086,7 +1096,7 @@ inline std::string url::origin() const { // Note: this library does not support blob URL store, so it allways assumes // URL's blob URL entry is null and retrieves origin from the URL's path. url path_url; - if (path_url.parse(get_part_view(PATH), nullptr) == validation_errc::ok && + if (path_url.parse(get_part_view(PATH)) == validation_errc::ok && path_url.is_http_scheme()) return path_url.origin(); } diff --git a/test/test-url.cpp b/test/test-url.cpp index d184302..bded516 100644 --- a/test/test-url.cpp +++ b/test/test-url.cpp @@ -180,6 +180,22 @@ TEST_CASE("url parsing constructor with base URL string") { // Parse URL +TEST_CASE("Two url::parse functions") { + upa::url url; + upa::url url_base; + + // first url::parse function + CHECK(url_base.parse("http://example.org") == upa::validation_errc::ok); + CHECK(url_base.href() == "http://example.org/"); + + CHECK(url.parse("/htap", &url_base) == upa::validation_errc::ok); + CHECK(url.href() == "http://example.org/htap"); + + // second url::parse function + CHECK(url.parse("/path", url_base) == upa::validation_errc::ok); + CHECK(url.href() == "http://example.org/path"); +} + TEST_CASE("url::parse must clear old URL data") { upa::url url;