50 return (c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z');
60 return is_alnum((
char) c) || c ==
'-' || c ==
'.' || c ==
'_' || c ==
'~';
70 return c ==
':' || c ==
'/' || c ==
'?' || c ==
'#' || c ==
'[' || c ==
']' ||
116 return is_alnum((
char) c) || c ==
'+' || c ==
'-' || c ==
'.';
137 c ==
'[' || c ==
']';
184 std::string_view _scheme;
185 std::string_view _user_info;
186 std::string_view _host;
187 std::string_view _port;
188 std::string_view _path;
189 std::string_view _raw_queries;
190 std::string_view _fragment;
194 constexpr static const char no_path[] =
"/";
196 bool parse_queries(std::size_t pos)
noexcept;
205 bool parse()
noexcept;
212 bool from(std::string
const &rhs)
noexcept;
219 bool from(std::string &&rhs)
noexcept;
226 void parse_query_parameters(
const char* begin,
const char* end);
251 uri(std::string
const &str,
int af = AF_INET);
258 uri(std::string &&str,
int af = AF_INET)
noexcept;
264 static const char tbl[256];
276 template <
typename _IT>
282 while (begin != end) {
285 if ((v1 =
tbl[(
unsigned char) *(begin++)]) < 0 ||
286 (v2 =
tbl[(
unsigned char) *(begin++)]) < 0) {
300 constexpr static const char hex[] =
"0123456789ABCDEF";
312 template <
typename _IT>
317 encoded.reserve(
static_cast<ptrdiff_t
>(end - begin) * 3);
318 for (
auto iter = begin; iter != end; ++iter) {
320 int ch =
static_cast<unsigned char>(*iter);
323 encoded.push_back(
'%');
324 encoded.push_back(
hex[(ch >> 4) & 0xF]);
325 encoded.push_back(
hex[ch & 0xF]);
329 encoded.push_back((
char) ch);
341 static std::string
decode(std::string_view input)
noexcept;
348 static std::string
encode(std::string_view input)
noexcept;
356 static std::string
decode(
const char *input, std::size_t size)
noexcept;
364 static std::string
encode(
const char *input, std::size_t size)
noexcept;
372 static uri parse(std::string
const &str,
int af = AF_INET)
noexcept;
406 [[nodiscard]]
inline auto
415 [[nodiscard]]
inline const auto &
424 [[nodiscard]]
inline auto
433 [[nodiscard]]
inline auto
442 [[nodiscard]]
inline auto
451 [[nodiscard]]
inline auto
460 [[nodiscard]] uint16_t
464 std::from_chars(
port().data(),
port().data() +
port().size(), p);
465 return static_cast<uint16_t
>(p);
472 [[nodiscard]]
inline auto
481 [[nodiscard]]
inline const auto &
490 [[nodiscard]]
inline const auto &
499 [[nodiscard]]
inline auto &
513 template <
typename T>
514 [[nodiscard]] std::string
const &
515 query(T &&name, std::size_t
const index = 0,
516 std::string
const ¬_found =
"")
const {
517 const auto &it = this->_queries.
find(std::forward<T>(name));
518 if (it != this->_queries.cend() && index < it->second.size())
519 return it->second[index];
528 [[nodiscard]]
inline auto
Platform, compiler, and C++ feature detection macros for the QB Framework.
auto find(T &&key)
Find a key-value pair by key, with the key converted to lowercase.
Definition unordered_map.h:335
uri & operator=(uri const &rhs)
Copy assignment operator.
auto scheme() const
Returns the scheme of this URI.
Definition uri.h:425
static bool is_valid_scheme(std::string_view scheme) noexcept
Validates if a string is a valid URI scheme.
uri(uri const &rhs)
Copy constructor.
auto af() const
Returns the address family of this URI.
Definition uri.h:407
uri(uri &&rhs) noexcept
Move constructor.
static std::string decode(const char *input, std::size_t size) noexcept
Decodes a URI-encoded memory block.
auto & queries()
Returns the parsed query parameters of this URI (non-const version)
Definition uri.h:500
static std::string encode(const char *input, std::size_t size) noexcept
Encodes a memory block in URI format.
static constexpr const char hex[]
Hexadecimal characters for encoding.
Definition uri.h:300
auto user_info() const
Returns the user information of this URI.
Definition uri.h:434
uri(std::string &&str, int af=AF_INET) noexcept
Constructor from a string (move version)
uri & operator=(uri &&rhs) noexcept
Move assignment operator.
auto fragment() const
Returns the fragment of this URI.
Definition uri.h:529
uri()=default
Default constructor.
uri & operator=(std::string const &str)
String assignment operator.
static const char tbl[256]
Conversion table for hexadecimal characters Used for decoding XX sequences to characters.
Definition uri.h:264
uri(std::string const &str, int af=AF_INET)
Constructor from a string.
static std::string decode(std::string_view input) noexcept
Decodes a URI-encoded string view.
std::string const & query(T &&name, std::size_t const index=0, std::string const ¬_found="") const
Returns the value of a specific query parameter.
Definition uri.h:515
static std::string decode(_IT begin, _IT end) noexcept
Decodes a sequence of URI-encoded characters.
Definition uri.h:278
uri & operator=(std::string &&str) noexcept
String move assignment operator.
auto path() const
Returns the path of this URI.
Definition uri.h:473
static bool normalize_path(std::string &path) noexcept
Normalizes a path by resolving '.
uint16_t u_port() const
Returns the port number of this URI as a numeric value.
Definition uri.h:461
static std::string encode(_IT begin, _IT end) noexcept
Encodes a sequence of characters in URI format.
Definition uri.h:314
const auto & encoded_queries() const
Returns the raw query string of this URI.
Definition uri.h:482
auto port() const
Returns the port number of this URI.
Definition uri.h:452
static bool is_valid_host(std::string_view host) noexcept
Validates if a string is a valid URI host.
static uri parse(std::string const &str, int af=AF_INET) noexcept
Creates and parses a URI from a string.
const auto & source() const
Returns the source string of this URI.
Definition uri.h:416
auto host() const
Returns the host name or IP address of this URI.
Definition uri.h:443
const auto & queries() const
Returns the parsed query parameters of this URI.
Definition uri.h:491
static std::string encode(std::string_view input) noexcept
Encodes a string view in URI format.
Configuration and platform-specific definitions for the QB IO library.
Optimized unordered map implementations.
icase_basic_map< qb::unordered_map< std::string, Value >, _Trait > icase_unordered_map
Case-insensitive unordered map using qb::unordered_map.
Definition unordered_map.h:422
bool is_user_info_character(int c)
Checks if a character is valid for URI user information.
Definition uri.h:125
bool is_authority_character(int c)
Checks if a character is valid for the authority part of a URI.
Definition uri.h:135
bool is_fragment_character(int c)
Checks if a character is valid for the fragment part of a URI.
Definition uri.h:167
bool is_alnum(int c)
Checks if a character is alphanumeric.
Definition uri.h:49
bool is_sub_delim(int c)
Checks if a character is a subdelimiter according to RFC 3986.
Definition uri.h:80
bool is_scheme_character(int c)
Checks if a character is valid for a URI scheme.
Definition uri.h:115
bool is_unreserved(int c)
Checks if a character is unreserved according to RFC 3986.
Definition uri.h:59
bool is_reserved(int c)
Checks if a character is reserved according to RFC 3986.
Definition uri.h:105
bool is_gen_delim(int c)
Checks if a character is a general delimiter according to RFC 3986.
Definition uri.h:69
bool is_query_character(int c)
Checks if a character is valid for the query part of a URI.
Definition uri.h:157
bool is_path_character(int c)
Checks if a character is valid for the path part of a URI.
Definition uri.h:146