Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::URI Class Reference

The URI class parses the given uri string according to the regex given in RFC3986. More...

#include <uri.h>

+ Collaboration diagram for lunchbox::URI:

Public Types

typedef boost::unordered_map
< std::string, std::string > 
KVMap
 
typedef KVMap::const_iterator ConstKVIter
 

Public Member Functions

LUNCHBOX_API URI (const std::string &uri)
 
LUNCHBOX_API URI (const URI &from)
 Copy-construct an URI. More...
 
LUNCHBOX_API URIoperator= (const URI &rhs)
 Assign the data from another URI. More...
 
Getters for the uri data @version 1.9.2
LUNCHBOX_API const std::string & getScheme () const
 
LUNCHBOX_API const std::string & getUserinfo () const
 
LUNCHBOX_API uint16_t getPort () const
 
LUNCHBOX_API const std::string & getHost () const
 
LUNCHBOX_API const std::string & getPath () const
 
LUNCHBOX_API const std::string & getQuery () const
 
LUNCHBOX_API const std::string & getFragment () const
 
Getters to query key-value data @version 1.9.2
LUNCHBOX_API ConstKVIter queryBegin () const
 
LUNCHBOX_API ConstKVIter queryEnd () const
 
LUNCHBOX_API ConstKVIter findQuery (const std::string &key) const
 

Detailed Description

The URI class parses the given uri string according to the regex given in RFC3986.

* http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment
* ^   ^  ^  ^               ^    ^     ^                 ^
* a   b  c  d               e    f     g                 h
*
* URI part  Range   String
* scheme    [a, b)  "http"
* userinfo [c, d) bob
* host  [d, e)  "www.example.com"
* port (e, f) 8080
* path  [f, g)  "/path/"
* query (g, h)  "key=value"
* fragment  (h,-) "fragment"
* 

Queries are parsed into key-value pairs and can be accessed using findQuery(), queryBegin() and queryEnd().

Example:

/* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <test.h>
#include <lunchbox/uri.h>
int main( int, char ** )
{
try
{
const std::string uriStr =
"http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment";
const lunchbox::URI uri( uriStr );
TESTINFO( uri.getScheme() == "http", uri.getScheme() );
TESTINFO( uri.getHost() == "www.example.com", uri.getHost( ));
TESTINFO( uri.getUserinfo() == "bob", uri.getUserinfo( ));
TESTINFO( uri.getPort() == 8080, uri.getPort( ));
TESTINFO( uri.getPath() == "/path/", uri.getPath( ));
TESTINFO( uri.getQuery() == "key=value,foo=bar", uri.getQuery( ));
TESTINFO( uri.getFragment() == "fragment", uri.getFragment( ));
TEST( uri.findQuery( "key" ) != uri.queryEnd( ));
TEST( uri.findQuery( "foo" ) != uri.queryEnd( ));
TEST( uri.findQuery( "bar" ) == uri.queryEnd( ));
TESTINFO( uri.findQuery( "key" )->second == "value",
uri.findQuery( "key" )->second );
TESTINFO( uri.findQuery( "foo" )->second == "bar",
uri.findQuery( "foo" )->second );
std::stringstream sstr;
sstr << uri;
TESTINFO( sstr.str() == uriStr, sstr.str() << " " << uriStr );
}
catch( std::exception& exception )
{
LBERROR << exception.what() << std::endl;
return EXIT_FAILURE;
}
try
{
const std::string uriStr = "Helloworld";
lunchbox::URI uri( uriStr );
}
catch( std::exception& exception )
{
return EXIT_SUCCESS;
}
TESTINFO( false, "Did not get exception on malformed URI" );
return EXIT_FAILURE;
}

Definition at line 55 of file uri.h.

Constructor & Destructor Documentation

LUNCHBOX_API lunchbox::URI::URI ( const std::string &  uri)
Parameters
uriURI string to parse.
Exceptions
std::exceptionfor incomplete URIs, and boost::bad_lexical_cast if the port is not a number.
Version
1.9.2
LUNCHBOX_API lunchbox::URI::URI ( const URI from)

Copy-construct an URI.

Version
1.9.2

Member Function Documentation

LUNCHBOX_API ConstKVIter lunchbox::URI::findQuery ( const std::string &  key) const
Returns
a const iterator to the given key, or queryEnd().
Version
1.9.2
LUNCHBOX_API URI& lunchbox::URI::operator= ( const URI rhs)

Assign the data from another URI.

Version
1.9.2
LUNCHBOX_API ConstKVIter lunchbox::URI::queryBegin ( ) const
Returns
a const iterator to the beginning of the query map.
Version
1.9.2
LUNCHBOX_API ConstKVIter lunchbox::URI::queryEnd ( ) const
Returns
a const iterator to end beginning of the query map.
Version
1.9.2

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