Lunchbox  1.10.0
 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 ()
 Construct an empty URI. More...
 
LUNCHBOX_API URI (const std::string &uri)
 
LUNCHBOX_API URI (const char *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
 
Access to key-value data in query @version 1.9.2
LUNCHBOX_API ConstKVIter queryBegin () const
 
LUNCHBOX_API ConstKVIter queryEnd () const
 
LUNCHBOX_API ConstKVIter findQuery (const std::string &key) const
 
LUNCHBOX_API void addQuery (const std::string &key, const std::string &value)
 Add a key-value pair to the query. More...
 

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";
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 );
uri.addQuery( "hans", "dampf" );
TESTINFO( uri.findQuery( "key" )->second == "value",
uri.findQuery( "key" )->second );
TESTINFO( uri.findQuery( "foo" )->second == "bar",
uri.findQuery( "foo" )->second );
TESTINFO( uri.findQuery( "hans" )->second == "dampf",
uri.findQuery( "hans" )->second );
TESTINFO( uri.getQuery().find( "hans=dampf" ) != std::string::npos,
uri.getQuery( ));
sstr.str( "" );
sstr << lunchbox::URI( "http://www.example.com/path" );
TESTINFO( sstr.str() == "http://www.example.com/path", sstr.str( ));
sstr.str( "" );
sstr << lunchbox::URI( "/path" );
TESTINFO( sstr.str() == "/path", sstr.str( ));
const lunchbox::URI hostPortURI( "foo://hostname:12345" );
TESTINFO( hostPortURI.getScheme() == "foo", uri.getScheme() );
TESTINFO( hostPortURI.getHost() == "hostname", uri.getHost( ));
TESTINFO( hostPortURI.getPort() == 12345, uri.getPort( ));
TEST( empty.getScheme().empty( ));
TEST( empty.getHost().empty( ));
TEST( empty.getUserinfo().empty( ));
TEST( empty.getPort() == 0 );
TEST( empty.getPath().empty( ));
TEST( empty.getQuery().empty( ));
TEST( empty.getFragment().empty( ));
lunchbox::URI file1( "/bla.txt" );
TEST( file1.getPath() == "/bla.txt" );
TEST( file1.getHost().empty( ));
TEST( file1.getScheme().empty( ));
lunchbox::URI file2( "bla.txt" );
TEST( file2.getPath() == "bla.txt" );
TEST( file2.getHost().empty( ));
TEST( file2.getScheme().empty( ));
lunchbox::URI file3( "file:///bla.txt" );
TEST( file3.getPath() == "/bla.txt" );
TEST( file3.getHost().empty( ));
TEST( file3.getScheme() == "file" );
lunchbox::URI file4( "file://bla.txt" );
TEST( file4.getPath() == "bla.txt" );
TEST( file4.getHost().empty( ));
TEST( file4.getScheme() == "file" );
lunchbox::URI file5( "host://bla.txt" );
TEST( file5.getHost() == "bla.txt" );
TEST( file5.getPath().empty( ));
TEST( file5.getScheme() == "host" );
}
catch( std::exception& exception )
{
LBERROR << exception.what() << std::endl;
return EXIT_FAILURE;
}
try
{
const std::string uriStr = "Helloworld#?#://##";
lunchbox::URI uri( uriStr );
TESTINFO( false, "Did not get exception on malformed URI: " << uri );
}
catch( std::exception& ) {}
return EXIT_SUCCESS;
}

Definition at line 55 of file uri.h.

Constructor & Destructor Documentation

LUNCHBOX_API lunchbox::URI::URI ( )

Construct an empty URI.

Version
1.9.2
LUNCHBOX_API lunchbox::URI::URI ( const std::string &  uri)
explicit
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 char *  uri)
explicit

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

LUNCHBOX_API lunchbox::URI::URI ( const URI from)

Copy-construct an URI.

Version
1.9.2

Member Function Documentation

LUNCHBOX_API void lunchbox::URI::addQuery ( const std::string &  key,
const std::string &  value 
)

Add a key-value pair to the query.

Version
1.9.2
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: