nlib
Uri.h
[詳解]
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
%エンコードされた文字列をデコードします。
Definition: Uri.h:43
static errno_t EncodeUriComponent(size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
文字列を%エンコードします。
Definition: Uri.h:49
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
DecodePath(written, buf, N, s)を呼び出します。
Definition: Uri.h:77
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
DecodeUriComponent(written, buf, N, s)を呼び出します。
Definition: Uri.h:57
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:163
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
EncodePath(written, buf, N, s)を呼び出します。
Definition: Uri.h:81
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:88
Uri() noexcept
デフォルトコンストラクタです。オブジェクトを初期化します。
Definition: Uri.h:86
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last) noexcept
DecodeUriComponent(written, buf, N, first, last)を呼び出します。
Definition: Uri.h:62
std::vectorに似ていますが、コピーできないオブジェクトを格納可能なクラスが定義されています。 ...
static errno_t EncodeUriComponent(size_t *written, char(&buf)[N], const char *first, const char *last, bool fragment_mode) noexcept
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
EncodeUriComponent(written, buf, N, s, fragment_mode)を呼び出します。
Definition: Uri.h:67
bool ComposeString(char(&buf)[N]) const noexcept
URI文字列を書き出します。
Definition: Uri.h:104
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
開発環境別の設定が書かれるファイルです。
一般的なURIをパースしたり構築したりするためのクラスです。
Definition: Uri.h:31
size_t nlib_strlen(const char *s)
内部でstrlen()を呼び出します。独自の実装が動作する場合もあります。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:229
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37