-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.hpp
120 lines (92 loc) · 3.18 KB
/
uri.hpp
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
#ifndef URI_HPP_INCLUDED
#define URI_HPP_INCLUDED
#include "dll_spec.h"
#include <cctype>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <boost/operators.hpp>
namespace uri {
enum class error {
// parser errors
invalid_syntax = 1,
};
class DLL_PUBLIC syntax_error : public std::system_error {
public:
syntax_error();
virtual ~syntax_error() noexcept;
};
const std::error_category& category();
struct components {
std::optional<std::string> scheme;
std::optional<std::string> authority; // further broken down into:
std::optional<std::string> userinfo; // from authority
std::optional<std::string> host; // from authority
std::optional<std::string> port; // from authority
std::optional<std::string> path;
std::optional<std::string> query;
std::optional<std::string> fragment;
};
DLL_PUBLIC bool parse_generic(std::string_view uri, components& comp);
DLL_PUBLIC bool parse_relative_ref(std::string_view uri, components& comp);
DLL_PUBLIC bool parse_reference(std::string_view uri, components& comp);
DLL_PUBLIC bool parse_absolute(std::string_view uri, components& comp);
DLL_PUBLIC std::string to_string(components const&);
DLL_PUBLIC std::string normalize(components);
enum class form : bool {
unnormalized,
normalized,
};
class DLL_PUBLIC uri : boost::operators<uri> {
public:
uri() {}
virtual ~uri() {}
// clang-format off
auto scheme() const { return parts_.scheme; }
auto authority() const { return parts_.authority; }
auto userinfo() const { return parts_.userinfo; }
auto host() const { return parts_.host; }
auto port() const { return parts_.port; }
auto path() const { return parts_.path; }
auto query() const { return parts_.query; }
auto fragment() const { return parts_.fragment; }
// clang-format on
components const& parts() const& { return parts_; }
components parts() && { return parts_; }
std::string const& string() const& { return uri_; }
std::string string() && { return uri_; }
bool empty() const { return uri_.empty(); }
bool operator<(uri const& rhs) const;
bool operator==(uri const& rhs) const;
protected:
explicit uri(std::string uri_in)
: uri_(std::move(uri_in))
{
}
std::string uri_;
components parts_; // All the string_views in parts_ point into uri_.
form form_{form::unnormalized};
};
// Derived types add only ctor()s that use different parsers.
class generic : public uri {
public:
generic(std::string uri_in, bool norm = false);
generic(components const& uri_in, bool norm = false);
};
class absolute : public uri {
public:
absolute(std::string uri_in, bool norm = false);
absolute(components const& uri_in, bool norm = false);
};
class reference : public uri {
public:
reference(std::string uri_in, bool norm = false);
reference(components const& uri_in, bool norm = false);
};
DLL_PUBLIC uri resolve_ref(absolute const& base, reference const& ref);
} // namespace uri
DLL_PUBLIC std::ostream& operator<<(std::ostream& os,
uri::components const& uri);
DLL_PUBLIC std::ostream& operator<<(std::ostream& os, uri::uri const&);
#endif // URI_HPP_INCLUDED