te::common::uri::query Class Reference

URI query component. More...

#include <query.h>

Inheritance diagram for te::common::uri::query:

Public Member Functions

std::string encoding () const
 Calculate encoded string. More...
 
const_iterator find (const std::string &key) const
 Find the const_iterator for the key. More...
 
iterator find (const std::string &key)
 Find the iterator for the key. More...
 
template<typename T >
bool find (const std::string &key, T &value, bool &is_null) const
 Find the key and convert its associated value, returning true if the key is found and is_null true if the key is found but the value string is empty or isspace. More...
 
 query ()
 Construct. More...
 
 query (const std::string &v, bool dosort=false)
 Construct from encoded string, for example "a=foo%20bar&b=1", and sort the keys if dosort is true. More...
 
void sort ()
 Sort the pairs by key. More...
 
bool sorted () const
 Test if sort() has been called. More...
 

Static Public Attributes

static const char KEY_VALUE_SEP_CHAR
 key-value separator char ('=') More...
 
static const char PAIRS_SEP_CHAR
 pairs separator char ('&') More...
 
static const char PAIRS_SEP_CHAR2
 pairs separator char (';') // added by Lauro More...
 

Private Attributes

bool sorted_
 

Friends

bool TECOMMONEXPORT parse (std::string::const_iterator &first, std::string::const_iterator last, query &v, std::string *errs)
 Parse URI query, returning whether found or not and advancing first and setting query if found. More...
 

Detailed Description

URI query component.

This parses the query component into a list of string pairs, the first is the key and the second the value.

Syntax (see uri_path for additional definitions): Both key and value are pct-decoded after parsing.

query   = kv-pair *( "&" kv-pair )
kv-pair = key "=" [ value ]
key     = 1*qchar
value   = 1*qchar
qchar   = <pchar except "=" and "&"> | "/" | "?"

Note that this provides optional sorting for faster finds when there are a non-trivial number of pairs. However, once the query is sorted pairs must not be inserted.

See also
http://tools.ietf.org/html/rfc3986#section-3.4

Definition at line 54 of file query.h.

Constructor & Destructor Documentation

te::common::uri::query::query ( )

Construct.

te::common::uri::query::query ( const std::string &  v,
bool  dosort = false 
)

Construct from encoded string, for example "a=foo%20bar&b=1", and sort the keys if dosort is true.

Note that this should not contain the leading '?'. See parse() for less strict construction.

Exceptions
std::invalid_argumentif missing key or invalid encoding

Member Function Documentation

std::string te::common::uri::query::encoding ( ) const

Calculate encoded string.

Referenced by te::common::uri::operator<<().

const_iterator te::common::uri::query::find ( const std::string &  key) const

Find the const_iterator for the key.

Note the search is linear if not sorted.

iterator te::common::uri::query::find ( const std::string &  key)

Find the iterator for the key.

Note the search is linear if not sorted.

template<typename T >
bool te::common::uri::query::find ( const std::string &  key,
T &  value,
bool &  is_null 
) const
inline

Find the key and convert its associated value, returning true if the key is found and is_null true if the key is found but the value string is empty or isspace.

The value is only set if found but not null. Note the search is linear.

Exceptions
std::invalid_argumentif not null and cannot convert

Definition at line 79 of file query.h.

References te::common::uri::convert().

void te::common::uri::query::sort ( )

Sort the pairs by key.

This will speed the find methods if there are more than a trivial number of pairs.

bool te::common::uri::query::sorted ( ) const
inline

Test if sort() has been called.

Definition at line 66 of file query.h.

Friends And Related Function Documentation

bool TECOMMONEXPORT parse ( std::string::const_iterator &  first,
std::string::const_iterator  last,
query v,
std::string *  errs 
)
friend

Parse URI query, returning whether found or not and advancing first and setting query if found.

Does not skip leading space.

If errs is specified the following take place:

  • Errors in parsing subcomponents, after the first, do not cause an immediate false return and errors messages are reported in the error string errs.
  • Errors are handled as follows:
    • Key-value pairs with an empty key are ignored.
    • Keys with missing '=' are ignored.
    • Errors decoding a key or value result in no decoding at all (i.e. assumes an unencoded '').

Member Data Documentation

const char te::common::uri::query::KEY_VALUE_SEP_CHAR
static

key-value separator char ('=')

Definition at line 88 of file query.h.

const char te::common::uri::query::PAIRS_SEP_CHAR
static

pairs separator char ('&')

Definition at line 86 of file query.h.

const char te::common::uri::query::PAIRS_SEP_CHAR2
static

pairs separator char (';') // added by Lauro

Definition at line 87 of file query.h.

bool te::common::uri::query::sorted_
private

Definition at line 91 of file query.h.


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