nlib
Uri.h
[詳解]
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
%エンコードされた文字列をデコードします。
Definition: Uri.h:42
static errno_t EncodeUriComponent(size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
文字列を%エンコードします。
Definition: Uri.h:48
static errno_t DecodePath(size_t *written, char(&buf)[N], const char *s) noexcept
DecodePath(written, buf, N, s)を呼び出します。
Definition: Uri.h:76
static errno_t DecodeUriComponent(size_t *written, char(&buf)[N], const char *s) noexcept
DecodeUriComponent(written, buf, N, s)を呼び出します。
Definition: Uri.h:56
static errno_t EncodePath(size_t *written, char(&buf)[N], const char *s) noexcept
EncodePath(written, buf, N, s)を呼び出します。
Definition: Uri.h:80
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:86
Uri() noexcept
デフォルトコンストラクタです。オブジェクトを初期化します。
Definition: Uri.h:85
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:87
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:61
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:71
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:66
bool ComposeString(char(&buf)[N]) const noexcept
URI文字列を書き出します。
Definition: Uri.h:102
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:99
開発環境別の設定が書かれるファイルです。
一般的なURIをパースしたり構築したりするためのクラスです。
Definition: Uri.h:30
size_t nlib_strlen(const char *s)
内部でstrlen()を呼び出します。独自の実装が動作する場合もあります。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:224
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37