13#include "../char_maps/module.hpp"
14#include "../utility/module.hpp"
19#include <unordered_map>
28hi_warning_ignore_msvc(26434);
31namespace hi {
inline namespace v1 {
53 constexpr URL() noexcept = default;
54 constexpr
URL(
URL const&) noexcept = default;
55 constexpr
URL(
URL&&) noexcept = default;
56 constexpr
URL& operator=(
URL const&) noexcept = default;
57 constexpr
URL& operator=(
URL&&) noexcept = default;
74 explicit URL(std::filesystem::path
const&
path) :
URI(make_file_url_string(
path)) {}
83 if (validate_scheme and not(not
scheme() or
scheme() ==
"file")) {
84 throw url_error(
"URL::generic_path() is only valid on a file: scheme URL");
89 hilet first = p.begin();
93 auto has_root_name =
false;
97 if (not server.empty() and server !=
"localhost") {
98 validate_file_server(server);
108 hi_assert(has_root_name ==
false or p.absolute());
110 if (p.double_absolute()) {
114 throw url_error(
"file URL has two server names.");
117 has_root_name =
true;
121 validate_file_server(*it);
127 auto empty_segment =
false;
129 validate_file_segment(*it);
130 empty_segment = it->empty();
132 if (it == first and empty_segment) {
136 }
else if (
auto i = it->find(
':'); i != std::string::npos) {
139 throw url_error(
"file URL contains a device name which is a security issue.");
142 if (has_root_name or p.absolute()) {
145 r += has_root_name ?
'$' :
':';
158 has_root_name =
true;
170 for (; it != last; ++it) {
171 validate_file_segment(*it);
172 empty_segment = it->empty();
177 if (not empty_segment) {
192 if (
auto scheme_ =
scheme()) {
193 if (scheme_ ==
"resource") {
199 throw url_error(std::format(
"Resource {} not found.", *
this));
202 }
else if (scheme_ ==
"file") {
205 throw url_error(
"URL can not be converted to a filesystem path.");
215 operator std::filesystem::path()
const
220 [[nodiscard]]
constexpr friend URL operator/(
URL const& base,
URI const& ref)
noexcept
222 return URL{up_cast<URI const&>(base) / ref};
225 [[nodiscard]]
constexpr friend URL operator/(
URL const& base, std::string_view ref)
noexcept
227 return URL{up_cast<URI const&>(base) / ref};
231 constexpr void static validate_file_segment(std::string_view segment)
233 for (
auto c : segment) {
234 if (c ==
'/' or c ==
'\\') {
235 throw url_error(
"Filename server name may not contain slash or back-slash.");
240 constexpr void static validate_file_server(std::string_view server)
242 for (
auto c : server) {
243 if (c ==
'/' or c ==
'\\') {
244 throw url_error(
"Filename segments may not contain slash or back-slash.");
249 static std::string make_file_url_string(std::filesystem::path
const&
path)
251 auto r = std::u8string{};
253 hilet root_name =
path.root_name().generic_u8string();
254 if (root_name.empty()) {
256 if (not
path.root_directory().empty()) {
258 r += u8
"file:" +
path.root_directory().generic_u8string();
264 }
else if (
hilet i = root_name.find(
':'); i != std::string::npos) {
267 r += u8
"file:///" + root_name +
path.root_directory().generic_u8string();
269 throw url_error(
"Paths containing a device are not allowed to be converted to a URL.");
273 r += u8
"file://" + root_name +
path.root_directory().generic_u8string();
274 if (not
path.root_directory().empty()) {
275 throw url_error(
"Invalid path contains server name without a root directory.");
279 return to_string(r +
path.relative_path().generic_u8string());
293template<
typename CharT>
294struct std::formatter<
hi::URL, CharT> : std::formatter<hi::URI, CharT> {
295 auto format(
hi::URL const& t,
auto& fc)
297 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:184
#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:537
constexpr std::optional< std::string > const & scheme() const noexcept
Get the scheme-component of the URI.
Definition URI.hpp:514
Universal Resource Locator.
Definition URL.hpp:49
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:74
constexpr URL(URI &&other) noexcept
Convert a URI to an URL.
Definition URL.hpp:65
operator std::filesystem::path() const
Definition URL.hpp:215
std::filesystem::path filesystem_path() const
Create a filesystem path from a file URL.
Definition URL.hpp:190
constexpr std::u8string filesystem_path_generic_u8string(bool validate_scheme=true) const
Return a generic path.
Definition URL.hpp:81
Definition exception.hpp:200