Loading...
Searching...
No Matches
urlparser.h
Go to the documentation of this file.
1
37#ifndef TLD_URLPARSER_H
38#define TLD_URLPARSER_H
39
40#include <iostream>
41#include <memory>
42#include <string>
43#include <vector>
44
45#ifndef PUBLIC_SUFFIX_LIST_DAT
46#define PUBLIC_SUFFIX_LIST_DAT "public_suffix_list.dat"
47#endif
48
49namespace TLD {
50constexpr bool DEFAULT_IGNORE_WWW = false;
51
56using QueryParams = std::vector<std::string>;
57
58class Host;
59
67class Url {
68 public:
73 static bool isPslLoaded() noexcept;
74
80 static std::string extractHost(const std::string& url) noexcept;
81
82 public:
89 Url(const std::string& url, const bool ignore_www = DEFAULT_IGNORE_WWW);
90
95 Url() noexcept = default;
96
102 bool operator==(const Url& other) const;
103
108 std::string str() const noexcept;
109
114 const std::string& protocol() const noexcept;
115
120 const std::string& subdomain() const noexcept;
121
126 const std::string& domain() const noexcept;
127
132 const std::string& suffix() const noexcept;
133
138 const std::string& query() const noexcept;
139
144 const std::string& fragment() const noexcept;
145
150 const std::string& userinfo() const noexcept;
151
156 std::string abspath() const noexcept;
157
162 std::string domainName() const noexcept;
163
168 const std::string& fulldomain() const noexcept;
169
174 const int port() const noexcept;
175
180 QueryParams params() const noexcept;
181
186 const Host& host() const;
187
188 private:
189 class Impl;
190 std::shared_ptr<Impl> impl; // since all methods are constants
191};
192
200class Host {
201 public:
208 static Host fromUrl(const std::string& url,
209 const bool ignore_www = DEFAULT_IGNORE_WWW);
210
216 static void loadPslFromPath(const std::string& filepath);
217
223 static void loadPslFromString(const std::string& filestr);
224
229 static bool isPslLoaded() noexcept;
230
236 static std::string_view removeWWW(const std::string_view& host) noexcept;
237
238 public:
245 Host(const std::string& host, const bool ignore_www = DEFAULT_IGNORE_WWW);
246
251 Host() = default;
252
258 bool operator==(const Host& other) const;
259
265 bool operator==(const std::string& other) const;
266
271 const std::string& suffix() const noexcept;
272
277 const std::string& domain() const noexcept;
278
283 std::string domainName() const noexcept;
284
289 const std::string& subdomain() const noexcept;
290
295 const std::string& fulldomain() const noexcept;
296
301 const std::string& str() const noexcept;
302
303 private:
304 class Impl;
305 std::shared_ptr<Impl> impl; // since all methods are constants
306};
307} // namespace TLD
308
315std::ostream& operator<<(std::ostream& os, const TLD::QueryParams& dt);
316
323std::ostream& operator<<(std::ostream& os, const TLD::Url& dt);
324
331std::ostream& operator<<(std::ostream& os, const TLD::Host& dt);
332#endif // TLD_URLPARSER_H
Represents a Host part of a URL.
Definition urlparser.h:200
static Host fromUrl(const std::string &url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Create a Host object from a URL string.
static void loadPslFromPath(const std::string &filepath)
Load the Public Suffix List from a file.
static void loadPslFromString(const std::string &filestr)
Load the Public Suffix List from a string.
static bool isPslLoaded() noexcept
Check if the Public Suffix List (PSL) is loaded.
Represents a URL.
Definition urlparser.h:67
static std::string extractHost(const std::string &url) noexcept
Extract the host from a given URL.
static bool isPslLoaded() noexcept
Check if the Public Suffix List (PSL) is loaded.
const std::string & protocol() const noexcept
Get the protocol of the URL.
const std::string & query() const noexcept
Get the query part of the URL.
const std::string & userinfo() const noexcept
Get the userinfo part of the URL.
const std::string & fulldomain() const noexcept
Get the full domain of the URL.
const std::string & suffix() const noexcept
Get the suffix of the URL.
const std::string & fragment() const noexcept
Get the fragment part of the URL.
const int port() const noexcept
Get the port of the URL.
std::string domainName() const noexcept
Get the domain name of the URL.
const Host & host() const
Get the host object of the URL.
QueryParams params() const noexcept
Get the parameters of the URL.
Url() noexcept=default
Default constructor for the Url class. Creates an empty URL object.
const std::string & domain() const noexcept
Get the domain of the URL.
const std::string & subdomain() const noexcept
Get the subdomain of the URL.
std::string str() const noexcept
Get the complete URL as a string.
std::string abspath() const noexcept
Get the absolute path of the URL.
Definition urlparser.h:49
std::vector< std::string > QueryParams
A vector of strings representing query parameters in a URL.
Definition urlparser.h:56
constexpr bool DEFAULT_IGNORE_WWW
Definition urlparser.h:50