1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/**
* @file url_base.h
* @brief Base class and common definitions for URL types.
*
* This file defines the `url_base` abstract base class from which both
* `ada::url` and `ada::url_aggregator` inherit. It also defines common
* enumerations like `url_host_type`.
*/
#ifndef ADA_URL_BASE_H
#define ADA_URL_BASE_H
#include "ada/common_defs.h"
#include "ada/scheme.h"
#include <string>
#include <string_view>
namespace ada {
/**
* @brief Enum representing the type of host in a URL.
*
* Used to distinguish between regular domain names, IPv4 addresses,
* and IPv6 addresses for proper parsing and serialization.
*/
enum url_host_type : uint8_t {
/** Regular domain name (e.g., "www.example.com") */
DEFAULT = 0,
/** IPv4 address (e.g., "127.0.0.1") */
IPV4 = 1,
/** IPv6 address (e.g., "[::1]" or "[2001:db8::1]") */
IPV6 = 2,
};
/**
* @brief Abstract base class for URL representations.
*
* The `url_base` class provides the common interface and state shared by
* both `ada::url` and `ada::url_aggregator`. It contains basic URL attributes
* like validity status and scheme type, but delegates component storage and
* access to derived classes.
*
* @note This is an abstract class and cannot be instantiated directly.
* Use `ada::url` or `ada::url_aggregator` instead.
*
* @see url
* @see url_aggregator
*/
struct url_base {
virtual ~url_base() = default;
/**
* Indicates whether the URL was successfully parsed.
* Set to `false` if parsing failed (e.g., invalid URL syntax).
*/
bool is_valid{true};
/**
* Indicates whether the URL has an opaque path (non-hierarchical).
* Opaque paths occur in non-special URLs like `mailto:` or `javascript:`.
*/
bool has_opaque_path{false};
/**
* The type of the URL's host (domain, IPv4, or IPv6).
*/
url_host_type host_type = url_host_type::DEFAULT;
/**
* @private
* Internal representation of the URL's scheme type.
*/
ada::scheme::type type{ada::scheme::type::NOT_SPECIAL};
/**
* Checks if the URL has a special scheme (http, https, ws, wss, ftp, file).
* Special schemes have specific parsing rules and default ports.
* @return `true` if the scheme is special, `false` otherwise.
*/
[[nodiscard]] ada_really_inline constexpr bool is_special() const noexcept;
/**
* Returns the URL's origin (scheme + host + port for special URLs).
* @return A newly allocated string containing the serialized origin.
* @see https://url.spec.whatwg.org/#concept-url-origin
*/
[[nodiscard]] virtual std::string get_origin() const = 0;
/**
* Validates whether the hostname is a valid domain according to RFC 1034.
* Checks that the domain and its labels have valid lengths.
* @return `true` if the domain is valid, `false` otherwise.
*/
[[nodiscard]] virtual bool has_valid_domain() const noexcept = 0;
/**
* @private
* Returns the default port for special schemes (e.g., 443 for https).
* Returns 0 for file:// URLs or non-special schemes.
*/
[[nodiscard]] inline uint16_t get_special_port() const noexcept;
/**
* @private
* Returns the default port for the URL's scheme, or 0 if none.
*/
[[nodiscard]] ada_really_inline uint16_t scheme_default_port() const noexcept;
/**
* @private
* Parses a port number from the input string.
* @param view The string containing the port to parse.
* @param check_trailing_content Whether to validate no trailing characters.
* @return Number of bytes consumed on success, 0 on failure.
*/
virtual size_t parse_port(std::string_view view,
bool check_trailing_content) = 0;
/** @private */
virtual ada_really_inline size_t parse_port(std::string_view view) {
return this->parse_port(view, false);
}
/**
* Returns a JSON string representation of this URL for debugging.
* @return A JSON-formatted string with URL information.
*/
[[nodiscard]] virtual std::string to_string() const = 0;
/** @private */
virtual inline void clear_pathname() = 0;
/** @private */
virtual inline void clear_search() = 0;
/** @private */
[[nodiscard]] virtual inline bool has_hash() const noexcept = 0;
/** @private */
[[nodiscard]] virtual inline bool has_search() const noexcept = 0;
}; // url_base
} // namespace ada
#endif
|