Servus  1.3.1
C++ network oriented utilities 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 
59 class URI
60 {
61 public:
62  typedef std::map< std::string, std::string > KVMap;
63  typedef KVMap::const_iterator ConstKVIter;
64 
66  SERVUS_API URI();
67 
73  SERVUS_API explicit URI( const std::string& uri );
74 
76  SERVUS_API explicit URI( const char* uri );
77 
79  SERVUS_API URI( const URI& from );
80 
81  SERVUS_API ~URI();
82 
84  SERVUS_API URI& operator = ( const URI& rhs );
85 
87  SERVUS_API bool operator == ( const URI& rhs ) const;
88 
90  SERVUS_API bool operator != ( const URI& rhs ) const;
91 
94  SERVUS_API const std::string& getScheme() const;
95  SERVUS_API const std::string& getUserinfo() const;
96  SERVUS_API uint16_t getPort() const;
97  SERVUS_API const std::string& getHost() const;
102  SERVUS_API std::string getAuthority() const;
103  SERVUS_API const std::string& getPath() const;
104  SERVUS_API const std::string& getQuery() const;
105  SERVUS_API const std::string& getFragment() const;
107 
110  SERVUS_API void setScheme( const std::string& scheme );
111  SERVUS_API void setUserInfo( const std::string& userinfo );
112  SERVUS_API void setHost( const std::string& host );
113  SERVUS_API void setPort( uint16_t port );
114  SERVUS_API void setPath( const std::string& path );
115  SERVUS_API void setQuery( const std::string& query );
116  SERVUS_API void setFragment( const std::string& fragment );
118 
124  SERVUS_API ConstKVIter queryBegin() const;
125 
129  SERVUS_API ConstKVIter queryEnd() const;
130 
134  SERVUS_API ConstKVIter findQuery( const std::string& key ) const;
135 
137  SERVUS_API void addQuery( const std::string& key,
138  const std::string& value );
140 
141 private:
142  detail::URI* const _impl;
143 };
144 
145 inline std::ostream& operator << ( std::ostream& os, const URI& uri )
146 {
147  if( !uri.getScheme().empty( ))
148  os << uri.getScheme() << "://";
149  // A valid URI can't contain the user info or port number alone, so if
150  // the host name is empty the other two field are simply ignored.
151  if( !uri.getHost().empty( ))
152  {
153  if( !uri.getUserinfo().empty( ))
154  os << uri.getUserinfo() << "@";
155  os << uri.getHost();
156  if( uri.getPort( ))
157  os << ':' << uri.getPort();
158  }
159  os << uri.getPath();
160  if( !uri.getQuery().empty( ))
161  os << '?' << uri.getQuery();
162  if( !uri.getFragment().empty( ))
163  os << '#' << uri.getFragment();
164  return os;
165 }
166 }
167 
168 namespace std
169 {
170 inline std::string to_string( const servus::URI& uri )
171 {
172  ostringstream os;
173  os << uri;
174  return os.str();
175 }
176 }
177 #endif // SERVUS_URI_H
void addQuery(const std::string &key, const std::string &value)
Add a key-value pair to the query.
The URI class parses the given uri using the generic syntax from RFC3986 and RFC6570.
Definition: uri.h:59
Defines export visibility macros for library Servus.
STL namespace.
URI & operator=(const URI &rhs)
Assign the data from another URI.
bool operator==(const URI &rhs) const
Equals operator.
ConstKVIter queryEnd() const
ConstKVIter queryBegin() const
ConstKVIter findQuery(const std::string &key) const
URI()
Construct an empty URI.
bool operator!=(const URI &rhs) const
Not equals operator.
std::string getAuthority() const
Return the compound authority part of the URI.