13#include "enum_flags.h"
14#include "string_ops.h"
28 using uri = std::string;
29 using uri_view = std::string_view;
31 enum class uri_error_code
43 authority_not_allowed_in_scheme,
44 authority_invalid_for_scheme,
45 authority_required_in_scheme,
48 user_info_not_allowed_in_scheme,
49 user_info_invalid_for_scheme,
50 user_info_required_in_scheme,
53 host_not_allowed_in_scheme,
54 host_invalid_for_scheme,
55 host_required_in_scheme,
58 port_not_allowed_in_scheme,
59 port_invalid_for_scheme,
60 port_required_in_scheme,
63 path_element_malformed,
66 query_not_allowed_in_scheme,
67 query_invalid_for_scheme,
68 query_required_in_scheme,
71 fragment_not_allowed_in_scheme,
72 fragment_invalid_for_scheme,
73 fragment_required_in_scheme,
75 invalid_percent_encoding,
77 no_scheme_specific_elements,
78 scheme_specific_element_malformed,
91 std::string authority{};
92 std::string user_info{};
96 std::vector<std::string> path_elements;
100 std::vector<std::pair<std::string, std::string>> query_elements;
101 std::string fragment{};
103 bool canonical_form =
false;
105 bool empty()
const noexcept {
return scheme.empty(); }
110 template <
typename T>
120 split_query_elements,
124 lowercase_when_appropriate,
133 enum class uri_compose_flags
137 lowercase_when_appropriate,
147 struct known_uri_scheme
158 return validate_authority(
decomposed.value().authority)
159 .and_then([&] {
return validate_path(
decomposed.value().path); })
168 return validate_user_info(fragment)
169 .and_then([&] {
return validate_host(fragment); })
170 .
and_then([&] {
return validate_port(fragment); });
172 virtual uri_error validate_user_info(std::string_view element)
const noexcept {
return {}; }
173 virtual uri_error validate_host(std::string_view element)
const noexcept {
return {}; }
174 virtual uri_error validate_port(std::string_view element)
const noexcept {
return {}; }
175 virtual uri_error validate_path(std::string_view element)
const noexcept {
return {}; }
176 virtual uri_error validate_query(std::string_view element)
const noexcept {
return {}; }
177 virtual uri_error validate_fragment(std::string_view element)
const noexcept {
return {}; }
179 virtual std::string_view default_authority()
const noexcept {
return {}; }
180 virtual std::string_view default_user_info()
const noexcept {
return {}; }
181 virtual std::string_view default_host()
const noexcept {
return {}; }
182 virtual std::string_view default_port()
const noexcept {
return {}; }
183 virtual std::string_view default_path()
const noexcept {
return {}; }
184 virtual std::string_view default_query()
const noexcept {
return {}; }
185 virtual std::string_view default_fragment()
const noexcept {
return {}; }
190 virtual std::vector<std::pair<std::string, std::string>> split_query_elements(std::string_view query)
const noexcept;
192 virtual std::string normalize_authority(std::string_view element)
const noexcept {
return std::string{ element }; }
193 virtual std::string normalize_user_info(std::string_view element)
const noexcept {
return std::string{ element }; }
194 virtual std::string normalize_host(std::string_view element)
const noexcept {
return std::string{ element }; }
195 virtual std::string normalize_port(std::string_view element)
const noexcept {
return std::string{ element }; }
196 virtual std::string normalize_path(std::string_view element)
const noexcept {
return std::string{ element }; }
197 virtual std::string normalize_query(std::string_view element)
const noexcept {
return std::string{ element }; }
198 virtual std::string normalize_fragment(std::string_view element)
const noexcept {
return std::string{ element }; }
212 return uri_error_code::no_scheme_specific_elements;
220 known_uri_scheme
const* query_uri_scheme(std::string_view scheme);
222 namespace known_schemes
224 struct file_scheme :
public known_uri_scheme
227 virtual std::string_view scheme()
const noexcept override {
return "file"; }
229 static bool is_local(decomposed_uri
const&
uri)
231 return uri.host.empty() ||
uri.host ==
"localhost";
234 inline const file_scheme file;
239 explicit uri_builder(
uri&
uri);
240 uri_builder(
uri&
uri, known_uri_scheme
const& scheme);
241 uri_builder(
const uri_builder&) =
delete;
242 uri_builder& operator= (
const uri_builder&) =
delete;
245 template <
class Source>
246 uri_builder& scheme(
const Source& scheme);
248 template <
class Source>
249 uri_builder& authority(
const Source& authority);
251 template <
class Source>
252 uri_builder& authority(
const Source& user_info,
const Source& host,
const Source& port);
254 template <
class Source>
255 uri_builder& user_info(
const Source& user_info);
257 template <
class Source>
258 uri_builder& host(
const Source& host);
260 template <
class Source>
261 uri_builder& port(
const Source& port);
263 template <
class Source>
264 uri_builder& path(
const Source& path);
266 template <
class Source>
267 uri_builder& query(
const Source& query);
269 template <
class Source>
270 uri_builder& fragment(
const Source& fragment);
284 struct is_error_code_enum<
ghassanpl::uri_error_code> : true_type {};
constexpr auto bit_count
Equal to the number of bits in the type.
uri_expected< uri > make_uri_safe_for_display(uri_view uri)
Removes data that should not be displayed to an untrusted user (user-info after the first ':',...
std::string uri
URIs are stored in a UTF-8 encoding where both non-ASCII code unit bytes as well as URI-reserved char...
uri_decompose_flags
Flags that modify how a URI string is decomposed into ghassanpl::decomposed_uri.
uri_expected< decomposed_uri > decompose_uri(uri_view uri, enum_flags< uri_decompose_flags > flags=enum_flags< uri_decompose_flags >::all())
This function will decompose URI into its composite elements, which includes percent-decoding all the...
@ use_well_known_port_numbers
if a port is not specified in the uri, the result will guess the port based on the scheme
Primary namespace for everything in this library.
Holds the constituents of a URI.
std::vector< std::string > normalized_path() const noexcept
Returns the path normalized by applying any "." or ".." elements.
EF_NODISCARD static EF_CONSTEXPR self_type all() EF_NOEXCEPT
Returns a value with all bits set (including the ones not in the enum, if any)