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