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

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

#include "nn/nlib/Uri.h"

公開メンバ関数

 Uri () noexcept
 デフォルトコンストラクタです。オブジェクトを初期化します。
 
bool Parse (const char *str) noexcept
 URI文字列をパースします。 [詳解]
 
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の各部分を設定します。 [詳解]
 
errno_t ComposeString (OutputStream *os) const noexcept
 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
 ポート番号の文字列を取得します。 [詳解]
 
const char * GetPath () const noexcept
 パスを取得します。 [詳解]
 
const char * GetQuery () const noexcept
 クエリ文字列を取得します [詳解]
 
const char * GetFragment () const noexcept
 フラグメント文字列を取得します [詳解]
 
void Reset () noexcept
 オブジェクトを初期化(リセット)します。
 

静的公開メンバ関数

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 (InputStream *is, OutputStream *os) noexcept
 %エンコードされた文字列をデコードします。 [詳解]
 
static errno_t EncodeUriComponent (InputStream *is, OutputStream *os, bool fragment_mode=false) noexcept
 文字列を%エンコードします。 [詳解]
 
static errno_t DecodeUriComponent (size_t *written, char *buf, size_t n, const char *s) noexcept
 %エンコードされた文字列をデコードします。 [詳解]
 
static errno_t EncodeUriComponent (size_t *written, char *buf, size_t n, const char *s, bool fragment_mode) noexcept
 文字列を%エンコードします。 [詳解]
 
static errno_t DecodePath (size_t *written, char *buf, size_t n, const char *s) noexcept
 %エンコードされた文字列をデコードします。 デコードされた文字が'/'の場合にEILSEQを返すこと以外は、DecodeUriComponent()と同じです。
 
static errno_t EncodePath (size_t *written, char *buf, size_t n, const char *s) noexcept
 文字列を%エンコードします。'/'を%エンコードしないこと以外は、EncodeUriComponent()と同じです。
 
template<size_t N>
static errno_t DecodeUriComponent (size_t *written, char(&buf)[N], const char *s) noexcept
 DecodeUriComponent(written, buf, N, s)を呼び出します。
 
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)を呼び出します。
 
template<size_t N>
static errno_t DecodePath (size_t *written, char(&buf)[N], const char *s) noexcept
 DecodePath(written, buf, N, s)を呼び出します。
 
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 / "-" / "." / "_" / "~")に属する文字です。 それ以外の文字は%エンコードされたままです。
詳しくはRFC 3986(https://www.ietf.org/rfc/rfc3986.txt)を御覧ください。
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"
各種例:
misc/uri/uri.cpp.

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

関数詳解

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

相対パスを解決します。

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

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

引数
[in]osURI文字列を書き出す出力ストリーム
戻り値
成功した場合は0
説明
  • EINVAL, osNULLだった場合
  • EIO, ストリームでのエラー
nn::nlib::Uri::ComposeString ( char *  buf,
size_t  size 
) const
noexcept

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

引数
[out]buf文字列が書きだされるバッファへのポインタ
[in]sizeバッファサイズ
戻り値
成功した場合はtrue
template<size_t N>
nn::nlib::Uri::ComposeString ( char(&)  buf[N]) const
inlinenoexcept

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

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

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

nn::nlib::Uri::DecodeUriComponent ( InputStream is,
OutputStream os 
)
staticnoexcept

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

引数
[in]isデコードされる文字列を取得するストリーム
[in]osデコードされた文字列が格納されるストリーム
戻り値
0ならば成功。それ以外ならエラー
説明
%エンコードされた文字は全てデコードされます。
戻り値が0以外の場合はエラーです。
  • EINVAL, is 又はosNULLです。
  • EIO, is 又はos でエラーが発生しました。
  • EILSEQ, URIとしては不正な文字を検出しました。
nn::nlib::Uri::DecodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  s 
)
staticnoexcept

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

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

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

引数
[in]isエンコードされる文字列を取得するストリーム
[in]osデコードされた文字列が格納されるストリーム
[in]fragment_modeエンコードする文字のカスタマイズ
戻り値
0ならば成功。それ以外ならエラー
説明
fragment_modefalseを指定した場合、%エンコードされる文字は、RFC3986のunreserved(ALPHA / DIGIT / "-" / "." / "_" / "~")以外の文字です。
fragment_modetrueを指定した場合は、%エンコードされる文字は、RFC3986のunreserved及び":" / "@" / "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" / "/" / "?"以外の文字です。
戻り値が0以外の場合はエラーです。
  • EINVAL, is 又はosNULLです。
  • EIO, is 又はos でエラーが発生しました。
nn::nlib::Uri::EncodeUriComponent ( size_t *  written,
char *  buf,
size_t  n,
const char *  s,
bool  fragment_mode 
)
staticnoexcept

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

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

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

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

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

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

ホスト名を取得します。

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

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

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

パスを取得します。

戻り値
パス文字列

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

nn::nlib::Uri::Parse ( const char *  str)
noexcept

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

引数
[in]strURI文字列
戻り値
成功した場合はtrue
説明
URI文字列が不正であった場合(利用不可能なキャラクタが入っていた等)は失敗します。 また、URI文字列の長さが2000文字以上の場合は失敗します。
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でない場合は、'/'で始まる必要があります。 %エンコードが必要な文字はあらかじめ%エンコードされている必要があります。

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