50#define URLPARSER_VERSION_MAJOR 2
51#define URLPARSER_VERSION_MINOR 3
52#define URLPARSER_VERSION_PATCH 3
54#define URLPARSER_VERSION_ENCODE(maj,min,pat) (((maj)*10000)+((min)*100)+(pat))
55#define URLPARSER_VERSION \
56 URLPARSER_VERSION_ENCODE(URLPARSER_VERSION_MAJOR,URLPARSER_VERSION_MINOR,URLPARSER_VERSION_PATCH)
59#define URLPARSER_VERSION_HEX \
60 ((URLPARSER_VERSION_MAJOR << 16) | (URLPARSER_VERSION_MINOR << 8) | URLPARSER_VERSION_PATCH)
63#define _URLPARSER_VERSION_XSTR(a,b,c) #a"."#b"."#c
64#define _URLPARSER_VERSION_STR(a,b,c) _URLPARSER_VERSION_XSTR(a,b,c)
67#define URLPARSER_VERSION_STRING \
68 _URLPARSER_VERSION_STR(URLPARSER_VERSION_MAJOR,URLPARSER_VERSION_MINOR,URLPARSER_VERSION_PATCH)
76#define URLPARSER_PUBLIC_SUFFIX_LIST_URL "https://publicsuffix.org/list/public_suffix_list.dat"
93# if defined(URLPARSER_STATIC)
95# elif defined(_WIN32) || defined(__CYGWIN__)
96# ifdef URLPARSER_BUILDING
97# define URLPARSER_API __declspec(dllexport)
99# define URLPARSER_API __declspec(dllimport)
101# elif defined(__GNUC__) || defined(__clang__)
102# define URLPARSER_API __attribute__((visibility("default")))
104# define URLPARSER_API
112# pragma warning(push)
113# pragma warning(disable : 4251)
122#include <string_view>
123#include <unordered_map>
236 bool operator==(const
hostname& other) const noexcept;
241 bool operator==(const std::
string& other) const noexcept;
244 const std::
string&
suffix() const noexcept;
246 const std::
string&
domain() const noexcept;
254 const std::
string&
str() const noexcept;
268 void ensure_parsed() const noexcept;
271 bool ignore_www_ = false;
272 mutable
bool parsed_ = false;
273 mutable std::
string domain_;
274 mutable std::
string subdomain_;
275 mutable std::
string suffix_;
276 mutable std::
string fulldomain_;
308 std::string_view
version() const noexcept {
return version_; }
343 std::
string suffix_of(const std::
string& hostname_text) const;
360 psl(const psl&) = delete;
361 psl& operator=(const psl&) = delete;
363 psl& operator=(psl&&) noexcept;
366 explicit psl(std::istream& stream);
368 size_t segment_count(const std::
string& text) const;
369 void add_rule(std::
string& rule,
int level_adjust,
size_t trim);
371 std::unique_ptr<
detail::suffix_table> levels_;
372 std::
string version_;
400 static
bool is_valid(std::string_view text) noexcept;
418 std::array<uint8_t, 4>
bytes() const noexcept;
440 value_ +=
static_cast<uint32_t
>(delta);
444 value_ -=
static_cast<uint32_t
>(delta);
452 return static_cast<int64_t
>(a.value_) -
static_cast<int64_t
>(b.value_);
463 explicit ipv4(uint32_t v) noexcept : value_(v) {}
494 static
bool is_valid(std::string_view text) noexcept;
509 std::array<uint8_t, 16>
bytes() const noexcept;
512 uint64_t
high64() const noexcept {
return hi_; }
514 uint64_t
low64() const noexcept {
return lo_; }
526 add_bits(~uint64_t{0});
536 add_bits(
static_cast<uint64_t
>(delta));
543 add_bits(~
static_cast<uint64_t
>(delta) + 1);
551 return a.hi_ == b.hi_ && a.lo_ == b.lo_;
555 return a.hi_ != b.hi_ ? a.hi_ < b.hi_ : a.lo_ < b.lo_;
562 ipv6(uint64_t hi, uint64_t lo) noexcept : hi_(hi), lo_(lo) {}
566 void add_bits(uint64_t low64_bits)
noexcept;
598 result.value_ = std::move(h);
659 bool is_hostname() const noexcept {
return std::holds_alternative<hostname>(value_); }
660 bool is_ipv4() const noexcept {
return std::holds_alternative<ipv4>(value_); }
661 bool is_ipv6() const noexcept {
return std::holds_alternative<ipv6>(value_); }
675 const ipv4*
try_ipv4() const noexcept {
return std::get_if<ipv4>(&value_); }
677 const ipv6*
try_ipv6() const noexcept {
return std::get_if<ipv6>(&value_); }
758 :
url(std::string_view(text), ignore_www) {}
760 :
url(std::string_view(text ? text :
""), ignore_www) {}
771 bool operator==(const
url& other) const noexcept;
774 std::
string str() const noexcept;
776 std::string_view
protocol() const noexcept {
return field(scheme_); }
778 std::string_view
query() const noexcept {
return field(query_); }
780 std::string_view
fragment() const noexcept {
return field(fragment_); }
782 std::string_view
userinfo() const noexcept {
return field(userinfo_); }
789 const std::string_view u =
userinfo();
790 return u.substr(0, u.find(
':'));
798 const std::string_view u =
userinfo();
799 const size_t colon = u.find(
':');
800 return colon == std::string_view::npos ? std::string_view() : u.substr(colon + 1);
803 std::string_view
path() const noexcept {
return field(path_); }
831 std::string_view
host_text() const noexcept {
return field(host_); }
833 int port() const noexcept {
return port_; }
873 using
search_params = std::unordered_map<std::
string, std::vector<std::
string>>;
891 std::string_view field(Span s)
const noexcept {
892 return std::string_view(storage_).substr(s.pos, s.len);
897 std::
string storage_;
898 Span scheme_, userinfo_, host_, path_, params_, query_, fragment_;
900 bool has_params_ = false;
901 bool has_query_ = false;
902 bool ignore_www_ = false;
903 mutable std::optional<
urlparser::host> host_cache_;
904 mutable std::optional<std::
string> abspath_cache_;
954 operator const url&()
const noexcept {
return url_; }
1105 return (set[c >> 6] >> (c & 63)) & 1ULL;
1185# pragma warning(pop)
hostname, ipv4, or ipv6: whatever a URL's host actually is (RFC 3986's host grammar),...
Definition urlparser.h:588
bool is_ipv6() const noexcept
Definition urlparser.h:661
const variant_type & variant() const noexcept
Direct access to the underlying variant - for std::visit, std::get, etc.
Definition urlparser.h:683
static host wrap(ipv4 v) noexcept
Definition urlparser.h:601
bool is_ip() const noexcept
True for either IP family - the opposite of is_hostname().
Definition urlparser.h:663
static host from_url(const char *url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Classify and parse the host of a URL string literal. Disambiguation overload, same reason as host(con...
const ipv6 & get_ipv6() const
Definition urlparser.h:670
const hostname & get_hostname() const
Definition urlparser.h:666
const hostname * try_hostname() const noexcept
nullptr if this isn't a hostname.
Definition urlparser.h:673
static host from_url(std::string_view url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Extract the host from a full URL and classify/parse it, in one step.
const ipv4 & get_ipv4() const
Definition urlparser.h:668
static host from_url(std::string &&url, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Extract the host from a full URL and classify/parse it, taking ownership of the URL string - reuses i...
static host wrap(ipv6 v) noexcept
Definition urlparser.h:606
std::variant< hostname, ipv4, ipv6 > variant_type
Definition urlparser.h:590
host(const char *host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Classify a host string literal. Disambiguation overload: without this, host("literal") would be ambig...
friend bool operator!=(const host &a, const host &b) noexcept
Definition urlparser.h:686
host(std::string &&host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Classify an already-isolated host string, taking ownership of it. When the text turns out to be a dom...
host() noexcept=default
Default-constructs an empty hostname.
friend bool operator==(const host &a, const host &b) noexcept
Definition urlparser.h:685
std::string str() const
The text form, whichever of hostname/ipv4/ipv6 this is.
bool is_hostname() const noexcept
Definition urlparser.h:659
bool is_ipv4() const noexcept
Definition urlparser.h:660
static host wrap(hostname h) noexcept
Wraps an already-classified hostname/ipv4/ipv6 as a host.
Definition urlparser.h:596
host(std::string_view host_text, bool ignore_www=DEFAULT_IGNORE_WWW) noexcept
Classify and parse an already-isolated host string (no scheme, no path - e.g. what url::host_text() g...
const ipv4 * try_ipv4() const noexcept
nullptr if this isn't an ipv4.
Definition urlparser.h:675
const ipv6 * try_ipv6() const noexcept
nullptr if this isn't an ipv6.
Definition urlparser.h:677
A domain name used as a URL's host, e.g. "www.example.com".
Definition urlparser.h:166
std::string normalized_ascii() const
The IDNA/Punycode-normalized, all-ASCII form of str(). Unlike str(), which keeps whatever the input l...
const std::string & full_domain() const noexcept
The full domain of the host (e.g., "example.com").
static std::string_view remove_www(const std::string_view &host) noexcept
Remove "www." from the beginning of a hostname string.
std::string domain_name() const noexcept
The domain name, i.e. "<domain>.<suffix>".
const std::string & suffix() const noexcept
The suffix of the host (e.g., "com" in "example.com").
const std::string & domain() const noexcept
The domain of the host (e.g., "example" in "www.example.com").
static hostname from_url(const char *url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Create a hostname from a URL string literal. Disambiguation overload: without this,...
static hostname from_url(std::string_view url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Create a hostname from a URL string, without taking ownership. Prefer this overload when the caller o...
static hostname from_url(std::string &&url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Create a hostname from a URL string, taking ownership of it. Prefer this overload when you already ha...
hostname(std::string host, const bool ignore_www=DEFAULT_IGNORE_WWW)
Construct a hostname object from a hostname string.
const std::string & subdomain() const noexcept
The subdomain of the host (e.g., "www" in "www.example.com").
static hostname from_url(const std::string &url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Create a hostname from a URL string.
const std::string & str() const noexcept
The complete host string (same as full_domain()).
hostname() noexcept=default
Default constructor for the hostname class. Creates an empty hostname object.
An IPv4 address used as a URL's host, e.g. "192.0.2.1".
Definition urlparser.h:388
ipv4 operator++(int) noexcept
Definition urlparser.h:424
friend bool operator>=(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:460
friend int64_t operator-(const ipv4 &a, const ipv4 &b) noexcept
Distance between two addresses (always fits in int64_t: the range is only 2^32).
Definition urlparser.h:451
friend ipv4 operator-(ipv4 lhs, int64_t delta) noexcept
Definition urlparser.h:449
friend ipv4 operator+(ipv4 lhs, int64_t delta) noexcept
Definition urlparser.h:448
static bool is_valid(std::string_view text) noexcept
Checks validity without throwing or constructing.
friend bool operator!=(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:456
friend bool operator==(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:455
uint32_t to_uint32() const noexcept
The address as a 32-bit integer (host byte order). Always valid - never throws.
Definition urlparser.h:415
ipv4() noexcept=default
Default-constructs 0.0.0.0.
static ipv4 from_url(std::string_view url)
Extract the host from a URL and parse it as an IPv4 address.
static ipv4 from_uint32(uint32_t address) noexcept
Constructs from a 32-bit representation (host byte order), e.g. 0xC0A80101 = 192.168....
std::array< uint8_t, 4 > bytes() const noexcept
The address as 4 bytes, most significant first.
std::string str() const
Canonical dotted-decimal text form, e.g. "192.0.2.1".
friend bool operator>(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:459
ipv4 & operator--() noexcept
Definition urlparser.h:429
friend bool operator<(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:457
friend bool operator<=(const ipv4 &a, const ipv4 &b) noexcept
Definition urlparser.h:458
ipv4 & operator-=(int64_t delta) noexcept
Definition urlparser.h:443
ipv4 & operator+=(int64_t delta) noexcept
Steps the address by delta, wrapping around mod 2^32.
Definition urlparser.h:439
ipv4 operator--(int) noexcept
Definition urlparser.h:433
An IPv6 address used as a URL's host, e.g. "2001:db8::1".
Definition urlparser.h:480
static bool is_valid(std::string_view text) noexcept
Checks validity (bracketed or bare) without throwing or constructing.
uint64_t low64() const noexcept
The low 64 bits (last 8 bytes).
Definition urlparser.h:514
friend bool operator>=(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:559
ipv6 operator++(int) noexcept
Definition urlparser.h:520
std::array< uint8_t, 16 > bytes() const noexcept
The address as 16 bytes, most significant first.
friend ipv6 operator-(ipv6 lhs, int64_t delta) noexcept
Definition urlparser.h:548
friend bool operator<(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:554
ipv6 & operator++() noexcept
Definition urlparser.h:516
friend bool operator==(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:550
friend bool operator>(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:558
static ipv6 from_url(std::string_view url)
Extract the host from a URL and parse it as an IPv6 address.
ipv6 & operator-=(int64_t delta) noexcept
Definition urlparser.h:539
std::string str() const
Canonical (RFC 5952) text form, no brackets: "2001:db8::1", not "[2001:db8::1]".
friend ipv6 operator+(ipv6 lhs, int64_t delta) noexcept
Definition urlparser.h:547
ipv6 & operator--() noexcept
Definition urlparser.h:525
friend bool operator<=(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:557
ipv6() noexcept=default
Default-constructs "::" (all zero).
static ipv6 from_uint64_pair(uint64_t high, uint64_t low) noexcept
Constructs from the two 64-bit halves (high64, then low64), in host byte order.
uint64_t high64() const noexcept
The high 64 bits (first 8 bytes).
Definition urlparser.h:512
ipv6 operator--(int) noexcept
Definition urlparser.h:529
friend bool operator!=(const ipv6 &a, const ipv6 &b) noexcept
Definition urlparser.h:553
ipv6 & operator+=(int64_t delta) noexcept
Steps the address by delta, wrapping around mod 2^128.
Definition urlparser.h:535
std::string suffix_of(const std::string &hostname_text) const
The public suffix of a hostname (e.g. "co.uk" for "example.co.uk"), with a best-effort single-label f...
std::string_view source_url() const noexcept
The URL the bundled PSL data was downloaded from.
std::string_view version() const noexcept
The "// VERSION: ..." timestamp from the loaded PSL data, empty if absent.
Definition urlparser.h:308
bool is_suffix(std::string_view text) const noexcept
Check whether text is itself a recognized public suffix (e.g. "co.uk", "com") - not the suffix of som...
void load_from_string(const std::string &filestr)
Load the Public Suffix List from a string, replacing whatever was loaded before (including the bundle...
bool is_loaded() const noexcept
Whether a PSL is currently loaded (true even for the bundled default).
static psl & instance() noexcept
The one, lazily-built, shared PSL instance.
void load_from_path(const std::string &filepath)
Load the Public Suffix List from a file, replacing whatever was loaded before (including the bundled ...
Parses an SSH "scp-like" address - user@host:path (what scp, rsync, sshfs, and git all accept,...
Definition urlparser.h:923
scp_url(std::string_view input)
Parse input - scp-like or already a normal URI - as a URL. scp-like input is normalize()d to "ssh://"...
const url & as_url() const noexcept
The parsed result, as a normal url.
Definition urlparser.h:952
static std::string normalize(std::string_view input)
Rewrite an scp-like address to an equivalent "ssh://" URI. Input that doesn't match is_scp_like() is ...
static bool is_scp_like(std::string_view input) noexcept
True iff input matches the scp-like address shape: [user@]host:path. Per git's own rule for this synt...
bool was_scp_like() const noexcept
Whether input (as given to the constructor) was scp-like, as opposed to already a normal URI.
Definition urlparser.h:957
Represents a URL.
Definition urlparser.h:711
static std::string extract_host(std::string_view url) noexcept
Extract the host from a given URL, without fully parsing it.
std::string_view userinfo() const noexcept
The userinfo of the URL (e.g., "username:password").
Definition urlparser.h:782
url(std::string_view url, const bool ignore_www=DEFAULT_IGNORE_WWW)
Construct a url object from a given URL string.
std::string str() const noexcept
The complete URL as a string.
std::string_view path() const noexcept
The raw path of the URL, exactly as it appears in the source (no dot-segment resolution - see abspath...
Definition urlparser.h:803
std::string_view query() const noexcept
The query string of the URL (e.g., "a=1&b=2").
Definition urlparser.h:778
std::string_view fragment() const noexcept
The fragment of the URL (the part after '#').
Definition urlparser.h:780
std::string_view username() const noexcept
The part of userinfo() before the first ':' (the whole userinfo when it has no ':'),...
Definition urlparser.h:788
std::unordered_map< std::string, std::vector< std::string > > search_params
A decoded key → values map of the query string (like JS's URLSearchParams, or ada_url....
Definition urlparser.h:873
search_params get_search_params() const
Build the decoded key → values map described on search_params above.
std::string_view host_text() const noexcept
The raw host text (e.g., "example.com", "192.0.2.1", or "[::1]"), before hostname/ip classification.
Definition urlparser.h:831
url() noexcept=default
Default constructor for the url class. Creates an empty URL object.
url(const char *text, const bool ignore_www=DEFAULT_IGNORE_WWW)
Definition urlparser.h:759
std::string normalized() const
The URL's canonical, normalized form (roughly WHATWG URL "serialize a URL" / ada's normalize_url()): ...
query_params params() const noexcept
The '&'-separated query parameters, split into a vector.
std::string_view password() const noexcept
The part of userinfo() after the first ':', raw and still percent-encoded; empty when userinfo() has ...
Definition urlparser.h:797
int effective_port() const noexcept
The port a connection to this URL uses: port() when the URL names one, else the default port of its s...
Definition urlparser.h:851
const urlparser::host & host() const noexcept
The host part of the URL, classified as a hostname, ipv4, or ipv6. Use std::get_if<hostname>/<ipv4>/<...
const std::string & abspath() const noexcept
The path, with '.'/'..' segments resolved and repeated '/' collapsed. A trailing '/' is kept (RFC 398...
static bool is_psl_loaded() noexcept
Check if the Public Suffix List (PSL) is loaded.
int port() const noexcept
The port number of the URL, or 0 if not specified.
Definition urlparser.h:833
std::string_view protocol() const noexcept
The protocol of the URL (e.g., "http", "https", "ftp").
Definition urlparser.h:776
static int default_port(std::string_view scheme) noexcept
The default port of a scheme, or 0 when the scheme has none. Covers the WHATWG "special" schemes (htt...
Exposes the library version (from the URLPARSER_VERSION_* macros) at runtime, without needing to #inc...
Definition urlparser.h:136
static unsigned int major() noexcept
Definition urlparser.h:138
static unsigned int hex() noexcept
Definition urlparser.h:141
static std::string_view string() noexcept
Definition urlparser.h:142
static unsigned int minor() noexcept
Definition urlparser.h:139
static unsigned int patch() noexcept
Definition urlparser.h:140
The library's Public Suffix List matcher.
Definition urlparser.h:289
IDNA (Internationalized Domain Names in Applications) support.
Definition urlparser.h:1061
URLPARSER_API std::string to_ascii(std::string_view utf8_domain)
Convert a UTF-8 domain (possibly containing international characters) to its canonical all-ASCII form...
Percent-encoding (RFC 3986 "quote") / percent-decoding ("unquote").
Definition urlparser.h:1098
constexpr bool contains(const character_set &set, unsigned char c) noexcept
Definition urlparser.h:1104
URLPARSER_API std::string encode(std::string_view input, const character_set &safe=unreserved_set())
Percent-encode ("quote") a string: every byte not in safe is replaced by XX (uppercase hex)....
URLPARSER_API std::string decode(std::string_view input)
Percent-decode ("unquote") a string: every XX triplet (two hex digits) becomes the corresponding byte...
URLPARSER_API character_set unreserved_set()
URLPARSER_API character_set path_safe_set()
std::array< uint64_t, 4 > character_set
Definition urlparser.h:1102
Definition urlparser.h:128
std::vector< std::string > query_params
A vector of strings representing query parameters in a URL.
Definition urlparser.h:152
scp_url git_url
Alias for scp_url. Most callers reaching for this class are parsing a git remote specifically (its mo...
Definition urlparser.h:972
constexpr bool DEFAULT_IGNORE_WWW
Definition urlparser.h:129
URLPARSER_API std::string build_search_params(const std::vector< std::pair< std::string, std::string > > &pairs)
Build an application/x-www-form-urlencoded query string from ordered key/value pairs - the inverse of...
URLPARSER_API std::string_view bare_host(std::string_view host_text) noexcept
A host without the brackets of an IPv6 literal: "::1" for "[::1]". Any other text comes back unchange...
std::string normalize(std::string_view input)
Convenience wrapper: url(input).normalized(). Prefer calling .normalized() directly on a url you alre...
Definition urlparser.h:1006
URLPARSER_API std::string resolve(std::string_view base, std::string_view ref)
Resolve a URL reference (possibly relative) against a base URL, per RFC 3986 §5 ("Reference Resolutio...
URLPARSER_API std::ostream & operator<<(std::ostream &os, const urlparser::query_params &dt)
Output stream operator for query_params.
#define URLPARSER_VERSION_STRING
Definition urlparser.h:67
#define URLPARSER_VERSION_MAJOR
Definition urlparser.h:50
#define URLPARSER_VERSION_HEX
Definition urlparser.h:59
#define URLPARSER_VERSION_MINOR
Definition urlparser.h:51
#define URLPARSER_API
Definition urlparser.h:104
#define URLPARSER_VERSION_PATCH
Definition urlparser.h:52