/src/wxwidgets/include/wx/uri.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/uri.h |
3 | | // Purpose: wxURI - Class for parsing URIs |
4 | | // Author: Ryan Norton |
5 | | // Vadim Zeitlin (UTF-8 URI support, many other changes) |
6 | | // Created: 07/01/2004 |
7 | | // Copyright: (c) 2004 Ryan Norton |
8 | | // 2008 Vadim Zeitlin |
9 | | // Licence: wxWindows Licence |
10 | | ///////////////////////////////////////////////////////////////////////////// |
11 | | |
12 | | #ifndef _WX_URI_H_ |
13 | | #define _WX_URI_H_ |
14 | | |
15 | | #include "wx/defs.h" |
16 | | #include "wx/object.h" |
17 | | #include "wx/string.h" |
18 | | #include "wx/arrstr.h" |
19 | | |
20 | | // Host Type that the server component can be |
21 | | enum wxURIHostType |
22 | | { |
23 | | wxURI_REGNAME, // Host is a normal register name (www.mysite.com etc.) |
24 | | wxURI_IPV4ADDRESS, // Host is a version 4 ip address (192.168.1.100) |
25 | | wxURI_IPV6ADDRESS, // Host is a version 6 ip address [aa:aa:aa:aa::aa:aa]:5050 |
26 | | wxURI_IPVFUTURE // Host is a future ip address (wxURI is unsure what kind) |
27 | | }; |
28 | | |
29 | | // Component Flags |
30 | | enum wxURIFieldType |
31 | | { |
32 | | wxURI_SCHEME = 1, |
33 | | wxURI_USERINFO = 2, |
34 | | wxURI_SERVER = 4, |
35 | | wxURI_PORT = 8, |
36 | | wxURI_PATH = 16, |
37 | | wxURI_QUERY = 32, |
38 | | wxURI_FRAGMENT = 64 |
39 | | }; |
40 | | |
41 | | // Miscellaneous other flags |
42 | | enum wxURIFlags |
43 | | { |
44 | | wxURI_STRICT = 1 |
45 | | }; |
46 | | |
47 | | |
48 | | // Generic class for parsing URIs. |
49 | | // |
50 | | // See RFC 3986 |
51 | | class WXDLLIMPEXP_BASE wxURI : public wxObject |
52 | | { |
53 | | public: |
54 | | wxURI() = default; |
55 | 0 | wxURI(const wxString& uri) { Create(uri); } |
56 | | |
57 | | wxURI(const wxURI& uri) = default; |
58 | | wxURI& operator=(const wxURI& uri) = default; |
59 | | |
60 | | wxURI(wxURI&& uri) = default; |
61 | | wxURI& operator=(wxURI&& uri) = default; |
62 | | |
63 | 0 | ~wxURI() = default; |
64 | | |
65 | | bool Create(const wxString& uri); |
66 | | |
67 | | wxURI& operator=(const wxString& string) |
68 | 0 | { |
69 | 0 | Create(string); |
70 | 0 | return *this; |
71 | 0 | } |
72 | | |
73 | | bool operator==(const wxURI& uri) const; |
74 | | |
75 | | // various accessors |
76 | 0 | bool IsEmpty() const { return m_fields == 0; } |
77 | | |
78 | 0 | bool HasScheme() const { return (m_fields & wxURI_SCHEME) != 0; } |
79 | 0 | bool HasUserInfo() const { return (m_fields & wxURI_USERINFO) != 0; } |
80 | 0 | bool HasServer() const { return (m_fields & wxURI_SERVER) != 0; } |
81 | 0 | bool HasPort() const { return (m_fields & wxURI_PORT) != 0; } |
82 | 0 | bool HasPath() const { return (m_fields & wxURI_PATH) != 0; } |
83 | 0 | bool HasQuery() const { return (m_fields & wxURI_QUERY) != 0; } |
84 | 0 | bool HasFragment() const { return (m_fields & wxURI_FRAGMENT) != 0; } |
85 | | |
86 | 0 | const wxString& GetScheme() const { return m_scheme; } |
87 | 0 | const wxString& GetPath() const { return m_path; } |
88 | 0 | const wxString& GetQuery() const { return m_query; } |
89 | 0 | const wxString& GetFragment() const { return m_fragment; } |
90 | 0 | const wxString& GetPort() const { return m_port; } |
91 | 0 | const wxString& GetUserInfo() const { return m_userinfo; } |
92 | 0 | const wxString& GetServer() const { return m_server; } |
93 | 0 | wxURIHostType GetHostType() const { return m_hostType; } |
94 | | |
95 | | // these functions only work if the user information part of the URI is in |
96 | | // the usual (but insecure and hence explicitly recommended against by the |
97 | | // RFC) "user:password" form |
98 | | wxString GetUser() const; |
99 | | wxString GetPassword() const; |
100 | | |
101 | | // Set username and password for the URI. This function is _not_ the exact |
102 | | // counterpart of GetUserInfo() because it takes unescaped strings, unlike |
103 | | // the latter, which returns them in the escaped form, hence it uses a |
104 | | // deliberately different name. |
105 | | void SetUserAndPassword(const wxString& user, const wxString& password = {}); |
106 | | |
107 | | // combine all URI components into a single string |
108 | | // |
109 | | // BuildURI() returns the real URI suitable for use with network libraries, |
110 | | // for example, while BuildUnescapedURI() returns a string suitable to be |
111 | | // shown to the user. |
112 | 0 | wxString BuildURI() const { return DoBuildURI(&wxURI::Nothing); } |
113 | 0 | wxString BuildUnescapedURI() const { return DoBuildURI(&wxURI::Unescape); } |
114 | | |
115 | | // the escaped URI should contain only ASCII characters, including possible |
116 | | // escape sequences |
117 | | static wxString Unescape(const wxString& escapedURI); |
118 | | |
119 | | |
120 | | void Resolve(const wxURI& base, int flags = wxURI_STRICT); |
121 | | bool IsReference() const; |
122 | | bool IsRelative() const; |
123 | | |
124 | | protected: |
125 | | void Clear(); |
126 | | |
127 | | // common part of BuildURI() and BuildUnescapedURI() |
128 | | wxString DoBuildURI(wxString (*funcDecode)(const wxString&)) const; |
129 | | |
130 | | // function which returns its argument unmodified, this is used by |
131 | | // BuildURI() to tell DoBuildURI() that nothing needs to be done with the |
132 | | // URI components |
133 | 0 | static wxString Nothing(const wxString& value) { return value; } |
134 | | |
135 | | bool Parse(const char* uri); |
136 | | |
137 | | const char* ParseAuthority (const char* uri); |
138 | | const char* ParseScheme (const char* uri); |
139 | | const char* ParseUserInfo (const char* uri); |
140 | | const char* ParseServer (const char* uri); |
141 | | const char* ParsePort (const char* uri); |
142 | | const char* ParsePath (const char* uri); |
143 | | const char* ParseQuery (const char* uri); |
144 | | const char* ParseFragment (const char* uri); |
145 | | |
146 | | |
147 | | static bool ParseH16(const char*& uri); |
148 | | static bool ParseIPv4address(const char*& uri); |
149 | | static bool ParseIPv6address(const char*& uri); |
150 | | static bool ParseIPvFuture(const char*& uri); |
151 | | |
152 | | // append next character pointer to by p to the string in an escaped form |
153 | | // and advance p past it |
154 | | // |
155 | | // if the next character is '%' and it's followed by 2 hex digits, they are |
156 | | // not escaped (again) by this function, this allows to keep (backwards- |
157 | | // compatible) ambiguity about the input format to wxURI::Create(): it can |
158 | | // be either already escaped or not |
159 | | void AppendNextEscaped(wxString& s, const char *& p); |
160 | | |
161 | | // convert hexadecimal digit to its value; return -1 if c isn't valid |
162 | | static int CharToHex(char c); |
163 | | |
164 | | // split an URI path string in its component segments (including empty and |
165 | | // "." ones, no post-processing is done) |
166 | | static wxArrayString SplitInSegments(const wxString& path); |
167 | | |
168 | | // various URI grammar helpers |
169 | | static bool IsUnreserved(char c); |
170 | | static bool IsReserved(char c); |
171 | | static bool IsGenDelim(char c); |
172 | | static bool IsSubDelim(char c); |
173 | | static bool IsHex(char c); |
174 | | static bool IsAlpha(char c); |
175 | | static bool IsDigit(char c); |
176 | | static bool IsEndPath(char c); |
177 | | static bool IsPCharNCNE(char c); |
178 | | static bool IsPCharNE(char c); |
179 | | |
180 | | wxString m_scheme; |
181 | | wxString m_path; |
182 | | wxString m_query; |
183 | | wxString m_fragment; |
184 | | |
185 | | wxString m_userinfo; |
186 | | wxString m_server; |
187 | | wxString m_port; |
188 | | |
189 | | wxURIHostType m_hostType = wxURI_REGNAME; |
190 | | |
191 | | // This is a combination of wxURIFieldType flags. |
192 | | unsigned m_fields = 0; |
193 | | |
194 | | wxDECLARE_DYNAMIC_CLASS(wxURI); |
195 | | }; |
196 | | |
197 | | #endif // _WX_URI_H_ |
198 | | |