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 
32  public:
33  // reserved = gen-delims / sub-delims
34  // gen - delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
35  // sub - delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
36  static bool IsReserved(int c) NLIB_NOEXCEPT;
37  // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
38  static bool IsUnreserved(int c) NLIB_NOEXCEPT;
39  // 0-9 / A-F / a-f
40  static bool IsHexDigit(int c) NLIB_NOEXCEPT;
41  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
42  const char* last) NLIB_NOEXCEPT;
43  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n,
44  const char* s) NLIB_NOEXCEPT {
45  return DecodeUriComponent(written, buf, n, s, s + nlib_strlen(s));
46  }
47  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
48  const char* last, bool fragment_mode) NLIB_NOEXCEPT;
49  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* s,
50  bool fragment_mode) NLIB_NOEXCEPT {
51  return EncodeUriComponent(written, buf, n, s, s + nlib_strlen(s), fragment_mode);
52  }
53  static errno_t DecodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
54  static errno_t EncodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
55 
56  template <size_t N>
57  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N],
58  const char* s) NLIB_NOEXCEPT {
59  return DecodeUriComponent(written, buf, N, s);
60  }
61  template <size_t N>
62  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N], const char* first,
63  const char* last) NLIB_NOEXCEPT {
64  return DecodeUriComponent(written, buf, N, first, last);
65  }
66  template <size_t N>
67  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* s,
68  bool fragment_mode) NLIB_NOEXCEPT {
69  return EncodeUriComponent(written, buf, N, s, fragment_mode);
70  }
71  template <size_t N>
72  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* first,
73  const char* last, bool fragment_mode) NLIB_NOEXCEPT {
74  return EncodeUriComponent(written, buf, N, first, last, fragment_mode);
75  }
76  template <size_t N>
77  static errno_t DecodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
78  return DecodePath(written, buf, N, s);
79  }
80  template <size_t N>
81  static errno_t EncodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
82  return EncodePath(written, buf, N, s);
83  }
84 
85  public:
86  Uri() NLIB_NOEXCEPT : prv_(NULL) {}
87  ~Uri() NLIB_NOEXCEPT;
88  NLIB_MOVE_MEMBER_HELPER_1(Uri, prv_);
89  bool Parse(const char* str) NLIB_NOEXCEPT;
90  // for example:
91  // http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag
92  // scheme = "http"
93  // userinfo = "foobar:pass"
94  // host = "www.example.com"
95  // port = "80"
96  // path = "/my/dir/index.html"
97  // query = "var=value"
98  // fragment = "frag"
99  bool SetUri(const char* scheme, const char* userinfo, const char* host,
100  const char* port, const char* path, const char* query,
101  const char* fragment) NLIB_NOEXCEPT;
102  bool ComposeString(char* buf, size_t size) const NLIB_NOEXCEPT;
103  template <size_t N>
104  bool ComposeString(char (&buf)[N]) const NLIB_NOEXCEPT {
105  return this->ComposeString(buf, N);
106  }
107 
108  // AddBaseUri("g/", "http://a/b/c/d;p?q") -> http://a/b/c/g/
109  // AddBaseUri("/g", "http://a/b/c/d;p?q") -> http://a/g
110  // AddBaseUri("//g", "http://a/b/c/d;p?q") -> http://g
111  bool AddBaseUri(const Uri& relative, const Uri& base) NLIB_NOEXCEPT;
112  // "http" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
113  const char* GetScheme() const NLIB_NOEXCEPT;
114  // "foobar:pass" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
115  const char* GetUserInfo() const NLIB_NOEXCEPT;
116  // "www.example.com" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
117  const char* GetHost() const NLIB_NOEXCEPT;
118  // "80" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
119  const char* GetPort() const NLIB_NOEXCEPT;
120  // "/my/dir/index.html" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
121  const char* GetPath() const NLIB_NOEXCEPT;
122  // "var=value" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
123  const char* GetQuery() const NLIB_NOEXCEPT;
124  // "frag" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
125  const char* GetFragment() const NLIB_NOEXCEPT;
126  void Reset() NLIB_NOEXCEPT;
127  void swap(Uri& rhs) NLIB_NOEXCEPT {
128  using std::swap;
129  swap(prv_, rhs.prv_);
130  }
131 
132  private:
133  NLIB_VIS_HIDDEN bool SetScheme_(const char* first, const char* last) NLIB_NOEXCEPT;
134  NLIB_VIS_HIDDEN bool SetUserInfo_(const char* first, const char* last) NLIB_NOEXCEPT;
135  NLIB_VIS_HIDDEN bool SetHost_(const char* first, const char* last) NLIB_NOEXCEPT;
136  NLIB_VIS_HIDDEN bool SetPort_(const char* first, const char* last) NLIB_NOEXCEPT;
137  NLIB_VIS_HIDDEN bool SetPath_(const char* first, const char* last) NLIB_NOEXCEPT;
138  NLIB_VIS_HIDDEN bool SetQuery_(const char* first, const char* last) NLIB_NOEXCEPT;
139  NLIB_VIS_HIDDEN bool SetFragment_(const char* first, const char* last) NLIB_NOEXCEPT;
140  NLIB_VIS_HIDDEN bool MergePath(const char* base, const char* relative) NLIB_NOEXCEPT;
141  NLIB_VIS_HIDDEN bool RemoveDotSegments(const char* path) NLIB_NOEXCEPT;
142 
143  private:
144  struct UriPrivate;
145  mutable UriPrivate* prv_;
147 };
148 
149 NLIB_NAMESPACE_END
150 NLIB_DEFINE_STD_SWAP(::nlib_ns::Uri)
151 #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:43
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:49
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodePath(written, buf, N, s) .
Definition: Uri.h:77
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodeUriComponent(written, buf, N, s).
Definition: Uri.h:57
#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:163
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls EncodePath(written, buf, N, s).
Definition: Uri.h:81
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:88
Uri() noexcept
Instantiates the object with default parameters (default constructor). Initializes an object...
Definition: Uri.h:86
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
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:62
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:72
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:67
bool ComposeString(char(&buf)[N]) const noexcept
Writes a URI string.
Definition: Uri.h:104
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
A file that contains the configuration information for each development environment.
The class for parsing and constructing regular URIs.
Definition: Uri.h:31
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:229
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37