Servus  1.5.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
32 {
33 class URI;
34 }
35 
62 class URI
63 {
64 public:
65  typedef std::map<std::string, std::string> KVMap;
66  typedef KVMap::const_iterator ConstKVIter;
67 
69  SERVUS_API URI();
70 
76  SERVUS_API explicit URI(const std::string& uri);
77 
79  SERVUS_API explicit URI(const char* uri);
80 
82  SERVUS_API URI(const URI& from);
83 
84  SERVUS_API ~URI();
85 
87  SERVUS_API URI& operator=(const URI& rhs);
88 
90  SERVUS_API bool operator==(const URI& rhs) const;
91 
93  SERVUS_API bool operator!=(const URI& rhs) const;
94 
97  SERVUS_API const std::string& getScheme() const;
98  SERVUS_API const std::string& getUserinfo() const;
99  SERVUS_API uint16_t getPort() const;
100  SERVUS_API const std::string& getHost() const;
105  SERVUS_API std::string getAuthority() const;
106  SERVUS_API const std::string& getPath() const;
107  SERVUS_API const std::string& getQuery() const;
108  SERVUS_API const std::string& getFragment() const;
110 
113  SERVUS_API void setScheme(const std::string& scheme);
114  SERVUS_API void setUserInfo(const std::string& userinfo);
115  SERVUS_API void setHost(const std::string& host);
116  SERVUS_API void setPort(uint16_t port);
117  SERVUS_API void setPath(const std::string& path);
118  SERVUS_API void setQuery(const std::string& query);
119  SERVUS_API void setFragment(const std::string& fragment);
121 
127  SERVUS_API ConstKVIter queryBegin() const;
128 
132  SERVUS_API ConstKVIter queryEnd() const;
133 
137  SERVUS_API ConstKVIter findQuery(const std::string& key) const;
138 
140  SERVUS_API void addQuery(const std::string& key, const std::string& value);
142 
143 private:
144  detail::URI* const _impl;
145 };
146 
147 inline std::ostream& operator<<(std::ostream& os, const URI& uri)
148 {
149  if (!uri.getScheme().empty())
150  os << uri.getScheme() << "://";
151  // A valid URI can't contain the user info or port number alone, so if
152  // the host name is empty the other two field are simply ignored.
153  if (!uri.getHost().empty())
154  {
155  if (!uri.getUserinfo().empty())
156  os << uri.getUserinfo() << "@";
157  os << uri.getHost();
158  if (uri.getPort())
159  os << ':' << uri.getPort();
160  }
161  os << uri.getPath();
162  if (!uri.getQuery().empty())
163  os << '?' << uri.getQuery();
164  if (!uri.getFragment().empty())
165  os << '#' << uri.getFragment();
166  return os;
167 }
168 }
169 
170 namespace std
171 {
172 inline std::string to_string(const servus::URI& uri)
173 {
174  ostringstream os;
175  os << uri;
176  return os.str();
177 }
178 }
179 #endif // SERVUS_URI_H
The URI class parses the given uri using the generic syntax from RFC3986 and RFC6570.
Definition: uri.h:62
Defines export visibility macros for library Servus.
STL namespace.