nlib
nn::nlib::HttpStyleUri Class Referencefinal

The class for parsing URIs that use the http and https schemes, and for constructing URI strings. More...

#include "nn/nlib/HttpStyleUri.h"

Public Types

typedef std::pair< const char *, const char * > KeyValue
 The type that stores query keys and values.
 

Public Member Functions

 HttpStyleUri () noexcept
 Instantiates the object with default parameters (default constructor). Initializes an object.
 
bool FromUri (const Uri &uri) noexcept
 Converts from a URI object. More...
 
bool ToUri (Uri *uri) const noexcept
 Converts to a URI object. More...
 
bool Parse (const char *str) noexcept
 Parses URIs. More...
 
bool ComposeString (char *buf, size_t size) const noexcept
 Writes a URI string. More...
 
template<size_t N>
bool ComposeString (char(&buf)[N]) const noexcept
 Calls ComposeString(buf, N).
 
bool SetScheme (const char *scheme) noexcept
 Sets the scheme. More...
 
bool SetUserInfo (const char *userinfo) noexcept
 Sets the user information. More...
 
bool SetHost (const char *host) noexcept
 Sets the hostname. It does not need to be percent-encoded. More...
 
bool SetPort (int port) noexcept
 Sets the port number. More...
 
bool SetPath (const char *path) noexcept
 Sets a path. More...
 
bool SetQuery (const char *query) noexcept
 Sets a query based on a query string. More...
 
bool SetFragment (const char *fragment) noexcept
 Sets a fragment string. More...
 
bool AddQuery (const char *key, const char *value) noexcept
 Adds a key and value to a query. More...
 
const char * GetScheme () const noexcept
 Gets the name of the URI scheme. The name is either http or https. More...
 
const char * GetUserInfo () const noexcept
 Gets the user information. More...
 
const char * GetHost () const noexcept
 Gets the hostname. More...
 
int GetPort () const noexcept
 Gets the port number. More...
 
size_t GetNumSegment () const noexcept
 Gets the number of stored segments. More...
 
const char * GetSegment (size_t i) const noexcept
 Specifies an index and gets a segment. More...
 
bool IsPathDirectory () const noexcept
 Gets whether the path is in the form of a directory path. More...
 
const char * GetQueryValue (const char *key) const noexcept
 Specifies a key and gets a value. If not found, returns NULL. More...
 
size_t GetNumQuery () const noexcept
 Gets the value corresponding to the key. If not found, returns NULL. More...
 
const KeyValueGetQueryKeyValue (size_t i) const noexcept
 Specifies an index and gets a key/value pair. If not found, returns NULL. More...
 
const char * GetFragment () const noexcept
 Gets a fragment string. More...
 
bool AddBaseUri (const Uri &relative, const HttpStyleUri &base) noexcept
 Resolves the relative path using the base URI. More...
 
void Reset () noexcept
 Initializes (resets) an object.
 

Detailed Description

The class for parsing URIs that use the http and https schemes, and for constructing URI strings.

