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