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