Skip to content

Commit

Permalink
Add defaulted host to URL
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Oct 5, 2023
1 parent 58c75e6 commit 99b3f2d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions libmamba/include/mamba/specs/conda_url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace mamba::specs
using Base::set_password;
using Base::clear_password;
using Base::authentication;
using Base::host_is_defaulted;
using Base::host;
using Base::set_host;
using Base::clear_host;
Expand Down
5 changes: 4 additions & 1 deletion libmamba/include/mamba/util/url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace mamba::util
/** Set a non-empty scheme. */
void set_scheme(std::string_view scheme);

/** Clear the scheme back to a defaulted value and return the old value (or empty). */
/** Clear the scheme back to a defaulted value and return the old value. */
auto clear_scheme() -> std::string;

/** Return the encoded user, or empty if none. */
Expand Down Expand Up @@ -129,6 +129,9 @@ namespace mamba::util
/** Return the encoded basic authentication string. */
[[nodiscard]] auto authentication() const -> std::string;

/** Return whether the host was defaulted, i.e. not explicitly set. */
[[nodiscard]] auto host_is_defaulted() const -> bool;

/** Return the encoded host, always non-empty except for file scheme. */
[[nodiscard]] auto host(Decode::no_type) const -> std::string_view;

Expand Down
18 changes: 12 additions & 6 deletions libmamba/src/util/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ namespace mamba::util

auto URL::clear_scheme() -> std::string
{
if (scheme_is_defaulted())
{
return std::string(https);
}
return std::exchange(m_scheme, "");
}

Expand Down Expand Up @@ -273,9 +277,14 @@ namespace mamba::util
return p.empty() ? u : util::concat(u, ':', p);
}

auto URL::host_is_defaulted() const -> bool
{
return m_host.empty();
}

auto URL::host(Decode::no_type) const -> std::string_view
{
if ((scheme() != "file") && m_host.empty())
if ((scheme() != "file") && host_is_defaulted())
{
return localhost;
}
Expand Down Expand Up @@ -305,12 +314,9 @@ namespace mamba::util

auto URL::clear_host() -> std::string
{
// Cheap == comparison that works because of class invariant
if (auto l_host = host(Decode::no); l_host.data() != m_host.data())
if (host_is_defaulted())
{
auto out = std::string(l_host);
set_host("", Encode::no);
return out;
return std::string(host(Decode::no));
}
return std::exchange(m_host, "");
}
Expand Down
23 changes: 23 additions & 0 deletions libmamba/tests/src/util/test_url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ TEST_SUITE("util::URL")
url.set_scheme("https");
}

SUBCASE("Default host")
{
URL url{};
CHECK(url.host_is_defaulted());
CHECK_EQ(url.host(), "localhost");

url.set_host("localhost");
CHECK_FALSE(url.host_is_defaulted());
CHECK_EQ(url.host(), "localhost");

url.set_host("");
CHECK(url.host_is_defaulted());
url.set_host("localhost");

url.set_host("test.org");
CHECK_FALSE(url.host_is_defaulted());
CHECK_EQ(url.host(), "test.org");

CHECK_EQ(url.clear_host(), "test.org");
CHECK(url.host_is_defaulted());
url.set_host("localhost");
}

SUBCASE("Invalid")
{
URL url{};
Expand Down

0 comments on commit 99b3f2d

Please sign in to comment.