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