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