]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
using separate imaglist on mac
[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
65571936 9// Licence: wxWindows licence
f4ada568
GL
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4ada568
GL
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
a5d46b73 23#if wxUSE_URL
f4ada568 24
3096bd2f
VZ
25#include "wx/string.h"
26#include "wx/list.h"
27#include "wx/utils.h"
c092213d 28#include "wx/module.h"
3096bd2f 29#include "wx/url.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{
f6bcfd97
BP
130 // If the URL was already parsed (m_protocol != NULL), pass this section.
131 if (!m_protocol)
132 {
f61815af
GL
133 // Clean up
134 CleanData();
f4ada568 135
b60b2ec8
RN
136 // Make sure we have a protocol/scheme
137 if (!HasScheme())
f6bcfd97 138 {
f4ada568 139 m_error = wxURL_SNTXERR;
cb719f2e 140 return false;
f4ada568 141 }
f61815af
GL
142
143 // Find and create the protocol object
f6bcfd97
BP
144 if (!FetchProtocol())
145 {
f61815af 146 m_error = wxURL_NOPROTO;
cb719f2e 147 return false;
f61815af
GL
148 }
149
150 // Do we need a host name ?
f6bcfd97
BP
151 if (m_protoinfo->m_needhost)
152 {
b60b2ec8
RN
153 // Make sure we have one, then
154 if (!HasServer())
f6bcfd97 155 {
f61815af 156 m_error = wxURL_SNTXERR;
cb719f2e 157 return false;
f61815af
GL
158 }
159 }
f4ada568
GL
160 }
161
ce195ee6 162#if wxUSE_PROTOCOL_HTTP
f6bcfd97
BP
163 if (m_useProxy)
164 {
f61815af 165 // Third, we rebuild the URL.
b60b2ec8 166 m_url = m_scheme + wxT(":");
f61815af 167 if (m_protoinfo->m_needhost)
b60b2ec8 168 m_url = m_url + wxT("//") + m_server;
f61815af
GL
169
170 // We initialize specific variables.
171 m_protocol = m_proxy; // FIXME: we should clone the protocol
f4ada568 172 }
ce195ee6 173#endif // wxUSE_PROTOCOL_HTTP
f4ada568
GL
174
175 m_error = wxURL_NOERR;
cb719f2e 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
f61815af 186 if (!m_useProxy)
ce195ee6 187#endif // wxUSE_PROTOCOL_HTTP
f4ada568
GL
188 delete m_protocol;
189}
190
191wxURL::~wxURL()
192{
25959b95 193 CleanData();
ce195ee6 194#if wxUSE_PROTOCOL_HTTP
25959b95
VZ
195 if (m_proxy && m_proxy != ms_proxyDefault)
196 delete m_proxy;
ce195ee6 197#endif // wxUSE_PROTOCOL_HTTP
25959b95
VZ
198#if wxUSE_URL_NATIVE
199 delete m_nativeImp;
8a4df159 200#endif
f4ada568
GL
201}
202
ce321570
RN
203// --------------------------------------------------------------
204// FetchProtocol
205// --------------------------------------------------------------
f4ada568
GL
206
207bool wxURL::FetchProtocol()
208{
b2b35524 209 wxProtoInfo *info = ms_protocols;
f4ada568 210
f6bcfd97
BP
211 while (info)
212 {
b60b2ec8 213 if (m_scheme == info->m_protoname)
f6bcfd97 214 {
b60b2ec8
RN
215 if (m_port.IsNull())
216 m_port = info->m_servname;
217 m_protoinfo = info;
218 m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
219 return true;
f4ada568
GL
220 }
221 info = info->next;
222 }
cb719f2e 223 return false;
f4ada568
GL
224}
225
fae05df5 226// --------------------------------------------------------------
ce321570 227// GetInputStream
fae05df5
GL
228// --------------------------------------------------------------
229
58c837a4 230wxInputStream *wxURL::GetInputStream()
f4ada568 231{
f6bcfd97
BP
232 if (!m_protocol)
233 {
f4ada568
GL
234 m_error = wxURL_NOPROTO;
235 return NULL;
236 }
237
238 m_error = wxURL_NOERR;
4860d40d 239 if (HasUserInfo())
f6bcfd97 240 {
4860d40d 241 size_t dwPasswordPos = m_userinfo.find(':');
b60b2ec8
RN
242
243 if (dwPasswordPos == wxString::npos)
4860d40d 244 m_protocol->SetUser(m_userinfo);
b60b2ec8
RN
245 else
246 {
4860d40d
RN
247 m_protocol->SetUser(m_userinfo(0, dwPasswordPos));
248 m_protocol->SetPassword(m_userinfo(dwPasswordPos+1, m_userinfo.length() + 1));
b60b2ec8 249 }
856d2e52
GL
250 }
251
25959b95
VZ
252#if wxUSE_URL_NATIVE
253 // give the native implementation to return a better stream
254 // such as the native WinINet functionality under MS-Windows
255 if (m_nativeImp)
256 {
257 wxInputStream *rc;
258 rc = m_nativeImp->GetInputStream(this);
259 if (rc != 0)
260 return rc;
261 }
262 // else use the standard behaviour
263#endif // wxUSE_URL_NATIVE
264
8a4df159 265#if wxUSE_SOCKETS
19e0e04b
RD
266 wxIPV4address addr;
267
f61815af 268 // m_protoinfo is NULL when we use a proxy
f6bcfd97
BP
269 if (!m_useProxy && m_protoinfo->m_needhost)
270 {
b60b2ec8 271 if (!addr.Hostname(m_server))
f6bcfd97 272 {
f4ada568
GL
273 m_error = wxURL_NOHOST;
274 return NULL;
275 }
276
b60b2ec8 277 addr.Service(m_port);
f4ada568 278
cb719f2e 279 if (!m_protocol->Connect(addr, true)) // Watcom needs the 2nd arg for some reason
8a2c6ef8 280 {
f4ada568
GL
281 m_error = wxURL_CONNERR;
282 return NULL;
283 }
284 }
8a4df159 285#endif
f4ada568 286
f61815af 287 // When we use a proxy, we have to pass the whole URL to it.
c08a3653
RN
288 wxInputStream *the_i_stream;
289
142b15c5 290 if (!m_useProxy)
c08a3653
RN
291 {
292 the_i_stream = m_protocol->GetInputStream(m_url);
293 }
294 else
295 {
296 wxString fullPath = m_path;
297
298 if (HasQuery())
299 fullPath += wxT("?") + m_query;
300
301 if (HasFragment())
302 fullPath += wxT("#") + m_fragment;
303
304 the_i_stream = m_protocol->GetInputStream(fullPath);
305 }
f61815af 306
f6bcfd97
BP
307 if (!the_i_stream)
308 {
f4ada568
GL
309 m_error = wxURL_PROTOERR;
310 return NULL;
311 }
312
313 return the_i_stream;
314}
315
ce195ee6 316#if wxUSE_PROTOCOL_HTTP
f4ada568
GL
317void wxURL::SetDefaultProxy(const wxString& url_proxy)
318{
b2b35524
VZ
319 if ( !url_proxy )
320 {
321 if ( ms_proxyDefault )
322 {
323 ms_proxyDefault->Close();
324 delete ms_proxyDefault;
325 ms_proxyDefault = NULL;
326 }
f61815af 327 }
f61815af 328 else
b2b35524
VZ
329 {
330 wxString tmp_str = url_proxy;
331 int pos = tmp_str.Find(wxT(':'));
cb719f2e 332 if (pos == wxNOT_FOUND)
b2b35524
VZ
333 return;
334
335 wxString hostname = tmp_str(0, pos),
336 port = tmp_str(pos+1, tmp_str.Length()-pos);
337 wxIPV4address addr;
338
339 if (!addr.Hostname(hostname))
340 return;
341 if (!addr.Service(port))
342 return;
343
344 if (ms_proxyDefault)
345 // Finally, when all is right, we connect the new proxy.
346 ms_proxyDefault->Close();
347 else
348 ms_proxyDefault = new wxHTTP();
cb719f2e 349 ms_proxyDefault->Connect(addr, true); // Watcom needs the 2nd arg for some reason
b2b35524 350 }
f4ada568
GL
351}
352
353void wxURL::SetProxy(const wxString& url_proxy)
354{
b2b35524
VZ
355 if ( !url_proxy )
356 {
357 if ( m_proxy && m_proxy != ms_proxyDefault )
358 {
359 m_proxy->Close();
360 delete m_proxy;
361 }
f4ada568 362
cb719f2e 363 m_useProxy = false;
b2b35524
VZ
364 }
365 else
366 {
367 wxString tmp_str;
368 wxString hostname, port;
369 int pos;
370 wxIPV4address addr;
371
372 tmp_str = url_proxy;
373 pos = tmp_str.Find(wxT(':'));
374 // This is an invalid proxy name.
cb719f2e 375 if (pos == wxNOT_FOUND)
b2b35524
VZ
376 return;
377
378 hostname = tmp_str(0, pos);
bb80bb5b 379 port = tmp_str(pos+1, tmp_str.Length()-pos);
b2b35524
VZ
380
381 addr.Hostname(hostname);
382 addr.Service(port);
383
384 // Finally, create the whole stuff.
385 if (m_proxy && m_proxy != ms_proxyDefault)
386 delete m_proxy;
387 m_proxy = new wxHTTP();
cb719f2e 388 m_proxy->Connect(addr, true); // Watcom needs the 2nd arg for some reason
b2b35524
VZ
389
390 CleanData();
391 // Reparse url.
cb719f2e 392 m_useProxy = true;
b2b35524
VZ
393 ParseURL();
394 }
f4ada568 395}
ce195ee6 396#endif // wxUSE_PROTOCOL_HTTP
35a4dab7 397
b2b35524 398// ----------------------------------------------------------------------
ce321570
RN
399// wxURLModule
400//
b2b35524
VZ
401// A module which deletes the default proxy if we created it
402// ----------------------------------------------------------------------
403
404#if wxUSE_SOCKETS
405
406class wxURLModule : public wxModule
407{
408public:
409 virtual bool OnInit();
410 virtual void OnExit();
411
412private:
413 DECLARE_DYNAMIC_CLASS(wxURLModule)
414};
415
416IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
417
418bool wxURLModule::OnInit()
419{
ce195ee6 420#if wxUSE_PROTOCOL_HTTP
b2b35524
VZ
421 // env var HTTP_PROXY contains the address of the default proxy to use if
422 // set, but don't try to create this proxy right now because it will slow
423 // down the program startup (especially if there is no DNS server
424 // available, in which case it may take up to 1 minute)
f6bcfd97 425
67acaf80 426 if ( wxGetenv(_T("HTTP_PROXY")) )
b2b35524 427 {
cb719f2e 428 wxURL::ms_useDefaultProxy = true;
b2b35524 429 }
ce195ee6 430#endif // wxUSE_PROTOCOL_HTTP
cb719f2e 431 return true;
b2b35524
VZ
432}
433
434void wxURLModule::OnExit()
435{
ce195ee6 436#if wxUSE_PROTOCOL_HTTP
b2b35524
VZ
437 delete wxURL::ms_proxyDefault;
438 wxURL::ms_proxyDefault = NULL;
ce195ee6 439#endif // wxUSE_PROTOCOL_HTTP
b2b35524
VZ
440}
441
442#endif // wxUSE_SOCKETS
a5d46b73
VZ
443
444#endif // wxUSE_URL
445