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