Skip to content

Commit

Permalink
Add default nullptr value to base parameter of url::parse
Browse files Browse the repository at this point in the history
Also add url::parse function with `const url& base` parameter.
  • Loading branch information
rmisev committed Oct 19, 2023
1 parent 877bcfe commit 9eec5ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions include/upa/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T, enable_if_str_arg_t<T> = 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<T>(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 <class T, enable_if_str_arg_t<T> = 0>
validation_errc parse(T&& str_url, const url& base) {
return parse(std::forward<T>(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.
Expand Down Expand Up @@ -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();
}
Expand Down
16 changes: 16 additions & 0 deletions test/test-url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 9eec5ee

Please sign in to comment.