examples/URL/main.cpp
Go to the documentation of this file.
1 //******************************************************************************************************************************************
2 //****************codigo da uripp - MIT*****************************************************************************************************
3 //******************************************************************************************************************************************
4 #include <terralib/common/URL.h>
6 
7 #include <cstdlib>
8 
9 int main(int /*argc*/, char** /*argv*/)
10 {
11  try
12  {
13  std::string s, protocol, login, password, host, path, query, fragment, eS, ePath, eQuery, eFragment, key, value;
14  int port, hostType;
15  bool isAbs, isDir, isEmpty;
16 
17  //neste exemplo o recurso a ser localizado eh um diretorio:
18  //o diretorio estah na raiz da maquina mail.google.com
19  //2 queries: hl=pt-BR e sh va=1
20  //fragment id: in+box/12ebb3a026a6d103
21  te::common::URL url("https://mail.google.com/mail/?hl=pt-BR&sh+va=1#in+box/12ebb3a026a6d103");
22  s = url.getString();
23  eS = url.getEncodedString();
24  protocol = url.getProtocol();
25  host = url.getHost();
26  port = url.getHostPort();
27  hostType = url.getHostType();
28  url.setLogin("lauro");
29  url.setPassword("mypass");
30  host = url.getHost();
31  port = url.getHostPort();
32  hostType = url.getHostType();
33  login = url.getLogin();
34  password = url.getPassword();
35 
36  //O teste abaixo troca o separador '&' por ';'
37  url = te::common::URL("https://mail.google.com/mail/?hl=pt-BR;sh+va=1#in+box/12ebb3a026a6d103");
38  s = url.getString();
39  eS = url.getEncodedString();
40  protocol = url.getProtocol();
41  host = url.getHost();
42  port = url.getHostPort();
43  hostType = url.getHostType();
44 
45  //colocando login e password
46  url.setLogin("lauro");
47  url.setPassword("mypass");
48  host = url.getHost();
49  port = url.getHostPort();
50  hostType = url.getHostType();
51  login = url.getLogin();
52  password = url.getPassword();
53  path = url.getPathString();
54  isAbs = url.isAbsolutePath();
55  isDir = url.isDirectoryPath();
56  isEmpty = url.isEmptyPath();
57  path = url.getPathString();
58  ePath = url.getEncodedPathString();
59  query = url.getQueryString();
60  eQuery = url.getEncodedQueryString();
61  eFragment = url.getEncodedFragmentString();
62 
63  url = te::common::URL("https://%fateis:my%23Senha@mail.google.com:1234/mail/?hl=pt-BR&sh+va=1#in+box/12ebb3a026a6d103");
64  s = url.getString();
65  eS = url.getEncodedString();
66  protocol = url.getProtocol();
67  host = url.getHost();
68  port = url.getHostPort();
69  hostType = url.getHostType();
70  login = url.getLogin();
71  password = url.getPassword();
72  path = url.getPathString();
73  isAbs = url.isAbsolutePath();
74  isDir = url.isDirectoryPath();
75  isEmpty = url.isEmptyPath();
76  path = url.getPathString();
77  ePath = url.getEncodedPathString();
78  query = url.getQueryString();
79  eQuery = url.getEncodedQueryString();
80  eFragment = url.getEncodedFragmentString();
81 
82  // teste para encontrar a chave "hl" e trocar seu valor para 'pt-PORTUGAL'
84  it->second = "pt-PORTUGAL";
85  query = url.getQueryString();
86  s = url.getString();
87  eS = url.getEncodedString();
88 
89  // O exemplo abaixo mostra uma URL que leva a uma pagina html
90  // query: NodeId=193
91  url.setEncodedString("http://br.franceguide.com/Viajante/Turismo-e-Deficiencia/home.html?NodeID=193");
92  s = url.getString();
93  eS = url.getEncodedString();
94 
95  // usando iterator para ver todos os queries.
97  for(qit = url.beginQuery(); qit != url.endQuery(); qit++)
98  {
99  key = qit->first;
100  value =qit->second;
101  }
102 
103  url = te::common::URL();
104  url.setProtocol("ftp");
105  // o protocolo ftp usa a porta 21 por default
106  url.setHost("www.dpi.inpe.br"); //default port for ftp: 21
107 
108  // alterando a porta default para 220
109  url.setHostPort(220);
110 
111  // mudando o dominio para www.ibm.com e a porta para 8020
112  url.setHost("www.ibm.com:8020");
113 
114  // configurando o dominio usando ip_adress e porta 8020
115  url.setHost("172.16.254.1:8020"); //ip_adress
116 
117  // configurando o dominio usando ipv6_adress e porta 8020
118  url.setHost("[2001:db8:0:1234:0:567:8:1]:8020"); //ipv6_adress
119 
120  // adicionando path
121  url.addPath("lauro hara");
122  url.addPath("informações");
123 
124 // url.setIsAbsolutePath(true); // ISSO AINDA ME DEIXA DUVIDAS...
125  url.setIsAbsolutePath(false); // ISSO AINDA ME DEIXA DUVIDAS...
126  // Acho que nao importa se o path eh ou nao absoluto, sempre tem
127  // que ter '/' depois do dominio. CERTO OU ERRADO??????
128 
129  // configurando queries com chaves e valores que usam '&'
130  // a entrada deve ser necessariamente sem codificacao
131  url.addQuery("key1", "value&1");
132  url.addQuery("key&2", "value&2");
133 
134  // configurando fragment id - entrada sem codificacao
135  url.setEncodedFragmentString("in+box/12ebb3a026a6d103");
136 
137  // configurando fragment id - entrada com codificacao
138  url.setFragmentString("in box/12ebb3a026a6d103");
139 
140  protocol = url.getProtocol();
141  host = url.getHost();
142  port = url.getHostPort();
143  hostType = url.getHostType();
144  path = url.getPathString();
145  isAbs = url.isAbsolutePath();
146  isDir = url.isDirectoryPath();
147  isEmpty = url.isEmptyPath();
148  ePath = url.getEncodedPathString();
149  eQuery = url.getEncodedQueryString();
150  query = url.getQueryString();
151  eFragment = url.getEncodedFragmentString();
152  fragment = url.getFragmentString();
153 
154  qit = url.beginQuery();
155  key = qit->first;
156  value =qit->second;
157  qit++;
158  key = qit->first;
159  value =qit->second;
160 
161  s = url.getString();
162  eS = url.getEncodedString();
163 
164  // ISSO AINDA ME DEIXA DUVIDAS...
165  // isso nao funciona!
166  url = te::common::URL("file:///lixo/lauro/RomeoAndJuliet.pdf");
167  host = url.getHost();
168  path = url.getPathString();
169  isAbs = url.isAbsolutePath();
170  s = url.getString();
171 
172  // isso funciona!
173  url = te::common::URL("file://C:/lixo/LauroPortoseguro.pdf");
174  host = url.getHost();
175  path = url.getPathString();
176  isAbs = url.isAbsolutePath();
177  s = url.getString();
178 
179  // parece que isso nao funciona!??????????????? pelo menos no visual c++
180  url = te::common::URL("file:../../../../lixo/lauro/RomeoAndJuliet.pdf");
181  host = url.getHost();
182  path = url.getPathString();
183  isAbs = url.isAbsolutePath();
184  s = url.getString();
185 
186  // segundo o Dihel isto nao faz sentido porque o protocolo file so serve para maquina local
187  url = te::common::URL("file:lauro:mySenha@C:/lixo/RomeoAndJuliet.pdf");
188  host = url.getHost();
189  path = url.getPathString();
190  isAbs = url.isAbsolutePath();
191  s = url.getString();
192 
193  // quando o arquivo esta em outra maquina deve-se usar outro protocolo
194  url = te::common::URL("http://lauro:mySenha@sputnik/lixo/lauro/Desktop/RomeoAndJuliet.pdf");
195  host = url.getHost();
196  path = url.getPathString();
197  isAbs = url.isAbsolutePath();
198  s = url.getString();
199 
200  url = te::common::URL("ftp://lauro:mySenha@sputnik:40/lixo/lauro/Desktop/RomeoAndJuliet.pdf");
201  host = url.getHost();
202  path = url.getPathString();
203  isAbs = url.isAbsolutePath();
204  s = url.getString();
205  eS = url.getEncodedString();
206 
207  // criando uma URL vazia
208  url = te::common::URL();
209  //preenchendo por partes
210  url.setProtocol("telnet");
211  url.setHost("lauro:mysenha@sputnik:45");
212 
213  s = url.getString();
214  eS = url.getEncodedString();
215 ////////////////////////////////////////////////////////////////////////////////////////////////
216 // usando diretamente a uripp //////////////////////////////////////////////////////////////////
217 ////////////////////////////////////////////////////////////////////////////////////////////////
218 
219  // bool e, n, r;
220  // std::string encodeStr;
221  //
222  // std::string senc = te::common::uri::urisyn::encode(te::common::uri::urisyn::PATH_TRAITS, "este faz ?teste/com dir/arq.txt");
223  // //te::common::uri::path path22("faz?teste/comdir/arq.txt");
224  //
225  // te::common::uri::path path1;
226  // path1 += "teste";
227  // path1 += "t%3";
228  // std::string ssa = path1.encoding();
229  // std::string ssa2 = path1.string();
230  //
231  // te::common::uri::uri URI("http://example.org/doc.pdf#view=fitb&nameddest=Chapter3");
232  // e = URI.empty();
233  // n = URI.is_null();
234  // r = URI.relative();
235  // te::common::uri::scheme& scheme = URI.scheme();
236  // scheme = te::common::uri::scheme("ftp"); //altera protocolo para ftp
237  // te::common::uri::scheme& scheme2 = URI.scheme();
238  // te::common::uri::authority& authority = URI.authority();
239  // authority = te::common::uri::authority("dpi.inpe.br:8020"); //altera domain name para dpi.inpe.br e porta 8020
240  // authority = te::common::uri::authority("172.16.254.1:8020"); //altera ip_adress para 172.16.254.1 e porta 8020
241  // authority = te::common::uri::authority("[2001:db8:0:1234:0:567:8:1]:8020"); //altera ipv6_adress para 2001:db8:0:1234:0:567:8:1 e porta 8020
242  // std::string host = authority.host();
243  // std::string s = authority.string();
244  // // há uma especificacao para gerar ip_adress e ipv6_adress. Caso o endereco esteja fora da especificacao ocorrera um ERRO.
245  // authority = te::common::uri::authority("localhost"); //altera para local
246  // te::common::uri::authority autority2 = URI.authority();
247  // te::common::uri::path& path = URI.path();
248  // path.clear();
249  // path += "dir+1";
250  // path += "dir 2";
251  // path += "dir 3";
252  // path += "dir 4";
253  // path += "teste.txt";
254  // std::string pathstr = path.encoding();
255  //// path.is_directory(true);
256  // path.absolute(true); //coloca "/" no inicio do path.
257  // path.is_directory(true); //coloca "/" no fim do path.
258  // pathstr = path.encoding();
259  // path.clear();
260  // path += te::common::uri::path("/exemplo/teste/dir+teste/arq.txt");
261  // pathstr = path.string();
262  // std::string pathstr2 = path.encoding();
263  // te::common::uri::path& path2 = URI.path();
264  // te::common::uri::query query = URI.query();
265  // te::common::uri::fragment fragment = URI.fragment();
266  // encodeStr = URI.encoding();
267  // query.push_back(std::make_pair("q 1", "aaa"));
268  // query.push_back(std::make_pair("q 2", "bbb"));
269  // std::string sq = query.encoding();
270  // te::common::uri::query::iterator qit = query.begin();
271  // sq = qit->first;
272  // sq = qit->second;
273  // qit++;
274  // sq = qit->first;
275  // sq = qit->second;
276  // query = te::common::uri::query("hl=pt-BR&sh+va=1");
277  //
278  // URI = te::common::uri::uri("https://mail.google.com/mail/?hl=pt-BR&sh+va=1#in+box/12ebb3a026a6d103");
279  // e = URI.empty();
280  // n = URI.is_null();
281  // r = URI.relative();
282  // scheme = URI.scheme();
283  // authority = URI.authority();
284  // path = URI.path();
285  // pathstr = te::common::uri::convert(path);
286  // bool pb = path.is_directory();
287  // query = URI.query();
288  // fragment = URI.fragment();
289  // std::string sf = fragment.string();
290  // std::string sfd = fragment.encoding();
291  //
292  // fragment = te::common::uri::fragment("in+box/12ebb3a026a6d103");
293  //
294  // URI = te::common::uri::uri("http://msdn.microsoft.com/en-us/library/35bhkfb6(v=vs.71).aspx");
295  // e = URI.empty();
296  // n = URI.is_null();
297  // r = URI.relative();
298  // scheme = URI.scheme();
299  // authority = URI.authority();
300  // path = URI.path();
301  // query = URI.query();
302  // fragment = URI.fragment();
303  //
304  // URI = te::common::uri::uri("http://br.franceguide.com/Viajante/Turismo-e-Deficiencia/home.html?NodeID=193");
305  // e = URI.empty();
306  // n = URI.is_null();
307  // r = URI.relative();
308  // scheme = URI.scheme();
309  // authority = URI.authority();
310  // path = URI.path();
311  // query = URI.query();
312  // fragment = URI.fragment();
313  //
314  // URI = te::common::uri::uri("http://translate.google.com.br/#en|pt|%0AWhen%20a%20system%20uses%20a%20local%20addressing%20scheme%2C%20it%20is%20useful%20to%20provide%20a%20mapping%20from%20local%20addresses%20into%20URIs%20so%20that%20references%20to%20objects%20within%20the%20addressing%20scheme%20may%20be%20referred%20to%20globally%2C%20and%20possibly%20accessed%20through%20gateway%20servers.%0A%0AFor%20a%20new%20naming%20scheme%2C%20any%20mapping%20scheme%20may%20be%20defined%20provided%20it%20is%20unambiguous%2C%20reversible%2C%20and%20provides%20valid%20URIs.%20It%20is%20recommended%20that%20where%20hierarchical%20aspects%20to%20the%20local%20naming%20scheme%20exist%2C%20they%20be%20mapped%20onto%20the%20hierarchical%20URL%20path%20syntax%20in%20order%20to%20allow%20the%20partial%20form%20to%20be%20used.%0A%0AIt%20is%20also%20recommended%20that%20the%20conventional%20scheme%20below%20be%20used%20in%20all%20cases%20except%20for%20any%20scheme%20which%20encodes%20binary%20data%20as%20opposed%20to%20text%2C%20in%20which%20case%20a%20more%20compact%20encoding%20such%20as%20pure%20hexadecimal%20or%20base%2064%20might%20be%20more%20appropriate.%20For%20example%2C%20the%20conventional%20URI%20encoding%20method%20is%20used%20for%20mapping%20WAIS%2C%20FTP%2C%20Prospero%20and%20Gopher%20addresses%20in%20the%20URI%20specification.%0A");
315  // e = URI.empty();
316  // n = URI.is_null();
317  // r = URI.relative();
318  // scheme = URI.scheme();
319  // authority = URI.authority();
320  // path = URI.path();
321  // query = URI.query();
322  // fragment = URI.fragment();
323  //
324  // te::common::uri::authority A("www.dpi.inpe.br");
325  // bool b = A.empty();
326  // bool b1 = A.is_null();
327  // host = A.host();
328  // int t = A.host_type();
329  // unsigned short port = A.port();
330  // s = A.string();
331  //
332  }
333  catch(te::common::Exception& e)
334  {
335  std::cout << std::endl << "An exception has occuried: " << e.what() << std::endl;
336 
337  std::cout << "Press Enter to exit..." << std::endl;
338  std::cin.get();
339 
340  return EXIT_FAILURE;
341  }
342 
343  std::cout << "Press Enter to exit..." << std::endl;
344  std::cin.get();
345 
346  return EXIT_SUCCESS;
347 }
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
std::string getPassword() const
It returns the password.
Definition: URL.cpp:291
void setHost(const std::string &s)
It sets the host.
Definition: URL.cpp:213
bool isDirectoryPath() const
It Checks if the path is directory.
Definition: URL.cpp:404
std::string getEncodedQueryString() const
It returns the encoded query.
Definition: URL.cpp:490
virtual const char * what() const
It outputs the exception message.
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
void setLogin(const std::string &s)
It sets the login.
Definition: URL.cpp:278
std::string getString()
It returns URL string.
Definition: URL.cpp:67
const std::string & getHost() const
It returns the host.
Definition: URL.cpp:201
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
bool isEmptyPath() const
It Checks if the path is empty.
Definition: URL.cpp:429
bool isAbsolutePath() const
It Checks if the path is absolute.
Definition: URL.cpp:379
A class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web...
Definition: URL.h:55
std::string getEncodedString()
It returns encoded URL string.
Definition: URL.cpp:115
std::string getPathString() const
It returns the path.
Definition: URL.cpp:316
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
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
std::string getLogin() const
It returns the login.
Definition: URL.cpp:266
void setEncodedString(const std::string &)
It sets the encoded URL string.
Definition: URL.cpp:163
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
void setFragmentString(const std::string &f)
It sets the fragment.
Definition: URL.cpp:657
int main(int, char **)
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
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void setHostPort(unsigned short p)
It sets the host port.
Definition: URL.cpp:253
uri::query::const_iterator const_queryIterator
query const iterator type
Definition: URL.h:59
std::string getQueryString()
It returns the query string.
Definition: URL.cpp:467
void addQuery(const std::string &key, const std::string &value)
It cleans the vector of queries.
Definition: URL.cpp:540