]> git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
set background style to wxBG_STYLE_COLOUR in SetBackgroundColour()
[wxWidgets.git] / src / common / url.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "url.h"
14 #endif
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
23 #if wxUSE_URL
24
25 #include "wx/string.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/module.h"
29 #include "wx/url.h"
30
31 #include <string.h>
32 #include <ctype.h>
33
34 IMPLEMENT_CLASS(wxProtoInfo, wxObject)
35 IMPLEMENT_CLASS(wxURL, wxURI)
36
37 // Protocols list
38 wxProtoInfo *wxURL::ms_protocols = NULL;
39
40 // Enforce linking of protocol classes:
41 USE_PROTOCOL(wxFileProto)
42
43 #if wxUSE_SOCKETS
44 USE_PROTOCOL(wxHTTP)
45 USE_PROTOCOL(wxFTP)
46
47 wxHTTP *wxURL::ms_proxyDefault = NULL;
48 bool wxURL::ms_useDefaultProxy = false;
49 #endif
50
51 // --------------------------------------------------------------
52 //
53 // wxURL
54 //
55 // --------------------------------------------------------------
56
57 // --------------------------------------------------------------
58 // Construction
59 // --------------------------------------------------------------
60
61 wxURL::wxURL(const wxString& url) : wxURI(url)
62 {
63 Init(url);
64 ParseURL();
65 }
66
67 wxURL::wxURL(const wxURI& url) : wxURI(url)
68 {
69 Init(url.BuildURI());
70 ParseURL();
71 }
72
73 void wxURL::Init(const wxString& url)
74 {
75 m_protocol = NULL;
76 m_error = wxURL_NOERR;
77 m_url = url;
78 #if wxUSE_URL_NATIVE
79 m_nativeImp = CreateNativeImpObject();
80 #endif
81
82 #if wxUSE_SOCKETS
83 if ( ms_useDefaultProxy && !ms_proxyDefault )
84 {
85 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
86
87 if ( !ms_proxyDefault )
88 {
89 // don't try again
90 ms_useDefaultProxy = false;
91 }
92 }
93
94 m_useProxy = ms_proxyDefault != NULL;
95 m_proxy = ms_proxyDefault;
96 #endif // wxUSE_SOCKETS
97
98 }
99
100 // --------------------------------------------------------------
101 // Assignment
102 // --------------------------------------------------------------
103
104 wxURL& wxURL::operator = (const wxURI& url)
105 {
106 wxURI::operator = (url);
107 Init(url.BuildURI());
108 ParseURL();
109 return *this;
110 }
111 wxURL& wxURL::operator = (const wxString& url)
112 {
113 wxURI::operator = (url);
114 Init(url);
115 ParseURL();
116 return *this;
117 }
118
119 // --------------------------------------------------------------
120 // ParseURL
121 //
122 // Builds the URL and takes care of the old protocol stuff
123 // --------------------------------------------------------------
124
125 bool wxURL::ParseURL()
126 {
127 // If the URL was already parsed (m_protocol != NULL), pass this section.
128 if (!m_protocol)
129 {
130 // Clean up
131 CleanData();
132
133 // Make sure we have a protocol/scheme
134 if (!HasScheme())
135 {
136 m_error = wxURL_SNTXERR;
137 return false;
138 }
139
140 // Find and create the protocol object
141 if (!FetchProtocol())
142 {
143 m_error = wxURL_NOPROTO;
144 return false;
145 }
146
147 // Do we need a host name ?
148 if (m_protoinfo->m_needhost)
149 {
150 // Make sure we have one, then
151 if (!HasServer())
152 {
153 m_error = wxURL_SNTXERR;
154 return false;
155 }
156 }
157 }
158
159 #if wxUSE_SOCKETS
160 if (m_useProxy)
161 {
162 // destroy the previously created protocol as we'll be using m_proxy
163 delete m_protocol;
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 }
173 #endif
174
175 m_error = wxURL_NOERR;
176 return true;
177 }
178
179 // --------------------------------------------------------------
180 // Destruction/Cleanup
181 // --------------------------------------------------------------
182
183 void wxURL::CleanData()
184 {
185 #if wxUSE_SOCKETS
186 if (!m_useProxy)
187 #endif
188 delete m_protocol;
189 }
190
191 wxURL::~wxURL()
192 {
193 CleanData();
194 #if wxUSE_SOCKETS
195 if (m_proxy && m_proxy != ms_proxyDefault)
196 delete m_proxy;
197 #endif
198 #if wxUSE_URL_NATIVE
199 delete m_nativeImp;
200 #endif
201 }
202
203 // --------------------------------------------------------------
204 // FetchProtocol
205 // --------------------------------------------------------------
206
207 bool wxURL::FetchProtocol()
208 {
209 wxProtoInfo *info = ms_protocols;
210
211 while (info)
212 {
213 if (m_scheme == info->m_protoname)
214 {
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;
220 }
221 info = info->next;
222 }
223 return false;
224 }
225
226 // --------------------------------------------------------------
227 // GetInputStream
228 // --------------------------------------------------------------
229
230 wxInputStream *wxURL::GetInputStream()
231 {
232 if (!m_protocol)
233 {
234 m_error = wxURL_NOPROTO;
235 return NULL;
236 }
237
238 m_error = wxURL_NOERR;
239 if (HasUser())
240 {
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 }
250 }
251
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
265 #if wxUSE_SOCKETS
266 wxIPV4address addr;
267
268 // m_protoinfo is NULL when we use a proxy
269 if (!m_useProxy && m_protoinfo->m_needhost)
270 {
271 if (!addr.Hostname(m_server))
272 {
273 m_error = wxURL_NOHOST;
274 return NULL;
275 }
276
277 addr.Service(m_port);
278
279 if (!m_protocol->Connect(addr, true)) // Watcom needs the 2nd arg for some reason
280 {
281 m_error = wxURL_CONNERR;
282 return NULL;
283 }
284 }
285 #endif
286
287 // When we use a proxy, we have to pass the whole URL to it.
288 wxInputStream *the_i_stream;
289
290 if (!m_useProxy)
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 }
306
307 if (!the_i_stream)
308 {
309 m_error = wxURL_PROTOERR;
310 return NULL;
311 }
312
313 return the_i_stream;
314 }
315
316 #if wxUSE_SOCKETS
317 void wxURL::SetDefaultProxy(const wxString& url_proxy)
318 {
319 if ( !url_proxy )
320 {
321 if ( ms_proxyDefault )
322 {
323 ms_proxyDefault->Close();
324 delete ms_proxyDefault;
325 ms_proxyDefault = NULL;
326 }
327 }
328 else
329 {
330 wxString tmp_str = url_proxy;
331 int pos = tmp_str.Find(wxT(':'));
332 if (pos == wxNOT_FOUND)
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();
349 ms_proxyDefault->Connect(addr, true); // Watcom needs the 2nd arg for some reason
350 }
351 }
352
353 void wxURL::SetProxy(const wxString& url_proxy)
354 {
355 if ( !url_proxy )
356 {
357 if ( m_proxy && m_proxy != ms_proxyDefault )
358 {
359 m_proxy->Close();
360 delete m_proxy;
361 }
362
363 m_useProxy = false;
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.
375 if (pos == wxNOT_FOUND)
376 return;
377
378 hostname = tmp_str(0, pos);
379 port = tmp_str(pos+1, tmp_str.Length()-pos);
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();
388 m_proxy->Connect(addr, true); // Watcom needs the 2nd arg for some reason
389
390 CleanData();
391 // Reparse url.
392 m_useProxy = true;
393 ParseURL();
394 }
395 }
396 #endif // wxUSE_SOCKETS
397
398 // ----------------------------------------------------------------------
399 // wxURLModule
400 //
401 // A module which deletes the default proxy if we created it
402 // ----------------------------------------------------------------------
403
404 #if wxUSE_SOCKETS
405
406 class wxURLModule : public wxModule
407 {
408 public:
409 virtual bool OnInit();
410 virtual void OnExit();
411
412 private:
413 DECLARE_DYNAMIC_CLASS(wxURLModule)
414 };
415
416 IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
417
418 bool wxURLModule::OnInit()
419 {
420 // env var HTTP_PROXY contains the address of the default proxy to use if
421 // set, but don't try to create this proxy right now because it will slow
422 // down the program startup (especially if there is no DNS server
423 // available, in which case it may take up to 1 minute)
424
425 if ( getenv("HTTP_PROXY") )
426 {
427 wxURL::ms_useDefaultProxy = true;
428 }
429
430 return true;
431 }
432
433 void wxURLModule::OnExit()
434 {
435 delete wxURL::ms_proxyDefault;
436 wxURL::ms_proxyDefault = NULL;
437 }
438
439 #endif // wxUSE_SOCKETS
440
441 #endif // wxUSE_URL
442