Description
The URI string of characters is checked, divided up, normalized, and stored. During parsing, partial strings such as the "../" and "./" dot-segments are normalized and then the whole string is divided into segments and stored. For example, a path like /test/../../../a/b/c would be normalized to /a/b/c and then divided into segments and stored. The percent-encoded characters in each segment are decoded.
Supports query strings that consist of a series of key=value pairs connected using the ampersand (&). The query string is stored in order of the ampersand signs (& that delimit the key/value pairs and the first equal sign (=) in each component that separates key and value. If there are duplicate key values, they are stored that way. Any percent-encoded characters in key and value are decoded.
Examples:
misc/uri/uri.cpp.

Definition at line 12 of file HttpStyleUri.h.

Member Function Documentation

§ AddBaseUri()

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

Resolves the relative path using the base URI.

Parameters
[in]relativeA relative path.
[in]baseA base URI.
Returns
Returns true when successful.
Description
If the function succeeds, the relative path gets stored in the object.

§ AddQuery()

nn::nlib::HttpStyleUri::AddQuery ( const char *  key,
const char *  value 
)
noexcept

Adds a key and value to a query.

Parameters
[in]keyA key.
[in]valueA value.
Returns
Returns true when successful.
Description
The key and value strings do not need to be percent-encoded

§ ComposeString()

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

Writes a URI string.

Parameters
[out]bufThe pointer to the buffer to which the string was written.
[in]sizeThe size of the buffer.
Returns
Returns true when successful.
Description
This function uses of the ToUri and Uri::ComposeString functions.

§ FromUri()

nn::nlib::HttpStyleUri::FromUri ( const Uri uri)
noexcept

Converts from a URI object.

Parameters
[in]uriThe reference to the URI object.
Returns
Returns true when successful.

§ GetFragment()

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

Gets a fragment string.

Returns
The pointer to the fragment string.
Description
The fragment strings that this function can get are completely percent-decoded.
The function might return a NULL pointer or an empty string. The meanings are slightly different.

§ GetHost()

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

Gets the hostname.

Returns
The hostname.

§ GetNumQuery()

nn::nlib::HttpStyleUri::GetNumQuery ( ) const
noexcept

Gets the value corresponding to the key. If not found, returns NULL.

Returns
The pointer to the value.
Description
The value of the key must be a percent-decoded string. The values that this function can get are also percent-decoded strings.

§ GetNumSegment()

nn::nlib::HttpStyleUri::GetNumSegment ( ) const
noexcept

Gets the number of stored segments.

Returns
The number of segments.

§ GetPort()

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

Gets the port number.

Returns
The port number.

§ GetQueryKeyValue()

nn::nlib::HttpStyleUri::GetQueryKeyValue ( size_t  i) const
noexcept

Specifies an index and gets a key/value pair. If not found, returns NULL.

Parameters
[in]iAn index.
Returns
The array to a key/value pair.
Description
The strings that this function can get for key and value are completely percent-decoded. key or value might be NULL or an empty string. This reflects small differences in the query string, as shown below.
  • If the query string is something like ?...&&..., key = NULL and value = NULL. <.i> If the query string is something like ?...&=&..., key = "" and value = "".

§ GetQueryValue()

nn::nlib::HttpStyleUri::GetQueryValue ( const char *  key) const
noexcept

Specifies a key and gets a value. If not found, returns NULL.

Parameters
[in]keyKey string.
Returns
The string used as the value.

§ GetScheme()

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

Gets the name of the URI scheme. The name is either http or https.

Returns
Returns the scheme name.

§ GetSegment()

nn::nlib::HttpStyleUri::GetSegment ( size_t  i) const
noexcept

Specifies an index and gets a segment.

Parameters
[in]iThe index for a segment.
Returns
The string of the specified segment.
Description
If the function could get the specified segment, it returns the pointer to that string. If it failed, it returns NULL. The segment string is percent-decoded.

§ GetUserInfo()

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

Gets the user information.

Returns
The user information.

§ IsPathDirectory()

nn::nlib::HttpStyleUri::IsPathDirectory ( ) const
noexcept

Gets whether the path is in the form of a directory path.

Returns
Returns true if the path is a directory path.
Description
Returns whether the path string ends with a forward slash ( '/' ).

§ Parse()

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

Parses URIs.

Parameters
[in]strThe URI string.
Returns
Returns true on success.

§ SetFragment()

nn::nlib::HttpStyleUri::SetFragment ( const char *  fragment)
noexcept

Sets a fragment string.

Parameters
[in]fragmentA fragment string.
Returns
Returns true when successful.
Description
The fragment string does not need to be percent-encoded

§ SetHost()

nn::nlib::HttpStyleUri::SetHost ( const char *  host)
noexcept

Sets the hostname. It does not need to be percent-encoded.

Parameters
[in]hostThe hostname.
Returns
Returns true when successful.

§ SetPath()

nn::nlib::HttpStyleUri::SetPath ( const char *  path)
noexcept

Sets a path.

Parameters
[in]pathThe path string.
Returns
Returns true when successful.
Description
The path is decomposed into its fragments and stored. The forward slash ( '/') is used as the delimiter. You cannot use '\' for this purpose. In addition, percent-encoding is not required (with the exception of '/').

§ SetPort()

nn::nlib::HttpStyleUri::SetPort ( int  port)
noexcept

Sets the port number.

Parameters
[in]portA port number.
Returns
Returns true when successful.
Description
When the object is first initialized, the port is set to 80. The port number can be set to any value between 0 and 65535.

§ SetQuery()

nn::nlib::HttpStyleUri::SetQuery ( const char *  query)
noexcept

Sets a query based on a query string.

Parameters
[in]queryA query string.
Returns
Returns true when successful.
Description
The query string does not need to be percent-encoded (with the exceptions of '&' and '=').

§ SetScheme()

nn::nlib::HttpStyleUri::SetScheme ( const char *  scheme)
noexcept

Sets the scheme.

Parameters
[in]schemeThe scheme (http or https).
Returns
Returns true when successful.
Description
For the parameter, you can specify either http or https. If you specify http, the port number is set to 80. If you specify https, the port number is set to 81. When the object is first initialized the scheme is set to http.

§ SetUserInfo()

nn::nlib::HttpStyleUri::SetUserInfo ( const char *  userinfo)
noexcept

Sets the user information.

Parameters
[in]userinfoThe user information.
Returns
Returns true when successful.
Description
This attaches a string in front of the hostname, as in userinfo@hostname. This is not used under normal circumstances.

§ ToUri()

nn::nlib::HttpStyleUri::ToUri ( Uri uri) const
noexcept

Converts to a URI object.

Parameters
[out]uriPointer to the URI object.
Returns
Returns true when successful.

The documentation for this class was generated from the following files: