Loading...
Searching...
No Matches
urlparser.h
Go to the documentation of this file.
1
42
43#ifndef URLPARSER_H
44#define URLPARSER_H
45
46// Single source of truth for the project version. Read by:
47// - cmake/DynamicVersion.cmake (sets PROJECT_VERSION for the CMake build)
48// - pyproject.toml's [tool.scikit-build.metadata.version] regex provider
49// - version.py (bump/tag helper)
50#define URLPARSER_VERSION_MAJOR 2
51#define URLPARSER_VERSION_MINOR 3
52#define URLPARSER_VERSION_PATCH 3
53
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)
57
59#define URLPARSER_VERSION_HEX \
60 ((URLPARSER_VERSION_MAJOR << 16) | (URLPARSER_VERSION_MINOR << 8) | URLPARSER_VERSION_PATCH)
61
62/* Internal helpers for URLPARSER_VERSION_STRING */
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)
65
67#define URLPARSER_VERSION_STRING \
68 _URLPARSER_VERSION_STR(URLPARSER_VERSION_MAJOR,URLPARSER_VERSION_MINOR,URLPARSER_VERSION_PATCH)
69
70// Single source of truth for the Public Suffix List URL. Read by:
71// - cmake/PublicSuffixList.cmake (downloads the .dat file this URL
72// points to, to embed into the compiled extension at build time)
73// - urlparser::psl::source_url() below (src/urlparser.cpp), so
74// psl.url / psl.update() in Python point at the same place the
75// build itself downloaded from - one URL, not two to keep in sync.
76#define URLPARSER_PUBLIC_SUFFIX_LIST_URL "https://publicsuffix.org/list/public_suffix_list.dat"
77
92#ifndef URLPARSER_API
93# if defined(URLPARSER_STATIC)
94# define URLPARSER_API
95# elif defined(_WIN32) || defined(__CYGWIN__)
96# ifdef URLPARSER_BUILDING
97# define URLPARSER_API __declspec(dllexport)
98# else
99# define URLPARSER_API __declspec(dllimport)
100# endif
101# elif defined(__GNUC__) || defined(__clang__)
102# define URLPARSER_API __attribute__((visibility("default")))
103# else
104# define URLPARSER_API
105# endif
106#endif
107
108#ifdef _MSC_VER
109// Exported classes hold std:: members (std::string, std::optional, ...);
110// MSVC warns that they lack a dll-interface. They are only touched through
111// the exported members, so the warning is noise here.
112# pragma warning(push)
113# pragma warning(disable : 4251)
114#endif
115
116#include <array>
117#include <cstdint>
118#include <iostream>
119#include <memory>
120#include <optional>
121#include <string>
122#include <string_view>
123#include <unordered_map>
124#include <utility>
125#include <variant>
126#include <vector>
127
128namespace urlparser {
129constexpr bool DEFAULT_IGNORE_WWW = false;
130
136class version {
137 public:
138 static unsigned int major() noexcept { return URLPARSER_VERSION_MAJOR; }
139 static unsigned int minor() noexcept { return URLPARSER_VERSION_MINOR; }
140 static unsigned int patch() noexcept { return URLPARSER_VERSION_PATCH; }
141 static unsigned int hex() noexcept { return URLPARSER_VERSION_HEX; }
142 static std::string_view string() noexcept { return URLPARSER_VERSION_STRING; }
143
144 private:
145 version();
146};
147
152using query_params = std::vector<std::string>;
153
167 public:
177 static hostname from_url(std::string_view url,
178 const bool ignore_www = DEFAULT_IGNORE_WWW);
179
186 static hostname from_url(const char* url,
187 const bool ignore_www = DEFAULT_IGNORE_WWW);
188
195 static hostname from_url(const std::string& url,
196 const bool ignore_www = DEFAULT_IGNORE_WWW);
197
209 static hostname from_url(std::string&& url,
210 const bool ignore_www = DEFAULT_IGNORE_WWW);
211
217 static std::string_view remove_www(const std::string_view& host) noexcept;
218
219 public:
225 hostname(std::string host, const bool ignore_www = DEFAULT_IGNORE_WWW);
226
231 hostname() noexcept = default;
232
236 bool operator==(const hostname& other) const noexcept;
237
241 bool operator==(const std::string& other) const noexcept;
242
244 const std::string& suffix() const noexcept;
246 const std::string& domain() const noexcept;
248 std::string domain_name() const noexcept;
250 const std::string& subdomain() const noexcept;
252 const std::string& full_domain() const noexcept;
254 const std::string& str() const noexcept;
263 std::string normalized_ascii() const;
264
265 private:
266 // Runs the PSL lookup + domain/subdomain/suffix split exactly once, the
267 // first time any of those fields is actually requested.
268 void ensure_parsed() const noexcept;
269
270 std::string host_;
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_;
277};
278
289namespace detail {
290// Wraps ankerl::unordered_dense::map<std::string, size_t> (vendored in
291// src/ankerl/ - see src/ankerl/README.md). Forward-
292// declared here and defined only in urlparser.cpp so that including this
293// public header doesn't require the vendored third-party header too -
294// psl::levels_ is a private implementation detail, and callers of the
295// public API never need to know what's actually inside it.
296class suffix_table;
297} // namespace detail
298
299class URLPARSER_API psl {
300 public:
302 static psl& instance() noexcept;
303
305 std::string_view source_url() const noexcept;
306
308 std::string_view version() const noexcept { return version_; }
309
311 bool is_loaded() const noexcept;
312
318 void load_from_path(const std::string& filepath);
319
325 void load_from_string(const std::string& filestr);
326
334 bool is_suffix(std::string_view text) const noexcept;
335
343 std::string suffix_of(const std::string& hostname_text) const;
344
345 private:
346 // levels_ is a std::unique_ptr<detail::suffix_table> (an incomplete
347 // type here), so the compiler-generated destructor/move members
348 // would need that type complete to know how to delete/move it -
349 // declared here, defined in urlparser.cpp where suffix_table is
350 // fully visible. Both the singleton instance (instance()'s static
351 // local) and the self-move-assignments in load_from_path()/
352 // load_from_string() are psl's own member functions, so these stay
353 // private along with the constructors - nothing outside the class
354 // should ever construct, move, or destroy a psl directly. Copy is
355 // deleted outright for the same reason (a unique_ptr member already
356 // makes an implicit copy ill-formed; an explicit delete fails at the
357 // call site with a clear reason instead of a unique_ptr error deep
358 // in a template instantiation).
359 ~psl();
360 psl(const psl&) = delete;
361 psl& operator=(const psl&) = delete;
362 psl(psl&&) noexcept;
363 psl& operator=(psl&&) noexcept;
364
365 psl() noexcept;
366 explicit psl(std::istream& stream);
367
368 size_t segment_count(const std::string& text) const;
369 void add_rule(std::string& rule, int level_adjust, size_t trim);
370
371 std::unique_ptr<detail::suffix_table> levels_;
372 std::string version_;
373};
374
389 public:
391 ipv4() noexcept = default;
392
397 ipv4(std::string_view text);
398
400 static bool is_valid(std::string_view text) noexcept;
401
403 static ipv4 from_uint32(uint32_t address) noexcept;
404
409 static ipv4 from_url(std::string_view url);
410
412 std::string str() const;
413
415 uint32_t to_uint32() const noexcept { return value_; }
416
418 std::array<uint8_t, 4> bytes() const noexcept;
419
420 ipv4& operator++() noexcept {
421 ++value_;
422 return *this;
423 }
424 ipv4 operator++(int) noexcept {
425 ipv4 tmp = *this;
426 ++value_;
427 return tmp;
428 }
429 ipv4& operator--() noexcept {
430 --value_;
431 return *this;
432 }
433 ipv4 operator--(int) noexcept {
434 ipv4 tmp = *this;
435 --value_;
436 return tmp;
437 }
438
439 ipv4& operator+=(int64_t delta) noexcept {
440 value_ += static_cast<uint32_t>(delta);
441 return *this;
442 }
443 ipv4& operator-=(int64_t delta) noexcept {
444 value_ -= static_cast<uint32_t>(delta);
445 return *this;
446 }
447
448 friend ipv4 operator+(ipv4 lhs, int64_t delta) noexcept { return lhs += delta; }
449 friend ipv4 operator-(ipv4 lhs, int64_t delta) noexcept { return lhs -= delta; }
451 friend int64_t operator-(const ipv4& a, const ipv4& b) noexcept {
452 return static_cast<int64_t>(a.value_) - static_cast<int64_t>(b.value_);
453 }
454
455 friend bool operator==(const ipv4& a, const ipv4& b) noexcept { return a.value_ == b.value_; }
456 friend bool operator!=(const ipv4& a, const ipv4& b) noexcept { return a.value_ != b.value_; }
457 friend bool operator<(const ipv4& a, const ipv4& b) noexcept { return a.value_ < b.value_; }
458 friend bool operator<=(const ipv4& a, const ipv4& b) noexcept { return a.value_ <= b.value_; }
459 friend bool operator>(const ipv4& a, const ipv4& b) noexcept { return a.value_ > b.value_; }
460 friend bool operator>=(const ipv4& a, const ipv4& b) noexcept { return a.value_ >= b.value_; }
461
462 private:
463 explicit ipv4(uint32_t v) noexcept : value_(v) {}
464 uint32_t value_ = 0;
465};
466
481 public:
483 ipv6() noexcept = default;
484
491 ipv6(std::string_view text);
492
494 static bool is_valid(std::string_view text) noexcept;
495
497 static ipv6 from_uint64_pair(uint64_t high, uint64_t low) noexcept;
498
503 static ipv6 from_url(std::string_view url);
504
506 std::string str() const;
507
509 std::array<uint8_t, 16> bytes() const noexcept;
510
512 uint64_t high64() const noexcept { return hi_; }
514 uint64_t low64() const noexcept { return lo_; }
515
516 ipv6& operator++() noexcept {
517 add_bits(1);
518 return *this;
519 }
520 ipv6 operator++(int) noexcept {
521 ipv6 tmp = *this;
522 ++(*this);
523 return tmp;
524 }
525 ipv6& operator--() noexcept {
526 add_bits(~uint64_t{0}); // two's-complement bit pattern of -1
527 return *this;
528 }
529 ipv6 operator--(int) noexcept {
530 ipv6 tmp = *this;
531 --(*this);
532 return tmp;
533 }
534
535 ipv6& operator+=(int64_t delta) noexcept {
536 add_bits(static_cast<uint64_t>(delta));
537 return *this;
538 }
539 ipv6& operator-=(int64_t delta) noexcept {
540 // -delta's bit pattern, computed in unsigned arithmetic so this is
541 // well-defined even when delta == INT64_MIN (no positive int64_t
542 // counterpart, so negating it as a signed value would overflow).
543 add_bits(~static_cast<uint64_t>(delta) + 1);
544 return *this;
545 }
546
547 friend ipv6 operator+(ipv6 lhs, int64_t delta) noexcept { return lhs += delta; }
548 friend ipv6 operator-(ipv6 lhs, int64_t delta) noexcept { return lhs -= delta; }
549
550 friend bool operator==(const ipv6& a, const ipv6& b) noexcept {
551 return a.hi_ == b.hi_ && a.lo_ == b.lo_;
552 }
553 friend bool operator!=(const ipv6& a, const ipv6& b) noexcept { return !(a == b); }
554 friend bool operator<(const ipv6& a, const ipv6& b) noexcept {
555 return a.hi_ != b.hi_ ? a.hi_ < b.hi_ : a.lo_ < b.lo_;
556 }
557 friend bool operator<=(const ipv6& a, const ipv6& b) noexcept { return !(b < a); }
558 friend bool operator>(const ipv6& a, const ipv6& b) noexcept { return b < a; }
559 friend bool operator>=(const ipv6& a, const ipv6& b) noexcept { return !(a < b); }
560
561 private:
562 ipv6(uint64_t hi, uint64_t lo) noexcept : hi_(hi), lo_(lo) {}
563
564 // Adds the 128-bit sign-extension of the two's-complement 64-bit
565 // pattern `low64_bits` to (hi_, lo_), wrapping mod 2^128.
566 void add_bits(uint64_t low64_bits) noexcept;
567
568 uint64_t hi_ = 0;
569 uint64_t lo_ = 0;
570};
571
589 public:
590 using variant_type = std::variant<hostname, ipv4, ipv6>;
591
593 host() noexcept = default;
594
596 static host wrap(hostname h) noexcept {
597 host result;
598 result.value_ = std::move(h);
599 return result;
600 }
601 static host wrap(ipv4 v) noexcept {
602 host result;
603 result.value_ = v;
604 return result;
605 }
606 static host wrap(ipv6 v) noexcept {
607 host result;
608 result.value_ = v;
609 return result;
610 }
611
621 host(std::string_view host_text, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
622
628 host(const char* host_text, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
629
637 host(std::string&& host_text, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
638
643 static host from_url(std::string_view url, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
644
649 static host from_url(const char* url, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
650
657 static host from_url(std::string&& url, bool ignore_www = DEFAULT_IGNORE_WWW) noexcept;
658
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_); }
663 bool is_ip() const noexcept { return !is_hostname(); }
664
666 const hostname& get_hostname() const { return std::get<hostname>(value_); }
668 const ipv4& get_ipv4() const { return std::get<ipv4>(value_); }
670 const ipv6& get_ipv6() const { return std::get<ipv6>(value_); }
671
673 const hostname* try_hostname() const noexcept { return std::get_if<hostname>(&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_); }
678
680 std::string str() const;
681
683 const variant_type& variant() const noexcept { return value_; }
684
685 friend bool operator==(const host& a, const host& b) noexcept { return a.value_ == b.value_; }
686 friend bool operator!=(const host& a, const host& b) noexcept { return !(a == b); }
687
688 private:
689 variant_type value_;
690};
691
712 public:
716 static bool is_psl_loaded() noexcept;
717
721 static std::string extract_host(std::string_view url) noexcept;
722
730 static std::string extract_host(const char* url) noexcept;
731
739 static std::string extract_host(std::string&& url) noexcept;
740
741 public:
748 url(std::string_view url, const bool ignore_www = DEFAULT_IGNORE_WWW);
749
757 url(const std::string& text, const bool ignore_www = DEFAULT_IGNORE_WWW)
758 : url(std::string_view(text), ignore_www) {}
759 url(const char* text, const bool ignore_www = DEFAULT_IGNORE_WWW)
760 : url(std::string_view(text ? text : ""), ignore_www) {}
761
766 url() noexcept = default;
767
771 bool operator==(const url& other) const noexcept;
772
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_); }
788 std::string_view username() const noexcept {
789 const std::string_view u = userinfo();
790 return u.substr(0, u.find(':'));
791 }
792
797 std::string_view password() const noexcept {
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);
801 }
802
803 std::string_view path() const noexcept { return field(path_); }
808 const std::string& abspath() const noexcept;
829 std::string normalized() const;
831 std::string_view host_text() const noexcept { return field(host_); }
833 int port() const noexcept { return port_; }
845 static int default_port(std::string_view scheme) noexcept;
851 int effective_port() const noexcept { return port_ != 0 ? port_ : default_port(protocol()); }
853 query_params params() const noexcept;
873 using search_params = std::unordered_map<std::string, std::vector<std::string>>;
881 const urlparser::host& host() const noexcept;
882
883 private:
884 // An (offset, length) pair into storage_ - not a raw pointer/view, so
885 // that copying a url (which deep-copies storage_) never needs to
886 // "rebase" anything: the offsets stay correct as-is.
887 struct Span {
888 uint32_t pos = 0;
889 uint32_t len = 0;
890 };
891 std::string_view field(Span s) const noexcept {
892 return std::string_view(storage_).substr(s.pos, s.len);
893 }
894
895 const urlparser::host& ensure_host() const noexcept;
896
897 std::string storage_;
898 Span scheme_, userinfo_, host_, path_, params_, query_, fragment_;
899 int port_ = 0;
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_;
905};
906
924 public:
933 static bool is_scp_like(std::string_view input) noexcept;
934
939 static std::string normalize(std::string_view input);
940
949 explicit scp_url(std::string_view input);
950
952 const url& as_url() const noexcept { return url_; }
954 operator const url&() const noexcept { return url_; }
955
957 bool was_scp_like() const noexcept { return was_scp_like_; }
958
959 private:
960 url url_;
961 bool was_scp_like_;
962};
963
973
987URLPARSER_API std::string build_search_params(const std::vector<std::pair<std::string, std::string>>& pairs);
988
998
1006inline std::string normalize(std::string_view input) { return url(input).normalized(); }
1007
1008} // namespace urlparser
1009
1013URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::query_params& dt);
1014
1018URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::url& dt);
1019
1023URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::hostname& dt);
1024
1028URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::ipv4& dt);
1029
1033URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::ipv6& dt);
1034
1038URLPARSER_API std::ostream& operator<<(std::ostream& os, const urlparser::host& dt);
1039
1040// ============================================================================
1041// IDNA (Internationalized Domain Names): Unicode <-> ASCII/Punycode.
1042// ============================================================================
1043namespace urlparser {
1044
1061namespace idna {
1062
1072URLPARSER_API std::string to_ascii(std::string_view utf8_domain);
1073
1074} // namespace idna
1075} // namespace urlparser
1076
1077// ============================================================================
1078// Percent-encoding ("quote") / percent-decoding ("unquote").
1079// ============================================================================
1080namespace urlparser {
1081
1098namespace percent_codec {
1099
1102using character_set = std::array<uint64_t, 4>;
1103
1104constexpr bool contains(const character_set& set, unsigned char c) noexcept {
1105 return (set[c >> 6] >> (c & 63)) & 1ULL;
1106}
1107
1111
1115
1123URLPARSER_API std::string decode(std::string_view input);
1124
1130URLPARSER_API std::string encode(std::string_view input,
1131 const character_set& safe = unreserved_set());
1132
1133} // namespace percent_codec
1134} // namespace urlparser
1135
1136// ============================================================================
1137// URL reference resolution ("join"): RFC 3986 §5.
1138// ============================================================================
1139namespace urlparser {
1140
1169URLPARSER_API std::string resolve(std::string_view base, std::string_view ref);
1170
1180URLPARSER_API std::string_view bare_host(std::string_view host_text) noexcept;
1181
1182} // namespace urlparser
1183
1184#ifdef _MSC_VER
1185# pragma warning(pop)
1186#endif
1187
1188#endif // URLPARSER_H
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