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 _rawQuery.clear();
253 return *this;
254 }
255
257 Url &setQueryValue(const String &key, const String &value) {
258 _query.insert(key, value);
259 _rawQuery.clear();
260 return *this;
261 }
262
264 Url &removeQueryValue(const String &key) {
265 _query.remove(key);
266 _rawQuery.clear();
267 return *this;
268 }
269
291 const String &rawQuery() const { return _rawQuery; }
292
304 Url &setRawQuery(const String &q) {
305 _rawQuery = q;
306 return *this;
307 }
308
310 Url &setFragment(const String &s) {
311 _fragment = s;
312 return *this;
313 }
314
324 Url &setHasAuthority(bool v) {
325 _hasAuthority = v;
326 return *this;
327 }
328
337 String toString() const;
338
358 String redactedString() const;
359
393 String briefForLog() const;
394
396 bool operator==(const Url &other) const;
397
399 bool operator!=(const Url &other) const { return !(*this == other); }
400
401 private:
402 String _scheme;
403 String _userInfo;
404 String _host;
405 int _port = PortUnset;
406 String _path;
407 Map<String, String> _query;
408 String _rawQuery;
409 String _fragment;
410 bool _hasAuthority = false;
411};
412
413PROMEKI_NAMESPACE_END
414
415PROMEKI_FORMAT_VIA_TOSTRING(promeki::Url);
416
417#endif // PROMEKI_ENABLE_CORE