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