]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
makefiles update
[wxWidgets.git] / src / common / url.cpp
CommitLineData
f4ada568
GL
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
55d99c7a 9// Licence: wxWindows licence
f4ada568
GL
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f4ada568
GL
13#pragma implementation "url.h"
14#endif
fcc6dddd
JS
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
a5d46b73 23#if wxUSE_URL
f4ada568 24
3096bd2f
VZ
25#include "wx/string.h"
26#include "wx/list.h"
27#include "wx/utils.h"
c092213d 28#include "wx/module.h"
3096bd2f 29#include "wx/url.h"
f4ada568 30
a5d46b73
VZ
31#include <string.h>
32#include <ctype.h>
33
f4ada568
GL
34IMPLEMENT_CLASS(wxProtoInfo, wxObject)
35IMPLEMENT_CLASS(wxURL, wxObject)
f4ada568
GL
36
37// Protocols list
b2b35524 38wxProtoInfo *wxURL::ms_protocols = NULL;
8a4df159 39
f92f546c 40// Enforce linking of protocol classes:
f92f546c
VS
41USE_PROTOCOL(wxFileProto)
42
8a4df159 43#if wxUSE_SOCKETS
f80eabe5
JS
44USE_PROTOCOL(wxHTTP)
45USE_PROTOCOL(wxFTP)
46
b2b35524
VZ
47 wxHTTP *wxURL::ms_proxyDefault = NULL;
48 bool wxURL::ms_useDefaultProxy = FALSE;
8a4df159 49#endif
f4ada568 50
fae05df5
GL
51// --------------------------------------------------------------
52// wxURL
53// --------------------------------------------------------------
f4ada568 54
fae05df5
GL
55// --------------------------------------------------------------
56// --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
57// --------------------------------------------------------------
f4ada568
GL
58
59wxURL::wxURL(const wxString& url)
60{
b2b35524
VZ
61 m_protocol = NULL;
62 m_error = wxURL_NOERR;
63 m_url = url;
64
8a4df159 65#if wxUSE_SOCKETS
b2b35524
VZ
66 if ( ms_useDefaultProxy && !ms_proxyDefault )
67 {
2b5f62a0 68 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
b2b35524
VZ
69
70 if ( !ms_proxyDefault )
71 {
72 // don't try again
73 ms_useDefaultProxy = FALSE;
74 }
75 }
76
77 m_useProxy = ms_proxyDefault != NULL;
78 m_proxy = ms_proxyDefault;
79#endif // wxUSE_SOCKETS
80
81 ParseURL();
f4ada568
GL
82}
83
84bool wxURL::ParseURL()
85{
86 wxString last_url = m_url;
87
f6bcfd97
BP
88 // If the URL was already parsed (m_protocol != NULL), pass this section.
89 if (!m_protocol)
90 {
f61815af
GL
91 // Clean up
92 CleanData();
f4ada568 93
f61815af 94 // Extract protocol name
f6bcfd97
BP
95 if (!PrepProto(last_url))
96 {
f4ada568
GL
97 m_error = wxURL_SNTXERR;
98 return FALSE;
99 }
f61815af
GL
100
101 // Find and create the protocol object
f6bcfd97
BP
102 if (!FetchProtocol())
103 {
f61815af
GL
104 m_error = wxURL_NOPROTO;
105 return FALSE;
106 }
107
108 // Do we need a host name ?
f6bcfd97
BP
109 if (m_protoinfo->m_needhost)
110 {
f61815af 111 // Extract it
f6bcfd97
BP
112 if (!PrepHost(last_url))
113 {
f61815af
GL
114 m_error = wxURL_SNTXERR;
115 return FALSE;
116 }
117 }
118
119 // Extract full path
f6bcfd97
BP
120 if (!PrepPath(last_url))
121 {
f61815af
GL
122 m_error = wxURL_NOPATH;
123 return FALSE;
124 }
f4ada568 125 }
f61815af 126 // URL parse finished.
f4ada568 127
8a4df159 128#if wxUSE_SOCKETS
f6bcfd97
BP
129 if (m_useProxy)
130 {
f61815af
GL
131 // We destroy the newly created protocol.
132 CleanData();
133
134 // Third, we rebuild the URL.
223d09f6 135 m_url = m_protoname + wxT(":");
f61815af 136 if (m_protoinfo->m_needhost)
223d09f6 137 m_url = m_url + wxT("//") + m_hostname;
f61815af
GL
138
139 m_url += m_path;
140
141 // We initialize specific variables.
142 m_protocol = m_proxy; // FIXME: we should clone the protocol
f4ada568 143 }
8a4df159 144#endif
f4ada568
GL
145
146 m_error = wxURL_NOERR;
147 return TRUE;
148}
149
150void wxURL::CleanData()
151{
8a4df159 152#if wxUSE_SOCKETS
f61815af 153 if (!m_useProxy)
8a4df159 154#endif
f4ada568
GL
155 delete m_protocol;
156}
157
158wxURL::~wxURL()
159{
160 CleanData();
8a4df159 161#if wxUSE_SOCKETS
b2b35524 162 if (m_proxy && m_proxy != ms_proxyDefault)
f61815af 163 delete m_proxy;
8a4df159 164#endif
f4ada568
GL
165}
166
fae05df5
GL
167// --------------------------------------------------------------
168// --------- wxURL urls decoders --------------------------------
169// --------------------------------------------------------------
170
f4ada568
GL
171bool wxURL::PrepProto(wxString& url)
172{
173 int pos;
174
175 // Find end
223d09f6 176 pos = url.Find(wxT(':'));
f4ada568
GL
177 if (pos == -1)
178 return FALSE;
179
180 m_protoname = url(0, pos);
181
182 url = url(pos+1, url.Length());
183
184 return TRUE;
185}
186
187bool wxURL::PrepHost(wxString& url)
188{
856d2e52 189 wxString temp_url;
f4ada568
GL
190 int pos, pos2;
191
58c837a4 192 if ((url.GetChar(0) != wxT('/')) || (url.GetChar(1) != wxT('/')))
f4ada568
GL
193 return FALSE;
194
195 url = url(2, url.Length());
196
223d09f6 197 pos = url.Find(wxT('/'));
f4ada568 198 if (pos == -1)
b7db6f0b 199 pos = url.Length();
f4ada568 200
856d2e52
GL
201 if (pos == 0)
202 return FALSE;
203
204 temp_url = url(0, pos);
223d09f6 205 url = url(url.Find(wxT('/')), url.Length());
856d2e52
GL
206
207 // Retrieve service number
223d09f6 208 pos2 = temp_url.Find(wxT(':'), TRUE);
f6bcfd97
BP
209 if (pos2 != -1 && pos2 < pos)
210 {
375abe3d 211 m_servname = temp_url(pos2+1, pos);
f4ada568
GL
212 if (!m_servname.IsNumber())
213 return FALSE;
856d2e52 214 temp_url = temp_url(0, pos2);
f4ada568
GL
215 }
216
856d2e52 217 // Retrieve user and password.
223d09f6 218 pos2 = temp_url.Find(wxT('@'));
856d2e52
GL
219 // Even if pos2 equals -1, this code is right.
220 m_hostname = temp_url(pos2+1, temp_url.Length());
f4ada568 221
223d09f6
KB
222 m_user = wxT("");
223 m_password = wxT("");
856d2e52
GL
224
225 if (pos2 == -1)
226 return TRUE;
227
228 temp_url = temp_url(0, pos2);
223d09f6 229 pos2 = temp_url.Find(wxT(':'));
856d2e52
GL
230
231 if (pos2 == -1)
232 return FALSE;
233
234 m_user = temp_url(0, pos2);
235 m_password = temp_url(pos2+1, url.Length());
f4ada568
GL
236
237 return TRUE;
238}
239
240bool wxURL::PrepPath(wxString& url)
241{
242 if (url.Length() != 0)
f61815af 243 m_path = ConvertToValidURI(url);
f4ada568 244 else
223d09f6 245 m_path = wxT("/");
f4ada568
GL
246 return TRUE;
247}
248
249bool wxURL::FetchProtocol()
250{
b2b35524 251 wxProtoInfo *info = ms_protocols;
f4ada568 252
f6bcfd97
BP
253 while (info)
254 {
255 if (m_protoname == info->m_protoname)
256 {
f4ada568
GL
257 if (m_servname.IsNull())
258 m_servname = info->m_servname;
259
260 m_protoinfo = info;
261 m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
f4ada568
GL
262 return TRUE;
263 }
264 info = info->next;
265 }
266 return FALSE;
267}
268
fae05df5
GL
269// --------------------------------------------------------------
270// --------- wxURL get ------------------------------------------
271// --------------------------------------------------------------
272
58c837a4 273wxInputStream *wxURL::GetInputStream()
f4ada568 274{
f6bcfd97
BP
275 if (!m_protocol)
276 {
f4ada568
GL
277 m_error = wxURL_NOPROTO;
278 return NULL;
279 }
280
281 m_error = wxURL_NOERR;
f6bcfd97
BP
282 if (m_user != wxT(""))
283 {
856d2e52
GL
284 m_protocol->SetUser(m_user);
285 m_protocol->SetPassword(m_password);
286 }
287
8a4df159 288#if wxUSE_SOCKETS
19e0e04b
RD
289 wxIPV4address addr;
290
f61815af 291 // m_protoinfo is NULL when we use a proxy
f6bcfd97
BP
292 if (!m_useProxy && m_protoinfo->m_needhost)
293 {
294 if (!addr.Hostname(m_hostname))
295 {
f4ada568
GL
296 m_error = wxURL_NOHOST;
297 return NULL;
298 }
299
300 addr.Service(m_servname);
301
8a2c6ef8
JS
302 if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason
303 {
f4ada568
GL
304 m_error = wxURL_CONNERR;
305 return NULL;
306 }
307 }
8a4df159 308#endif
f4ada568 309
f61815af 310 // When we use a proxy, we have to pass the whole URL to it.
999836aa
VZ
311 wxInputStream *the_i_stream =
312 (m_useProxy) ? m_protocol->GetInputStream(m_url) :
313 m_protocol->GetInputStream(m_path);
f61815af 314
f6bcfd97
BP
315 if (!the_i_stream)
316 {
f4ada568
GL
317 m_error = wxURL_PROTOERR;
318 return NULL;
319 }
320
321 return the_i_stream;
322}
323
8a4df159 324#if wxUSE_SOCKETS
f4ada568
GL
325void wxURL::SetDefaultProxy(const wxString& url_proxy)
326{
b2b35524
VZ
327 if ( !url_proxy )
328 {
329 if ( ms_proxyDefault )
330 {
331 ms_proxyDefault->Close();
332 delete ms_proxyDefault;
333 ms_proxyDefault = NULL;
334 }
f61815af 335 }
f61815af 336 else
b2b35524
VZ
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 }
f4ada568
GL
359}
360
361void wxURL::SetProxy(const wxString& url_proxy)
362{
b2b35524
VZ
363 if ( !url_proxy )
364 {
365 if ( m_proxy && m_proxy != ms_proxyDefault )
366 {
367 m_proxy->Close();
368 delete m_proxy;
369 }
f4ada568 370
b2b35524
VZ
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);
bb80bb5b 387 port = tmp_str(pos+1, tmp_str.Length()-pos);
b2b35524
VZ
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 }
f4ada568 403}
b2b35524 404#endif // wxUSE_SOCKETS
35a4dab7 405
f6bcfd97 406wxString wxURL::ConvertToValidURI(const wxString& uri, const wxChar* delims)
14906731 407{
fae05df5
GL
408 wxString out_str;
409 wxString hexa_code;
410 size_t i;
411
f6bcfd97
BP
412 for (i = 0; i < uri.Len(); i++)
413 {
fae05df5
GL
414 wxChar c = uri.GetChar(i);
415
223d09f6 416 if (c == wxT(' '))
f6bcfd97
BP
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 {
223d09f6 438 hexa_code.Printf(wxT("%%%02X"), c);
5a96d2f4 439 out_str += hexa_code;
f6bcfd97
BP
440 }
441 else
442 {
5a96d2f4 443 out_str += c;
f6bcfd97 444 }
5a96d2f4 445 }
fae05df5 446 }
19e0e04b 447
fae05df5 448 return out_str;
14906731
GL
449}
450
aa6d9706
GL
451wxString wxURL::ConvertFromURI(const wxString& uri)
452{
aa6d9706
GL
453 wxString new_uri;
454
74b31181 455 size_t i = 0;
f6bcfd97
BP
456 while (i < uri.Len())
457 {
74b31181 458 int code;
f6bcfd97
BP
459 if (uri[i] == wxT('%'))
460 {
aa6d9706 461 i++;
223d09f6
KB
462 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
463 code = (uri[i] - wxT('A') + 10) * 16;
a03c55c1
VZ
464 else if (uri[i] >= wxT('a') && uri[i] <= wxT('f'))
465 code = (uri[i] - wxT('a') + 10) * 16;
aa6d9706 466 else
223d09f6 467 code = (uri[i] - wxT('0')) * 16;
f6bcfd97 468
aa6d9706 469 i++;
223d09f6
KB
470 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
471 code += (uri[i] - wxT('A')) + 10;
a03c55c1
VZ
472 else if (uri[i] >= wxT('a') && uri[i] <= wxT('f'))
473 code += (uri[i] - wxT('a')) + 10;
aa6d9706 474 else
223d09f6 475 code += (uri[i] - wxT('0'));
f6bcfd97 476
aa6d9706
GL
477 i++;
478 new_uri += (wxChar)code;
479 continue;
480 }
481 new_uri += uri[i];
482 i++;
483 }
484 return new_uri;
485}
b2b35524
VZ
486
487// ----------------------------------------------------------------------
488// A module which deletes the default proxy if we created it
489// ----------------------------------------------------------------------
490
491#if wxUSE_SOCKETS
492
493class wxURLModule : public wxModule
494{
495public:
496 virtual bool OnInit();
497 virtual void OnExit();
498
499private:
500 DECLARE_DYNAMIC_CLASS(wxURLModule)
501};
502
503IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule)
504
505bool wxURLModule::OnInit()
506{
507 // env var HTTP_PROXY contains the address of the default proxy to use if
508 // set, but don't try to create this proxy right now because it will slow
509 // down the program startup (especially if there is no DNS server
510 // available, in which case it may take up to 1 minute)
f6bcfd97 511
b2b35524
VZ
512 if ( getenv("HTTP_PROXY") )
513 {
514 wxURL::ms_useDefaultProxy = TRUE;
515 }
516
517 return TRUE;
518}
519
520void wxURLModule::OnExit()
521{
522 delete wxURL::ms_proxyDefault;
523 wxURL::ms_proxyDefault = NULL;
524}
525
526#endif // wxUSE_SOCKETS
a5d46b73
VZ
527
528#endif // wxUSE_URL
529