nlib
Uri.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_URI_H_
17 #define INCLUDE_NN_NLIB_URI_H_
18 
19 #include <utility>
20 
21 #include "nn/nlib/Config.h"
22 #include "nn/nlib/Nlist.h"
23 #include "nn/nlib/Swap.h"
24 
25 NLIB_NAMESPACE_BEGIN
26 
27 class InputStream;
28 class OutputStream;
29 class MemoryOutputStream;
30 
31 template<size_t N = 1024>
33  public:
34  UriQueryEncoder() NLIB_NOEXCEPT { cur_ = &buf_[0]; }
35  errno_t Append(const char* key, const char* value) NLIB_NOEXCEPT;
36  const char* c_str() const NLIB_NOEXCEPT {
37  *cur_ = '\0';
38  return &buf_[0];
39  }
40  operator const char*() const NLIB_NOEXCEPT { return c_str(); }
41  std::pair<const char*, const char*> GetData() const NLIB_NOEXCEPT {
42  return std::make_pair(&buf_[0], cur_);
43  }
44 
45  private:
46  char* cur_;
47  char buf_[N];
48 };
49 
50 struct UriPrivate;
52  public:
53  static bool
54  IsHostName(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
55  static bool IsHostName(const nlib_utf8_t* str) NLIB_NOEXCEPT {
56  return IsHostName(str, str + nlib_strlen(str));
57  }
58  static bool
59  IsIpv4(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
60  static bool IsIpv4(const nlib_utf8_t* str) NLIB_NOEXCEPT {
61  return IsIpv4(str, str + nlib_strlen(str));
62  }
63  static bool
64  IsIpv6(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
65  static bool IsIpv6(const nlib_utf8_t* str) NLIB_NOEXCEPT {
66  return IsIpv6(str, str + nlib_strlen(str));
67  }
68  static bool
69  IsEmailAddress(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
70  static bool IsEmailAddress(const nlib_utf8_t* str) NLIB_NOEXCEPT {
71  return IsEmailAddress(str, str + nlib_strlen(str));
72  }
73  static bool IsUri(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
74  static bool IsUri(const nlib_utf8_t* str) NLIB_NOEXCEPT {
75  return IsUri(str, str + nlib_strlen(str));
76  }
77  static bool
78  IsUriReference(const nlib_utf8_t* first, const nlib_utf8_t* last) NLIB_NOEXCEPT NLIB_NONNULL;
79  static bool IsUriReference(const nlib_utf8_t* str) NLIB_NOEXCEPT {
80  return IsUriReference(str, str + nlib_strlen(str));
81  }
82 
83  // reserved = gen-delims / sub-delims
84  // gen - delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
85  // sub - delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
86  static bool IsReserved(int c) NLIB_NOEXCEPT;
87  // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
88  static bool IsUnreserved(int c) NLIB_NOEXCEPT;
89  // 0-9 / A-F / a-f
90  static bool IsHexDigit(int c) NLIB_NOEXCEPT;
91 
92  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
93  const char* last) NLIB_NOEXCEPT;
94  template<size_t N>
95  static errno_t
96  DecodeUriComponent(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
97  return DecodeUriComponent(written, buf, N, s);
98  }
99  static errno_t
100  DecodeUriComponent(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT {
101  return DecodeUriComponent(written, buf, n, s, s + nlib_strlen(s));
102  }
103  template<size_t N>
104  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N], const char* first,
105  const char* last) NLIB_NOEXCEPT {
106  return DecodeUriComponent(written, buf, N, first, last);
107  }
108  static std::pair<errno_t, size_t>
109  DecodeUriComponent(char* buf, size_t n, const char* first, const char* last) NLIB_NOEXCEPT {
110  size_t written;
111  errno_t e = DecodeUriComponent(&written, buf, n, first, last);
112  return std::make_pair(e, written);
113  }
114  template<size_t N>
115  static std::pair<errno_t, size_t>
116  DecodeUriComponent(char (&buf)[N], const char* first, const char* last) NLIB_NOEXCEPT {
117  return DecodeUriComponent(buf, N, first, last);
118  }
119  static std::pair<errno_t, size_t>
120  DecodeUriComponent(char* buf, size_t n, const char* s) NLIB_NOEXCEPT {
121  return DecodeUriComponent(buf, n, s, s + nlib_strlen(s));
122  }
123  template<size_t N>
124  static std::pair<errno_t, size_t>
125  DecodeUriComponent(char (&buf)[N], const char* s) NLIB_NOEXCEPT {
126  return DecodeUriComponent(buf, N, s, s + nlib_strlen(s));
127  }
128  static errno_t DecodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
129  template<size_t N>
130  static errno_t DecodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
131  return DecodePath(written, buf, N, s);
132  }
133  static std::pair<errno_t, size_t> DecodePath(char* buf, size_t n, const char* s) NLIB_NOEXCEPT {
134  size_t written;
135  errno_t e = DecodePath(&written, buf, n, s);
136  return std::make_pair(e, written);
137  }
138  template<size_t N>
139  static std::pair<errno_t, size_t> DecodePath(char (&buf)[N], const char* s) NLIB_NOEXCEPT {
140  return DecodePath(buf, N, s);
141  }
142 
143  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
144  const char* last, bool fragment_mode) NLIB_NOEXCEPT;
145  template<size_t N>
146  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* first,
147  const char* last, bool fragment_mode) NLIB_NOEXCEPT {
148  return EncodeUriComponent(written, buf, N, first, last, fragment_mode);
149  }
150  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* s,
151  bool fragment_mode) NLIB_NOEXCEPT {
152  return EncodeUriComponent(written, buf, n, s, s + nlib_strlen(s), fragment_mode);
153  }
154  template<size_t N>
155  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* s,
156  bool fragment_mode) NLIB_NOEXCEPT {
157  return EncodeUriComponent(written, buf, N, s, fragment_mode);
158  }
159  static std::pair<errno_t, size_t>
160  EncodeUriComponent(char* buf, size_t n, const char* first, const char* last,
161  bool fragment_mode) NLIB_NOEXCEPT {
162  size_t written;
163  errno_t e = EncodeUriComponent(&written, buf, n, first, last, fragment_mode);
164  return std::make_pair(e, written);
165  }
166  template<size_t N>
167  static std::pair<errno_t, size_t>
168  EncodeUriComponent(char (&buf)[N], const char* first, const char* last,
169  bool fragment_mode) NLIB_NOEXCEPT {
170  return EncodeUriComponent(buf, N, first, last, fragment_mode);
171  }
172  static std::pair<errno_t, size_t>
173  EncodeUriComponent(char* buf, size_t n, const char* s, bool fragment_mode) {
174  return EncodeUriComponent(buf, n, s, s + nlib_strlen(s), fragment_mode);
175  }
176  template<size_t N>
177  static std::pair<errno_t, size_t>
178  EncodeUriComponent(char (&buf)[N], const char* s, bool fragment_mode) {
179  return EncodeUriComponent(buf, N, s, s + nlib_strlen(s), fragment_mode);
180  }
181 
182  static errno_t EncodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
183  template<size_t N>
184  static errno_t EncodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
185  return EncodePath(written, buf, N, s);
186  }
187  static std::pair<errno_t, size_t> EncodePath(char* buf, size_t n, const char* s) NLIB_NOEXCEPT {
188  size_t written;
189  errno_t e = EncodePath(&written, buf, n, s);
190  return std::make_pair(e, written);
191  }
192  template<size_t N>
193  static std::pair<errno_t, size_t> EncodePath(char (&buf)[N], const char* s) NLIB_NOEXCEPT {
194  return EncodePath(buf, N, s);
195  }
196 
197  public:
198  NLIB_CEXPR Uri() NLIB_NOEXCEPT : prv_(nullptr) {}
199  ~Uri() NLIB_NOEXCEPT { Reset(); }
200  NLIB_DEFMOVE_PIMPL(Uri);
201  bool Parse(const char* first, const char* last) NLIB_NOEXCEPT;
202  bool Parse(const char* str) NLIB_NOEXCEPT { return Parse(str, str + nlib_strlen(str)); }
203  // for example:
204  // http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag
205  // scheme = "http"
206  // userinfo = "foobar:pass"
207  // host = "www.example.com"
208  // port = "80"
209  // path = "/my/dir/index.html"
210  // query = "var=value"
211  // fragment = "frag"
212  bool SetUri(const char* scheme, const char* userinfo, const char* host, const char* port,
213  const char* path, const char* query, const char* fragment) NLIB_NOEXCEPT;
214  bool IsAbsolute() const NLIB_NOEXCEPT { return GetScheme() != nullptr; }
215  bool ComposeString(char* buf, size_t size) const NLIB_NOEXCEPT;
216  template<size_t N>
217  bool ComposeString(char (&buf)[N]) const NLIB_NOEXCEPT {
218  return this->ComposeString(buf, N);
219  }
220 
221  // AddBaseUri("g/", "http://a/b/c/d;p?q") -> http://a/b/c/g/
222  // AddBaseUri("/g", "http://a/b/c/d;p?q") -> http://a/g
223  // AddBaseUri("//g", "http://a/b/c/d;p?q") -> http://g
224  bool AddBaseUri(const Uri& relative, const Uri& base) NLIB_NOEXCEPT;
225  // "http" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
226  const char* GetScheme() const NLIB_NOEXCEPT;
227  // "foobar:pass" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
228  const char* GetUserInfo() const NLIB_NOEXCEPT;
229  // "www.example.com" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
230  const char* GetHost() const NLIB_NOEXCEPT;
231  // "80" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
232  const char* GetPort() const NLIB_NOEXCEPT;
233  int GetPortNumber() const NLIB_NOEXCEPT;
234  // "/my/dir/index.html" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
235  const char* GetPath() const NLIB_NOEXCEPT;
236  // "var=value" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
237  const char* GetQuery() const NLIB_NOEXCEPT;
238  // "frag" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
239  const char* GetFragment() const NLIB_NOEXCEPT;
240 
241  bool SetScheme(const char* first, const char* last) NLIB_NOEXCEPT;
242  bool SetUserInfo(const char* first, const char* last) NLIB_NOEXCEPT;
243  bool SetHost(const char* first, const char* last) NLIB_NOEXCEPT;
244  bool SetPort(const char* first, const char* last) NLIB_NOEXCEPT;
245  bool SetPath(const char* first, const char* last) NLIB_NOEXCEPT;
246  bool SetQuery(const char* first, const char* last) NLIB_NOEXCEPT;
247  bool SetFragment(const char* first, const char* last) NLIB_NOEXCEPT;
248  bool SetScheme(const char* scheme) NLIB_NOEXCEPT {
249  return scheme ? SetScheme(scheme, scheme + nlib_strlen(scheme))
250  : SetScheme(nullptr, nullptr);
251  }
252  bool SetUserInfo(const char* userinfo) NLIB_NOEXCEPT {
253  return userinfo ? SetUserInfo(userinfo, userinfo + nlib_strlen(userinfo))
254  : SetUserInfo(nullptr, nullptr);
255  }
256  bool SetHost(const char* host) NLIB_NOEXCEPT {
257  return host ? SetHost(host, host + nlib_strlen(host)) : SetHost(nullptr, nullptr);
258  }
259  bool SetPort(const char* port) NLIB_NOEXCEPT {
260  return port ? SetPort(port, port + nlib_strlen(port)) : SetPort(nullptr, nullptr);
261  }
262  bool SetPortNumber(int port) NLIB_NOEXCEPT {
263  size_t cnt;
264  char buf[16];
265  nlib_snprintf(&cnt, buf, "%d", port);
266  return SetPort(&buf[0], &buf[0] + cnt);
267  }
268  bool SetPath(const char* path) NLIB_NOEXCEPT {
269  return path ? SetPath(path, path + nlib_strlen(path)) : SetPath(nullptr, nullptr);
270  }
271  bool SetQuery(const char* query) NLIB_NOEXCEPT {
272  return query ? SetQuery(query, query + nlib_strlen(query)) : SetQuery(nullptr, nullptr);
273  }
274  bool SetFragment(const char* fragment) NLIB_NOEXCEPT {
275  return fragment ? SetFragment(fragment, fragment + nlib_strlen(fragment))
276  : SetFragment(nullptr, nullptr);
277  }
278  template<size_t N>
280  std::pair<const char*, const char*> x = encoder.GetData();
281  return SetQueryByQueryEncoder_(x.first, x.second);
282  }
283 
284  void Reset() NLIB_NOEXCEPT;
285  enum Error {
286  kOk = 0,
294  kBufferSizeNotEnough
295  };
296  Error GetError() const NLIB_NOEXCEPT;
297  NLIB_SAFE_BOOL(Uri, GetError() == kOk);
298 
299  private:
300  bool SetQueryByQueryEncoder_(const char* first, const char* last) NLIB_NOEXCEPT;
301  UriPrivate* prv_;
303  friend NLIB_VIS_PUBLIC bool operator==(const Uri& lhs, const Uri& rhs) NLIB_NOEXCEPT;
304  friend NLIB_VIS_PUBLIC bool operator<(const Uri& lhs, const Uri& rhs) NLIB_NOEXCEPT;
305 };
306 
307 NLIB_VIS_PUBLIC bool operator==(const Uri& lhs, const Uri& rhs) NLIB_NOEXCEPT;
308 NLIB_EQUAL_OPERATOR(Uri)
309 NLIB_VIS_PUBLIC bool operator<(const Uri& lhs, const Uri& rhs) NLIB_NOEXCEPT;
310 NLIB_COMPARE_OPERATOR(Uri)
311 
312 template<size_t N>
313 errno_t UriQueryEncoder<N>::Append(const char* key, const char* value) NLIB_NOEXCEPT {
314  // cur_ unchanged if not successful
315  char* p = cur_;
316  if (p != &buf_[0]) {
317  if (p == &buf_[N - 1]) return ERANGE;
318  *p++ = '&';
319  }
320  errno_t e;
321  size_t written;
322  if (key) {
323  e = Uri::EncodeUriComponent(&written, p, &buf_[N] - p, key, false);
324  if (e != 0) return e;
325  p += written;
326  }
327  if (p == &buf_[N - 1]) return ERANGE;
328  *p++ = '=';
329  if (value) {
330  e = Uri::EncodeUriComponent(&written, p, &buf_[N] - p, value, false);
331  if (e != 0) return e;
332  p += written;
333  }
334  cur_ = p;
335  return 0;
336 }
337 
338 template<size_t K = 64, size_t V = 192>
340  public:
342  ptr_end_(nullptr),
343  ptr_key_(nullptr),
344  ptr_eq_(nullptr),
345  ptr_and_(nullptr),
346  buf_key_valid_(false),
347  buf_value_valid_(false) {
348  buf_key_[0] = '\0';
349  buf_value_[0] = '\0';
350  }
351  errno_t Init(const Uri& uri) NLIB_NOEXCEPT;
352  void Reset() NLIB_NOEXCEPT;
353  bool HasNext() NLIB_NOEXCEPT;
354  errno_t MoveNext() NLIB_NOEXCEPT;
355  std::pair<errno_t, const char*> GetKey() NLIB_NOEXCEPT;
356  std::pair<errno_t, const char*> GetValue() NLIB_NOEXCEPT;
357  std::pair<errno_t, const char*> GetFirstValueByName(const char* key) NLIB_NOEXCEPT;
358  void Rewind() NLIB_NOEXCEPT;
359 
360  private:
361  static std::pair<errno_t, const char*> Error(errno_t e) NLIB_NOEXCEPT {
362  return std::make_pair(e, static_cast<const char*>(nullptr));
363  }
364  errno_t GetKeyWithCompare_(const char* key) NLIB_NOEXCEPT {
365  std::pair<errno_t, const char*> x = GetKey();
366  if (x.first != 0) return x.first;
367  if (strcmp(key, x.second) == 0) return 0;
368  return ESRCH;
369  }
370  void SpotKeyValue() NLIB_NOEXCEPT;
371 
372  const char* ptr_beg_;
373  const char* ptr_end_;
374  const char* ptr_key_; // key or p_end_ or nullptr
375  const char* ptr_eq_; // '=' or ptr_and_
376  const char* ptr_and_; // '&' or p_end_ or nullptr
377  bool buf_key_valid_;
378  bool buf_value_valid_;
379  char buf_key_[K];
380  char buf_value_[V];
381 };
382 
383 template<size_t K, size_t V>
384 errno_t UriQueryDecoder<K, V>::Init(const Uri& uri) NLIB_NOEXCEPT {
385  if (!uri) return EINVAL;
386  const char* p = uri.GetQuery();
387  if (p) {
388  ptr_beg_ = ptr_key_ = p;
389  ptr_end_ = p + nlib_strlen(p);
390  } else {
391  ptr_beg_ = ptr_end_ = &buf_key_[0];
392  }
393  ptr_eq_ = ptr_and_ = nullptr;
394  buf_key_valid_ = buf_value_valid_ = false;
395  return 0;
396 }
397 
398 template<size_t K, size_t V>
400  ptr_beg_ = ptr_end_ = ptr_key_ = ptr_eq_ = ptr_and_ = nullptr;
401  buf_key_valid_ = buf_value_valid_ = false;
402 }
403 
404 template<size_t K, size_t V>
406  if (ptr_key_ == ptr_end_) return false;
407  if (NLIB_LIKELY(*ptr_key_ != '&')) return true;
408  ptr_eq_ = ptr_and_ = nullptr;
409  buf_key_valid_ = buf_value_valid_ = false;
410  do {
411  ++ptr_key_;
412  } while (ptr_key_ != ptr_end_ && *ptr_key_ == '&');
413  return ptr_key_ != ptr_end_;
414 }
415 
416 template<size_t K, size_t V>
418  if (!HasNext()) return ENOENT;
419  if (ptr_and_) {
420  ptr_key_ = (ptr_and_ != ptr_end_) ? ptr_and_ + 1 : ptr_end_;
421  } else {
422  const void* p = nlib_memchr(ptr_key_, '&', std::distance(ptr_key_, ptr_end_));
423  ptr_key_ = (p != nullptr) ? static_cast<const char*>(p) + 1 : ptr_end_;
424  }
425  ptr_eq_ = ptr_and_ = nullptr;
426  buf_key_valid_ = buf_value_valid_ = false;
427  return 0;
428 }
429 
430 template<size_t K, size_t V>
431 std::pair<errno_t, const char*> UriQueryDecoder<K, V>::GetKey() NLIB_NOEXCEPT {
432  if (buf_key_valid_) return std::make_pair(0, &buf_key_[0]);
433  if (!ptr_key_) return Error(EBADF);
434  if (!HasNext()) return Error(ENOENT);
435  if (!ptr_eq_) SpotKeyValue();
436  size_t w;
437  errno_t e = Uri::DecodeUriComponent(&w, buf_key_, K, ptr_key_, ptr_eq_);
438  if (e != 0) return Error(e);
439  buf_key_valid_ = true;
440  return std::make_pair(0, &buf_key_[0]);
441 }
442 
443 template<size_t K, size_t V>
444 std::pair<errno_t, const char*> UriQueryDecoder<K, V>::GetValue() NLIB_NOEXCEPT {
445  if (buf_value_valid_) return std::make_pair(0, &buf_value_[0]);
446  if (!ptr_key_) return Error(EBADF);
447  if (!HasNext()) return Error(ENOENT);
448  if (!ptr_eq_) SpotKeyValue();
449  if (ptr_eq_ != ptr_and_) {
450  size_t w;
451  errno_t e = Uri::DecodeUriComponent(&w, buf_value_, V, ptr_eq_ + 1, ptr_and_);
452  if (e != 0) return Error(e);
453  } else {
454  buf_value_[0] = '\0';
455  }
456  buf_value_valid_ = true;
457  return std::make_pair(0, &buf_value_[0]);
458 }
459 
460 template<size_t K, size_t V>
461 std::pair<errno_t, const char*>
463  errno_t e;
464  do {
465  e = GetKeyWithCompare_(key);
466  if (e == 0) return std::make_pair(0, &buf_key_[0]);
467  if (e != ESRCH) return Error(e);
468  } while (MoveNext() == 0);
469  return Error(ESRCH);
470 }
471 
472 template<size_t K, size_t V>
474  ptr_key_ = ptr_beg_;
475  ptr_eq_ = ptr_and_ = nullptr;
476  buf_key_valid_ = buf_value_valid_ = false;
477 }
478 
479 template<size_t K, size_t V>
481  const void* p_and = nlib_memchr(ptr_key_, '&', std::distance(ptr_key_, ptr_end_));
482  ptr_and_ = p_and ? static_cast<const char*>(p_and) : ptr_end_;
483  const void* p_eq = nlib_memchr(ptr_key_, '=', std::distance(ptr_key_, ptr_and_));
484  ptr_eq_ = p_eq ? static_cast<const char*>(p_eq) : ptr_and_;
485 }
486 
487 NLIB_NAMESPACE_END
488 #ifndef __cpp_rvalue_references
489 NLIB_DEFINE_STD_SWAP(::nlib_ns::Uri)
490 #endif
491 #endif // INCLUDE_NN_NLIB_URI_H_
static std::pair< errno_t, size_t > DecodeUriComponent(char(&buf)[N], const char *first, const char *last) noexcept
A template overload of the above function.
Definition: Uri.h:116
static errno_t DecodeUriComponent(size_t *written, char *buf, size_t n, const char *s) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:100
bool SetQuery(const char *query) noexcept
Sets the query string, excluding the "?" character at its beginning.
Definition: Uri.h:271
bool SetPath(const char *path) noexcept
Sets the path.
Definition: Uri.h:268
static errno_t EncodeUriComponent(size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
Definition: Uri.h:150
static std::pair< errno_t, size_t > DecodeUriComponent(char *buf, size_t n, const char *s) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:120
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
A template overload of the above function.
Definition: Uri.h:130
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
A template overload of the above function.
Definition: Uri.h:96
static std::pair< errno_t, size_t > DecodeUriComponent(char(&buf)[N], const char *s) noexcept
A template overload of the above function.
Definition: Uri.h:125
static std::pair< errno_t, size_t > DecodePath(char(&buf)[N], const char *s) noexcept
A parameter omitted version of the above function which passes settings as the default value...
Definition: Uri.h:139
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:183
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:199
static bool IsIpv4(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:60
static std::pair< errno_t, size_t > EncodePath(char *buf, size_t n, const char *s) noexcept
test
Definition: Uri.h:187
STL namespace.
bool SetFragment(const char *fragment) noexcept
Sets the fragment string, excluding the "#" character at its beginning.
Definition: Uri.h:274
#define NLIB_CEXPR14
constexpr is defined if C++14 constexpr is available for use. If not, holds an empty string...
Definition: Config.h:112
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
A template overload of the above function.
Definition: Uri.h:184
constexpr Uri() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Uri.h:198
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last) noexcept
A template overload of the above function.
Definition: Uri.h:104
const void * nlib_memchr(const void *s, int c, size_t n)
Searches the n bytes from the start of the memory region (s, s + n) and returns a pointer to byte c...
errno_t nlib_snprintf(size_t *count, char(&buf)[N], const char *fmt,...) noexcept
A template overload of the above function.
Definition: Config.h:479
Invalid argument.
Definition: Uri.h:287
constexpr UriQueryDecoder() noexcept
Instantiates the object with default parameters (default constructor). Requires initialization with I...
Definition: Uri.h:341
The object has not been initialized.
Definition: Uri.h:289
Defines the class that resembles std::vector but can store objects that cannot be copied...
bool IsAbsolute() const noexcept
Returns true if the URI is an absolute URI. This function determines the string is an absolute URI if...
Definition: Uri.h:214
bool SetPortNumber(int port) noexcept
Sets the port number.
Definition: Uri.h:262
static std::pair< errno_t, size_t > EncodeUriComponent(char *buf, size_t n, const char *s, bool fragment_mode)
Definition: Uri.h:173
An absolute URI needs to be specified.
Definition: Uri.h:291
Failed to allocate memory.
Definition: Uri.h:288
static std::pair< errno_t, size_t > EncodeUriComponent(char *buf, size_t n, const char *first, const char *last, bool fragment_mode) noexcept
Percent-encodes a string. Returns a pair of the error value and the number of bytes in the encoded st...
Definition: Uri.h:160
bool SetHost(const char *host) noexcept
Sets the hostname.
Definition: Uri.h:256
#define NLIB_LIKELY(x)
Indicates to the compiler that condition x is likely to be true.
Definition: Platform_unix.h:97
static errno_t EncodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last, bool fragment_mode) noexcept
A template overload of the above function.
Definition: Uri.h:146
static bool IsEmailAddress(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:70
const char * c_str() const noexcept
Returns the encoded query string.
Definition: Uri.h:36
static errno_t EncodeUriComponent(size_t *written, char(&buf)[N], const char *s, bool fragment_mode) noexcept
A template overload of the above function.
Definition: Uri.h:155
bool ComposeString(char(&buf)[N]) const noexcept
A template overload of the above function.
Definition: Uri.h:217
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
A file that contains the configuration information for each development environment.
Failed to normalize the (absolute) path.
Definition: Uri.h:292
static std::pair< errno_t, size_t > EncodeUriComponent(char(&buf)[N], const char *s, bool fragment_mode)
A template overload of the above function.
Definition: Uri.h:178
bool SetPort(const char *port) noexcept
Sets the port number as a string, excluding the colon.
Definition: Uri.h:259
static bool IsUriReference(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:79
The class for parsing and constructing regular URIs.
Definition: Uri.h:51
static bool IsHostName(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:55
Invalid URI.
Definition: Uri.h:290
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
static bool IsIpv6(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:65
bool Parse(const char *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:202
The class template to parse URI queries.
Definition: Uri.h:339
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
static std::pair< errno_t, size_t > EncodeUriComponent(char(&buf)[N], const char *first, const char *last, bool fragment_mode) noexcept
A template overload of the above function.
Definition: Uri.h:168
static std::pair< errno_t, size_t > DecodePath(char *buf, size_t n, const char *s) noexcept
Decodes a percent-encoded string. This function is the same as DecodeUriComponent, except that it returns EILSEQ if the decoded character is the forward slash (&#39;/&#39;).
Definition: Uri.h:133
bool SetQuery(const UriQueryEncoder< N > &encoder) noexcept
Sets the query string, excluding the "?" character at its beginning.
Definition: Uri.h:279
UriQueryEncoder() noexcept
Instantiates the object with default parameters (default constructor).
Definition: Uri.h:34
std::pair< const char *, const char * > GetData() const noexcept
Returns the beginning and end of the encoded query string.
Definition: Uri.h:41
The class for creating URI queries.
Definition: Uri.h:32
static std::pair< errno_t, size_t > EncodePath(char(&buf)[N], const char *s) noexcept
A template overload of the above function.
Definition: Uri.h:193
static bool IsUri(const nlib_utf8_t *str) noexcept
A parameter omitted version of the above function which receives a null terminated string...
Definition: Uri.h:74
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
The URI is too long.
Definition: Uri.h:293
static std::pair< errno_t, size_t > DecodeUriComponent(char *buf, size_t n, const char *first, const char *last) noexcept
Decodes a percent-encoded string. Returns a pair of the error value and the number of bytes in the de...
Definition: Uri.h:109
bool SetUserInfo(const char *userinfo) noexcept
Sets user-specific information, including the user name and password, associated with the specified U...
Definition: Uri.h:252
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:303
Error
An error value, as an enumerator, that can be obtained with GetError().
Definition: Uri.h:285
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37