libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
url.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <promeki/namespace.h>
14#include <promeki/string.h>
15#include <promeki/stringlist.h>
16#include <promeki/map.h>
17#include <promeki/error.h>
18#include <promeki/result.h>
19#include <promeki/datatype.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class DataStream;
24
73class Url {
74 public:
75 PROMEKI_DATATYPE(Url, DataTypeUrl, 1)
76
77
78 Error writeToStream(DataStream &s) const;
80 template <uint32_t V> static Result<Url> readFromStream(DataStream &s);
81
83 static constexpr int PortUnset = -1;
84
100 static Result<Url> fromString(const String &s);
101
116 static String percentEncode(const String &s, const char *safe = nullptr);
117
125 static String percentDecode(const String &s, Error *err = nullptr);
126
128 Url() = default;
129
140 explicit Url(const String &s) : Url(fromString(s).first()) {}
141
143 explicit Url(const char *s) : Url(fromString(String(s)).first()) {}
144
151 bool isValid() const { return !_scheme.isEmpty(); }
152
154 const String &scheme() const { return _scheme; }
155
157 const String &userInfo() const { return _userInfo; }
158
165 const String &host() const { return _host; }
166
170 int port() const { return _port; }
171
173 const String &path() const { return _path; }
174
184 const Map<String, String> &query() const { return _query; }
185
195 String queryValue(const String &key, const String &defaultValue = String()) const {
196 return _query.value(key, defaultValue);
197 }
198
204 bool hasQueryValue(const String &key) const { return _query.contains(key); }
205
207 const String &fragment() const { return _fragment; }
208
218 bool hasAuthority() const { return _hasAuthority; }
219
221 Url &setScheme(const String &s);
222
224 Url &setUserInfo(const String &s) {
225 _userInfo = s;
226 return *this;
227 }
228
235 Url &setHost(const String &s);
236
238 Url &setPort(int p) {
239 _port = p;
240 return *this;
241 }
242
244 Url &setPath(const String &s) {
245 _path = s;
246 return *this;
247 }
248
250 Url &setQuery(const Map<String, String> &q) {
251 _query = q;
252 return *this;
253 }
254
256 Url &setQueryValue(const String &key, const String &value) {
257 _query.insert(key, value);
258 return *this;
259 }
260
262 Url &removeQueryValue(const String &key) {
263 _query.remove(key);
264 return *this;
265 }
266
268 Url &setFragment(const String &s) {
269 _fragment = s;
270 return *this;
271 }
272
282 Url &setHasAuthority(bool v) {
283 _hasAuthority = v;
284 return *this;
285 }
286
295 String toString() const;
296
316 String redactedString() const;
317
319 bool operator==(const Url &other) const;
320
322 bool operator!=(const Url &other) const { return !(*this == other); }
323
324 private:
325 String _scheme;
326 String _userInfo;
327 String _host;
328 int _port = PortUnset;
329 String _path;
330 Map<String, String> _query;
331 String _fragment;
332 bool _hasAuthority = false;
333};
334
335PROMEKI_NAMESPACE_END
336
337PROMEKI_FORMAT_VIA_TOSTRING(promeki::Url);
338
339#endif // PROMEKI_ENABLE_CORE