GCS  0.2.3
gu_uri.hpp
1 /*
2  * Copyright (C) 2009-2012 Codership Oy <info@codership.com>
3  *
4  * $Id$
5  */
16 #ifndef __GU_URI_HPP__
17 #define __GU_URI_HPP__
18 
19 #include <string>
20 #include <map>
21 #include <list>
22 
23 #include "gu_utils.hpp"
24 #include "gu_regex.hpp"
25 
26 namespace gu
27 {
36  typedef std::multimap<std::string, std::string> URIQueryList;
37 
41  class URI
42  {
43  public:
47  class Authority
48  {
49  public:
50 
57  const std::string& user() const
58  {
59  return user_.str();
60  }
61 
68  const std::string& host() const
69  {
70  return host_.str();
71  }
72 
79  const std::string& port() const
80  {
81  return port_.str();
82  }
83  private:
84  friend class gu::URI;
85  Authority() : user_(), host_(), port_() { }
86  RegEx::Match user_;
87  RegEx::Match host_;
88  RegEx::Match port_;
89  };
90 
91  typedef std::vector<Authority> AuthorityList;
92 
102  URI (const std::string&, bool strict = true);
103 
108  const std::string& to_string() const
109  {
110  if (modified_) recompose();
111  return str_;
112  }
113 
120  const std::string& get_scheme() const
121  {
122  return scheme_.str();
123  }
124 
131  std::string get_authority() const;
132 
139  const std::string& get_user() const
140  {
141  if (authority_.empty())
142  throw NotSet();
143  return authority_.front().user();
144  }
145 
152  const std::string& get_host() const
153  {
154  if (authority_.empty())
155  throw NotSet();
156  return authority_.front().host();
157  }
158 
165  const std::string& get_port() const
166  {
167  if (authority_.empty())
168  throw NotSet();
169  return authority_.front().port();
170  }
171 
177  const AuthorityList& get_authority_list() const
178  {
179  return authority_;
180  }
181 
187  const std::string& get_path() const
188  {
189  return path_.str();
190  }
191 
198  const std::string& get_fragment() const
199  {
200  return fragment_.str();
201  }
202 
206  void set_query_param(const std::string&, const std::string&,
207  bool override);
208  void set_option(const std::string& key, const std::string& val)
209  {
210  set_query_param(key, val, true);
211  }
212  void append_option(const std::string& key, const std::string& val)
213  {
214  set_query_param(key, val, false);
215  }
219  const URIQueryList& get_query_list() const { return query_list_; }
220 
225  const std::string& get_option(const std::string&) const;
226 
227  const std::string& get_option(const std::string& opt,
228  const std::string& def) const
229  {
230  try { return get_option(opt); }
231  catch (NotFound& ) { return def ; }
232  }
233 
234  private:
235  bool modified_;
236  mutable std::string str_;
238  RegEx::Match scheme_;
239  AuthorityList authority_;
240  RegEx::Match path_;
241  RegEx::Match fragment_;
242  URIQueryList query_list_;
247  void parse (const std::string& s, bool strict);
248 
252  void recompose() const;
253 
255  std::string get_authority(const Authority&) const;
256 
257  static const char* const uri_regex_;
258  static RegEx const regex_;
259  };
260 
261  inline std::ostream& operator<<(std::ostream& os, const URI& uri)
262  {
263  os << uri.to_string();
264  return os;
265  }
266 }
267 
268 #endif /* __GU_URI_HPP__ */
std::multimap< std::string, std::string > URIQueryList
URIQueryList.
Definition: gu_uri.hpp:36
Utility class to parse URIs.
Definition: gu_uri.hpp:41
Definition: gu_regex.hpp:50
const AuthorityList & get_authority_list() const
Get authority list.
Definition: gu_uri.hpp:177
const std::string & get_path() const
Get URI path.
Definition: gu_uri.hpp:187
void set_query_param(const std::string &, const std::string &, bool override)
Add query param to URI.
const std::string & user() const
Get "user" part of authority.
Definition: gu_uri.hpp:57
const std::string & get_user() const
Get "user" part of the first entry in authority list.
Definition: gu_uri.hpp:139
const std::string & get_port() const
Get "port" part of the first entry in authority list.
Definition: gu_uri.hpp:165
const std::string & to_string() const
Get URI string.
Definition: gu_uri.hpp:108
const std::string & get_option(const std::string &) const
return opton by name,
const std::string & get_host() const
Get "host" part of the first entry in authority list.
Definition: gu_uri.hpp:152
URI(const std::string &, bool strict=true)
Construct URI from string.
const std::string & get_scheme() const
Get URI scheme.
Definition: gu_uri.hpp:120
const URIQueryList & get_query_list() const
Get URI query list.
Definition: gu_uri.hpp:219
std::string get_authority() const
Get URI authority component.
Definition: gu_uri.hpp:47
const std::string & host() const
Get "host" part of authority.
Definition: gu_uri.hpp:68
const std::string & get_fragment() const
Get URI path.
Definition: gu_uri.hpp:198
Definition: gu_exception.hpp:16
const std::string & port() const
Get "port" part of authority.
Definition: gu_uri.hpp:79