Servus  1.2.0
A small set of network oriented utilities in C++ including a zeroconf implementation.
uri.h
1 /* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch
2  * 2014, Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Servus <https://github.com/HBPVIS/Servus>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef SERVUS_URI_H
21 #define SERVUS_URI_H
22 
23 #include <servus/api.h>
24 #include <servus/types.h>
25 
26 #include <map>
27 #include <sstream>
28 
29 namespace servus
30 {
31 namespace detail { class URI; }
32 
55 class URI
56 {
57 public:
58  typedef std::map< std::string, std::string > KVMap;
59  typedef KVMap::const_iterator ConstKVIter;
60 
62  SERVUS_API URI();
63 
69  SERVUS_API explicit URI( const std::string& uri );
70 
72  SERVUS_API explicit URI( const char* uri );
73 
75  SERVUS_API URI( const URI& from );
76 
77  SERVUS_API ~URI();
78 
80  SERVUS_API URI& operator = ( const URI& rhs );
81 
83  SERVUS_API bool operator == ( const URI& rhs ) const;
84 
86  SERVUS_API bool operator != ( const URI& rhs ) const;
87 
90  SERVUS_API const std::string& getScheme() const;
91  SERVUS_API const std::string& getUserinfo() const;
92  SERVUS_API uint16_t getPort() const;
93  SERVUS_API const std::string& getHost() const;
98  SERVUS_API std::string getAuthority() const;
99  SERVUS_API const std::string& getPath() const;
100  SERVUS_API const std::string& getQuery() const;
101  SERVUS_API const std::string& getFragment() const;
103 
106  SERVUS_API void setScheme( const std::string& scheme );
107  SERVUS_API void setUserInfo( const std::string& userinfo );
108  SERVUS_API void setHost( const std::string& host );
109  SERVUS_API void setPort( uint16_t port );
110  SERVUS_API void setPath( const std::string& path );
111  SERVUS_API void setFragment( const std::string& fragment );
113 
119  SERVUS_API ConstKVIter queryBegin() const;
120 
124  SERVUS_API ConstKVIter queryEnd() const;
125 
129  SERVUS_API ConstKVIter findQuery( const std::string& key ) const;
130 
132  SERVUS_API void addQuery( const std::string& key,
133  const std::string& value );
135 
136 private:
137  detail::URI* const _impl;
138 };
139 
140 inline std::ostream& operator << ( std::ostream& os, const URI& uri )
141 {
142  if( !uri.getScheme().empty( ))
143  os << uri.getScheme() << "://";
144  // A valid URI can't contain the user info or port number alone, so if
145  // the host name is empty the other two field are simply ignored.
146  if( !uri.getHost().empty( ))
147  {
148  if( !uri.getUserinfo().empty( ))
149  os << uri.getUserinfo() << "@";
150  os << uri.getHost();
151  if( uri.getPort( ))
152  os << ':' << uri.getPort();
153  }
154  os << uri.getPath();
155  if( !uri.getQuery().empty( ))
156  os << '?' << uri.getQuery();
157  if( !uri.getFragment().empty( ))
158  os << '#' << uri.getFragment();
159  return os;
160 }
161 }
162 
163 namespace std
164 {
165 inline std::string to_string( const servus::URI& uri )
166 {
167  ostringstream os;
168  os << uri;
169  return os.str();
170 }
171 }
172 #endif // SERVUS_URI_H
SERVUS_API URI & operator=(const URI &rhs)
Assign the data from another URI.
The URI class parses the given uri using the generic syntax from RFC3986.
Definition: uri.h:55
SERVUS_API ConstKVIter queryBegin() const
Defines export visibility macros for library Servus.
SERVUS_API bool operator==(const URI &rhs) const
Equals operator.
STL namespace.
SERVUS_API bool operator!=(const URI &rhs) const
Not equals operator.
SERVUS_API std::string getAuthority() const
Return the compound authority part of the URI.
SERVUS_API ConstKVIter queryEnd() const
SERVUS_API void addQuery(const std::string &key, const std::string &value)
Add a key-value pair to the query.
SERVUS_API ConstKVIter findQuery(const std::string &key) const
SERVUS_API URI()
Construct an empty URI.