nlib
nn::nlib::Uri クラスfinal

一般的なURIをパースしたり構築したりするためのクラスです。 [詳解]

#include "nn/nlib/Uri.h"

公開型

enum  Error {
  kOk = 0,
  kInvalidParam,
  kOutOfMemory,
  kNotInitialized,
  kInvalidUri,
  kAbsoluteUriRequired,
  kPathNormalizationError,
  kUriTooLong,
  kBufferSizeNotEnough
}
 GetError()で取得することのできるエラー値の列挙型です。 [詳解]
 

公開メンバ関数

constexpr Uri () noexcept
 デフォルトコンストラクタです。オブジェクトを初期化します。
 
bool Parse (const char *first, const char *last) noexcept
 URI文字列をパースします。 [詳解]
 
bool Parse (const char *str) noexcept
 Parse(str, str + strlen(str))を返します。
 
bool SetUri (const char *scheme, const char *userinfo, const char *host, const char *port, const char *path, const char *query, const char *fragment) noexcept
 URIの各部分を設定します。 [詳解]
 
bool IsAbsolute () const noexcept
 URIが絶対URIであればtrueを返します。この関数ではschemeが指定されていれば絶対URIであると判断しています。
 
bool ComposeString (char *buf, size_t size) const noexcept
 URI文字列を書き出します。 [詳解]
 
template<size_t N>
bool ComposeString (char(&buf)[N]) const noexcept
 URI文字列を書き出します。 [詳解]
 
bool AddBaseUri (const Uri &relative, const Uri &base) noexcept
 相対パスを解決します。 [詳解]
 
const char * GetScheme () const noexcept
 URIのスキーム(http, file等)の文字列を返します。 [詳解]
 
const char * GetUserInfo () const noexcept
 指定した URI に関連付けられているユーザー名、パスワードなどのユーザー固有の情報を取得します。 [詳解]
 
const char * GetHost () const noexcept
 ホスト名を取得します。 [詳解]
 
const char * GetPort () const noexcept
 ポート番号の文字列を取得します。 [詳解]
 
int GetPortNumber () const noexcept
 ポート番号を取得します。ポート番号の文字列が設定されていてint型へ変換可能な場合は非負の整数を返し、それ以外の場合は-1を返します。
 
const char * GetPath () const noexcept
 パスを取得します。 [詳解]
 
const char * GetQuery () const noexcept
 クエリ文字列を取得します [詳解]
 
const char * GetFragment () const noexcept
 フラグメント文字列を取得します [詳解]
 
bool SetScheme (const char *first, const char *last) noexcept
 スキームの文字列(末尾の:は含まない)を設定します。
 
bool SetUserInfo (const char *first, const char *last) noexcept
 指定した URI に関連付けられているユーザー名、パスワードなどのユーザー固有の情報を設定します。
 
bool SetHost (const char *first, const char *last) noexcept
 ホスト名を設定します。
 
bool SetPort (const char *first, const char *last) noexcept
 ポート番号の文字列(:は含まない)を設定します。
 
bool SetPath (const char *first, const char *last) noexcept
 パスを設定します。
 
bool SetQuery (const char *first, const char *last) noexcept
 クエリ文字列(先頭の?は含まない)を設定します。
 
bool SetFragment (const char *first, const char *last) noexcept
 フラグメント文字列(先頭の::は含まない)を設定します。
 
bool SetScheme (const char *scheme) noexcept
 スキームの文字列(末尾の:は含まない)を設定します。
 
bool SetUserInfo (const char *userinfo) noexcept
 指定した URI に関連付けられているユーザー名、パスワードなどのユーザー固有の情報を設定します。
 
bool SetHost (const char *host) noexcept
 ホスト名を設定します。
 
bool SetPort (const char *port) noexcept
 ポート番号の文字列(:は含まない)を設定します。
 
bool SetPortNumber (int port) noexcept
 ポート番号を設定します。
 
bool SetPath (const char *path) noexcept
 パスを設定します。
 
bool SetQuery (const char *query) noexcept
 クエリ文字列(先頭の?は含まない)を設定します。
 
bool SetFragment (const char *fragment) noexcept
 フラグメント文字列(先頭の::は含まない)を設定します。
 
template<size_t N>
bool SetQuery (const UriQueryEncoder< N > &encoder) noexcept
 クエリ文字列(先頭の?は含まない)を設定します。
 
void Reset () noexcept
 オブジェクトを初期化(リセット)します。
 
Error GetError () const noexcept
 エラーが発生した場合に詳細な理由を取得することができます。
 

静的公開メンバ関数

