All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
authority.cpp
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 #include "authority.h"
23 #include "ip_address.h"
24 #include "ipv6_address.h"
25 #include "domain_name.h"
26 #include <sstream>
27 #include <stdexcept>
28 #include <cstdlib>
29 #include <cstdio>
30 
31 namespace te
32 {
33  namespace common
34  {
35  namespace uri
36  {
37  std::map<std::string, unsigned short> authority::protocol2DefaultPort_; // Added by Lauro
38 
39  const char authority::IP_LITERAL_BEGIN_CHAR = '[';
40  const char authority::IP_LITERAL_END_CHAR = ']';
41  const char authority::PORT_SEPARATOR_CHAR = ':';
43  authority::authority(const std::string& v) : port_(0) {
45  std::string::const_iterator first = v.begin();
46  if (!parse(first, v.end(), *this) || first != v.end())
47  throw std::invalid_argument("invalid URI authority: \"" + v + "\"");
48  }
50  if(protocol2DefaultPort_.empty()) {
51  char buf[1000];
52  unsigned short v, i, ii;
53  std::string s, protocol;
54  FILE* fp = fopen(TE_PROTOCOL_DEFAULT_PORTS_FILE, "r"); // path to be used in release mode
55  if(fp == (FILE*)null) // path to be used in debugger mode
56  fp = fopen("../../../../src/terralib/common/URI/protocolPorts.txt", "r");
57 
58  while(fgets(buf, 1000, fp) != (char*)null) {
59  s = buf;
60  if(s.find("Port Assignments:") != std::string::npos)
61  break;
62  }
63 
64  while(fgets(buf, 1000, fp) != (char*)null)
65  {
66  if(buf[0] == '#' || buf[0] == '\n' || buf[0] == ' ' || buf[0] == '\t')
67  continue;
68  s = buf;
69  if(s.find("/") == std::string::npos)
70  continue;
71 
72  i = 0;
73  while(!(s[i] == ' ' || s[i] == '\t'))
74  i++;
75  protocol = s.substr(0, i);
76  while(s[i] == ' ' || s[i] == '\t')
77  i++;
78  ii = i;
79  while(s[i] != '/')
80  i++;
81  v = (unsigned short)atoi(s.substr(ii, i-ii).c_str());
82  protocol2DefaultPort_[protocol] = v;
83  }
84  fclose(fp);
85  }
86  }
87  void authority::setDefaultPort(const std::string& scheme) {
88  if(protocol2DefaultPort_.find(scheme) == protocol2DefaultPort_.end())
89  port_ = 0;
90  else
91  port_ = protocol2DefaultPort_[scheme];
92  }
93  int authority::getDefaultPort(const std::string& scheme) {
94  if(protocol2DefaultPort_.find(scheme) == protocol2DefaultPort_.end())
95  return 0;
96  else
97  return protocol2DefaultPort_[scheme];
98  }
99  void authority::setLogin(const std::string& login) {
100  login_ = login;
101  }
102  std::string authority::getLogin() const{ // Lauro
103  return login_;
104  }
105  std::string authority::getEncodedLogin() const{ // Lauro
106  std::string s;
107  std::string::const_iterator it = login_.begin();
108  while(it != login_.end())
109  {
110  char c = *it;
111  if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
112  s += c;
113  else
114  encodeAndConcate(it, s);
115  it++;
116  }
117  return s;
118  }
119  void authority::setPassword(const std::string& password) { // Lauro
120  password_ = password;
121  }
122  std::string authority::getPassword() const{ // Lauro
123  return password_;
124  }
125  std::string authority::getEncodedPassword() const{ // Lauro
126  std::string s;
127  std::string::const_iterator it = password_.begin();
128  while(it != password_.end())
129  {
130  char c = *it;
131  if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
132  s += c;
133  else
134  encodeAndConcate(it, s);
135  it++;
136  }
137  return s;
138  }
139  std::string authority::string() const { // Lauro
140  std::ostringstream oss;
141  operator <<(oss);
142  return oss.str();
143  }
144  std::ostream& authority::operator <<(std::ostream& os) const { // Lauro
145  if(login_.empty() == false)
146  os << login_;
147  if(password_.empty() == false)
148  os << ":" << password_;
149  if(login_.empty() == false)
150  os << "@";
151  os << host_;
152  if (port_)
153  os << PORT_SEPARATOR_CHAR << port_;
154  return os;
155  }
156  bool parse(std::string::const_iterator& first, std::string::const_iterator last, authority& v) {
157  parseLoginPassword(first, last, v);
158  std::string::const_iterator f = first;
160  // Just parse to get the end.
161  // Note string is not put in canonical form.
162  ip_address ipaddr;
163  if (parse(f, last, ipaddr)) // IP address.
165  else {
166  domain_name dn;
167  if (parse(f, last, dn)) // Domain name.
169  else {
170  if (f == last || *f != authority::IP_LITERAL_BEGIN_CHAR)
171  return false;
172  ++f;
173  ipv6_address ipv6addr;
174  if (!parse(f, last, ipv6addr)) // Not IP v6 address.
175  return false;
176  if (f == last || *f != authority::IP_LITERAL_END_CHAR)
177  return false;
179  ++f;
180  }
181  }
182  std::string host(first, f);
183  unsigned int port = 0;
184  if (f != last && *f == authority::PORT_SEPARATOR_CHAR) {
185  ++f;
186  std::string::const_iterator anchor = f;
187  for (; f != last; ++f) {
188  char c = *f;
189  if (c < '0' || '9' < c)
190  break;
191  else {
192  port = port * 10 + (c - '0');
193  if (65535 < port)
194  return false; // Port out of range.
195  }
196  }
197  if (f == anchor)
198  return false; // Separator but no digits.
199  }
200  v.host_type_ = e;
201  v.host_ = host;
202  v.port_ = (unsigned short)port;
203  first = f;
204  return true;
205  }
206 
207  bool parseLoginPassword(std::string::const_iterator& first, std::string::const_iterator last, authority& v) { // Lauro
208  std::string::const_iterator f = first;
209  std::string login, password;
210 
211  while(f != last && *f != '/')
212  {
213  if(*f == '@')
214  break;
215  f++;
216  }
217  if(!(f == last || *f == '/'))
218  {
219  f = first;
220  while(*f != '@')
221  {
222  if(*f != ':')
223  {
224  if(*f != '%')
225  login += *f;
226  else
227  {
228  if(decodeAndConcate(f, login) == false)
229  return false;
230  }
231  }
232  else
233  {
234  f++;
235  while(*f != '@')
236  {
237  if(*f != '%')
238  password += *f;
239  else
240  {
241  if(decodeAndConcate(f, password) == false)
242  return false;
243  }
244  f++;
245  }
246  f++;
247  first = f;
248  break;
249  }
250  f++;
251  }
252 
253  if(login.empty() == false)
254  v.setLogin(login);
255  if(password.empty() == false)
256  v.setPassword(password);
257  }
258  return true;
259  }
260 
261  bool decodeAndConcate(std::string::const_iterator& f, std::string& s) // Lauro
262  {
263  char c, c1, c2;
264  f++;
265  if(*f >= '0' && *f <= '9')
266  c1 = *f - 0x30;
267  else if(*f >= 'a' && *f <= 'f')
268  c1 = *f - 0x57;
269  else if(*f >= 'A' && *f <= 'F')
270  c1 = *f - 0x37;
271  else
272  return false;
273  f++;
274  if(*f >= '0' && *f <= '9')
275  c2 = *f - 0x30;
276  else if(*f >= 'a' && *f <= 'f')
277  c2 = *f - 0x57;
278  else if(*f >= 'A' && *f <= 'F')
279  c2 = *f - 0x37;
280  else
281  return false;
282  c = (c1 << 4) | c2;
283  s += c;
284  return true;
285  }
286 
287  bool encodeAndConcate(std::string::const_iterator& f, std::string& s) // Lauro
288  {
289  char buf[3];
290  unsigned char c = *f;
291  s += '%';
292  sprintf(buf, "%x", c);
293  buf[2] = 0;
294  s += buf;
295  return true;
296  }
297  }
298  }
299 }
bool parseLoginPassword(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Definition: authority.cpp:207
std::string string() const
Calculate string.
Definition: authority.cpp:139
void setDefaultPort(const std::string &scheme)
Set Default port for the protocol. Added by Lauro.
Definition: authority.cpp:87
void setLogin(const std::string &login)
Set login. Added by Lauro.
Definition: authority.cpp:99
friend 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...
Definition: authority.cpp:156
Uniform Resource Identifier (URI) reference.
Definition: uri.h:110
bool 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...
Definition: authority.cpp:156
URI authority component (without userinfo).
Definition: authority.h:49
#define TE_PROTOCOL_DEFAULT_PORTS_FILE
Definition: Config.h:36
URI scheme component.
Definition: scheme.h:40
std::string getLogin() const
Get login. Added by Lauro.
Definition: authority.cpp:102
int getDefaultPort(const std::string &scheme)
Get Default port for the protocol. Added by Lauro.
Definition: authority.cpp:93
bool encodeAndConcate(std::string::const_iterator &f, std::string &s)
Definition: authority.cpp:287
std::string getEncodedLogin() const
Get endoded login. Added by Lauro.
Definition: authority.cpp:105
static const char IP_LITERAL_BEGIN_CHAR
IP literal begin ('[')
Definition: authority.h:79
void setPassword(const std::string &password)
Set password. Added by Lauro.
Definition: authority.cpp:119
unsigned short port_
Definition: authority.h:87
static std::map< std::string, unsigned short > protocol2DefaultPort_
Protocol to default port. Added by Lauro.
Definition: authority.h:92
std::string getEncodedPassword() const
Get encoded password. Added by Lauro.
Definition: authority.cpp:125
host_type_e
Enumeration.
Definition: authority.h:52
static const char IP_LITERAL_END_CHAR
IP literal end (']')
Definition: authority.h:80
std::string getPassword() const
Get password. Added by Lauro.
Definition: authority.cpp:122
std::ostream & operator<<(std::ostream &os) const
Stream out.
Definition: authority.cpp:144
static const char PORT_SEPARATOR_CHAR
port separator (':')
Definition: authority.h:81
bool decodeAndConcate(std::string::const_iterator &f, std::string &s)
Definition: authority.cpp:261