dodo  0.0.1
A C++ library to create containerized Linux services
uri.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file uri.hpp
20  * Defines the dodo::network::URI class.
21  */
22 
23 #ifndef dodo_network_uri_hpp
24 #define dodo_network_uri_hpp
25 
26 #include <string>
27 
28 namespace dodo {
29 
30  namespace network {
31 
32  /**
33  * Uniform Resource Identifier.
34  * @see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
35  */
36  class URI {
37  public:
38 
39  /**
40  * Construct an empty URI.
41  */
42  URI() { reset(); };
43 
44  /**
45  * Construct an URI from iits components.
46  * @param scheme The scheme.
47  * @param userinfo The userinfo.
48  * @param host The host.
49  * @param port The port.
50  * @param path The path.
51  * @param query The query.
52  * @param fragment The fragment.
53  */
54  URI( const std::string &scheme,
55  const std::string &userinfo,
56  const std::string &host,
57  const std::string &port,
58  const std::string &path,
59  const std::string &query,
60  const std::string &fragment ) :
61  scheme_(scheme),
62  userinfo_(userinfo),
63  host_(host),
64  port_(port),
65  path_(path),
66  query_(query),
67  fragment_(fragment) {};
68 
69  /**
70  * Copy constructor.
71  * @param uri The URI to copy.
72  */
73  URI( const URI &uri ) { *this = uri; };
74 
75  /**
76  * Desctructor.
77  */
78  ~URI() {};
79 
80  /**
81  * Parse the string as an URI. Rteurn false and set idxfail to the failing char
82  * if the pasre fails, true if the pasre succeeds.
83  * @param s The string to parse
84  * @param idxfail If parse fails, the index of the failing character
85  * @return true if the pasre was successful.
86  */
87  bool parse( const std::string &s, size_t &idxfail );
88 
89  /**
90  * Return the URI as a std::string.
91  * @return the std::string representation of the URI.
92  */
93  std::string asString() const;
94 
95  /**
96  * Return the URI scheme std::string.
97  * @return the scheme of the URI.
98  */
99  std::string getScheme() const { return scheme_; };
100 
101  /**
102  * Set the URI scheme.
103  * @param scheme The scheme to set.
104  */
105  void setScheme( const std::string &scheme ) { scheme_ = scheme; };
106 
107  /**
108  * Return the user-info std::string.
109  * @return the user-info of the URI.
110  */
111  std::string getUserInfo() const { return userinfo_; };
112 
113  /**
114  * Set the URI user-info.
115  * @param userinfo The user-info to set.
116  */
117  void setUserInfo( const std::string &userinfo ) { userinfo_ = userinfo; };
118 
119  /**
120  * Return the host std::string.
121  * @return the host of the URI.
122  */
123  std::string getHost() const { return host_; };
124 
125  /**
126  * Set the URI host.
127  * @param host The host to set.
128  */
129  void setHost( const std::string &host ) { host_ = host; };
130 
131  /**
132  * Return the port std::string.
133  * @return the port of the URI.
134  */
135  std::string getPort() const { return port_; };
136 
137  /**
138  * Set the URI port.
139  * @param port The port to set.
140  */
141  void setPort( const std::string &port ) { port_ = port; };
142 
143  /**
144  * Return the path std::string.
145  * @return the path of the URI.
146  */
147  std::string getPath() const { return path_; };
148 
149  /**
150  * Set the URI path.
151  * @param path The path to set.
152  */
153  void setPath( const std::string &path ) { path_ = path; };
154 
155  /**
156  * Return the query std::string.
157  * @return the query of the URI.
158  */
159  std::string getQuery() const { return query_; };
160 
161  /**
162  * Set the URI query.
163  * @param query The query to set.
164  */
165  void setQuery( const std::string &query ) { query_ = query; };
166 
167  /**
168  * Return the fragment std::string.
169  * @return the fragment of the URI.
170  */
171  std::string getFragment() const { return fragment_; };
172 
173  /**
174  * Set the URI fragment.
175  * @param fragment The fragment to set.
176  */
177  void setFragment( const std::string &fragment ) { fragment_ = fragment; };
178 
179  /**
180  * Assignment operator.
181  * @param uri The URI to assign.
182  * @return a reference to this URI.
183  */
184  URI& operator=( const URI& uri ) {
185  scheme_ = uri.scheme_;
186  userinfo_ = uri.userinfo_;
187  host_ = uri.host_;
188  port_ = uri.port_;
189  path_ = uri.path_;
190  query_ = uri.query_;
191  fragment_ = uri.fragment_;
192  return *this;
193  }
194 
195  /**
196  * Equality operator.
197  * @param uri The URI to compare to.
198  * @return true if the URIs are equal.
199  */
200  bool operator==( const URI& uri ) {
201  return scheme_ == uri.scheme_ &&
202  userinfo_ == uri.userinfo_ &&
203  host_ == uri.host_ &&
204  port_ == uri.port_ &&
205  path_ == uri.path_ &&
206  query_ == uri.query_ &&
207  fragment_ == uri.fragment_;
208  }
209 
210  /**
211  * Inequality operator.
212  * @param uri The URI to compare to.
213  * @return true if the URIs are unequal.
214  */
215  bool operator!=( const URI& uri ) {
216  return scheme_ != uri.scheme_ ||
217  userinfo_ != uri.userinfo_ ||
218  host_ != uri.host_ ||
219  port_ != uri.port_ ||
220  path_ != uri.path_ ||
221  query_ != uri.query_ ||
222  fragment_ != uri.fragment_;
223  }
224 
225  /**
226  * Ordering operator.
227  * @param uri The URI to compare to.
228  * @return true if this URI has lower order.
229  */
230  bool operator<( const URI& uri ) {
231  return scheme_ < uri.scheme_ ||
232  ( scheme_ == uri.scheme_ && userinfo_ < uri.userinfo_ ) ||
233  ( scheme_ == uri.scheme_ && userinfo_ == uri.userinfo_ && host_ < uri.host_ ) ||
234  ( scheme_ == uri.scheme_ && userinfo_ == uri.userinfo_ && host_ == uri.host_ && port_ < uri.port_ ) ||
235  ( scheme_ == uri.scheme_ && userinfo_ == uri.userinfo_ && host_ == uri.host_ && port_ == uri.port_ && path_ < uri.path_ ) ||
236  ( scheme_ == uri.scheme_ && userinfo_ == uri.userinfo_ && host_ == uri.host_ && port_ == uri.port_ && path_ == uri.path_ && query_ < uri.query_ ) ||
237  ( scheme_ == uri.scheme_ && userinfo_ == uri.userinfo_ && host_ == uri.host_ && port_ == uri.port_ && path_ == uri.path_ && query_ == uri.query_ && fragment_ < uri.fragment_ );
238  }
239 
240  private:
241 
242  /** States for the parsingg automaton. */
243  enum ParseState {
244  psSchemeStart,
245  psSchemeEnd,
246  psAuthorityStart,
247  psUserInfoEnd,
248  psHostStart,
249  psHostEnd,
250  psPortStart,
251  psPortEnd,
252  psPathStart,
253  psPathEnd,
254  psQueryStart,
255  psQueryEnd,
256  psFragmentStart,
257  psTCP6Start,
258  psPCTEncoded,
259 
260  psError,
261  psDone
262  };
263 
264  /**
265  * Check if c is a valid Octet
266  * @param c The char to check.
267  * @return true if the char is ok.
268  */
269  bool verifyOctetChar( char c );
270 
271  /**
272  * Check if c is a valid Scheme char
273  * @param c The char to check.
274  * @return true if the char is ok.
275  */
276  bool verifySchemeChar( char c );
277 
278  /**
279  * Check if c is a valid UserInfo or host char
280  * @param c The char to check.
281  * @return true if the char is ok.
282  */
283  bool verifyUserInfoHostChar( char c );
284 
285  /**
286  * Check if c is a valid Path char
287  * @param c The char to check.
288  * @return true if the char is ok.
289  */
290  bool verifyPathChar( char c );
291 
292  /**
293  * Check if c is a valid Fragment char
294  * @param c The char to check.
295  * @return true if the char is ok.
296  */
297  bool verifyQueryFragmentChar( char c );
298 
299  /**
300  * Check if c is a valid TCP6 address char
301  * @param c The char to check.
302  * @return true if the char is ok.
303  */
304  bool verifyTCP6Char( char c );
305 
306  /**
307  * Reste all data to empty strings.
308  */
309  void reset();
310 
311  /** The scheme */
312  std::string scheme_;
313  /** The userinfo */
314  std::string userinfo_;
315  /** The host */
316  std::string host_;
317  /** The port */
318  std::string port_;
319  /** The path */
320  std::string path_;
321  /** The query */
322  std::string query_;
323  /** The fragment */
324  std::string fragment_;
325  };
326 
327  }
328 
329 }
330 
331 #endif
dodo::network::URI::operator=
URI & operator=(const URI &uri)
Assignment operator.
Definition: uri.hpp:184
dodo::network::URI::verifyPathChar
bool verifyPathChar(char c)
Check if c is a valid Path char.
Definition: uri.cpp:94
dodo::network::URI::setScheme
void setScheme(const std::string &scheme)
Set the URI scheme.
Definition: uri.hpp:105
dodo::network::URI::operator<
bool operator<(const URI &uri)
Ordering operator.
Definition: uri.hpp:230
dodo::network::URI::operator!=
bool operator!=(const URI &uri)
Inequality operator.
Definition: uri.hpp:215
dodo::network::URI::asString
std::string asString() const
Return the URI as a std::string.
Definition: uri.cpp:43
dodo::network::URI::getUserInfo
std::string getUserInfo() const
Return the user-info std::string.
Definition: uri.hpp:111
dodo::network::URI::ParseState
ParseState
States for the parsingg automaton.
Definition: uri.hpp:243
dodo::network::URI::userinfo_
std::string userinfo_
The userinfo.
Definition: uri.hpp:314
dodo::network::URI::URI
URI()
Construct an empty URI.
Definition: uri.hpp:42
dodo::network::URI::verifyQueryFragmentChar
bool verifyQueryFragmentChar(char c)
Check if c is a valid Fragment char.
Definition: uri.cpp:101
dodo::network::URI::getScheme
std::string getScheme() const
Return the URI scheme std::string.
Definition: uri.hpp:99
dodo::network::URI
Uniform Resource Identifier.
Definition: uri.hpp:36
dodo::network::URI::setPath
void setPath(const std::string &path)
Set the URI path.
Definition: uri.hpp:153
dodo::network::URI::getPath
std::string getPath() const
Return the path std::string.
Definition: uri.hpp:147
dodo::network::URI::path_
std::string path_
The path.
Definition: uri.hpp:320
dodo::network::URI::setPort
void setPort(const std::string &port)
Set the URI port.
Definition: uri.hpp:141
dodo::network::URI::reset
void reset()
Reste all data to empty strings.
Definition: uri.cpp:33
dodo::network::URI::verifyUserInfoHostChar
bool verifyUserInfoHostChar(char c)
Check if c is a valid UserInfo or host char.
Definition: uri.cpp:79
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::URI::host_
std::string host_
The host.
Definition: uri.hpp:316
dodo::network::URI::port_
std::string port_
The port.
Definition: uri.hpp:318
dodo::network::URI::parse
bool parse(const std::string &s, size_t &idxfail)
Parse the string as an URI.
Definition: uri.cpp:109
dodo::network::URI::getPort
std::string getPort() const
Return the port std::string.
Definition: uri.hpp:135
dodo::network::URI::fragment_
std::string fragment_
The fragment.
Definition: uri.hpp:324
dodo::network::URI::scheme_
std::string scheme_
The scheme.
Definition: uri.hpp:312
dodo::network::URI::query_
std::string query_
The query.
Definition: uri.hpp:322
dodo::network::URI::setFragment
void setFragment(const std::string &fragment)
Set the URI fragment.
Definition: uri.hpp:177
dodo::network::URI::getQuery
std::string getQuery() const
Return the query std::string.
Definition: uri.hpp:159
dodo::network::URI::URI
URI(const std::string &scheme, const std::string &userinfo, const std::string &host, const std::string &port, const std::string &path, const std::string &query, const std::string &fragment)
Construct an URI from iits components.
Definition: uri.hpp:54
dodo::network::URI::setQuery
void setQuery(const std::string &query)
Set the URI query.
Definition: uri.hpp:165
dodo::network::URI::verifyTCP6Char
bool verifyTCP6Char(char c)
Check if c is a valid TCP6 address char.
Definition: uri.cpp:73
dodo::network::URI::~URI
~URI()
Desctructor.
Definition: uri.hpp:78
dodo::network::URI::verifySchemeChar
bool verifySchemeChar(char c)
Check if c is a valid Scheme char.
Definition: uri.cpp:56
dodo::network::URI::getFragment
std::string getFragment() const
Return the fragment std::string.
Definition: uri.hpp:171
dodo::network::URI::getHost
std::string getHost() const
Return the host std::string.
Definition: uri.hpp:123
dodo::network::URI::setUserInfo
void setUserInfo(const std::string &userinfo)
Set the URI user-info.
Definition: uri.hpp:117
dodo::network::URI::setHost
void setHost(const std::string &host)
Set the URI host.
Definition: uri.hpp:129
dodo::network::URI::verifyOctetChar
bool verifyOctetChar(char c)
Check if c is a valid Octet.
Definition: uri.cpp:64
dodo::network::URI::URI
URI(const URI &uri)
Copy constructor.
Definition: uri.hpp:73
dodo::network::URI::operator==
bool operator==(const URI &uri)
Equality operator.
Definition: uri.hpp:200