ARC SDK
URL.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_URL_H__
4 #define __ARC_URL_H__
5 
6 #include <iostream>
7 #include <list>
8 #include <map>
9 #include <string>
10 
11 
12 // Default ports for different protocols
13 #define RC_DEFAULT_PORT 389
14 #define HTTP_DEFAULT_PORT 80
15 #define HTTPS_DEFAULT_PORT 443
16 #define HTTPG_DEFAULT_PORT 8443
17 #define LDAP_DEFAULT_PORT 389
18 #define FTP_DEFAULT_PORT 21
19 #define GSIFTP_DEFAULT_PORT 2811
20 #define LFC_DEFAULT_PORT 5010
21 #define XROOTD_DEFAULT_PORT 1094
22 #define S3_DEFAULT_PORT 80
23 #define S3_HTTPS_DEFAULT_PORT 443
24 
25 
26 namespace Arc {
27 
28  class URLLocation;
29 
31 
88  class URL {
89 
90  public:
92  URL();
93 
95 
102  URL(const std::string& url, bool encoded = false, int defaultPort = -1, const std::string& defaultPath = "");
103 
105  virtual ~URL();
106 
108  enum Scope {
109  base, onelevel, subtree
110  };
111 
113 
116  void URIDecode(void);
117 
119  const std::string& Protocol() const;
120 
122  void ChangeProtocol(const std::string& newprot);
123 
125  const std::string& Username() const;
126 
128  const std::string& Passwd() const;
129 
131  const std::string& Host() const;
132 
134  void ChangeHost(const std::string& newhost);
135 
137  int Port() const;
138 
140  void ChangePort(int newport);
141 
143  const std::string& Path() const;
144 
146  std::string FullPath() const;
147 
149 
151  std::string FullPathURIEncoded() const;
152 
154  void ChangePath(const std::string& newpath);
155 
157  void ChangeFullPath(const std::string& newpath, bool encoded = false);
158 
160  void ChangeURL(const std::string& newurl, bool encoded = false);
161 
163  const std::map<std::string, std::string>& HTTPOptions() const;
164 
166 
170  const std::string& HTTPOption(const std::string& option,
171  const std::string& undefined = "") const;
172 
174 
176  bool AddHTTPOption(const std::string& option, const std::string& value,
177  bool overwrite = true);
178 
180 
181  void RemoveHTTPOption(const std::string& option);
182 
184  const std::list<std::string>& LDAPAttributes() const;
185 
187  void AddLDAPAttribute(const std::string& attribute);
188 
190  Scope LDAPScope() const;
191 
193  void ChangeLDAPScope(const Scope newscope);
194 
196  const std::string& LDAPFilter() const;
197 
199  void ChangeLDAPFilter(const std::string& newfilter);
200 
202  const std::map<std::string, std::string>& Options() const;
203 
205 
209  const std::string& Option(const std::string& option,
210  const std::string& undefined = "") const;
211 
213  const std::map<std::string, std::string>& MetaDataOptions() const;
214 
216 
220  const std::string& MetaDataOption(const std::string& option,
221  const std::string& undefined = "") const;
222 
224 
230  bool AddOption(const std::string& option, const std::string& value,
231  bool overwrite = true);
232 
234 
237  bool AddOption(const std::string& option, bool overwrite = true);
238 
240  void AddMetaDataOption(const std::string& option, const std::string& value,
241  bool overwrite = true);
242 
244  void AddLocation(const URLLocation& location);
245 
247  const std::list<URLLocation>& Locations() const;
248 
250  const std::map<std::string, std::string>& CommonLocOptions() const;
251 
253 
257  const std::string& CommonLocOption(const std::string& option,
258  const std::string&
259  undefined = "") const;
260 
262 
263  void RemoveOption(const std::string& option);
264 
266 
267  void RemoveMetaDataOption(const std::string& option);
268 
270  virtual std::string str(bool encode = false) const;
271 
273  virtual std::string plainstr(bool encode = false) const;
274 
276  virtual std::string fullstr(bool encode = false) const;
277 
279  virtual std::string ConnectionURL() const;
280 
282  bool operator<(const URL& url) const;
283 
285  bool operator==(const URL& url) const;
286 
288  operator bool() const;
290  bool operator!() const;
291 
293  bool StringMatches(const std::string& str) const;
294 
295 
297  std::map<std::string, std::string> ParseOptions(const std::string& optstring,
298  char separator, bool encoded = false);
299 
301 
304  static std::string OptionString(const std::map<std::string,
305  std::string>& options, char separator, bool encode = false);
306 
308 
309  static std::string URIEncode(const std::string& str);
310 
312 
313  static std::string URIDecode(const std::string& str);
314 
315  protected:
317  std::string protocol;
318 
320  std::string username;
321 
323  std::string passwd;
324 
326  std::string host;
327 
329  bool ip6addr;
330 
332  int port;
333 
335  std::string path;
336 
338  std::map<std::string, std::string> httpoptions;
339 
341  std::map<std::string, std::string> metadataoptions;
342 
344  std::list<std::string> ldapattributes;
345 
348 
350  std::string ldapfilter;
351 
353  std::map<std::string, std::string> urloptions;
354 
356  std::list<URLLocation> locations;
357 
359  std::map<std::string, std::string> commonlocoptions;
360 
362  bool valid;
363 
365  static std::string BaseDN2Path(const std::string&);
366 
368  static std::string Path2BaseDN(const std::string&);
369 
371  friend std::ostream& operator<<(std::ostream& out, const URL& u);
372 
374  void ParsePath(bool encoded = false);
375  };
376 
377 
379 
383  : public URL {
384 
385  public:
387  URLLocation(const std::string& url = "");
388 
390  URLLocation(const std::string& url, const std::string& name);
391 
393  URLLocation(const URL& url);
394 
396  URLLocation(const URL& url, const std::string& name);
397 
399  URLLocation(const std::map<std::string, std::string>& options,
400  const std::string& name);
401 
403  virtual ~URLLocation();
404 
406  const std::string& Name() const;
407 
409  virtual std::string str(bool encode = false) const;
410 
412  virtual std::string fullstr(bool encode = false) const;
413 
414  protected:
416  std::string name;
417  };
418 
419 
421 
423  class PathIterator {
424  public:
426 
428  PathIterator(const std::string& path, bool end = false);
429  ~PathIterator();
430 
433 
436 
438  operator bool() const;
439 
441  std::string operator*() const;
442 
444  std::string Rest() const;
445 
446  private:
447  const std::string& path;
448  std::string::size_type pos;
449  bool end;
450  bool done;
451  };
452 
453 
455  std::list<URL> ReadURLList(const URL& urllist);
456 
457 } // namespace Arc
458 
459 #endif // __ARC_URL_H__
const std::map< std::string, std::string > & Options() const
Returns URL options if any.
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
bool operator!() const
Check if instance does not hold valid URL.
std::string ldapfilter
LDAP filter of the url.
Definition: URL.h:350
virtual ~URL()
Empty destructor.
static std::string Path2BaseDN(const std::string &)
a private method that converts an ldap path to a basedn.
std::list< std::string > ldapattributes
LDAP attributes of the url.
Definition: URL.h:344
std::map< std::string, std::string > urloptions
options of the url.
Definition: URL.h:353
std::string host
hostname of the url.
Definition: URL.h:326
const std::map< std::string, std::string > & MetaDataOptions() const
Returns metadata options if any.
std::string Rest() const
Returns part of initial path from one after current till end.
std::string username
username of the url.
Definition: URL.h:320
const std::string & CommonLocOption(const std::string &option, const std::string &undefined="") const
Returns the value of a common location option.
Class to hold a resolved URL location.
Definition: URL.h:382
virtual ~URLLocation()
URLLocation destructor.
std::list< URL > ReadURLList(const URL &urllist)
Reads a list of URLs from a file.
int Port() const
Returns the port of the URL.
URL()
Empty constructor. URL object is invalid.
void AddLocation(const URLLocation &location)
Adds a Location.
std::map< std::string, std::string > metadataoptions
Meta data options.
Definition: URL.h:341
const std::string & Host() const
Returns the hostname of the URL.
void AddLDAPAttribute(const std::string &attribute)
Adds an LDAP attribute.
Class to iterate through elements of a path.
Definition: URL.h:423
void ChangeLDAPFilter(const std::string &newfilter)
Changes the LDAP filter.
void URIDecode(void)
Perform decoding of stored URL parts according to RFC 3986.
PathIterator(const std::string &path, bool end=false)
Constructor accepts path and stores it internally.
std::map< std::string, std::string > httpoptions
HTTP options of the url.
Definition: URL.h:338
virtual std::string str(bool encode=false) const
Returns a string representation of the URL including meta-options.
const std::string & HTTPOption(const std::string &option, const std::string &undefined="") const
Returns the value of an HTTP option.
std::string path
the url path.
Definition: URL.h:335
virtual std::string str(bool encode=false) const
Returns a string representation of the URLLocation.
void ParsePath(bool encoded=false)
Convenience method for splitting schema specific part into path and options.
void RemoveMetaDataOption(const std::string &option)
Remove a metadata option if exits.
void ChangePath(const std::string &newpath)
Changes the path of the URL.
PathIterator & operator++()
Advances iterator to point at next path element.
std::string protocol
the url protocol.
Definition: URL.h:317
std::map< std::string, std::string > ParseOptions(const std::string &optstring, char separator, bool encoded=false)
Parse a string of options separated by separator into an attribute->value map.
const std::string & MetaDataOption(const std::string &option, const std::string &undefined="") const
Returns the value of a metadata option.
const std::map< std::string, std::string > & CommonLocOptions() const
Returns the common location options if any.
Scope
Scope for LDAP URLs.
Definition: URL.h:108
Scope ldapscope
LDAP scope of the url.
Definition: URL.h:347
const std::string & Protocol() const
Returns the protocol of the URL.
bool ip6addr
if host is IPv6 numerical address notation.
Definition: URL.h:329
void AddMetaDataOption(const std::string &option, const std::string &value, bool overwrite=true)
Adds a metadata option.
bool AddOption(const std::string &option, const std::string &value, bool overwrite=true)
Adds a URL option with the given value.
const std::map< std::string, std::string > & HTTPOptions() const
Returns HTTP options if any.
void RemoveOption(const std::string &option)
Removes a URL option if exists.
void ChangeFullPath(const std::string &newpath, bool encoded=false)
Changes the path of the URL and all options attached.
const std::string & Name() const
Returns the URLLocation name.
bool operator<(const URL &url) const
Compares one URL to another.
const std::string & LDAPFilter() const
Returns the LDAP filter.
virtual std::string ConnectionURL() const
Returns a string representation with protocol, host and port only.
int port
portnumber of the url.
Definition: URL.h:332
std::string operator*() const
Returns part of initial path from first till and including current.
bool StringMatches(const std::string &str) const
Returns true if string matches url.
PathIterator & operator--()
Moves iterator to element before current.
const std::list< URLLocation > & Locations() const
Returns the locations if any.
std::list< URLLocation > locations
locations for index server URLs.
Definition: URL.h:356
Class to represent general URLs.
Definition: URL.h:88
std::string passwd
password of the url.
Definition: URL.h:323
void ChangeURL(const std::string &newurl, bool encoded=false)
Changes whole URL possibly taking into account current one if new URL is relative.
void ChangeLDAPScope(const Scope newscope)
Changes the LDAP scope.
void RemoveHTTPOption(const std::string &option)
Removes a HTTP option if exists.
bool AddHTTPOption(const std::string &option, const std::string &value, bool overwrite=true)
Adds a HTP option with the given value.
bool valid
flag to describe validity of URL
Definition: URL.h:362
static std::string URIEncode(const std::string &str)
Perform encoding according to RFC 3986.
URLLocation(const std::string &url="")
Creates a URLLocation from a string representation.
static std::string OptionString(const std::map< std::string, std::string > &options, char separator, bool encode=false)
Returns a string representation of the options given in the options map.
void ChangePort(int newport)
Changes the port of the URL.
friend std::ostream & operator<<(std::ostream &out, const URL &u)
Overloaded operator << to print a URL.
void ChangeHost(const std::string &newhost)
Changes the hostname of the URL.
virtual std::string fullstr(bool encode=false) const
Returns a string representation including options and locations.
const std::string & Passwd() const
Returns the password of the URL.
static std::string BaseDN2Path(const std::string &)
a private method that converts an ldap basedn to a path.
void ChangeProtocol(const std::string &newprot)
Changes the protocol of the URL.
bool operator==(const URL &url) const
Is one URL equal to another?
std::string name
the URLLocation name as registered in the indexing service.
Definition: URL.h:416
const std::string & Username() const
Returns the username of the URL.
std::string FullPathURIEncoded() const
Returns the path and all options, URI-encoded according to RFC 3986.
Scope LDAPScope() const
Returns the LDAP scope.
std::string FullPath() const
Returns the path of the URL with all options attached.
std::map< std::string, std::string > commonlocoptions
common location options for index server URLs.
Definition: URL.h:359
virtual std::string fullstr(bool encode=false) const
Returns a string representation including options and locations.
virtual std::string plainstr(bool encode=false) const
Returns a string representation of the URL without any options.
const std::string & Option(const std::string &option, const std::string &undefined="") const
Returns the value of a URL option.
const std::list< std::string > & LDAPAttributes() const
Returns the LDAP attributes if any.
const std::string & Path() const
Returns the path of the URL.