nlib
Uri.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_URI_H_
4 #define INCLUDE_NN_NLIB_URI_H_
5 
6 #include <utility>
7 
8 #include "nn/nlib/Config.h"
9 #include "nn/nlib/Nlist.h"
10 
11 NLIB_NAMESPACE_BEGIN
12 
13 class InputStream;
14 class OutputStream;
15 class MemoryOutputStream;
16 
18  public:
19  // reserved = gen-delims / sub-delims
20  // gen - delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
21  // sub - delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
22  static bool IsReserved(int c) NLIB_NOEXCEPT;
23  // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
24  static bool IsUnreserved(int c) NLIB_NOEXCEPT;
25  // 0-9 / A-F / a-f
26  static bool IsHexDigit(int c) NLIB_NOEXCEPT;
27  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
28  const char* last) NLIB_NOEXCEPT;
29  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n,
30  const char* s) NLIB_NOEXCEPT {
31  return DecodeUriComponent(written, buf, n, s, s + nlib_strlen(s));
32  }
33  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
34  const char* last, bool fragment_mode) NLIB_NOEXCEPT;
35  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* s,
36  bool fragment_mode) NLIB_NOEXCEPT {
37  return EncodeUriComponent(written, buf, n, s, s + nlib_strlen(s), fragment_mode);
38  }
39  static errno_t DecodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
40  static errno_t EncodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
41 
42  template <size_t N>
43  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N],
44  const char* s) NLIB_NOEXCEPT {
45  return DecodeUriComponent(written, buf, N, s);
46  }
47  template <size_t N>
48  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N], const char* first,
49  const char* last) NLIB_NOEXCEPT {
50  return DecodeUriComponent(written, buf, N, first, last);
51  }
52  template <size_t N>
53  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* s,
54  bool fragment_mode) NLIB_NOEXCEPT {
55  return EncodeUriComponent(written, buf, N, s, fragment_mode);
56  }
57  template <size_t N>
58  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* first,
59  const char* last, bool fragment_mode) NLIB_NOEXCEPT {
60  return EncodeUriComponent(written, buf, N, first, last, fragment_mode);
61  }
62  template <size_t N>
63  static errno_t DecodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
64  return DecodePath(written, buf, N, s);
65  }
66  template <size_t N>
67  static errno_t EncodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
68  return EncodePath(written, buf, N, s);
69  }
70 
71  public:
72  Uri() NLIB_NOEXCEPT : prv_(NULL) {}
73  ~Uri() NLIB_NOEXCEPT;
74  bool Parse(const char* str) NLIB_NOEXCEPT;
75  // for example:
76  // http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag
77  // scheme = "http"
78  // userinfo = "foobar:pass"
79  // host = "www.example.com"
80  // port = "80"
81  // path = "/my/dir/index.html"
82  // query = "var=value"
83  // fragment = "frag"
84  bool SetUri(const char* scheme, const char* userinfo, const char* host,
85  const char* port, const char* path, const char* query,
86  const char* fragment) NLIB_NOEXCEPT;
87  bool ComposeString(char* buf, size_t size) const NLIB_NOEXCEPT;
88  template <size_t N>
89  bool ComposeString(char (&buf)[N]) const NLIB_NOEXCEPT {
90  return this->ComposeString(buf, N);
91  }
92 
93  // AddBaseUri("g/", "http://a/b/c/d;p?q") -> http://a/b/c/g/
94  // AddBaseUri("/g", "http://a/b/c/d;p?q") -> http://a/g
95  // AddBaseUri("//g", "http://a/b/c/d;p?q") -> http://g
96  bool AddBaseUri(const Uri& relative, const Uri& base) NLIB_NOEXCEPT;
97  // "http" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
98  const char* GetScheme() const NLIB_NOEXCEPT;
99  // "foobar:pass" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
100  const char* GetUserInfo() const NLIB_NOEXCEPT;
101  // "www.example.com" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
102  const char* GetHost() const NLIB_NOEXCEPT;
103  // "80" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
104  const char* GetPort() const NLIB_NOEXCEPT;
105  // "/my/dir/index.html" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
106  const char* GetPath() const NLIB_NOEXCEPT;
107  // "var=value" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
108  const char* GetQuery() const NLIB_NOEXCEPT;
109  // "frag" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
110  const char* GetFragment() const NLIB_NOEXCEPT;
111  void Reset() NLIB_NOEXCEPT;
112 
113  private:
114  NLIB_VIS_HIDDEN bool SetScheme_(const char* first, const char* last) NLIB_NOEXCEPT;
115  NLIB_VIS_HIDDEN bool SetUserInfo_(const char* first, const char* last) NLIB_NOEXCEPT;
116  NLIB_VIS_HIDDEN bool SetHost_(const char* first, const char* last) NLIB_NOEXCEPT;
117  NLIB_VIS_HIDDEN bool SetPort_(const char* first, const char* last) NLIB_NOEXCEPT;
118  NLIB_VIS_HIDDEN bool SetPath_(const char* first, const char* last) NLIB_NOEXCEPT;
119  NLIB_VIS_HIDDEN bool SetQuery_(const char* first, const char* last) NLIB_NOEXCEPT;
120  NLIB_VIS_HIDDEN bool SetFragment_(const char* first, const char* last) NLIB_NOEXCEPT;
121  NLIB_VIS_HIDDEN bool MergePath(const char* base, const char* relative) NLIB_NOEXCEPT;
122  NLIB_VIS_HIDDEN bool RemoveDotSegments(const char* path) NLIB_NOEXCEPT;
123 
124  private:
125  struct UriPrivate;
126  mutable UriPrivate* prv_;
127 };
128 
129 NLIB_NAMESPACE_END
130 
131 #endif // INCLUDE_NN_NLIB_URI_H_
static errno_t DecodeUriComponent(size_t *written, char *buf, size_t n, const char *s) noexcept
Decodes a percent-encoded string.
Definition: Uri.h:29
static errno_t EncodeUriComponent(size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
Percent-encodes a string.
Definition: Uri.h:35
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodePath(written, buf, N, s) .
Definition: Uri.h:63
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodeUriComponent(written, buf, N, s).
Definition: Uri.h:43
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls EncodePath(written, buf, N, s).
Definition: Uri.h:67
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
Uri() noexcept
Instantiates the object with default parameters (default constructor). Initializes an object...
Definition: Uri.h:72
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last) noexcept
Calls DecodeUriComponent(written, buf, N, first, last).
Definition: Uri.h:48
Defines the class that resembles std::vector but can store objects that cannot be copied...
static errno_t EncodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last, bool fragment_mode) noexcept
Calls EncodeUriComponent(written, buf, N, first, last, fragment_mode).
Definition: Uri.h:58
static errno_t EncodeUriComponent(size_t *written, char(&buf)[N], const char *s, bool fragment_mode) noexcept
Calls EncodeUriComponent(written, buf, N, s, fragment_mode).
Definition: Uri.h:53
bool ComposeString(char(&buf)[N]) const noexcept
Writes a URI string.
Definition: Uri.h:89
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
The class for parsing and constructing regular URIs.
Definition: Uri.h:17
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24