]> git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
Include wx/list.h according to precompiled headers of wx/wx.h (with other minor clean...
[wxWidgets.git] / src / common / url.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_URL
20
21 #ifndef WX_PRECOMP
22 #include "wx/list.h"
23 #endif
24
25 #include "wx/string.h"
26 #include "wx/utils.h"
27 #include "wx/module.h"
28 #include "wx/url.h"
29
30 #include <string.h>
31 #include <ctype.h>
32
33 IMPLEMENT_CLASS(wxProtoInfo, wxObject)
34 IMPLEMENT_CLASS(wxURL, wxURI)
35
36 // Protocols list
37 wxProtoInfo *wxURL::ms_protocols = NULL;
38
39 // Enforce linking of protocol classes:
40 USE_PROTOCOL(wxFileProto)
41
42 #if wxUSE_PROTOCOL_HTTP
43 USE_PROTOCOL(wxHTTP)
44
45 wxHTTP *wxURL::ms_proxyDefault = NULL;
46 bool wxURL::ms_useDefaultProxy = false;
47 #endif
48
49 #if wxUSE_PROTOCOL_FTP
50 USE_PROTOCOL(wxFTP)
51 #endif
52
53 // --------------------------------------------------------------
54 //
55 // wxURL
56 //
57 // --------------------------------------------------------------
58
59 // --------------------------------------------------------------
60 // Construction
61 // --------------------------------------------------------------
62
63 wxURL::wxURL(const wxString& url) : wxURI(url)
64 {
65 Init(url);
66 ParseURL();
67 }
68
69 wxURL::wxURL(const wxURI& url) : wxURI(url)
70 {
71 Init(url.BuildURI());
72 ParseURL();
73 }
74
75 void wxURL::Init(const wxString& url)
76 {
77 m_protocol = NULL;
78 m_error = wxURL_NOERR;
79 m_url = url;
80 #if wxUSE_URL_NATIVE
81 m_nativeImp = CreateNativeImpObject();
82 #endif
83
84 #if wxUSE_PROTOCOL_HTTP
85 if ( ms_useDefaultProxy && !ms_proxyDefault )
86 {
87 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
88
89 if ( !ms_proxyDefault )
90 {
91 // don't try again
92 ms_useDefaultProxy = false;
93 }
94 }
95
96 m_useProxy = ms_proxyDefault != NULL;
97 m_proxy = ms_proxyDefault;
98 #endif // wxUSE_PROTOCOL_HTTP
99
100 }
101
102 // --------------------------------------------------------------
103 // Assignment
104 // --------------------------------------------------------------
105
106 wxURL& wxURL::operator = (const wxURI& url)
107 {
108 wxURI::operator = (url);
109 Init(url.BuildURI());
110 ParseURL();
111 return *this;
112 }
113 wxURL& wxURL::operator = (const wxString& url)
114 {
115 wxURI::operator = (url);
116 Init(url);
117 ParseURL();
118 return *this;
119 }
120
121 // --------------------------------------------------------------
122 // ParseURL
123 //
124 // Builds the URL and takes care of the old protocol stuff
125 // --------------------------------------------------------------
126
127 bool wxURL::ParseURL()
128 {
129 // If the URL was already parsed (m_protocol != NULL), pass this section.
130 if (!m_protocol)
131 {
132 // Clean up
133 CleanData();
134
135 // Make sure we have a protocol/scheme
136 if (!HasScheme())
137 {
138 m_error = wxURL_SNTXERR;
139 return false;
140 }
141
142 // Find and create the protocol object
143 if (!FetchProtocol())
144 {
145 m_error = wxURL_NOPROTO;
146 return false;
147 }
148
149 // Do we need a host name ?
150 if (m_protoinfo->m_needhost)
151 {
152 // Make sure we have one, then
153 if (!HasServer())
154 {
155 m_error = wxURL_SNTXERR;
156 return false;
157 }
158 }
159 }
160
161 #if wxUSE_PROTOCOL_HTTP
162 if (m_useProxy)
163 {
164 // Third, we rebuild the URL.
165 m_url = m_scheme + wxT(":");
166 if (m_protoinfo->m_needhost)
167 m_url = m_url + wxT("//") + m_server;
168
169 // We initialize specific variables.
170 m_protocol = m_proxy; // FIXME: we should clone the protocol
171 }
172 #endif // wxUSE_PROTOCOL_HTTP
173
174 m_error = wxURL_NOERR;
175 return true;
176 }
177
178 // --------------------------------------------------------------
179 // Destruction/Cleanup
180 // --------------------------------------------------------------
181
182 void wxURL::CleanData()
183 {
184 #if wxUSE_PROTOCOL_HTTP
185 if (!m_useProxy)
186 #endif // wxUSE_PROTOCOL_HTTP
187 delete m_protocol;
188 }
189
190 wxURL::~wxURL()
191 {
192 CleanData();
193 #if wxUSE_PROTOCOL_HTTP
194 if (m_proxy && m_proxy != ms_proxyDefault)
195 delete m_proxy;
196 #endif // wxUSE_PROTOCOL_HTTP
197 #if wxUSE_URL_NATIVE
198 delete m_nativeImp;
199 #endif
200 }
201
202 // --------------------------------------------------------------
203 // FetchProtocol
204 // --------------------------------------------------------------
205
206 bool wxURL::FetchProtocol()
207 {
208 wxProtoInfo *info = ms_protocols;
209
210 while (info)
211 {
212 if (m_scheme == info->m_protoname)
213 {
214 if (m_port.IsNull())
215 m_port = info->m_servname;
216 m_protoinfo = info;
217 m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
218 return true;
219 }
220 info = info->next;
221 }
222 return false;
223 }
224
225 // --------------------------------------------------------------
226 // GetInputStream
227 // --------------------------------------------------------------
228
229 wxInputStream *wxURL::GetInputStream()
230 {
231 if (!m_protocol)
232 {
233 m_error = wxURL_NOPROTO;
234 return NULL;
235 }
236
237 m_error = wxURL_NOERR;
238 if (HasUserInfo())
239 {
240 size_t dwPasswordPos = m_userinfo.find(':');
241
242 if (dwPasswordPos == wxString::npos)
243 m_protocol->SetUser(m_userinfo);
244 else
245 {
246 m_protocol->SetUser(m_userinfo(0, dwPasswordPos));
247 m_protocol->SetPassword(m_userinfo(dwPasswordPos+1, m_userinfo.length() + 1));
248 }
249 }
250
251 #if wxUSE_URL_NATIVE
252 // give the native implementation to return a better stream
253 // such as the native WinINet functionality under MS-Windows
254 if (m_nativeImp)
255 {
256 wxInputStream *rc;
257 rc = m_nativeImp->GetInputStream(this);
258 if (rc != 0)
259 return rc;
260 }
261 // else use the standard behaviour
262 #endif // wxUSE_URL_NATIVE
263
264 #if wxUSE_SOCKETS
265 wxIPV4address addr;
266
267 // m_protoinfo is NULL when we use a proxy
268 if (!m_useProxy && m_protoinfo->m_needhost)
269 {
270 if (!addr.Hostname(m_server))
271 {
272 m_error = wxURL_NOHOST;
273 return NULL;
274 }
275
276 addr.Service(m_port);
277
278 if (!m_protocol->Connect(addr, true)) // Watcom needs the 2nd arg for some reason
279 {
280 m_error = wxURL_CONNERR;
281 return NULL;
282 }
283 }
284 #endif
285
286 wxString fullPath;
287
288 // When we use a proxy, we have to pass the whole URL to it.
289 if (m_useProxy)
290 fullPath += m_url;
291
292 if(m_path.empty())
293 fullPath += wxT("/");
294 else
295 fullPath += m_path;
296
297 if (HasQuery())
298 fullPath += wxT("?") + m_query;
299
300 if (HasFragment())
301 fullPath += wxT("#") + m_fragment;
302
303 wxInputStream *the_i_stream = m_protocol->GetInputStream(fullPath);
304
305 if (!the_i_stream)
306 {
307 m_error = wxURL_PROTOERR;
308 return NULL;
309 }
310
311 return the_i_stream;
312 }
313
314 #if wxUSE_PROTOCOL_HTTP
315 void wxURL::SetDefaultProxy(const wxString& url_proxy)
316 {
317 if ( !url_proxy )
318 {
319 if ( ms_proxyDefault )
320 {
321 ms_proxyDefault->Close();
322 delete ms_proxyDefault;
323 ms_proxyDefault = NULL;
324 }
325 }
326 else
327 {
328 wxString tmp_str = url_proxy;
329 int pos = tmp_str.Find(wxT(':'));
330 if (pos == wxNOT_FOUND)
331 return;
332
333 wxString hostname = tmp_str(0, pos),
334 port = tmp_str(pos+1, tmp_str.length()-pos);
335 wxIPV4address addr;
336
337 if (!addr.Hostname(hostname))
338 return;
339 if (!addr.Service(port))
340 return;
341
342 if (ms_proxyDefault)
343 // Finally, when all is right, we connect the new proxy.
344 ms_proxyDefault->Close();
345 else
346 ms_proxyDefault = new wxHTTP();
347 ms_proxyDefault->Connect(addr, true); // Watcom needs the 2nd arg for some reason
348 }
349 }
350
351 void wxURL::SetProxy(const wxString& url_proxy)
352 {
353 if ( !url_proxy )
354 {
355 if ( m_proxy && m_proxy != ms_proxyDefault )
356 {
357 m_proxy->Close();
358 delete m_proxy;
359 }
360
361 m_useProxy = false;
362 }
363 else
364 {
365 wxString tmp_str;
366 wxString hostname, port;
367 int pos;
368 wxIPV4address addr;
369
370 tmp_str = url_proxy;
371 pos = tmp_str.Find(wxT(':'));
372 // This is an invalid proxy name.
373 if (pos == wxNOT_FOUND)
374 return;
375
376 hostname = tmp_str(0, pos);
377 port = tmp_str(pos+1, tmp_str.length()-pos);
378
379 addr.Hostname(hostname);
380 addr.Service(port);
381
382 // Finally, create the whole stuff.
383 if (m_proxy && m_proxy != ms_proxyDefault)
384 delete m_proxy;
385 m_proxy = new wxHTTP();
386 m_proxy->Connect(addr, true); // Watcom needs the 2nd arg for some reason
387
388 CleanData();
389 // Reparse url.
390 m_useProxy = true;
391 ParseURL();
392 }
393 }
394 #endif // wxUSE_PROTOCOL_HTTP
395
396 // ----------------------------------------------------------------------
397 // wxURLModule
398 //
399 // A module which deletes the default proxy if we created it
400 // ----------------------------------------------------------------------
401
402 #if wxUSE_SOCKETS
403
404 class wxURLModule : public wxModule
405 {
406 public:
407 virtual bool OnInit();
408 virtual void OnExit();
409
410 private:
411 DECLARE_DYNAMIC_CLASS(wxURLModule)
412 };
413
414 IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
415
416 bool wxURLModule::OnInit()
417 {
418 #if wxUSE_PROTOCOL_HTTP
419 // env var HTTP_PROXY contains the address of the default proxy to use if
420 // set, but don't try to create this proxy right now because it will slow
421 // down the program startup (especially if there is no DNS server
422 // available, in which case it may take up to 1 minute)
423
424 if ( wxGetenv(_T("HTTP_PROXY")) )
425 {
426 wxURL::ms_useDefaultProxy = true;
427 }
428 #endif // wxUSE_PROTOCOL_HTTP
429 return true;
430 }
431
432 void wxURLModule::OnExit()
433 {
434 #if wxUSE_PROTOCOL_HTTP
435 delete wxURL::ms_proxyDefault;
436 wxURL::ms_proxyDefault = NULL;
437 #endif // wxUSE_PROTOCOL_HTTP
438 }
439
440 #endif // wxUSE_SOCKETS
441
442 // ---------------------------------------------------------------------------
443 //
444 // wxURL Compatibility
445 //
446 // ---------------------------------------------------------------------------
447
448 #if WXWIN_COMPATIBILITY_2_4
449
450 #include "wx/url.h"
451
452 wxString wxURL::GetProtocolName() const
453 {
454 return m_scheme;
455 }
456
457 wxString wxURL::GetHostName() const
458 {
459 return m_server;
460 }
461
462 wxString wxURL::GetPath() const
463 {
464 return m_path;
465 }
466
467 //Note that this old code really doesn't convert to a URI that well and looks
468 //more like a dirty hack than anything else...
469
470 wxString wxURL::ConvertToValidURI(const wxString& uri, const wxChar* delims)
471 {
472 wxString out_str;
473 wxString hexa_code;
474 size_t i;
475
476 for (i = 0; i < uri.Len(); i++)
477 {
478 wxChar c = uri.GetChar(i);
479
480 if (c == wxT(' '))
481 {
482 // GRG, Apr/2000: changed to "%20" instead of '+'
483
484 out_str += wxT("%20");
485 }
486 else
487 {
488 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
489 //
490 // - Alphanumeric characters are never escaped
491 // - Unreserved marks are never escaped
492 // - Delimiters must be escaped if they appear within a component
493 // but not if they are used to separate components. Here we have
494 // no clear way to distinguish between these two cases, so they
495 // are escaped unless they are passed in the 'delims' parameter
496 // (allowed delimiters).
497
498 static const wxChar marks[] = wxT("-_.!~*()'");
499
500 if ( !wxIsalnum(c) && !wxStrchr(marks, c) && !wxStrchr(delims, c) )
501 {
502 hexa_code.Printf(wxT("%%%02X"), c);
503 out_str += hexa_code;
504 }
505 else
506 {
507 out_str += c;
508 }
509 }
510 }
511
512 return out_str;
513 }
514
515 wxString wxURL::ConvertFromURI(const wxString& uri)
516 {
517 return wxURI::Unescape(uri);
518 }
519
520 #endif //WXWIN_COMPATIBILITY_2_4
521
522 #endif // wxUSE_URL