authority.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009 zooml.com
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #ifndef __TERRALIB_COMMON_INTERNAL_AUTHORITY_H
23 #define __TERRALIB_COMMON_INTERNAL_AUTHORITY_H
24 #include "../Config.h"
25 #include <string>
26 #include <map> // Added by Lauro
27 #include <iostream>
28 namespace te
29 {
30  namespace common
31  {
32  namespace uri
33  {
34  /** \brief URI authority component (without userinfo).
35  *
36  * The authority string and the type of the authority is stored in this
37  * object. The syntax is checked with the corresponding
38  * type. For example if this indicates \c DOMAIN_NAME then
39  * the string can be parsed as a \c domain_name object.
40  *
41  * Syntax: This does not support userinfo nor IPvFuture.
42  * Requires domain-name instead of reg-name.
43  * <pre>
44  * authority = host [ ":" port ]
45  * host = IP-literal | IPv4address | domain-name
46  * IP-literal = "[" IPv6address "]"
47  * port = *DIGIT
48  * </pre> */
50  public:
51  /// Enumeration.
52  enum host_type_e {
54  DOMAIN_NAME, ///< domain name
55  IP_ADDRESS, ///< IP address
56  IPV6_ADDRESS ///< IP v6 address
57  };
58  authority(); ///< Construct.
59  /// Construct from string. This test to be sure host
60  /// is of a valid form (see class description).
61  /// @exception std::invalid_argument if invalid or empty
62  authority(const std::string& v);
63  bool empty() const {return is_null();} ///< Test if null/empty.
64  bool is_null() const {return host_.empty();} ///< Test if null/empty.
65  const std::string& host() const {return host_;} ///< Get host.
66  host_type_e host_type() const {return host_type_;} ///< Get host type.
67  unsigned short port() const {return port_;} ///< Get port (0 if none).
68  void port(unsigned short p) {port_ = p;} ///< Set port. Added by Lauro
69  void setDefaultPort(const std::string& scheme);///< Set Default port for the protocol. Added by Lauro
70  int getDefaultPort(const std::string& scheme);///< Get Default port for the protocol. Added by Lauro
71  void setLogin(const std::string& login);///< Set login. Added by Lauro
72  std::string getLogin() const;///< Get login. Added by Lauro
73  std::string getEncodedLogin() const;///< Get endoded login. Added by Lauro
74  void setPassword(const std::string& password);///< Set password. Added by Lauro
75  std::string getPassword() const;///< Get password. Added by Lauro
76  std::string getEncodedPassword() const;///< Get encoded password. Added by Lauro
77  std::string string() const; ///< Calculate string.
78  std::ostream& operator <<(std::ostream& os) const; ///< Stream out.
79  static const char IP_LITERAL_BEGIN_CHAR; ///< IP literal begin ('[')
80  static const char IP_LITERAL_END_CHAR; ///< IP literal end (']')
81  static const char PORT_SEPARATOR_CHAR; ///< port separator (':')
82  private:
83  friend bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, authority& v);
84  void loadDefaultPorts(); // Added by Lauro
86  std::string host_;
87  unsigned short port_;
88  std::string login_;
89  std::string password_;
90 
91  /// Protocol to default port. Added by Lauro
92  static std::map<std::string, unsigned short> protocol2DefaultPort_;
93  };
94  /// Stream out URI authority.
95  inline std::ostream& operator <<(std::ostream& os, const authority& v) {return v.operator <<(os);}
96  /// Parse URI authority, returning whether found or not and advancing
97  /// first and setting authority if found. Does not skip leading space.
98  bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, authority& v);
99  bool parseLoginPassword(std::string::const_iterator& first, std::string::const_iterator last, authority& v); // Added by Lauro
100  bool decodeAndConcate(std::string::const_iterator& it, std::string& s); // Added by Lauro
101  bool encodeAndConcate(std::string::const_iterator& it, std::string& s); // Added by Lauro
102  }
103  }
104 }
105 #endif
bool empty() const
Test if null/empty.
Definition: authority.h:63
bool parseLoginPassword(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
void port(unsigned short p)
Set port. Added by Lauro.
Definition: authority.h:68
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
bool is_null() const
Test if null/empty.
Definition: authority.h:64
Uniform Resource Identifier (URI) reference.
Definition: uri.h:110
bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Parse URI authority, returning whether found or not and advancing first and setting authority if foun...
URI authority component (without userinfo).
Definition: authority.h:49
static std::map< std::string, unsigned short > protocol2DefaultPort_
Protocol to default port. Added by Lauro.
Definition: authority.h:92
URI scheme component.
Definition: scheme.h:40
bool encodeAndConcate(std::string::const_iterator &it, std::string &s)
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
static const char IP_LITERAL_BEGIN_CHAR
IP literal begin ('[')
Definition: authority.h:79
bool decodeAndConcate(std::string::const_iterator &it, std::string &s)
unsigned short port_
Definition: authority.h:87
unsigned short port() const
Get port (0 if none).
Definition: authority.h:67
host_type_e
Enumeration.
Definition: authority.h:52
static const char IP_LITERAL_END_CHAR
IP literal end (']')
Definition: authority.h:80
const std::string & host() const
Get host.
Definition: authority.h:65
static const char PORT_SEPARATOR_CHAR
port separator (':')
Definition: authority.h:81
host_type_e host_type() const
Get host type.
Definition: authority.h:66