libUTL++
URI.h
1 #pragma once
2 
4 
5 #include <libutl/String.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
13 class Array;
14 class BitArray;
15 class Collection;
16 class RBtree;
17 
19 
48 
50 class URI : public Object
51 {
53 
54 public:
59  URI(const String& uri)
60  {
61  init();
62  set(uri);
63  }
64 
65  virtual void copy(const Object& rhs);
66 
67  virtual void steal(Object& rhs);
68 
69  virtual int compare(const Object& rhs) const;
70 
71  virtual String toString() const;
72 
73  virtual size_t innerAllocatedSize() const;
74 
76 
77 
78  bool
79  isRelative() const
80  {
81  return (_scheme == nullptr);
82  }
83 
85  String get() const;
86 
88  const String&
89  scheme() const
90  {
91  return (_scheme == nullptr) ? emptyString : *_scheme;
92  }
93 
95  String authority() const;
96 
98  String userinfo() const;
99 
101  const String&
102  username() const
103  {
104  return (_username == nullptr) ? emptyString : *_username;
105  }
106 
108  const String&
109  password() const
110  {
111  return (_password == nullptr) ? emptyString : *_password;
112  }
113 
115  const String&
116  hostname() const
117  {
118  return (_hostname == nullptr) ? emptyString : *_hostname;
119  }
120 
122  uint_t
123  port() const
124  {
125  return _port;
126  }
127 
129  String fullPath() const;
130 
132  const String&
133  path() const
134  {
135  return (_path == nullptr) ? emptyString : *_path;
136  }
137 
139  String fullFilename() const;
140 
142  const String&
143  filename() const
144  {
145  return (_filename == nullptr) ? emptyString : *_filename;
146  }
147 
149  const String&
150  extension() const
151  {
152  return (_extension == nullptr) ? emptyString : *_extension;
153  }
154 
156  size_t numSegments() const;
157 
165  String segment(size_t id) const;
166 
168  String query() const;
169 
171  size_t queryVarsCount() const;
172 
179  void queryVarNameValue(size_t id, String& name, String& value) const;
180 
185  void queryVarsCopy(Collection& col) const;
186 
188  const String&
189  fragment() const
190  {
191  return (_fragment == nullptr) ? emptyString : *_fragment;
192  }
194 
196 
197 
198  void clear();
199 
201  void set(const String& uri);
202 
204  void appendSlash();
205 
207  void removeSlash();
208 
210  void
211  setScheme(const String& scheme)
212  {
213  delete _scheme;
214  _scheme = scheme.clone();
215  }
216 
222  void
223  setUserinfo(const String& username, const String& password)
224  {
225  delete _username;
226  _username = username.clone();
227  delete _password;
228  _password = password.clone();
229  }
230 
232  void
233  setHostname(const String& hostname)
234  {
235  delete _hostname;
236  _hostname = hostname.clone();
237  }
238 
240  void
242  {
243  _port = port;
244  }
245 
247  void setFullPath(const String& fp);
248 
250  void
251  setPath(const String& path)
252  {
253  delete _path;
254  _path = path.clone();
255  }
256 
258  void addQueryVariable(const String& name, const String& value);
259 
261  void
262  setFilename(const String& filename)
263  {
264  delete _filename;
265  _filename = filename.clone();
266  }
267 
269  void
270  setExtension(const String& extension)
271  {
272  delete _extension;
273  _extension = extension.clone();
274  }
276 
278 
279 
280  operator String() const
281  {
282  return get();
283  }
284 
286  URI operator+(const char* rhs) const
287  {
288  URI uri(get());
289  uri.setFullPath(fullPath() + rhs);
290  return uri;
291  }
292 
294  URI operator+(const String& rhs) const
295  {
296  URI uri(get());
297  uri.setFullPath(fullPath() + rhs);
298  return uri;
299  }
300 
302  URI& operator+=(const char* rhs)
303  {
304  setFullPath(fullPath() + rhs);
305  return self;
306  }
307 
309  URI& operator+=(const String& rhs)
310  {
311  setFullPath(fullPath() + rhs);
312  return self;
313  }
315 
317 
318 
319  static String percentEncode(const String& str, bool isPath = false);
320 
322  static String percentDecode(const String& str);
324 
326 
327 
328  static void utl_init();
329 
331  static void utl_deInit();
333 private:
334  void init();
335  void deInit();
336 
337 private:
338  static BitArray* _unreservedChars;
339 
340 private:
341  String* _scheme;
342  String* _username;
343  String* _password;
344  String* _hostname;
345  uint_t _port;
346  String* _path;
347  String* _filename;
348  String* _extension;
349  Array* _queryVars;
350  String* _fragment;
351 };
352 
354 
355 UTL_NS_END;
void setHostname(const String &hostname)
Set the hostname.
Definition: URI.h:233
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
uint_t port() const
Get the port.
Definition: URI.h:123
void setPath(const String &path)
Set the path.
Definition: URI.h:251
void deInit()
De-initialize UTL++.
const String & extension() const
Get the extension.
Definition: URI.h:150
URI & operator+=(const String &rhs)
Append the given string to the full path.
Definition: URI.h:309
URI operator+(const char *rhs) const
Get a copy of self with the given string appended to the full path.
Definition: URI.h:286
void setExtension(const String &extension)
Set the extension.
Definition: URI.h:270
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Character string.
Definition: String.h:31
void setFilename(const String &filename)
Set the filename.
Definition: URI.h:262
SortedCollection that stores objects in an array.
Definition: Array.h:116
const String emptyString
An empty string.
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
URI(const String &uri)
Constructor.
Definition: URI.h:59
const String & scheme() const
Get the scheme.
Definition: URI.h:89
Generic URI (Universal Resource Identifier).
Definition: URI.h:50
const String & username() const
Get the username.
Definition: URI.h:102
bool isRelative() const
Relative URI?
Definition: URI.h:79
const String & filename() const
Get the filename.
Definition: URI.h:143
const String & path() const
Get the path.
Definition: URI.h:133
void setUserinfo(const String &username, const String &password)
Set the "userinfo" (username & password).
Definition: URI.h:223
const String & fragment() const
Get the fragment identifier.
Definition: URI.h:189
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
URI operator+(const String &rhs) const
Get a copy of self with the given string appended to the full path.
Definition: URI.h:294
void setScheme(const String &scheme)
Set the scheme.
Definition: URI.h:211
URI & operator+=(const char *rhs)
Append the given string to the full path.
Definition: URI.h:302
const String & hostname() const
Get the hostname.
Definition: URI.h:116
const String & password() const
Get the password.
Definition: URI.h:109
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void setFullPath(const String &fp)
Set the full path (path + filename + extension).
void setPort(uint_t port)
Set the port.
Definition: URI.h:241
void init()
Initialize UTL++.
Array of n-bit values.
Definition: BitArray.h:36
A collection of objects.
Definition: Collection.h:62