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