Line data Source code
1 :
2 : /* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch
3 : * 2014, Stefan.Eilemann@epfl.ch
4 : *
5 : * This file is part of Lunchbox <https://github.com/Eyescale/Lunchbox>
6 : *
7 : * This library is free software; you can redistribute it and/or modify it under
8 : * the terms of the GNU Lesser General Public License version 2.1 as published
9 : * by the Free Software Foundation.
10 : *
11 : * This library is distributed in the hope that it will be useful, but WITHOUT
12 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 : * details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public License
17 : * along with this library; if not, write to the Free Software Foundation, Inc.,
18 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : */
20 :
21 : #ifndef LUNCHBOX_URI_H
22 : #define LUNCHBOX_URI_H
23 :
24 : #include <lunchbox/api.h>
25 : #include <lunchbox/types.h>
26 : #include <boost/unordered_map.hpp> // iterator typedefs
27 :
28 : namespace lunchbox
29 : {
30 : namespace detail { class URI; }
31 :
32 : /**
33 : * The URI class parses the given uri string according to the regex given in
34 : * RFC3986.
35 : * @verbatim
36 : * http://bob@www.example.com:8080/path/?key=value,foo=bar#fragment
37 : * ^ ^ ^ ^ ^ ^ ^ ^
38 : * a b c d e f g h
39 : *
40 : * URI part Range String
41 : * scheme [a, b) "http"
42 : * userinfo [c, d) bob
43 : * host [d, e) "www.example.com"
44 : * port (e, f) 8080
45 : * path [f, g) "/path/"
46 : * query (g, h) "key=value"
47 : * fragment (h,-) "fragment"
48 : * @endverbatim
49 : *
50 : * Queries are parsed into key-value pairs and can be accessed using
51 : * findQuery(), queryBegin() and queryEnd().
52 : *
53 : * Example: @include tests/uri.cpp
54 : */
55 : class URI
56 : {
57 : public:
58 : typedef boost::unordered_map< std::string, std::string > KVMap;
59 : typedef KVMap::const_iterator ConstKVIter;
60 :
61 : /**
62 : * @param uri URI string to parse.
63 : * @throw std::exception for incomplete URIs, and boost::bad_lexical_cast
64 : * if the port is not a number.
65 : * @version 1.9.2
66 : */
67 : LUNCHBOX_API URI( const std::string& uri );
68 :
69 : /** Copy-construct an URI. @version 1.9.2 */
70 : LUNCHBOX_API URI( const URI& from );
71 :
72 : LUNCHBOX_API ~URI();
73 :
74 : /** Assign the data from another URI. @version 1.9.2 */
75 : LUNCHBOX_API URI& operator = ( const URI& rhs );
76 :
77 : /** @name Getters for the uri data @version 1.9.2 */
78 : //@{
79 : LUNCHBOX_API const std::string& getScheme() const;
80 : LUNCHBOX_API const std::string& getUserinfo() const;
81 : LUNCHBOX_API uint16_t getPort() const;
82 : LUNCHBOX_API const std::string& getHost() const;
83 : LUNCHBOX_API const std::string& getPath() const;
84 : LUNCHBOX_API const std::string& getQuery() const;
85 : LUNCHBOX_API const std::string& getFragment() const;
86 : //@}
87 :
88 : /** @name Getters to query key-value data @version 1.9.2 */
89 : //@{
90 : /**
91 : * @return a const iterator to the beginning of the query map.
92 : * @version 1.9.2
93 : */
94 : LUNCHBOX_API ConstKVIter queryBegin() const;
95 :
96 : /**
97 : * @return a const iterator to end beginning of the query map.
98 : * @version 1.9.2
99 : */
100 : LUNCHBOX_API ConstKVIter queryEnd() const;
101 :
102 : /**
103 : * @return a const iterator to the given key, or queryEnd().
104 : * @version 1.9.2
105 : */
106 : LUNCHBOX_API ConstKVIter findQuery( const std::string& key ) const;
107 : //@}
108 :
109 : private:
110 : detail::URI* const _impl;
111 : };
112 :
113 1 : inline std::ostream& operator << ( std::ostream& os, const URI& uri )
114 : {
115 1 : os << uri.getScheme() << "://";
116 1 : if( !uri.getUserinfo().empty( ))
117 1 : os << uri.getUserinfo() << "@";
118 1 : os << uri.getHost();
119 1 : if( uri.getPort( ))
120 1 : os << ':' << uri.getPort();
121 1 : os << uri.getPath() << '?' << uri.getQuery();
122 1 : if( !uri.getFragment().empty( ))
123 1 : os << '#' << uri.getFragment();
124 1 : return os;
125 : }
126 :
127 : }
128 : #endif // LUNCHBOX_URI_H
|