static bool IsHostName (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がホスト名の要件(RFC1034 3.1)を満たすかどうかを判定します。 [詳解]
 
static bool IsHostName (const nlib_utf8_t *str) noexcept
 IsHostName(str, str + strlen(str))を返します。
 
static bool IsIpv4 (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がIPv4の要件を満たすかどうかを判定します。 [詳解]
 
static bool IsIpv4 (const nlib_utf8_t *str) noexcept
 IsIpv4(str, str + strlen(str))を返します。
 
static bool IsIpv6 (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がIPv6の要件を満たすかどうかを判定します。 [詳解]
 
static bool IsIpv6 (const nlib_utf8_t *str) noexcept
 IsIpv6(str, str + strlen(str))を返します。
 
static bool IsEmailAddress (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がemailアドレスの要件(RFC5322 3.4)を満たすかどうかを判定します [詳解]
 
static bool IsEmailAddress (const nlib_utf8_t *str) noexcept
 IsEmailAddress(str, str + strlen(str))を返します。
 
static bool IsUri (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がURIであるかどうかを判定します。相対パスの場合、URIではなくURI参照となることに注意してください。 [詳解]
 
static bool IsUri (const nlib_utf8_t *str) noexcept
 IsUri(str, str + strlen(str))を返します。
 
static bool IsUriReference (const nlib_utf8_t *first, const nlib_utf8_t *last) noexcept
 文字列がURI参照であるかどうかを判定します。 [詳解]
 
static bool IsUriReference (const nlib_utf8_t *str) noexcept
 IsUriReference(str, str + strlen(str))を返します。
 
static bool IsReserved (int c) noexcept
 c がRFC3986のReserved Characters であればtrueを返します。
 
static bool IsUnreserved (int c) noexcept
 c がRFC3986のUnreserved Characters であればtrueを返します。
 
static bool IsHexDigit (int c) noexcept
 16進数の数値であればtrueを返します。
 
static errno_t DecodeUriComponent (size_t *written, char *buf, size_t n, const char *first, const char *last) noexcept
 %エンコードされた文字列をデコードします。 [詳解]
 
template<size_t N>
static errno_t DecodeUriComponent (size_t *written, char(&buf)[N], const char *s) noexcept
 DecodeUriComponent(written, buf, N, s)を呼び出します。
 
static errno_t DecodeUriComponent (size_t *written, char *buf, size_t n, const char *s) noexcept
 %エンコードされた文字列をデコードします。 [詳解]
 
template<size_t N>
static errno_t DecodeUriComponent (size_t *written, char(&buf)[N], const char *first, const char *last) noexcept
 DecodeUriComponent(written, buf, N, first, last)を呼び出します。
 
static errno_t EncodeUriComponent (size_t *written, char *buf, size_t n, const char *first, const char *last, bool fragment_mode) noexcept
 文字列を%エンコードします。 [詳解]
 
template<size_t N>
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)を呼び出します。
 
static errno_t EncodeUriComponent (size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
 文字列を%エンコードします。 [詳解]
 
template<size_t N>
static errno_t EncodeUriComponent (size_t *written, char(&buf)[N], const char *s, bool fragment_mode) noexcept
 EncodeUriComponent(written, buf, N, s, fragment_mode)を呼び出します。
 
static errno_t DecodePath (size_t *written, char *buf, size_t n, const char *s) noexcept
 %エンコードされた文字列をデコードします。 デコードされた文字が'/'の場合にEILSEQを返すこと以外は、DecodeUriComponent()と同じです。
 
template<size_t N>
static errno_t DecodePath (size_t *written, char(&buf)[N], const char *s) noexcept
 DecodePath(written, buf, N, s)を呼び出します。
 
static errno_t EncodePath (size_t *written, char *buf, size_t n, const char *s) noexcept
 文字列を%エンコードします。'/'を%エンコードしないこと以外は、EncodeUriComponent()と同じです。
 
template<size_t N>
static errno_t EncodePath (size_t *written, char(&buf)[N], const char *s) noexcept
 EncodePath(written, buf, N, s)を呼び出します。
 

詳解

一般的なURIをパースしたり構築したりするためのクラスです。

説明
HttpStyleUri, FileStyleUriはこのクラスを利用してURIをパースしています。
相対URI(相対パス)を利用する場合にもこのクラスを用います。 パースする際には、%エンコードされた文字の一部をデコードします。 デコードされる文字は、RFC3986のunreserved(ALPHA / DIGIT / "-" / "." / "_" / "~")に属する文字です。 それ以外の文字は%エンコードされたままです。
Uri uri;
uri.Parse("http://www.example.com/dir/index.html?query=test#part1");
uri.GetScheme(); // "http"
uri.GetHost(); // "www.example.com"
uri.GetPath(); // "/dir/index.html"
uri.GetQuery(); // "query=test"
uri.GetFragment(); // "part1"
参照
https://www.ietf.org/rfc/rfc3986.txt (RFC 3986)
http://www.eonet.ne.jp/~h-hash/rfc_ja/rfc3986.ja.html (RFC 3986, in Japanese)
各種例:
misc/uri/uri.cpp.

Uri.h51 行目に定義があります。

列挙型メンバ詳解

◆ Error

GetError()で取得することのできるエラー値の列挙型です。

列挙値
kOk 

エラーは発生していません。

kInvalidParam 

引数が無効です。

kOutOfMemory 

メモリの確保に失敗しました。

kNotInitialized 

オブジェクトが初期化されていない状態です。

kInvalidUri 

URIが無効です。

kAbsoluteUriRequired 

絶対URIを指定する必要があります。

kPathNormalizationError 

(絶対)パスの正規化に失敗しました。

kUriTooLong 

URIが長すぎます。

kBufferSizeNotEnough 

バッファ・サイズが十分ではありません。

Uri.h292 行目に定義があります。

関数詳解

◆ AddBaseUri()

nn::nlib::Uri::AddBaseUri ( const Uri relative,
const Uri base 
)
noexcept

相対パスを解決します。

引数
[in]relative相対パス
[in]base基底となるURI
戻り値
成功した場合はtrue
説明
相対パスにスキームが指定されている場合は基底となるURIとスキームが同一でも絶対パスのように扱われます。

◆ ComposeString() [1/2]

nn::nlib::Uri::ComposeString ( char *  buf,
size_t  size 
) const
noexcept

URI文字列を書き出します。

引数
[out]buf文字列が書きだされるバッファへのポインタ
[in]sizeバッファサイズ
戻り値
成功した場合はtrue

◆ ComposeString() [2/2]

template<size_t N>
nn::nlib::Uri::ComposeString ( char(&)  buf[N]) const
inlinenoexcept

URI文字列を書き出します。

引数
[out]buf文字列が書きだされるバッファ
戻り値
成功した場合はtrue

Uri.h224 行目に定義があります。

◆ DecodeUriComponent() [1/2]

nn::nlib::Uri::DecodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  first,
const char *  last 
)
staticnoexcept

%エンコードされた文字列をデコードします。

引数
[in]writtenデコードされた文字列のバイト数(ヌル文字を含まず)
[out]bufデコードされた文字列が格納される領域へのポインタ
[in]nバッファのサイズ
[in]firstデコードされる文字列の開始位置
[in]lastデコードされる文字列の終了位置
戻り値
0成功
EINVALfirst, lastががNULL, 又はbufNULLかつn が0でない場合
EILSEQ%エンコード文字列が間違っていた場合
ERANGEbuf のサイズが足りなかった場合
説明
%エンコードされた文字は全てデコードされます。 bufNULL, n に0を入れて実行すると、written に必要なバイト数が格納されます。

◆ DecodeUriComponent() [2/2]

nn::nlib::Uri::DecodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  s 
)
inlinestaticnoexcept

%エンコードされた文字列をデコードします。

引数
[in]writtenデコードされた文字列のバイト数(ヌル文字を含まず)
[out]bufデコードされた文字列が格納される領域へのポインタ
[in]nバッファのサイズ
[in]sデコードされる文字列
戻り値
0成功
EINVALsNULL, 又はbufNULLかつn が0でない場合
EILSEQ%エンコード文字列が間違っていた場合
ERANGEbuf のサイズが足りなかった場合
説明
%エンコードされた文字は全てデコードされます。 bufNULL, n に0を入れて実行すると、written に必要なバイト数が格納されます。

Uri.h100 行目に定義があります。

◆ EncodeUriComponent() [1/2]

nn::nlib::Uri::EncodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  first,
const char *  last,
bool  fragment_mode 
)
staticnoexcept

文字列を%エンコードします。

引数
[in]writtenエンコードされた文字列のバイト数(ヌル文字を含まず)
[out]bufエンコードされた文字列が格納される領域へのポインタ
[in]nバッファのサイズ
[in]firstエンコードされる文字列の開始位置
[in]lastエンコードされる文字列の終了位置
[in]fragment_modeエンコードする文字のカスタマイズ
戻り値
0成功
EINVALfirst, lastNULL, 又はbufNULLかつn が0でない場合
ERANGEbuf のサイズが足りなかった場合
説明
bufNULL, n に0を入れて実行すると、written に必要なバイト数が格納されます。

◆ EncodeUriComponent() [2/2]

nn::nlib::Uri::EncodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  s,
bool  fragment_mode 
)
inlinestaticnoexcept

文字列を%エンコードします。

引数
[in]writtenエンコードされた文字列のバイト数(ヌル文字を含まず)
[out]bufエンコードされた文字列が格納される領域へのポインタ
[in]nバッファのサイズ
[in]sエンコードされる文字列
[in]fragment_modeエンコードする文字のカスタマイズ
戻り値
0成功
EINVALsNULL, 又はbufNULLかつn が0でない場合
ERANGEbuf のサイズが足りなかった場合
説明
bufNULL, n に0を入れて実行すると、written に必要なバイト数が格納されます。

Uri.h137 行目に定義があります。

◆ GetFragment()

nn::nlib::Uri::GetFragment ( ) const
noexcept

フラグメント文字列を取得します

戻り値
フラグメント文字列(先頭の::は含まない)

◆ GetHost()

nn::nlib::Uri::GetHost ( ) const
noexcept

ホスト名を取得します。

戻り値
ホスト名(/は含まない)

◆ GetPath()

nn::nlib::Uri::GetPath ( ) const
noexcept

パスを取得します。

戻り値
パス文字列

◆ GetPort()

nn::nlib::Uri::GetPort ( ) const
noexcept

ポート番号の文字列を取得します。

戻り値
ポート番号の文字列(:は含まない)
説明
文字列中には0-9の文字のみが含まれます。先頭に0が続く場合も可能です。

◆ GetQuery()

nn::nlib::Uri::GetQuery ( ) const
noexcept

クエリ文字列を取得します

戻り値
クエリ文字列(先頭の?は含まない)

◆ GetScheme()

nn::nlib::Uri::GetScheme ( ) const
noexcept

URIのスキーム(http, file等)の文字列を返します。

戻り値
スキームの文字列(末尾の:は含まない)
説明
スキームが指定されていない場合(相対パス)はNULLを返します。

◆ GetUserInfo()

nn::nlib::Uri::GetUserInfo ( ) const
noexcept

指定した URI に関連付けられているユーザー名、パスワードなどのユーザー固有の情報を取得します。

戻り値
ユーザー固有の情報の文字列

◆ IsEmailAddress()

nn::nlib::Uri::IsEmailAddress ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がemailアドレスの要件(RFC5322 3.4)を満たすかどうかを判定します

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がemailアドレスの要件に適合する場合はtrue、そうでなければfalse
参照
https://tools.ietf.org/html/rfc5322#section-3.4
http://srgia.com/docs/rfc5322j.html#p3.4

◆ IsHostName()

nn::nlib::Uri::IsHostName ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がホスト名の要件(RFC1034 3.1)を満たすかどうかを判定します。

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がホスト名の場合はtrue、そうでなければfalse
参照
https://tools.ietf.org/html/rfc1034
http://srgia.com/docs/rfc1034j.html

◆ IsIpv4()

nn::nlib::Uri::IsIpv4 ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がIPv4の要件を満たすかどうかを判定します。

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がIPv4に適合する場合はtrue、そうでなければfalse

◆ IsIpv6()

nn::nlib::Uri::IsIpv6 ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がIPv6の要件を満たすかどうかを判定します。

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がIPv6に適合する場合はtrue、そうでなければfalse

◆ IsUri()

nn::nlib::Uri::IsUri ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がURIであるかどうかを判定します。相対パスの場合、URIではなくURI参照となることに注意してください。

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がURIである場合はtrue、そうでなければfalse

◆ IsUriReference()

nn::nlib::Uri::IsUriReference ( const nlib_utf8_t first,
const nlib_utf8_t last 
)
staticnoexcept

文字列がURI参照であるかどうかを判定します。

引数
[in]first検査する文字列の先頭
[in]last検査する文字列の末尾
戻り値
文字列がURI参照である場合はtrue、そうでなければfalse

◆ Parse()

nn::nlib::Uri::Parse ( const char *  first,
const char *  last 
)
noexcept

URI文字列をパースします。

引数
[in]firstパースする文字列の先頭
[in]lastパースする文字列の末尾
戻り値
成功した場合はtrue
説明
URI文字列が不正であった場合(利用不可能なキャラクタが入っていた等)は失敗します。 また、URI文字列の長さが2000文字以上の場合は失敗します。

◆ SetUri()

nn::nlib::Uri::SetUri ( const char *  scheme,
const char *  userinfo,
const char *  host,
const char *  port,
const char *  path,
const char *  query,
const char *  fragment 
)
noexcept

URIの各部分を設定します。

引数
[in]schemeスキーム名(末尾の':'は含まない)
[in]userinfoユーザ情報
[in]hostホスト名(先頭の'@'は含まない)
[in]portポート番号(先頭の':'は含まない)
[in]pathパス
[in]queryクエリ文字列(先頭の'?'は含まない)
[in]fragmentフラグメント文字列(先頭の'#'は含まない)
戻り値
成功した場合はtrue
説明
URIは、<scheme>://<userinfo>\<host>:<port><path>?<query>#<fragment> のように構成されます。 path については、userinfo, host, port のどれかがNULLでない場合は、'/'で始まる必要があります。 %エンコードが必要な文字はあらかじめ%エンコードされている必要があります。

このクラス詳解は次のファイルから抽出されました: