8 #ifndef MOD_SERVLET_URI_H
9 #define MOD_SERVLET_URI_H
21 #include <experimental/string_view>
28 using std::experimental::string_view;
35 using std::runtime_error::runtime_error;
42 using std::runtime_error::runtime_error;
279 URI() : _uri{}, _uri_view{_uri}, _scheme{_uri}, _user_info{_uri}, _host{_uri},
280 _port{_uri}, _path{_uri}, _query{_uri}, _fragment{_uri} {}
345 explicit URI(
string_type&& uri_str) : _uri{std::move(uri_str)} { _initialize(); }
373 explicit URI(
const char* uri_str) : _uri{uri_str} { _initialize(); }
390 template <
class InputIter>
391 URI(InputIter first, InputIter last) : _uri{
string_type(first, last)} { _initialize(); }
472 URI(
const URI &other) : _uri{other._uri}, _uri_view{_uri}, _normalized{other._normalized} { _move_parts(other); }
478 URI(
URI &&other) noexcept : _uri{std::move(other._uri)}, _uri_view{_uri} { _move_parts(other); }
555 URI &
operator=(
const char* uri_str) { _uri = uri_str; _initialize();
return *
this; }
561 void swap(
URI &other) noexcept;
647 uint16_t
port() const noexcept {
return _port_i; }
744 void parse_query(std::function<
void(std::string&&, std::string&&)> consumer)
823 const std::string&
string() const noexcept {
return _uri; }
855 bool empty() const noexcept {
return _uri.empty(); }
1041 int compare(const
URI &other) const noexcept;
1071 void _move_parts(const
URI& other);
1072 void _resize_parts(std::
size_t offset, int_fast16_t resize_bytes);
1073 void _decode_encoded_unreserved_chars();
1081 uint16_t _port_i = 0;
1085 bool _normalized = false;
1087 static const std::vector<std::pair<std::
string, uint16_t>> DEFAULT_PORTS;
1096 template<typename CharT, typename Traits>
1097 inline std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& out, const
URI& uri)
1099 return out << uri.uri_view();
1113 inline bool operator==(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() == uri2.uri_view(); }
1123 inline bool operator==(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() == uri2; }
1132 inline bool operator==(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 == uri2.string(); }
1141 inline bool operator==(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() == uri2; }
1150 inline bool operator==(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 == uri2.uri_view(); }
1162 inline bool operator!=(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() != uri2.uri_view(); }
1171 inline bool operator!=(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() != uri2; }
1180 inline bool operator!=(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 != uri2.string(); }
1189 inline bool operator!=(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() != uri2; }
1198 inline bool operator!=(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 != uri2.uri_view(); }
1210 inline bool operator<(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() < uri2.uri_view(); }
1219 inline bool operator<(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() < uri2; }
1228 inline bool operator<(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 < uri2.string(); }
1237 inline bool operator<(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() < uri2; }
1246 inline bool operator<(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 < uri2.uri_view(); }
1258 inline bool operator>(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() > uri2.uri_view(); }
1267 inline bool operator>(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() > uri2; }
1276 inline bool operator>(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 > uri2.string(); }
1285 inline bool operator>(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() > uri2; }
1294 inline bool operator>(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 > uri2.uri_view(); }
1306 inline bool operator<=(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() <= uri2.uri_view(); }
1315 inline bool operator<=(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() <= uri2; }
1324 inline bool operator<=(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 <= uri2.string(); }
1333 inline bool operator<=(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() <= uri2; }
1342 inline bool operator<=(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 <= uri2.uri_view(); }
1354 inline bool operator>=(
const URI& uri1,
const URI& uri2) noexcept {
return uri1.uri_view() >= uri2.uri_view(); }
1363 inline bool operator>=(
const URI& uri1,
const URI::string_type& uri2) noexcept {
return uri1.string() >= uri2; }
1372 inline bool operator>=(
const URI::string_type& uri1,
const URI& uri2) noexcept {
return uri1 >= uri2.string(); }
1381 inline bool operator>=(
const URI& uri1,
const URI::string_view& uri2) noexcept {
return uri1.uri_view() >= uri2; }
1390 inline bool operator>=(
const URI::string_view& uri1,
const URI& uri2) noexcept {
return uri1 >= uri2.uri_view(); }
1397 inline void swap(
URI &lhs,
URI &rhs) noexcept { lhs.swap(rhs); }
1401 #endif // MOD_SERVLET_URI_H
URI & operator=(const string_type &uri_str)
String assignment operator.
Definition: uri.h:510
string_view path() const noexcept
Returns the path view component of this URI.
Definition: uri.h:681
bool empty() const noexcept
Tells whether or not this URI is empty.
Definition: uri.h:855
URI(string_type &&uri_str)
Constructs a URI by parsing the given string.
Definition: uri.h:345
void set_port(string_view port)
Sets port component to this URI.
void normalize()
Normalizes this URI.
~URI() noexcept
Destructor.
Definition: uri.h:483
void swap(URI &other) noexcept
Swaps one uri object with another.
URI(InputIter first, InputIter last)
Constructs a URI by parsing the given string.
Definition: uri.h:391
int compare(const URI &other) const noexcept
Compares this URI against another.
void set_query(string_view query)
Sets query component to this URI.
string_view query() const noexcept
Returns the query view component of this URI.
Definition: uri.h:704
std::experimental::string_view string_view
The URI string_view.
Definition: uri.h:269
URI create_with_normalized_path() const
Create copy of this URI with normalized path.
URI & operator=(const URI &other)
Copy assignment operator.
static uint16_t get_default_port(string_view scheme)
Returns default port for given scheme or 0 if the default port could not be found.
URI create_normalized() const
Create normalized copy of this URI.
URI(URI &&other) noexcept
Move constructor.
Definition: uri.h:478
URI(const string_type &uri_str)
Constructs a URI by parsing the given string.
Definition: uri.h:331
URI relativize(const URI &uri) const
Relativizes the given URI against this URI.
void set_fragment(string_view fragment)
Sets fragment component to this URI.
string_view scheme() const noexcept
Returns the scheme component of this URI.
Definition: uri.h:580
Exception used in the mod_servlet.
URI()
Default constructor.
Definition: uri.h:279
string_view fragment() const noexcept
Returns the fragment view component of this URI.
Definition: uri.h:775
string_view user_info() const noexcept
Returns the userInfo component of this URI.
Definition: uri.h:600
URI(string_view scheme, string_view host, uint16_t port, string_view path, string_view query)
Constructs a hierarchical URI from the given components.
Definition: uri.h:465
const_iterator end() const noexcept
Returns an iterator at the last+1 element in the underlying URI view.
Definition: uri.h:573
URI(const URI &other)
Copy constructor.
Definition: uri.h:472
URI(const char *uri_str)
Constructs a URI by parsing the given string.
Definition: uri.h:373
std::string to_ASCII_string() const
Returns the content of this URI as a US-ASCII string.
uint16_t port() const noexcept
Returns the port component of this URI.
Definition: uri.h:647
URI resolve(const URI &uri) const
Resolves the given URI against this URI.
URI & operator=(string_type &&uri_str)
Move string assignment operator.
Definition: uri.h:525
URI & operator=(const char *uri_str)
C-string assignment operator.
Definition: uri.h:555
bool is_opaque() const noexcept
Tells whether or not this URI is opaque.
void set_scheme(string_view scheme)
Sets scheme component to this URI.
string_view port_view() const noexcept
Returns the port view component of this URI.
Definition: uri.h:640
string_view authority() const noexcept
Returns the raw authority component of this URI.
const_iterator begin() const noexcept
Returns an iterator at the first element in the underlying URI view.
Definition: uri.h:567
std::string string_type
The URI string_type.
Definition: uri.h:264
const std::string & string() const noexcept
Returns the URI as a std::string object.
Definition: uri.h:823
bool is_absolute() const noexcept
Tells whether or not this URI is absolute.
Definition: uri.h:864
URI(string_view uri_str)
Constructs a URI by parsing the given string.
Definition: uri.h:359
void add_to_query(string_view name, string_view value)
Adds name-value pair to the current query component of this URI.
string_view uri_view() const noexcept
Returns the string view representation of this URI.
Definition: uri.h:817
Represents a Uniform Resource Identifier (URI) reference.
Definition: uri.h:258
bool has_authority() const noexcept
Tests whether this URI has a valid authority.
Exception thrown while parsing URI if syntax error encountered.
Definition: uri.h:33
void normalize_path()
Normalizes this URI's path.
string_view host() const noexcept
Returns the host component of this URI.
Definition: uri.h:620
void set_user_info(string_view user_info)
Sets user_info component to this URI.
URI & operator=(string_view uri_str)
String view assignment operator.
Definition: uri.h:540
static std::string decode(string_view str)
Decode all the persent encoded characters in the string.
std::string && string_move()
Returns the URI as a std::string&& object.
Definition: uri.h:832
void set_host(string_view host)
Sets host component to this URI.
Exception thrown when it is impossible to build URI from parts.
Definition: uri.h:40
void set_path(string_view path)
Sets path component to this URI.
string_view::const_iterator const_iterator
The URI const_iterator type.
Definition: uri.h:274
void parse_query(std::function< void(std::string &&, std::string &&)> consumer)
Parses this URI's query into name-value pairs and feeds them to a given consumer. ...
Definition: uri.h:744