]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
Better alphabetized the event handler macros
[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
8a4df159 43#if wxUSE_SOCKETS
f80eabe5
JS
44USE_PROTOCOL(wxHTTP)
45USE_PROTOCOL(wxFTP)
46
b2b35524 47 wxHTTP *wxURL::ms_proxyDefault = NULL;
cb719f2e 48 bool wxURL::ms_useDefaultProxy = false;
8a4df159 49#endif
f4ada568 50
fae05df5 51// --------------------------------------------------------------
b60b2ec8
RN
52//
53// wxURL
54//
fae05df5 55// --------------------------------------------------------------
f4ada568 56
ce321570
RN
57// --------------------------------------------------------------
58// Construction
59// --------------------------------------------------------------
60
b60b2ec8
RN
61wxURL::wxURL(const wxString& url) : wxURI(url)
62{
63 Init(url);
64 ParseURL();
65}
66
67wxURL::wxURL(const wxURI& url) : wxURI(url)
68{
86470d43 69 Init(url.BuildURI());
b60b2ec8
RN
70 ParseURL();
71}
72
b60b2ec8 73void wxURL::Init(const wxString& url)
f4ada568 74{
b2b35524
VZ
75 m_protocol = NULL;
76 m_error = wxURL_NOERR;
77 m_url = url;
25959b95
VZ
78#if wxUSE_URL_NATIVE
79 m_nativeImp = CreateNativeImpObject();
80#endif
b2b35524 81
8a4df159 82#if wxUSE_SOCKETS
b2b35524
VZ
83 if ( ms_useDefaultProxy && !ms_proxyDefault )
84 {
2b5f62a0 85 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
b2b35524
VZ
86
87 if ( !ms_proxyDefault )
88 {
89 // don't try again
cb719f2e 90 ms_useDefaultProxy = false;
b2b35524
VZ
91 }
92 }
93
94 m_useProxy = ms_proxyDefault != NULL;
95 m_proxy = ms_proxyDefault;
96#endif // wxUSE_SOCKETS
97
f4ada568 98}
ce321570
RN
99
100// --------------------------------------------------------------
101// Assignment
102// --------------------------------------------------------------
103
104wxURL& wxURL::operator = (const wxURI& url)
105{
106 wxURI::operator = (url);
107 Init(url.BuildURI());
108 ParseURL();
109 return *this;
110}
111wxURL& wxURL::operator = (const wxString& url)
112{
113 wxURI::operator = (url);
114 Init(url);
115 ParseURL();
116 return *this;
117}
118
b60b2ec8
RN
119// --------------------------------------------------------------
120// ParseURL
121//
122// Builds the URL and takes care of the old protocol stuff
123// --------------------------------------------------------------
f4ada568
GL
124
125bool wxURL::ParseURL()
126{
f6bcfd97
BP
127 // If the URL was already parsed (m_protocol != NULL), pass this section.
128 if (!m_protocol)
129 {
f61815af
GL
130 // Clean up
131 CleanData();
f4ada568 132
b60b2ec8
RN
133 // Make sure we have a protocol/scheme
134 if (!HasScheme())
f6bcfd97 135 {
f4ada568 136 m_error = wxURL_SNTXERR;
cb719f2e 137 return false;
f4ada568 138 }
f61815af
GL
139
140 // Find and create the protocol object
f6bcfd97
BP
141 if (!FetchProtocol())
142 {
f61815af 143 m_error = wxURL_NOPROTO;
cb719f2e 144 return false;
f61815af
GL
145 }
146
147 // Do we need a host name ?
f6bcfd97
BP
148 if (m_protoinfo->m_needhost)
149 {
b60b2ec8
RN
150 // Make sure we have one, then
151 if (!HasServer())
f6bcfd97 152 {
f61815af 153 m_error = wxURL_SNTXERR;
cb719f2e 154 return false;
f61815af
GL
155 }
156 }
f4ada568
GL
157 }
158
8a4df159 159#if wxUSE_SOCKETS
f6bcfd97
BP
160 if (m_useProxy)
161 {
0c2a5de2
VZ
162 // destroy the previously created protocol as we'll be using m_proxy
163 delete m_protocol;
f61815af
GL
164
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 }
8a4df159 173#endif
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{
8a4df159 185#if wxUSE_SOCKETS
f61815af 186 if (!m_useProxy)
8a4df159 187#endif
f4ada568
GL
188 delete m_protocol;
189}
190
191wxURL::~wxURL()
192{
25959b95 193 CleanData();
8a4df159 194#if wxUSE_SOCKETS
25959b95
VZ
195 if (m_proxy && m_proxy != ms_proxyDefault)
196 delete m_proxy;
197#endif
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;
b60b2ec8 239 if (HasUser())
f6bcfd97 240 {
b60b2ec8
RN
241 size_t dwPasswordPos = m_user.find(':');
242
243 if (dwPasswordPos == wxString::npos)
244 m_protocol->SetUser(m_user);
245 else
246 {
247 m_protocol->SetUser(m_user(0, dwPasswordPos));
248 m_protocol->SetPassword(m_user(dwPasswordPos+1, m_user.length() + 1));
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.
cb719f2e 288 wxInputStream *the_i_stream =
999836aa
VZ
289 (m_useProxy) ? m_protocol->GetInputStream(m_url) :
290 m_protocol->GetInputStream(m_path);
f61815af 291
f6bcfd97
BP
292 if (!the_i_stream)
293 {
f4ada568
GL
294 m_error = wxURL_PROTOERR;
295 return NULL;
296 }
297
298 return the_i_stream;
299}
300
8a4df159 301#if wxUSE_SOCKETS
f4ada568
GL
302void wxURL::SetDefaultProxy(const wxString& url_proxy)
303{
b2b35524
VZ
304 if ( !url_proxy )
305 {
306 if ( ms_proxyDefault )
307 {
308 ms_proxyDefault->Close();
309 delete ms_proxyDefault;
310 ms_proxyDefault = NULL;
311 }
f61815af 312 }
f61815af 313 else
b2b35524
VZ
314 {
315 wxString tmp_str = url_proxy;
316 int pos = tmp_str.Find(wxT(':'));
cb719f2e 317 if (pos == wxNOT_FOUND)
b2b35524
VZ
318 return;
319
320 wxString hostname = tmp_str(0, pos),
321 port = tmp_str(pos+1, tmp_str.Length()-pos);
322 wxIPV4address addr;
323
324 if (!addr.Hostname(hostname))
325 return;
326 if (!addr.Service(port))
327 return;
328
329 if (ms_proxyDefault)
330 // Finally, when all is right, we connect the new proxy.
331 ms_proxyDefault->Close();
332 else
333 ms_proxyDefault = new wxHTTP();
cb719f2e 334 ms_proxyDefault->Connect(addr, true); // Watcom needs the 2nd arg for some reason
b2b35524 335 }
f4ada568
GL
336}
337
338void wxURL::SetProxy(const wxString& url_proxy)
339{
b2b35524
VZ
340 if ( !url_proxy )
341 {
342 if ( m_proxy && m_proxy != ms_proxyDefault )
343 {
344 m_proxy->Close();
345 delete m_proxy;
346 }
f4ada568 347
cb719f2e 348 m_useProxy = false;
b2b35524
VZ
349 }
350 else
351 {
352 wxString tmp_str;
353 wxString hostname, port;
354 int pos;
355 wxIPV4address addr;
356
357 tmp_str = url_proxy;
358 pos = tmp_str.Find(wxT(':'));
359 // This is an invalid proxy name.
cb719f2e 360 if (pos == wxNOT_FOUND)
b2b35524
VZ
361 return;
362
363 hostname = tmp_str(0, pos);
bb80bb5b 364 port = tmp_str(pos+1, tmp_str.Length()-pos);
b2b35524
VZ
365
366 addr.Hostname(hostname);
367 addr.Service(port);
368
369 // Finally, create the whole stuff.
370 if (m_proxy && m_proxy != ms_proxyDefault)
371 delete m_proxy;
372 m_proxy = new wxHTTP();
cb719f2e 373 m_proxy->Connect(addr, true); // Watcom needs the 2nd arg for some reason
b2b35524
VZ
374
375 CleanData();
376 // Reparse url.
cb719f2e 377 m_useProxy = true;
b2b35524
VZ
378 ParseURL();
379 }
f4ada568 380}
b2b35524 381#endif // wxUSE_SOCKETS
35a4dab7 382
b2b35524 383// ----------------------------------------------------------------------
ce321570
RN
384// wxURLModule
385//
b2b35524
VZ
386// A module which deletes the default proxy if we created it
387// ----------------------------------------------------------------------
388
389#if wxUSE_SOCKETS
390
391class wxURLModule : public wxModule
392{
393public:
394 virtual bool OnInit();
395 virtual void OnExit();
396
397private:
398 DECLARE_DYNAMIC_CLASS(wxURLModule)
399};
400
401IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
402
403bool wxURLModule::OnInit()
404{
405 // env var HTTP_PROXY contains the address of the default proxy to use if
406 // set, but don't try to create this proxy right now because it will slow
407 // down the program startup (especially if there is no DNS server
408 // available, in which case it may take up to 1 minute)
f6bcfd97 409
b2b35524
VZ
410 if ( getenv("HTTP_PROXY") )
411 {
cb719f2e 412 wxURL::ms_useDefaultProxy = true;
b2b35524
VZ
413 }
414
cb719f2e 415 return true;
b2b35524
VZ
416}
417
418void wxURLModule::OnExit()
419{
420 delete wxURL::ms_proxyDefault;
421 wxURL::ms_proxyDefault = NULL;
422}
423
424#endif // wxUSE_SOCKETS
a5d46b73
VZ
425
426#endif // wxUSE_URL
427