nlib
Uri.h
Go to the documentation of this file.
1 
2 /*---------------------------------------------------------------------------*
3 
4  Project: CrossRoad
5  Copyright (C)2012-2016 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
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 
24 NLIB_NAMESPACE_BEGIN
25 
26 class InputStream;
27 class OutputStream;
28 class MemoryOutputStream;
29 
31  public:
32  // reserved = gen-delims / sub-delims
33  // gen - delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
34  // sub - delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
35  static bool IsReserved(int c) NLIB_NOEXCEPT;
36  // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
37  static bool IsUnreserved(int c) NLIB_NOEXCEPT;
38  // 0-9 / A-F / a-f
39  static bool IsHexDigit(int c) NLIB_NOEXCEPT;
40  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
41  const char* last) NLIB_NOEXCEPT;
42  static errno_t DecodeUriComponent(size_t* written, char* buf, size_t n,
43  const char* s) NLIB_NOEXCEPT {
44  return DecodeUriComponent(written, buf, n, s, s + nlib_strlen(s));
45  }
46  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* first,
47  const char* last, bool fragment_mode) NLIB_NOEXCEPT;
48  static errno_t EncodeUriComponent(size_t* written, char* buf, size_t n, const char* s,
49  bool fragment_mode) NLIB_NOEXCEPT {
50  return EncodeUriComponent(written, buf, n, s, s + nlib_strlen(s), fragment_mode);
51  }
52  static errno_t DecodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
53  static errno_t EncodePath(size_t* written, char* buf, size_t n, const char* s) NLIB_NOEXCEPT;
54 
55  template <size_t N>
56  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N],
57  const char* s) NLIB_NOEXCEPT {
58  return DecodeUriComponent(written, buf, N, s);
59  }
60  template <size_t N>
61  static errno_t DecodeUriComponent(size_t* written, char (&buf)[N], const char* first,
62  const char* last) NLIB_NOEXCEPT {
63  return DecodeUriComponent(written, buf, N, first, last);
64  }
65  template <size_t N>
66  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* s,
67  bool fragment_mode) NLIB_NOEXCEPT {
68  return EncodeUriComponent(written, buf, N, s, fragment_mode);
69  }
70  template <size_t N>
71  static errno_t EncodeUriComponent(size_t* written, char (&buf)[N], const char* first,
72  const char* last, bool fragment_mode) NLIB_NOEXCEPT {
73  return EncodeUriComponent(written, buf, N, first, last, fragment_mode);
74  }
75  template <size_t N>
76  static errno_t DecodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
77  return DecodePath(written, buf, N, s);
78  }
79  template <size_t N>
80  static errno_t EncodePath(size_t* written, char (&buf)[N], const char* s) NLIB_NOEXCEPT {
81  return EncodePath(written, buf, N, s);
82  }
83 
84  public:
85  Uri() NLIB_NOEXCEPT : prv_(NULL) {}
86  ~Uri() NLIB_NOEXCEPT;
87  bool Parse(const char* str) NLIB_NOEXCEPT;
88  // for example:
89  // http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag
90  // scheme = "http"
91  // userinfo = "foobar:pass"
92  // host = "www.example.com"
93  // port = "80"
94  // path = "/my/dir/index.html"
95  // query = "var=value"
96  // fragment = "frag"
97  bool SetUri(const char* scheme, const char* userinfo, const char* host,
98  const char* port, const char* path, const char* query,
99  const char* fragment) NLIB_NOEXCEPT;
100  bool ComposeString(char* buf, size_t size) const NLIB_NOEXCEPT;
101  template <size_t N>
102  bool ComposeString(char (&buf)[N]) const NLIB_NOEXCEPT {
103  return this->ComposeString(buf, N);
104  }
105 
106  // AddBaseUri("g/", "http://a/b/c/d;p?q") -> http://a/b/c/g/
107  // AddBaseUri("/g", "http://a/b/c/d;p?q") -> http://a/g
108  // AddBaseUri("//g", "http://a/b/c/d;p?q") -> http://g
109  bool AddBaseUri(const Uri& relative, const Uri& base) NLIB_NOEXCEPT;
110  // "http" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
111  const char* GetScheme() const NLIB_NOEXCEPT;
112  // "foobar:pass" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
113  const char* GetUserInfo() const NLIB_NOEXCEPT;
114  // "www.example.com" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
115  const char* GetHost() const NLIB_NOEXCEPT;
116  // "80" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
117  const char* GetPort() const NLIB_NOEXCEPT;
118  // "/my/dir/index.html" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
119  const char* GetPath() const NLIB_NOEXCEPT;
120  // "var=value" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
121  const char* GetQuery() const NLIB_NOEXCEPT;
122  // "frag" from "http://foobar:pass@www.example.com:80/my/dir/index.html?var=value#frag"
123  const char* GetFragment() const NLIB_NOEXCEPT;
124  void Reset() NLIB_NOEXCEPT;
125 
126  private:
127  NLIB_VIS_HIDDEN bool SetScheme_(const char* first, const char* last) NLIB_NOEXCEPT;
128  NLIB_VIS_HIDDEN bool SetUserInfo_(const char* first, const char* last) NLIB_NOEXCEPT;
129  NLIB_VIS_HIDDEN bool SetHost_(const char* first, const char* last) NLIB_NOEXCEPT;
130  NLIB_VIS_HIDDEN bool SetPort_(const char* first, const char* last) NLIB_NOEXCEPT;
131  NLIB_VIS_HIDDEN bool SetPath_(const char* first, const char* last) NLIB_NOEXCEPT;
132  NLIB_VIS_HIDDEN bool SetQuery_(const char* first, const char* last) NLIB_NOEXCEPT;
133  NLIB_VIS_HIDDEN bool SetFragment_(const char* first, const char* last) NLIB_NOEXCEPT;
134  NLIB_VIS_HIDDEN bool MergePath(const char* base, const char* relative) NLIB_NOEXCEPT;
135  NLIB_VIS_HIDDEN bool RemoveDotSegments(const char* path) NLIB_NOEXCEPT;
136 
137  private:
138  struct UriPrivate;
139  mutable UriPrivate* prv_;
140 };
141 
142 NLIB_NAMESPACE_END
143 
144 #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:42
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:48
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodePath(written, buf, N, s) .
Definition: Uri.h:76
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
Calls DecodeUriComponent(written, buf, N, s).
Definition: Uri.h:56
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
Calls EncodePath(written, buf, N, s).
Definition: Uri.h:80
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:86
Uri() noexcept
Instantiates the object with default parameters (default constructor). Initializes an object...
Definition: Uri.h:85
#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
Calls DecodeUriComponent(written, buf, N, first, last).
Definition: Uri.h:61
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:71
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:66
bool ComposeString(char(&buf)[N]) const noexcept
Writes a URI string.
Definition: Uri.h:102
#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:30
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:224
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37