13#include "../char_maps/module.hpp"
14#include "../utility/module.hpp"
19#include <unordered_map>
28hi_warning_ignore_msvc(26434);
30namespace hi {
inline namespace v1 {
52 constexpr URL() noexcept = default;
53 constexpr
URL(
URL const&) noexcept = default;
54 constexpr
URL(
URL&&) noexcept = default;
55 constexpr
URL& operator=(
URL const&) noexcept = default;
56 constexpr
URL& operator=(
URL&&) noexcept = default;
72 constexpr explicit URL(std::string_view str) :
URI(str) {}
88 constexpr explicit URL(
const char *str) :
URL(
std::string_view{str}) {}
97 explicit URL(std::filesystem::path
const&
path) :
URI(make_file_url_string(
path)) {}
106 if (validate_scheme and not(not
scheme() or
scheme() ==
"file")) {
107 throw url_error(
"URL::generic_path() is only valid on a file: scheme URL");
112 hilet first = p.begin();
113 hilet last = p.end();
116 auto has_root_name =
false;
120 if (not server.empty() and server !=
"localhost") {
121 validate_file_server(server);
122 has_root_name =
true;
131 hi_assert(has_root_name ==
false or p.absolute());
133 if (p.double_absolute()) {
137 throw url_error(
"file URL has two server names.");
140 has_root_name =
true;
144 validate_file_server(*it);
150 auto empty_segment =
false;
152 validate_file_segment(*it);
153 empty_segment = it->empty();
155 if (it == first and empty_segment) {
159 }
else if (
auto i = it->find(
':'); i != std::string::npos) {
162 throw url_error(
"file URL contains a device name which is a security issue.");
165 if (has_root_name or p.absolute()) {
168 r += has_root_name ?
'$' :
':';
181 has_root_name =
true;
193 for (; it != last; ++it) {
194 validate_file_segment(*it);
195 empty_segment = it->empty();
200 if (not empty_segment) {
215 if (
auto scheme_ =
scheme()) {
216 if (scheme_ ==
"resource") {
222 throw url_error(std::format(
"Resource {} not found.", *
this));
225 }
else if (scheme_ ==
"file") {
228 throw url_error(
"URL can not be converted to a filesystem path.");
238 operator std::filesystem::path()
const
243 [[nodiscard]]
constexpr friend URL operator/(
URL const& base,
URI const& ref)
noexcept
245 return URL{up_cast<URI const&>(base) / ref};
248 [[nodiscard]]
constexpr friend URL operator/(
URL const& base, std::string_view ref)
noexcept
250 return URL{up_cast<URI const&>(base) / ref};
254 constexpr void static validate_file_segment(std::string_view segment)
256 for (
auto c : segment) {
257 if (c ==
'/' or c ==
'\\') {
258 throw url_error(
"Filename server name may not contain slash or back-slash.");
263 constexpr void static validate_file_server(std::string_view server)
265 for (
auto c : server) {
266 if (c ==
'/' or c ==
'\\') {
267 throw url_error(
"Filename segments may not contain slash or back-slash.");
272 static std::string make_file_url_string(std::filesystem::path
const&
path)
274 auto r = std::u8string{};
276 hilet root_name =
path.root_name().generic_u8string();
277 if (root_name.empty()) {
279 if (not
path.root_directory().empty()) {
281 r += u8
"file:" +
path.root_directory().generic_u8string();
287 }
else if (
hilet i = root_name.find(
':'); i != std::string::npos) {
290 r += u8
"file:///" + root_name +
path.root_directory().generic_u8string();
292 throw url_error(
"Paths containing a device are not allowed to be converted to a URL.");
296 r += u8
"file://" + root_name +
path.root_directory().generic_u8string();
297 if (not
path.root_directory().empty()) {
298 throw url_error(
"Invalid path contains server name without a root directory.");
302 return to_string(r +
path.relative_path().generic_u8string());
316template<
typename CharT>
317struct std::formatter<
hi::URL, CharT> : std::formatter<hi::URI, CharT> {
318 auto format(
hi::URL const& t,
auto& fc)
320 return std::formatter<hi::URI, CharT>::format(t, fc);
functions to locate files and directories.
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
constexpr std::u8string to_u8string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:111
std::optional< std::filesystem::path > find_path(path_location location, std::filesystem::path const &ref) noexcept
Find a path.
Definition path_location.hpp:83
@ resource_dirs
The location of application resources.
@ other
The gui_event does not have associated data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
A Uniform Resource Identifier.
Definition URI.hpp:37
constexpr std::optional< authority_type > const & authority() const noexcept
Get the authority-component of the URI.
Definition URI.hpp:550
constexpr std::optional< std::string > const & scheme() const noexcept
Get the scheme-component of the URI.
Definition URI.hpp:527
Universal Resource Locator.
Definition URL.hpp:48
constexpr URL() noexcept=default
Create an empty URL.
URL(std::filesystem::path const &path)
Convert a filesystem-path to a file-scheme URL.
Definition URL.hpp:97
constexpr URL(URI &&other) noexcept
Convert a URI to an URL.
Definition URL.hpp:64
operator std::filesystem::path() const
Definition URL.hpp:238
constexpr URL(const char *str)
Construct a URI from a string.
Definition URL.hpp:88
constexpr URL(std::string const &str)
Construct a URI from a string.
Definition URL.hpp:80
std::filesystem::path filesystem_path() const
Create a filesystem path from a file URL.
Definition URL.hpp:213
constexpr std::u8string filesystem_path_generic_u8string(bool validate_scheme=true) const
Return a generic path.
Definition URL.hpp:104
constexpr URL(std::string_view str)
Construct a URI from a string.
Definition URL.hpp:72
Definition exception.hpp:214