All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
URL.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/URL.cpp
22 
23  \brief A Uniform Resource Locator.
24 */
25 
26 // TerraLib
27 #include "Exception.h"
28 #include "Translator.h"
29 #include "URL.h"
30 
31 // uripp - MIT
32 #include "URI/authority.h"
33 #include "URI/domain_name.h"
34 #include "URI/fragment.h"
35 #include "URI/ip_address.h"
36 #include "URI/ipv6_address.h"
37 #include "URI/path.h"
38 #include "URI/query.h"
39 #include "URI/scheme.h"
40 #include "URI/uri.h"
41 #include "URI/urisyn.h"
42 #include "URI/utils.h"
43 
44 // STL
45 #include <cstdio>
46 #include <stdexcept>
47 
49 {
50 }
51 
52 te::common::URL::URL(const std::string& s) // encoded string
53 {
54  try
55  {
56  m_uri = te::common::uri::uri(s);
57  }
58  catch(std::exception& e)
59  {
60  throw(Exception(e.what()));
61  }
62 }
63 
65 {
66 }
67 
69 {
70  try
71  {
72  std::string ss, s, login, password;
73  std::string protocol = getProtocol();
74  s = protocol + "://";
75 
77  login = a.getLogin();
78  password = a.getPassword();
79  if(login.empty() == false)
80  s += login;
81  if(password.empty() == false)
82  s += ":" + password;
83  if(login.empty() == false || password.empty() == false)
84  s += '@';
85 
86  s += getHost();
87  int port = getHostPort();
88 
89  if(port != a.getDefaultPort(protocol))
90  {
91  s += ":";
92  char buf[10];
93  sprintf(buf, "%d", port);
94  s += buf;
95  }
96 
97  ss = getPathString();
98  if(ss.empty() == false)
99  s += ss;
100  ss = getQueryString();
101  if(ss.empty() == false)
102  s += "?" + ss;
103 
104  ss = getFragmentString();
105  if(ss.empty() == false)
106  s += "#" + ss;
107 
108  return s;
109  }
110  catch(std::exception& e)
111  {
112  throw(Exception(e.what()));
113  }
114 }
115 
117 {
118  try
119  {
120  std::string ss, s, login, password;
121  std::string protocol = getProtocol();
122  s = protocol + "://";
123 
125  login = a.getEncodedLogin();
126  password = a.getEncodedPassword();
127  if(login.empty() == false)
128  s += login;
129  if(password.empty() == false)
130  s += ":" + password;
131  if(login.empty() == false || password.empty() == false)
132  s += '@';
133 
134  s += getHost();
135  int port = getHostPort();
136 
137  if(port != a.getDefaultPort(protocol))
138  {
139  s += ":";
140  char buf[10];
141  sprintf(buf, "%d", port);
142  s += buf;
143  }
144 
145  ss = getEncodedPathString();
146  if(ss.empty() == false)
147  s += ss;
148  ss = getEncodedQueryString();
149  if(ss.empty() == false)
150  s += "?" + ss;
151 
152  ss = getEncodedFragmentString();
153  if(ss.empty() == false)
154  s += "#" + ss;
155 
156  return s;
157  }
158  catch(std::exception& e)
159  {
160  throw(Exception(e.what()));
161  }
162 }
163 
164 void te::common::URL::setEncodedString(const std::string& s)
165 {
166  try
167  {
168  *this = URL(s);
169  }
170  catch(std::exception& e)
171  {
172  throw(Exception(e.what()));
173  }
174 }
175 
176 const std::string& te::common::URL::getProtocol() const
177 {
178  try
179  {
180  return m_uri.scheme().string();
181  }
182  catch(std::exception& e)
183  {
184  throw(Exception(e.what()));
185  }
186 }
187 
188 void te::common::URL::setProtocol(const std::string& s)
189 {
190  try
191  {
192  te::common::uri::scheme& scheme = m_uri.scheme();
193  scheme = te::common::uri::scheme(s);
194  setHostPort(m_uri.authority().getDefaultPort(s));
195  }
196  catch(std::exception& e)
197  {
198  throw(Exception(e.what()));
199  }
200 }
201 
202 const std::string& te::common::URL::getHost() const
203 {
204  try
205  {
206  return m_uri.authority().host();
207  }
208  catch(std::exception& e)
209  {
210  throw(Exception(e.what()));
211  }
212 }
213 
214 void te::common::URL::setHost(const std::string& s)
215 {
216  try
217  {
220  std::string protocol = getProtocol();
221  if(a.port() == 0)
222  a.setDefaultPort(protocol);
223  }
224  catch(std::exception& e)
225  {
226  throw(Exception(e.what()));
227  }
228 }
229 
231 {
232  try
233  {
234  return m_uri.authority().host_type();
235  }
236  catch(std::exception& e)
237  {
238  throw(Exception(e.what()));
239  }
240 }
241 
242 unsigned short te::common::URL::getHostPort() const
243 {
244  try
245  {
246  return m_uri.authority().port();
247  }
248  catch(std::exception& e)
249  {
250  throw(Exception(e.what()));
251  }
252 }
253 
254 void te::common::URL::setHostPort(unsigned short p)
255 {
256  try
257  {
259  a.port(p);
260  }
261  catch(std::exception& e)
262  {
263  throw(Exception(e.what()));
264  }
265 }
266 
267 std::string te::common::URL::getLogin() const
268 {
269  try
270  {
271  return m_uri.authority().getLogin();
272  }
273  catch(std::exception& e)
274  {
275  throw(Exception(e.what()));
276  }
277 }
278 
279 void te::common::URL::setLogin(const std::string& s)
280 {
281  try
282  {
284  a.setLogin(s);
285  }
286  catch(std::exception& e)
287  {
288  throw(Exception(e.what()));
289  }
290 }
291 
292 std::string te::common::URL::getPassword() const
293 {
294  try
295  {
296  return m_uri.authority().getPassword();
297  }
298  catch(std::exception& e)
299  {
300  throw(Exception(e.what()));
301  }
302 }
303 
304 void te::common::URL::setPassword(const std::string& s)
305 {
306  try
307  {
309  a.setPassword(s);
310  }
311  catch(std::exception& e)
312  {
313  throw(Exception(e.what()));
314  }
315 }
316 
318 {
319  try
320  {
321  return m_uri.path().string();
322  }
323  catch(std::exception& e)
324  {
325  throw(Exception(e.what()));
326  }
327 }
328 
330 {
331  try
332  {
333  return m_uri.path().encoding();
334  }
335  catch(std::exception& e)
336  {
337  throw(Exception(e.what()));
338  }
339 }
340 
341 void te::common::URL::setEncodedPathString(const std::string& spath)
342 {
343  try
344  {
345  te::common::uri::path& path = m_uri.path();
346  path = te::common::uri::path(spath);
347  }
348  catch(std::exception& e)
349  {
350  throw(Exception(e.what()));
351  }
352 }
353 
355 {
356  try
357  {
358  te::common::uri::path& path = m_uri.path();
359  path.clear();
360  }
361  catch(std::exception& e)
362  {
363  throw(Exception(e.what()));
364  }
365 }
366 
367 void te::common::URL::addPath(const std::string& p)
368 {
369  try
370  {
371  te::common::uri::path& path = m_uri.path();
372  path += p;
373  }
374  catch(std::exception& e)
375  {
376  throw(Exception(e.what()));
377  }
378 }
379 
381 {
382  try
383  {
384  return m_uri.path().absolute();
385  }
386  catch(std::exception& e)
387  {
388  throw(Exception(e.what()));
389  }
390 }
391 
393 {
394  try
395  {
396  te::common::uri::path& path = m_uri.path();
397  path.absolute(v);
398  }
399  catch(std::exception& e)
400  {
401  throw(Exception(e.what()));
402  }
403 }
404 
406 {
407  try
408  {
409  return m_uri.path().is_directory();
410  }
411  catch(std::exception& e)
412  {
413  throw(Exception(e.what()));
414  }
415 }
416 
418 {
419  try
420  {
421  te::common::uri::path& path = m_uri.path();
422  path.is_directory(v);
423  }
424  catch(std::exception& e)
425  {
426  throw(Exception(e.what()));
427  }
428 }
429 
431 {
432  try
433  {
434  return m_uri.path().empty();
435  }
436  catch(std::exception& e)
437  {
438  throw(Exception(e.what()));
439  }
440 }
441 
443 {
444  try
445  {
446  const te::common::uri::path& path = m_uri.path();
447  return path.begin();
448  }
449  catch(std::exception& e)
450  {
451  throw(Exception(e.what()));
452  }
453 }
454 
456 {
457  try
458  {
459  const te::common::uri::path& path = m_uri.path();
460  return path.end();
461  }
462  catch(std::exception& e)
463  {
464  throw(Exception(e.what()));
465  }
466 }
467 
469 {
470  try
471  {
472  std::string s;
473  te::common::URL::const_queryIterator it = beginQuery();
474  while(it != endQuery())
475  {
476  s += it->first;
477  s += "=";
478  s += it->second;
479  it++;
480  if(it != endQuery())
481  s += "&";
482  }
483  return s;
484  }
485  catch(std::exception& e)
486  {
487  throw(Exception(e.what()));
488  }
489 }
490 
492 {
493  try
494  {
495  return m_uri.query().encoding();
496  }
497  catch(std::exception& e)
498  {
499  throw(Exception(e.what()));
500  }
501 }
502 
503 void te::common::URL::setEncodedQueryString(const std::string& squery)
504 {
505  try
506  {
507  te::common::uri::query& query = m_uri.query();
508  query = te::common::uri::query(squery);
509  }
510  catch(std::exception& e)
511  {
512  throw(Exception(e.what()));
513  }
514 }
515 
517 {
518  try
519  {
520  return m_uri.query().size();
521  }
522  catch(std::exception& e)
523  {
524  throw(Exception(e.what()));
525  }
526 }
527 
529 {
530  try
531  {
532  te::common::uri::query& query = m_uri.query();
533  query = te::common::uri::query();
534  }
535  catch(std::exception& e)
536  {
537  throw(Exception(e.what()));
538  }
539 }
540 
541 void te::common::URL::addQuery(const std::string& key, const std::string& value)
542 {
543  try
544  {
545  te::common::uri::query& query = m_uri.query();
546  query.push_back(std::make_pair(key, value));
547  }
548  catch(std::exception& e)
549  {
550  throw(Exception(e.what()));
551  }
552 }
553 
555 {
556  try
557  {
558  te::common::uri::query& query = m_uri.query();
559  return query.begin();
560  }
561  catch(std::exception& e)
562  {
563  throw(Exception(e.what()));
564  }
565 }
566 
568 {
569  try
570  {
571  te::common::uri::query& query = m_uri.query();
572  return query.end();
573  }
574  catch(std::exception& e)
575  {
576  throw(Exception(e.what()));
577  }
578 }
579 
581 {
582  try
583  {
584  const te::common::uri::query& query = m_uri.query();
585  return query.begin();
586  }
587  catch(std::exception& e)
588  {
589  throw(Exception(e.what()));
590  }
591 }
592 
594 {
595  try
596  {
597  const te::common::uri::query& query = m_uri.query();
598  return query.end();
599  }
600  catch(std::exception& e)
601  {
602  throw(Exception(e.what()));
603  }
604 }
605 
607 {
608  try
609  {
610  te::common::uri::query& query = m_uri.query();
611  te::common::uri::query::iterator it = query.find(key);
612  return it;
613  }
614  catch(std::exception& e)
615  {
616  throw(Exception(e.what()));
617  }
618 }
619 
621 {
622  try
623  {
624  const te::common::uri::query& query = m_uri.query();
625  te::common::uri::query::const_iterator it = query.find(key);
626  return it;
627  }
628  catch(std::exception& e)
629  {
630  throw(Exception(e.what()));
631  }
632 }
633 
635 {
636  try
637  {
638  return m_uri.fragment().string();
639  }
640  catch(std::exception& e)
641  {
642  throw(Exception(e.what()));
643  }
644 }
645 
647 {
648  try
649  {
650  return m_uri.fragment().encoding();
651  }
652  catch(std::exception& e)
653  {
654  throw(Exception(e.what()));
655  }
656 }
657 
658 void te::common::URL::setFragmentString(const std::string& f)
659 {
660  try
661  {
664  setEncodedFragmentString(s);
665  }
666  catch(std::exception& e)
667  {
668  throw(Exception(e.what()));
669  }
670 }
671 
673 {
674  try
675  {
676  te::common::uri::fragment& fragment = m_uri.fragment();
677  fragment = te::common::uri::fragment(f);
678  }
679  catch(std::exception& e)
680  {
681  throw(Exception(e.what()));
682  }
683 }
path()
Construct.
Definition: path.cpp:32
void setEncodedFragmentString(const std::string &f)
It sets the encoded fragment.
Definition: URL.cpp:672
uri::query::iterator queryIterator
query iterator type
Definition: URL.h:58
void setProtocol(const std::string &s)
It sets the protocol.
Definition: URL.cpp:188
void clearPath()
It cleans the path.
Definition: URL.cpp:354
std::string getPassword() const
It returns the password.
Definition: URL.cpp:292
void setHost(const std::string &s)
It sets the host.
Definition: URL.cpp:214
bool isDirectoryPath() const
It Checks if the path is directory.
Definition: URL.cpp:405
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
std::string getEncodedQueryString() const
It returns the encoded query.
Definition: URL.cpp:491
const_iterator end() const
Get iterator at end.
Definition: path.h:112
std::string getFragmentString() const
It returns the fragment.
Definition: URL.cpp:634
te::common::URL::queryIterator findQueryKey(const std::string &key)
it finds the key and return query iterator.
Definition: URL.cpp:606
query()
Construct.
Definition: query.cpp:35
Uniform Resource Identifier (URI) reference.
Definition: uri.h:110
void setLogin(const std::string &s)
It sets the login.
Definition: URL.cpp:279
URI path component.
Definition: path.h:51
std::string getString()
It returns URL string.
Definition: URL.cpp:68
URI fragment component.
Definition: fragment.h:41
bool is_directory() const
Test if a directory, meaning the path ends in a "/".
Definition: path.h:68
const_pathIterator endPath() const
It returns the end const path iterator.
Definition: URL.cpp:455
const std::string & getHost() const
It returns the host.
Definition: URL.cpp:202
void clear()
Clear segments and reset absolute and is_directory.
Definition: path.cpp:40
URI authority component (without userinfo).
Definition: authority.h:49
void setEncodedPathString(const std::string &path)
It sets the encoded path.
Definition: URL.cpp:341
void setPassword(const std::string &s)
It sets the password.
Definition: URL.cpp:304
std::string getEncodedPathString() const
It returns the encoded path.
Definition: URL.cpp:329
URI scheme component.
Definition: scheme.h:40
bool isEmptyPath() const
It Checks if the path is empty.
Definition: URL.cpp:430
bool isAbsolutePath() const
It Checks if the path is absolute.
Definition: URL.cpp:380
std::string encode(const traits &ts, const std::string &comp)
Encode the URI (sub) component.
Definition: urisyn.cpp:157
const_pathIterator beginPath() const
It returns the begin const path iterator.
Definition: URL.cpp:442
A helper class for URL encode/decode.
void clearQuery()
It cleans the vector of queries.
Definition: URL.cpp:528
A class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web...
Definition: URL.h:55
std::string getLogin() const
Get login. Added by Lauro.
Definition: authority.cpp:102
std::string getEncodedString()
It returns encoded URL string.
Definition: URL.cpp:116
int getDefaultPort(const std::string &scheme)
Get Default port for the protocol. Added by Lauro.
Definition: authority.cpp:93
const_iterator begin() const
Get iterator at beginning.
Definition: path.h:111
bool absolute() const
Test if absolute, meaning path begins with "/".
Definition: path.h:64
std::string getPathString() const
It returns the path.
Definition: URL.cpp:317
std::string getEncodedLogin() const
Get endoded login. Added by Lauro.
Definition: authority.cpp:105
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
unsigned short getHostPort() const
It returns the host port.
Definition: URL.cpp:242
void setIsAbsolutePath(bool v)
It sets the absolute path propertie.
Definition: URL.cpp:392
This class is designed for dealing with multi-language text translation in TerraLib.
std::string getEncodedFragmentString() const
It returns encoded the fragment.
Definition: URL.cpp:646
void setPassword(const std::string &password)
Set password. Added by Lauro.
Definition: authority.cpp:119
void setEncodedQueryString(const std::string &query)
It sets the encoded query.
Definition: URL.cpp:503
std::string getLogin() const
It returns the login.
Definition: URL.cpp:267
const_iterator find(const std::string &key) const
Find the const_iterator for the key.
Definition: query.cpp:61
void setEncodedString(const std::string &)
It sets the encoded URL string.
Definition: URL.cpp:164
URL()
Default constructor of a new URL.
Definition: URL.cpp:48
int getQuerySize() const
It returns the query size.
Definition: URL.cpp:516
queryIterator beginQuery()
It returns the begin query iterator.
Definition: URL.cpp:554
uri::authority::host_type_e getHostType() const
It returns the host type.
Definition: URL.cpp:230
URI query component.
Definition: query.h:54
void setIsDirectoryPath(bool v)
It sets the directory path propertie.
Definition: URL.cpp:417
void setFragmentString(const std::string &f)
It sets the fragment.
Definition: URL.cpp:658
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
queryIterator endQuery()
It returns the end query iterator.
Definition: URL.cpp:567
const std::string & getProtocol() const
It returns the protocol.
Definition: URL.cpp:176
void addPath(const std::string &p)
It adds the path.
Definition: URL.cpp:367
unsigned short port() const
Get port (0 if none).
Definition: authority.h:67
std::string getEncodedPassword() const
Get encoded password. Added by Lauro.
Definition: authority.cpp:125
scheme()
Construct null.
Definition: scheme.cpp:32
host_type_e
Enumeration.
Definition: authority.h:52
void setHostPort(unsigned short p)
It sets the host port.
Definition: URL.cpp:254
uri::path::const_iterator const_pathIterator
path const iterator type
Definition: URL.h:60
uri::query::const_iterator const_queryIterator
query const iterator type
Definition: URL.h:59
const traits FRAGMENT_TRAITS
fragment traits
Definition: urisyn.cpp:115
~URL()
Destructor.
Definition: URL.cpp:64
std::string getPassword() const
Get password. Added by Lauro.
Definition: authority.cpp:122
std::string getQueryString()
It returns the query string.
Definition: URL.cpp:468
void addQuery(const std::string &key, const std::string &value)
It cleans the vector of queries.
Definition: URL.cpp:541