Skip to content

Commit

Permalink
fix the problem of fulldomain and remove host name
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadRaziei committed Nov 13, 2023
1 parent f029261 commit ae8e16c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
11 changes: 9 additions & 2 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main() {


tic;
const auto hostName = url.hostName();
const auto hostName = url.fulldomain();
toc;
tic;
const auto host = url.host();
Expand Down Expand Up @@ -146,7 +146,14 @@ int main() {
show(TLD::Url("http://mohammad:123@www.google.com?about", true).host());
show(TLD::Url("https://www.p30download.ir", false).host());

show(TLD::Url("http://mohammad:123@www.google.com?about", true).hostName()); // TODO: fix hostName Issue for ignore_www
show(TLD::Url("http://mohammad:123@www.google.com?about", true).fulldomain()); // TODO: fix hostName Issue for ignore_www
show(TLD::Host("http://mohammad:123@www.google.com?about", true) == "google.com"); // TODO: fix hostName Issue for ignore_www
const TLD::Host host2 = TLD::Host::fromUrl("http://mohammad:123@www.google.com?about", true);
const TLD::Url url2("http://mohammad:123@www.google.com?about", true);
tic;
const auto a = url2.fulldomain();
toc;
show(a); // TODO: add clone version to copy without sharing

printf("\ngood bye :)\n");
return 0;
Expand Down
3 changes: 2 additions & 1 deletion include/urlparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Url {
const int port() const noexcept;
QueryParams params() const noexcept;
const Host& host() const;
const std::string& hostName() const;

private:
class Impl;
Expand All @@ -56,9 +55,11 @@ class Host {
static void loadPslFromPath(const std::string& filepath);
static void loadPslFromString(const std::string& filestr);
static bool isPslLoaded() noexcept;
static std::string_view removeWWW(const std::string_view&) noexcept;

public:
Host(const std::string& host, const bool ignore_www = DEFAULT_IGNORE_WWW);
Host() = default;

bool operator==(const Host&) const;
bool operator==(const std::string&) const;
Expand Down
4 changes: 3 additions & 1 deletion src/binding/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ NB_MODULE(_core, m) {
.def_static("load_psl_from_string", &TLD::Host::loadPslFromString,
nb::arg("string"))
.def_static("is_psl_loaded", &TLD::Host::isPslLoaded)
.def_static("removeWWW", &TLD::Host::removeWWW, nb::arg("hoststr"))
.def_prop_ro("subdomain", &TLD::Host::subdomain)
.def_prop_ro("domain", &TLD::Host::domain)
.def_prop_ro("domain_name", &TLD::Host::domainName)
.def_prop_ro("fulldomain", &TLD::Host::fulldomain)
.def_prop_ro("suffix", &TLD::Host::suffix)
// .def("__eq__", &TLD::Host::operator==) // TODO: fix this operator for 2 types
.def("to_dict", host_to_dict)
.def("to_json", host_to_json)
.def("__str__", &TLD::Host::str)
Expand All @@ -119,7 +121,6 @@ NB_MODULE(_core, m) {
.def_prop_ro("protocol", &TLD::Url::protocol)
.def_prop_ro("userinfo", &TLD::Url::userinfo)
.def_prop_ro("host", &TLD::Url::host)
.def_prop_ro("host_name", &TLD::Url::hostName)
.def_prop_ro("subdomain", &TLD::Url::subdomain)
.def_prop_ro("domain", &TLD::Url::domain)
.def_prop_ro("fulldomain", &TLD::Url::fulldomain)
Expand All @@ -129,6 +130,7 @@ NB_MODULE(_core, m) {
.def_prop_ro("params", &TLD::Url::params)
.def_prop_ro("query", &TLD::Url::query)
.def_prop_ro("fragment", &TLD::Url::fragment)
// .def("__eq__", &TLD::Url::operator==) // TODO: fix this operator for 2 types
.def("to_dict", url_to_dict)
.def("to_json", url_to_json)
.def("__str__", &TLD::Url::str)
Expand Down
9 changes: 8 additions & 1 deletion src/urlparser_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TLD::Host::Impl::Impl(const std::string& host_, const bool ignore_www)
if (www_pos != std::string::npos)
return;
} else {
subdomain_pos = 4;
subdomain_pos = 4; // length of "www."
fulldomain_ = fulldomain_.substr(4);
}
}
Expand Down Expand Up @@ -168,3 +168,10 @@ bool TLD::Host::operator==(const TLD::Host& other) const {
bool TLD::Host::operator==(const std::string& host) const {
return impl->fulldomain() == host;
}
std::string_view TLD::Host::removeWWW(const std::string_view& host) noexcept {
if (const size_t www_pos = host.find("www.");
www_pos == std::string_view::npos || www_pos != 0) {
return host;
}
return host.substr(4); // 4 is the length of the "www."
}
20 changes: 12 additions & 8 deletions src/urlparser_url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class TLD::Url::Impl : public URL::Url {
Impl(const std::string& url, const bool ignore_www);

const TLD::Host* getHost() noexcept;
inline const std::string& hostName();

private:
std::unique_ptr<TLD::Host> host_obj = nullptr;
const bool ignore_www;
const bool ignore_www = DEFAULT_IGNORE_WWW;
};

inline std::vector<std::string> split(const std::string& str,
Expand All @@ -43,25 +44,28 @@ bool TLD::Url::isPslLoaded() noexcept {
////////////////////////////////////////////////////////////////////

TLD::Url::Impl::Impl(const std::string& url, const bool ignore_www)
: URL::Url(url), ignore_www(ignore_www) {}
: URL::Url(url) , ignore_www(ignore_www) {}

TLD::Url::Url(const std::string& url, const bool ignore_www)
: impl(std::make_unique<TLD::Url::Impl>(url, ignore_www)) {}

const TLD::Host* TLD::Url::Impl::getHost() noexcept {
if (!host_obj)
host_obj = std::make_unique<TLD::Host>(host_, ignore_www);
host_obj = std::make_unique<TLD::Host>(hostName(), false);
/// we set ignore_www to false because we remove www in hostName function
return host_obj.get();
}
const std::string& TLD::Url::Impl::hostName() {
if (ignore_www){
host_ = TLD::Host::removeWWW(host_);
}
return host_;
}

const TLD::Host& TLD::Url::host() const {
return *impl->getHost();
}

const std::string& TLD::Url::hostName() const {
return impl->host_;
}

/// suffix
const std::string& TLD::Url::suffix() const noexcept {
return impl->getHost()->suffix();
Expand All @@ -79,7 +83,7 @@ const std::string& TLD::Url::domain() const noexcept {

/// fulldomain
const std::string& TLD::Url::fulldomain() const noexcept {
return impl->getHost()->fulldomain();
return impl->hostName();
}

/// domainName
Expand Down

0 comments on commit ae8e16c

Please sign in to comment.