]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 | ||
f4ada568 GL |
23 | #include <string.h> |
24 | #include <ctype.h> | |
25 | ||
3096bd2f VZ |
26 | #include "wx/string.h" |
27 | #include "wx/list.h" | |
28 | #include "wx/utils.h" | |
c092213d | 29 | #include "wx/module.h" |
3096bd2f | 30 | #include "wx/url.h" |
f4ada568 | 31 | |
f4ada568 GL |
32 | IMPLEMENT_CLASS(wxProtoInfo, wxObject) |
33 | IMPLEMENT_CLASS(wxURL, wxObject) | |
f4ada568 GL |
34 | |
35 | // Protocols list | |
b2b35524 | 36 | wxProtoInfo *wxURL::ms_protocols = NULL; |
8a4df159 RR |
37 | |
38 | #if wxUSE_SOCKETS | |
b2b35524 VZ |
39 | wxHTTP *wxURL::ms_proxyDefault = NULL; |
40 | bool wxURL::ms_useDefaultProxy = FALSE; | |
8a4df159 | 41 | #endif |
f4ada568 | 42 | |
fae05df5 GL |
43 | // -------------------------------------------------------------- |
44 | // wxURL | |
45 | // -------------------------------------------------------------- | |
f4ada568 | 46 | |
fae05df5 GL |
47 | // -------------------------------------------------------------- |
48 | // --------- wxURL CONSTRUCTOR DESTRUCTOR ----------------------- | |
49 | // -------------------------------------------------------------- | |
f4ada568 GL |
50 | |
51 | wxURL::wxURL(const wxString& url) | |
52 | { | |
b2b35524 VZ |
53 | m_protocol = NULL; |
54 | m_error = wxURL_NOERR; | |
55 | m_url = url; | |
56 | ||
8a4df159 | 57 | #if wxUSE_SOCKETS |
b2b35524 VZ |
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(); | |
f4ada568 GL |
74 | } |
75 | ||
76 | bool wxURL::ParseURL() | |
77 | { | |
78 | wxString last_url = m_url; | |
79 | ||
f61815af GL |
80 | // If the URL was already parsed (so m_protocol != NULL), we pass this section. |
81 | if (!m_protocol) { | |
f4ada568 | 82 | |
f61815af GL |
83 | // Clean up |
84 | CleanData(); | |
f4ada568 | 85 | |
f61815af GL |
86 | // Extract protocol name |
87 | if (!PrepProto(last_url)) { | |
f4ada568 GL |
88 | m_error = wxURL_SNTXERR; |
89 | return FALSE; | |
90 | } | |
f61815af GL |
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 | } | |
f4ada568 | 112 | } |
f61815af | 113 | // URL parse finished. |
f4ada568 | 114 | |
8a4df159 | 115 | #if wxUSE_SOCKETS |
f61815af GL |
116 | if (m_useProxy) { |
117 | // We destroy the newly created protocol. | |
118 | CleanData(); | |
119 | ||
120 | // Third, we rebuild the URL. | |
223d09f6 | 121 | m_url = m_protoname + wxT(":"); |
f61815af | 122 | if (m_protoinfo->m_needhost) |
223d09f6 | 123 | m_url = m_url + wxT("//") + m_hostname; |
f61815af GL |
124 | |
125 | m_url += m_path; | |
126 | ||
127 | // We initialize specific variables. | |
128 | m_protocol = m_proxy; // FIXME: we should clone the protocol | |
f4ada568 | 129 | } |
8a4df159 | 130 | #endif |
f4ada568 GL |
131 | |
132 | m_error = wxURL_NOERR; | |
133 | return TRUE; | |
134 | } | |
135 | ||
136 | void wxURL::CleanData() | |
137 | { | |
8a4df159 | 138 | #if wxUSE_SOCKETS |
f61815af | 139 | if (!m_useProxy) |
8a4df159 | 140 | #endif |
f4ada568 GL |
141 | delete m_protocol; |
142 | } | |
143 | ||
144 | wxURL::~wxURL() | |
145 | { | |
146 | CleanData(); | |
8a4df159 | 147 | #if wxUSE_SOCKETS |
b2b35524 | 148 | if (m_proxy && m_proxy != ms_proxyDefault) |
f61815af | 149 | delete m_proxy; |
8a4df159 | 150 | #endif |
f4ada568 GL |
151 | } |
152 | ||
fae05df5 GL |
153 | // -------------------------------------------------------------- |
154 | // --------- wxURL urls decoders -------------------------------- | |
155 | // -------------------------------------------------------------- | |
156 | ||
f4ada568 GL |
157 | bool wxURL::PrepProto(wxString& url) |
158 | { | |
159 | int pos; | |
160 | ||
161 | // Find end | |
223d09f6 | 162 | pos = url.Find(wxT(':')); |
f4ada568 GL |
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 | { | |
856d2e52 | 175 | wxString temp_url; |
f4ada568 GL |
176 | int pos, pos2; |
177 | ||
58c837a4 | 178 | if ((url.GetChar(0) != wxT('/')) || (url.GetChar(1) != wxT('/'))) |
f4ada568 GL |
179 | return FALSE; |
180 | ||
181 | url = url(2, url.Length()); | |
182 | ||
223d09f6 | 183 | pos = url.Find(wxT('/')); |
f4ada568 | 184 | if (pos == -1) |
b7db6f0b | 185 | pos = url.Length(); |
f4ada568 | 186 | |
856d2e52 GL |
187 | if (pos == 0) |
188 | return FALSE; | |
189 | ||
190 | temp_url = url(0, pos); | |
223d09f6 | 191 | url = url(url.Find(wxT('/')), url.Length()); |
856d2e52 GL |
192 | |
193 | // Retrieve service number | |
223d09f6 | 194 | pos2 = temp_url.Find(wxT(':'), TRUE); |
f4ada568 | 195 | if (pos2 != -1 && pos2 < pos) { |
375abe3d | 196 | m_servname = temp_url(pos2+1, pos); |
f4ada568 GL |
197 | if (!m_servname.IsNumber()) |
198 | return FALSE; | |
856d2e52 | 199 | temp_url = temp_url(0, pos2); |
f4ada568 GL |
200 | } |
201 | ||
856d2e52 | 202 | // Retrieve user and password. |
223d09f6 | 203 | pos2 = temp_url.Find(wxT('@')); |
856d2e52 GL |
204 | // Even if pos2 equals -1, this code is right. |
205 | m_hostname = temp_url(pos2+1, temp_url.Length()); | |
f4ada568 | 206 | |
223d09f6 KB |
207 | m_user = wxT(""); |
208 | m_password = wxT(""); | |
856d2e52 GL |
209 | |
210 | if (pos2 == -1) | |
211 | return TRUE; | |
212 | ||
213 | temp_url = temp_url(0, pos2); | |
223d09f6 | 214 | pos2 = temp_url.Find(wxT(':')); |
856d2e52 GL |
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()); | |
f4ada568 GL |
221 | |
222 | return TRUE; | |
223 | } | |
224 | ||
225 | bool wxURL::PrepPath(wxString& url) | |
226 | { | |
227 | if (url.Length() != 0) | |
f61815af | 228 | m_path = ConvertToValidURI(url); |
f4ada568 | 229 | else |
223d09f6 | 230 | m_path = wxT("/"); |
f4ada568 GL |
231 | return TRUE; |
232 | } | |
233 | ||
234 | bool wxURL::FetchProtocol() | |
235 | { | |
b2b35524 | 236 | wxProtoInfo *info = ms_protocols; |
f4ada568 GL |
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(); | |
f4ada568 GL |
245 | return TRUE; |
246 | } | |
247 | info = info->next; | |
248 | } | |
249 | return FALSE; | |
250 | } | |
251 | ||
fae05df5 GL |
252 | // -------------------------------------------------------------- |
253 | // --------- wxURL get ------------------------------------------ | |
254 | // -------------------------------------------------------------- | |
255 | ||
58c837a4 | 256 | wxInputStream *wxURL::GetInputStream() |
f4ada568 | 257 | { |
f4ada568 GL |
258 | wxInputStream *the_i_stream = NULL; |
259 | ||
f4ada568 GL |
260 | if (!m_protocol) { |
261 | m_error = wxURL_NOPROTO; | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | m_error = wxURL_NOERR; | |
223d09f6 | 266 | if (m_user != wxT("")) { |
856d2e52 GL |
267 | m_protocol->SetUser(m_user); |
268 | m_protocol->SetPassword(m_password); | |
269 | } | |
270 | ||
8a4df159 | 271 | #if wxUSE_SOCKETS |
19e0e04b RD |
272 | wxIPV4address addr; |
273 | ||
f61815af GL |
274 | // m_protoinfo is NULL when we use a proxy |
275 | if (!m_useProxy && m_protoinfo->m_needhost) { | |
f4ada568 GL |
276 | if (!addr.Hostname(m_hostname)) { |
277 | m_error = wxURL_NOHOST; | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | addr.Service(m_servname); | |
282 | ||
8a2c6ef8 JS |
283 | if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason |
284 | { | |
f4ada568 GL |
285 | m_error = wxURL_CONNERR; |
286 | return NULL; | |
287 | } | |
288 | } | |
8a4df159 | 289 | #endif |
f4ada568 | 290 | |
f61815af GL |
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 | ||
f4ada568 GL |
297 | if (!the_i_stream) { |
298 | m_error = wxURL_PROTOERR; | |
299 | return NULL; | |
300 | } | |
301 | ||
302 | return the_i_stream; | |
303 | } | |
304 | ||
8a4df159 | 305 | #if wxUSE_SOCKETS |
f4ada568 GL |
306 | void wxURL::SetDefaultProxy(const wxString& url_proxy) |
307 | { | |
b2b35524 VZ |
308 | if ( !url_proxy ) |
309 | { | |
310 | if ( ms_proxyDefault ) | |
311 | { | |
312 | ms_proxyDefault->Close(); | |
313 | delete ms_proxyDefault; | |
314 | ms_proxyDefault = NULL; | |
315 | } | |
f61815af | 316 | } |
f61815af | 317 | else |
b2b35524 VZ |
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 | } | |
f4ada568 GL |
340 | } |
341 | ||
342 | void wxURL::SetProxy(const wxString& url_proxy) | |
343 | { | |
b2b35524 VZ |
344 | if ( !url_proxy ) |
345 | { | |
346 | if ( m_proxy && m_proxy != ms_proxyDefault ) | |
347 | { | |
348 | m_proxy->Close(); | |
349 | delete m_proxy; | |
350 | } | |
f4ada568 | 351 | |
b2b35524 VZ |
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 | } | |
f4ada568 | 384 | } |
b2b35524 | 385 | #endif // wxUSE_SOCKETS |
35a4dab7 | 386 | |
14906731 GL |
387 | wxString wxURL::ConvertToValidURI(const wxString& uri) |
388 | { | |
fae05df5 GL |
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 | ||
223d09f6 KB |
396 | if (c == wxT(' ')) |
397 | out_str += wxT('+'); | |
5a96d2f4 | 398 | else { |
223d09f6 KB |
399 | if (!isalpha(c) && c != wxT('.') && c != wxT('+') && c != wxT('/')) { |
400 | hexa_code.Printf(wxT("%%%02X"), c); | |
5a96d2f4 GL |
401 | out_str += hexa_code; |
402 | } else | |
403 | out_str += c; | |
404 | } | |
fae05df5 | 405 | } |
19e0e04b | 406 | |
fae05df5 | 407 | return out_str; |
14906731 GL |
408 | } |
409 | ||
aa6d9706 GL |
410 | wxString wxURL::ConvertFromURI(const wxString& uri) |
411 | { | |
aa6d9706 GL |
412 | wxString new_uri; |
413 | ||
74b31181 | 414 | size_t i = 0; |
aa6d9706 | 415 | while (i<uri.Len()) { |
74b31181 | 416 | int code; |
223d09f6 | 417 | if (uri[i] == wxT('%')) { |
aa6d9706 | 418 | i++; |
223d09f6 KB |
419 | if (uri[i] >= wxT('A') && uri[i] <= wxT('F')) |
420 | code = (uri[i] - wxT('A') + 10) * 16; | |
aa6d9706 | 421 | else |
223d09f6 | 422 | code = (uri[i] - wxT('0')) * 16; |
aa6d9706 | 423 | i++; |
223d09f6 KB |
424 | if (uri[i] >= wxT('A') && uri[i] <= wxT('F')) |
425 | code += (uri[i] - wxT('A')) + 10; | |
aa6d9706 | 426 | else |
223d09f6 | 427 | code += (uri[i] - wxT('0')); |
aa6d9706 GL |
428 | i++; |
429 | new_uri += (wxChar)code; | |
430 | continue; | |
431 | } | |
432 | new_uri += uri[i]; | |
433 | i++; | |
434 | } | |
435 | return new_uri; | |
436 | } | |
b2b35524 VZ |
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 |