]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
Fixed TablePrivileges() function (I HOPE!) so it will work with all supported databases
[wxWidgets.git] / src / common / url.cpp
CommitLineData
f4ada568
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: url.cpp
3// Purpose: URL parser
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/1997
7// RCS-ID: $Id$
8// Copyright: (c) 1997, 1998 Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "url.h"
14#endif
fcc6dddd
JS
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
f4ada568
GL
23#include <string.h>
24#include <ctype.h>
25
3096bd2f
VZ
26#include "wx/string.h"
27#include "wx/list.h"
28#include "wx/utils.h"
c092213d 29#include "wx/module.h"
3096bd2f 30#include "wx/url.h"
f4ada568 31
f4ada568
GL
32IMPLEMENT_CLASS(wxProtoInfo, wxObject)
33IMPLEMENT_CLASS(wxURL, wxObject)
f4ada568
GL
34
35// Protocols list
b2b35524 36wxProtoInfo *wxURL::ms_protocols = NULL;
8a4df159 37
f92f546c
VS
38// Enforce linking of protocol classes:
39USE_PROTOCOL(wxHTTP)
40USE_PROTOCOL(wxFTP)
41USE_PROTOCOL(wxFileProto)
42
8a4df159 43#if wxUSE_SOCKETS
b2b35524
VZ
44 wxHTTP *wxURL::ms_proxyDefault = NULL;
45 bool wxURL::ms_useDefaultProxy = FALSE;
8a4df159 46#endif
f4ada568 47
fae05df5
GL
48// --------------------------------------------------------------
49// wxURL
50// --------------------------------------------------------------
f4ada568 51
fae05df5
GL
52// --------------------------------------------------------------
53// --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
54// --------------------------------------------------------------
f4ada568
GL
55
56wxURL::wxURL(const wxString& url)
57{
b2b35524
VZ
58 m_protocol = NULL;
59 m_error = wxURL_NOERR;
60 m_url = url;
61
8a4df159 62#if wxUSE_SOCKETS
b2b35524
VZ
63 if ( ms_useDefaultProxy && !ms_proxyDefault )
64 {
65 SetDefaultProxy(getenv("HTTP_PROXY"));
66
67 if ( !ms_proxyDefault )
68 {
69 // don't try again
70 ms_useDefaultProxy = FALSE;
71 }
72 }
73
74 m_useProxy = ms_proxyDefault != NULL;
75 m_proxy = ms_proxyDefault;
76#endif // wxUSE_SOCKETS
77
78 ParseURL();
f4ada568
GL
79}
80
81bool wxURL::ParseURL()
82{
83 wxString last_url = m_url;
84
f6bcfd97
BP
85 // If the URL was already parsed (m_protocol != NULL), pass this section.
86 if (!m_protocol)
87 {
f61815af
GL
88 // Clean up
89 CleanData();
f4ada568 90
f61815af 91 // Extract protocol name
f6bcfd97
BP
92 if (!PrepProto(last_url))
93 {
f4ada568
GL
94 m_error = wxURL_SNTXERR;
95 return FALSE;
96 }
f61815af
GL
97
98 // Find and create the protocol object
f6bcfd97
BP
99 if (!FetchProtocol())
100 {
f61815af
GL
101 m_error = wxURL_NOPROTO;
102 return FALSE;
103 }
104
105 // Do we need a host name ?
f6bcfd97
BP
106 if (m_protoinfo->m_needhost)
107 {
f61815af 108 // Extract it
f6bcfd97
BP
109 if (!PrepHost(last_url))
110 {
f61815af
GL
111 m_error = wxURL_SNTXERR;
112 return FALSE;
113 }
114 }
115
116 // Extract full path
f6bcfd97
BP
117 if (!PrepPath(last_url))
118 {
f61815af
GL
119 m_error = wxURL_NOPATH;
120 return FALSE;
121 }
f4ada568 122 }
f61815af 123 // URL parse finished.
f4ada568 124
8a4df159 125#if wxUSE_SOCKETS
f6bcfd97
BP
126 if (m_useProxy)
127 {
f61815af
GL
128 // We destroy the newly created protocol.
129 CleanData();
130
131 // Third, we rebuild the URL.
223d09f6 132 m_url = m_protoname + wxT(":");
f61815af 133 if (m_protoinfo->m_needhost)
223d09f6 134 m_url = m_url + wxT("//") + m_hostname;
f61815af
GL
135
136 m_url += m_path;
137
138 // We initialize specific variables.
139 m_protocol = m_proxy; // FIXME: we should clone the protocol
f4ada568 140 }
8a4df159 141#endif
f4ada568
GL
142
143 m_error = wxURL_NOERR;
144 return TRUE;
145}
146
147void wxURL::CleanData()
148{
8a4df159 149#if wxUSE_SOCKETS
f61815af 150 if (!m_useProxy)
8a4df159 151#endif
f4ada568
GL
152 delete m_protocol;
153}
154
155wxURL::~wxURL()
156{
157 CleanData();
8a4df159 158#if wxUSE_SOCKETS
b2b35524 159 if (m_proxy && m_proxy != ms_proxyDefault)
f61815af 160 delete m_proxy;
8a4df159 161#endif
f4ada568
GL
162}
163
fae05df5
GL
164// --------------------------------------------------------------
165// --------- wxURL urls decoders --------------------------------
166// --------------------------------------------------------------
167
f4ada568
GL
168bool wxURL::PrepProto(wxString& url)
169{
170 int pos;
171
172 // Find end
223d09f6 173 pos = url.Find(wxT(':'));
f4ada568
GL
174 if (pos == -1)
175 return FALSE;
176
177 m_protoname = url(0, pos);
178
179 url = url(pos+1, url.Length());
180
181 return TRUE;
182}
183
184bool wxURL::PrepHost(wxString& url)
185{
856d2e52 186 wxString temp_url;
f4ada568
GL
187 int pos, pos2;
188
58c837a4 189 if ((url.GetChar(0) != wxT('/')) || (url.GetChar(1) != wxT('/')))
f4ada568
GL
190 return FALSE;
191
192 url = url(2, url.Length());
193
223d09f6 194 pos = url.Find(wxT('/'));
f4ada568 195 if (pos == -1)
b7db6f0b 196 pos = url.Length();
f4ada568 197
856d2e52
GL
198 if (pos == 0)
199 return FALSE;
200
201 temp_url = url(0, pos);
223d09f6 202 url = url(url.Find(wxT('/')), url.Length());
856d2e52
GL
203
204 // Retrieve service number
223d09f6 205 pos2 = temp_url.Find(wxT(':'), TRUE);
f6bcfd97
BP
206 if (pos2 != -1 && pos2 < pos)
207 {
375abe3d 208 m_servname = temp_url(pos2+1, pos);
f4ada568
GL
209 if (!m_servname.IsNumber())
210 return FALSE;
856d2e52 211 temp_url = temp_url(0, pos2);
f4ada568
GL
212 }
213
856d2e52 214 // Retrieve user and password.
223d09f6 215 pos2 = temp_url.Find(wxT('@'));
856d2e52
GL
216 // Even if pos2 equals -1, this code is right.
217 m_hostname = temp_url(pos2+1, temp_url.Length());
f4ada568 218
223d09f6
KB
219 m_user = wxT("");
220 m_password = wxT("");
856d2e52
GL
221
222 if (pos2 == -1)
223 return TRUE;
224
225 temp_url = temp_url(0, pos2);
223d09f6 226 pos2 = temp_url.Find(wxT(':'));
856d2e52
GL
227
228 if (pos2 == -1)
229 return FALSE;
230
231 m_user = temp_url(0, pos2);
232 m_password = temp_url(pos2+1, url.Length());
f4ada568
GL
233
234 return TRUE;
235}
236
237bool wxURL::PrepPath(wxString& url)
238{
239 if (url.Length() != 0)
f61815af 240 m_path = ConvertToValidURI(url);
f4ada568 241 else
223d09f6 242 m_path = wxT("/");
f4ada568
GL
243 return TRUE;
244}
245
246bool wxURL::FetchProtocol()
247{
b2b35524 248 wxProtoInfo *info = ms_protocols;
f4ada568 249
f6bcfd97
BP
250 while (info)
251 {
252 if (m_protoname == info->m_protoname)
253 {
f4ada568
GL
254 if (m_servname.IsNull())
255 m_servname = info->m_servname;
256
257 m_protoinfo = info;
258 m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
f4ada568
GL
259 return TRUE;
260 }
261 info = info->next;
262 }
263 return FALSE;
264}
265
fae05df5
GL
266// --------------------------------------------------------------
267// --------- wxURL get ------------------------------------------
268// --------------------------------------------------------------
269
58c837a4 270wxInputStream *wxURL::GetInputStream()
f4ada568 271{
f4ada568
GL
272 wxInputStream *the_i_stream = NULL;
273
f6bcfd97
BP
274 if (!m_protocol)
275 {
f4ada568
GL
276 m_error = wxURL_NOPROTO;
277 return NULL;
278 }
279
280 m_error = wxURL_NOERR;
f6bcfd97
BP
281 if (m_user != wxT(""))
282 {
856d2e52
GL
283 m_protocol->SetUser(m_user);
284 m_protocol->SetPassword(m_password);
285 }
286
8a4df159 287#if wxUSE_SOCKETS
19e0e04b
RD
288 wxIPV4address addr;
289
f61815af 290 // m_protoinfo is NULL when we use a proxy
f6bcfd97
BP
291 if (!m_useProxy && m_protoinfo->m_needhost)
292 {
293 if (!addr.Hostname(m_hostname))
294 {
f4ada568
GL
295 m_error = wxURL_NOHOST;
296 return NULL;
297 }
298
299 addr.Service(m_servname);
300
8a2c6ef8
JS
301 if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason
302 {
f4ada568
GL
303 m_error = wxURL_CONNERR;
304 return NULL;
305 }
306 }
8a4df159 307#endif
f4ada568 308
f61815af
GL
309 // When we use a proxy, we have to pass the whole URL to it.
310 if (m_useProxy)
311 the_i_stream = m_protocol->GetInputStream(m_url);
312 else
313 the_i_stream = m_protocol->GetInputStream(m_path);
314
f6bcfd97
BP
315 if (!the_i_stream)
316 {
f4ada568
GL
317 m_error = wxURL_PROTOERR;
318 return NULL;
319 }
320
321 return the_i_stream;
322}
323
8a4df159 324#if wxUSE_SOCKETS
f4ada568
GL
325void wxURL::SetDefaultProxy(const wxString& url_proxy)
326{
b2b35524
VZ
327 if ( !url_proxy )
328 {
329 if ( ms_proxyDefault )
330 {
331 ms_proxyDefault->Close();
332 delete ms_proxyDefault;
333 ms_proxyDefault = NULL;
334 }
f61815af 335 }
f61815af 336 else
b2b35524
VZ
337 {
338 wxString tmp_str = url_proxy;
339 int pos = tmp_str.Find(wxT(':'));
340 if (pos == -1)
341 return;
342
343 wxString hostname = tmp_str(0, pos),
344 port = tmp_str(pos+1, tmp_str.Length()-pos);
345 wxIPV4address addr;
346
347 if (!addr.Hostname(hostname))
348 return;
349 if (!addr.Service(port))
350 return;
351
352 if (ms_proxyDefault)
353 // Finally, when all is right, we connect the new proxy.
354 ms_proxyDefault->Close();
355 else
356 ms_proxyDefault = new wxHTTP();
357 ms_proxyDefault->Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
358 }
f4ada568
GL
359}
360
361void wxURL::SetProxy(const wxString& url_proxy)
362{
b2b35524
VZ
363 if ( !url_proxy )
364 {
365 if ( m_proxy && m_proxy != ms_proxyDefault )
366 {
367 m_proxy->Close();
368 delete m_proxy;
369 }
f4ada568 370
b2b35524
VZ
371 m_useProxy = FALSE;
372 }
373 else
374 {
375 wxString tmp_str;
376 wxString hostname, port;
377 int pos;
378 wxIPV4address addr;
379
380 tmp_str = url_proxy;
381 pos = tmp_str.Find(wxT(':'));
382 // This is an invalid proxy name.
383 if (pos == -1)
384 return;
385
386 hostname = tmp_str(0, pos);
387 port = tmp_str(pos, tmp_str.Length()-pos);
388
389 addr.Hostname(hostname);
390 addr.Service(port);
391
392 // Finally, create the whole stuff.
393 if (m_proxy && m_proxy != ms_proxyDefault)
394 delete m_proxy;
395 m_proxy = new wxHTTP();
396 m_proxy->Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
397
398 CleanData();
399 // Reparse url.
400 m_useProxy = TRUE;
401 ParseURL();
402 }
f4ada568 403}
b2b35524 404#endif // wxUSE_SOCKETS
35a4dab7 405
f6bcfd97 406wxString wxURL::ConvertToValidURI(const wxString& uri, const wxChar* delims)
14906731 407{
fae05df5
GL
408 wxString out_str;
409 wxString hexa_code;
410 size_t i;
411
f6bcfd97
BP
412 for (i = 0; i < uri.Len(); i++)
413 {
fae05df5
GL
414 wxChar c = uri.GetChar(i);
415
223d09f6 416 if (c == wxT(' '))
f6bcfd97
BP
417 {
418 // GRG, Apr/2000: changed to "%20" instead of '+'
419
420 out_str += wxT("%20");
421 }
422 else
423 {
424 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
425 //
426 // - Alphanumeric characters are never escaped
427 // - Unreserved marks are never escaped
428 // - Delimiters must be escaped if they appear within a component
429 // but not if they are used to separate components. Here we have
430 // no clear way to distinguish between these two cases, so they
431 // are escaped unless they are passed in the 'delims' parameter
432 // (allowed delimiters).
433
434 static const wxChar marks[] = wxT("-_.!~*()'");
435
436 if ( !wxIsalnum(c) && !wxStrchr(marks, c) && !wxStrchr(delims, c) )
437 {
223d09f6 438 hexa_code.Printf(wxT("%%%02X"), c);
5a96d2f4 439 out_str += hexa_code;
f6bcfd97
BP
440 }
441 else
442 {
5a96d2f4 443 out_str += c;
f6bcfd97 444 }
5a96d2f4 445 }
fae05df5 446 }
19e0e04b 447
fae05df5 448 return out_str;
14906731
GL
449}
450
aa6d9706
GL
451wxString wxURL::ConvertFromURI(const wxString& uri)
452{
aa6d9706
GL
453 wxString new_uri;
454
74b31181 455 size_t i = 0;
f6bcfd97
BP
456 while (i < uri.Len())
457 {
74b31181 458 int code;
f6bcfd97
BP
459 if (uri[i] == wxT('%'))
460 {
aa6d9706 461 i++;
223d09f6
KB
462 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
463 code = (uri[i] - wxT('A') + 10) * 16;
aa6d9706 464 else
223d09f6 465 code = (uri[i] - wxT('0')) * 16;
f6bcfd97 466
aa6d9706 467 i++;
223d09f6
KB
468 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
469 code += (uri[i] - wxT('A')) + 10;
aa6d9706 470 else
223d09f6 471 code += (uri[i] - wxT('0'));
f6bcfd97 472
aa6d9706
GL
473 i++;
474 new_uri += (wxChar)code;
475 continue;
476 }
477 new_uri += uri[i];
478 i++;
479 }
480 return new_uri;
481}
b2b35524
VZ
482
483// ----------------------------------------------------------------------
484// A module which deletes the default proxy if we created it
485// ----------------------------------------------------------------------
486
487#if wxUSE_SOCKETS
488
489class wxURLModule : public wxModule
490{
491public:
492 virtual bool OnInit();
493 virtual void OnExit();
494
495private:
496 DECLARE_DYNAMIC_CLASS(wxURLModule)
497};
498
499IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
500
501bool wxURLModule::OnInit()
502{
503 // env var HTTP_PROXY contains the address of the default proxy to use if
504 // set, but don't try to create this proxy right now because it will slow
505 // down the program startup (especially if there is no DNS server
506 // available, in which case it may take up to 1 minute)
f6bcfd97 507
b2b35524
VZ
508 if ( getenv("HTTP_PROXY") )
509 {
510 wxURL::ms_useDefaultProxy = TRUE;
511 }
512
513 return TRUE;
514}
515
516void wxURLModule::OnExit()
517{
518 delete wxURL::ms_proxyDefault;
519 wxURL::ms_proxyDefault = NULL;
520}
521
522#endif // wxUSE_SOCKETS