]>
Commit | Line | Data |
---|---|---|
f4ada568 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8ecff181 | 2 | // Name: src/common/url.cpp |
f4ada568 GL |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fcc6dddd JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
8ecff181 | 16 | #pragma hdrstop |
fcc6dddd JS |
17 | #endif |
18 | ||
a5d46b73 | 19 | #if wxUSE_URL |
f4ada568 | 20 | |
df91131c WS |
21 | #include "wx/url.h" |
22 | ||
8ecff181 WS |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/list.h" | |
df91131c | 25 | #include "wx/string.h" |
de6185e2 | 26 | #include "wx/utils.h" |
02761f6c | 27 | #include "wx/module.h" |
8ecff181 WS |
28 | #endif |
29 | ||
a5d46b73 VZ |
30 | #include <string.h> |
31 | #include <ctype.h> | |
32 | ||
f4ada568 | 33 | IMPLEMENT_CLASS(wxProtoInfo, wxObject) |
b60b2ec8 | 34 | IMPLEMENT_CLASS(wxURL, wxURI) |
f4ada568 GL |
35 | |
36 | // Protocols list | |
b2b35524 | 37 | wxProtoInfo *wxURL::ms_protocols = NULL; |
8a4df159 | 38 | |
f92f546c | 39 | // Enforce linking of protocol classes: |
f92f546c VS |
40 | USE_PROTOCOL(wxFileProto) |
41 | ||
ce195ee6 | 42 | #if wxUSE_PROTOCOL_HTTP |
f80eabe5 | 43 | USE_PROTOCOL(wxHTTP) |
f80eabe5 | 44 | |
b2b35524 | 45 | wxHTTP *wxURL::ms_proxyDefault = NULL; |
cb719f2e | 46 | bool wxURL::ms_useDefaultProxy = false; |
8a4df159 | 47 | #endif |
f4ada568 | 48 | |
ce195ee6 MB |
49 | #if wxUSE_PROTOCOL_FTP |
50 | USE_PROTOCOL(wxFTP) | |
51 | #endif | |
52 | ||
fae05df5 | 53 | // -------------------------------------------------------------- |
b60b2ec8 RN |
54 | // |
55 | // wxURL | |
56 | // | |
fae05df5 | 57 | // -------------------------------------------------------------- |
f4ada568 | 58 | |
ce321570 RN |
59 | // -------------------------------------------------------------- |
60 | // Construction | |
61 | // -------------------------------------------------------------- | |
62 | ||
b60b2ec8 RN |
63 | wxURL::wxURL(const wxString& url) : wxURI(url) |
64 | { | |
65 | Init(url); | |
66 | ParseURL(); | |
67 | } | |
68 | ||
69 | wxURL::wxURL(const wxURI& url) : wxURI(url) | |
70 | { | |
86470d43 | 71 | Init(url.BuildURI()); |
b60b2ec8 RN |
72 | ParseURL(); |
73 | } | |
74 | ||
b60b2ec8 | 75 | void wxURL::Init(const wxString& url) |
f4ada568 | 76 | { |
b2b35524 VZ |
77 | m_protocol = NULL; |
78 | m_error = wxURL_NOERR; | |
79 | m_url = url; | |
25959b95 VZ |
80 | #if wxUSE_URL_NATIVE |
81 | m_nativeImp = CreateNativeImpObject(); | |
82 | #endif | |
b2b35524 | 83 | |
ce195ee6 | 84 | #if wxUSE_PROTOCOL_HTTP |
b2b35524 VZ |
85 | if ( ms_useDefaultProxy && !ms_proxyDefault ) |
86 | { | |
2b5f62a0 | 87 | SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) ); |
b2b35524 VZ |
88 | |
89 | if ( !ms_proxyDefault ) | |
90 | { | |
91 | // don't try again | |
cb719f2e | 92 | ms_useDefaultProxy = false; |
b2b35524 VZ |
93 | } |
94 | } | |
95 | ||
96 | m_useProxy = ms_proxyDefault != NULL; | |
97 | m_proxy = ms_proxyDefault; | |
ce195ee6 | 98 | #endif // wxUSE_PROTOCOL_HTTP |
b2b35524 | 99 | |
f4ada568 | 100 | } |
ce321570 RN |
101 | |
102 | // -------------------------------------------------------------- | |
103 | // Assignment | |
104 | // -------------------------------------------------------------- | |
105 | ||
106 | wxURL& wxURL::operator = (const wxURI& url) | |
107 | { | |
108 | wxURI::operator = (url); | |
109 | Init(url.BuildURI()); | |
110 | ParseURL(); | |
111 | return *this; | |
112 | } | |
113 | wxURL& wxURL::operator = (const wxString& url) | |
114 | { | |
115 | wxURI::operator = (url); | |
116 | Init(url); | |
117 | ParseURL(); | |
118 | return *this; | |
119 | } | |
120 | ||
b60b2ec8 RN |
121 | // -------------------------------------------------------------- |
122 | // ParseURL | |
123 | // | |
124 | // Builds the URL and takes care of the old protocol stuff | |
125 | // -------------------------------------------------------------- | |
f4ada568 GL |
126 | |
127 | bool wxURL::ParseURL() | |
128 | { | |
fd725bce WS |
129 | // If the URL was already parsed (m_protocol != NULL), pass this section. |
130 | if (!m_protocol) | |
f6bcfd97 | 131 | { |
fd725bce WS |
132 | // Clean up |
133 | CleanData(); | |
f61815af | 134 | |
fd725bce WS |
135 | // Make sure we have a protocol/scheme |
136 | if (!HasScheme()) | |
137 | { | |
138 | m_error = wxURL_SNTXERR; | |
139 | return false; | |
140 | } | |
f61815af | 141 | |
fd725bce WS |
142 | // Find and create the protocol object |
143 | if (!FetchProtocol()) | |
144 | { | |
145 | m_error = wxURL_NOPROTO; | |
146 | return false; | |
147 | } | |
148 | ||
149 | // Do we need a host name ? | |
150 | if (m_protoinfo->m_needhost) | |
151 | { | |
152 | // Make sure we have one, then | |
153 | if (!HasServer()) | |
154 | { | |
155 | m_error = wxURL_SNTXERR; | |
156 | return false; | |
157 | } | |
158 | } | |
f61815af | 159 | } |
f4ada568 | 160 | |
ce195ee6 | 161 | #if wxUSE_PROTOCOL_HTTP |
fd725bce WS |
162 | if (m_useProxy) |
163 | { | |
164 | // Third, we rebuild the URL. | |
165 | m_url = m_scheme + wxT(":"); | |
166 | if (m_protoinfo->m_needhost) | |
167 | m_url = m_url + wxT("//") + m_server; | |
168 | ||
169 | // We initialize specific variables. | |
170 | m_protocol = m_proxy; // FIXME: we should clone the protocol | |
171 | } | |
ce195ee6 | 172 | #endif // wxUSE_PROTOCOL_HTTP |
f4ada568 | 173 | |
fd725bce WS |
174 | m_error = wxURL_NOERR; |
175 | return true; | |
f4ada568 GL |
176 | } |
177 | ||
ce321570 RN |
178 | // -------------------------------------------------------------- |
179 | // Destruction/Cleanup | |
180 | // -------------------------------------------------------------- | |
181 | ||
f4ada568 GL |
182 | void wxURL::CleanData() |
183 | { | |
ce195ee6 | 184 | #if wxUSE_PROTOCOL_HTTP |
fd725bce | 185 | if (!m_useProxy) |
ce195ee6 | 186 | #endif // wxUSE_PROTOCOL_HTTP |
5f3742c2 KH |
187 | if (m_protocol) |
188 | // Need to safely delete the socket (pending events) | |
189 | m_protocol->Destroy(); | |
f4ada568 GL |
190 | } |
191 | ||
192 | wxURL::~wxURL() | |
193 | { | |
25959b95 | 194 | CleanData(); |
ce195ee6 | 195 | #if wxUSE_PROTOCOL_HTTP |
25959b95 VZ |
196 | if (m_proxy && m_proxy != ms_proxyDefault) |
197 | delete m_proxy; | |
ce195ee6 | 198 | #endif // wxUSE_PROTOCOL_HTTP |
25959b95 VZ |
199 | #if wxUSE_URL_NATIVE |
200 | delete m_nativeImp; | |
8a4df159 | 201 | #endif |
f4ada568 GL |
202 | } |
203 | ||
ce321570 RN |
204 | // -------------------------------------------------------------- |
205 | // FetchProtocol | |
206 | // -------------------------------------------------------------- | |
f4ada568 GL |
207 | |
208 | bool wxURL::FetchProtocol() | |
209 | { | |
fd725bce | 210 | wxProtoInfo *info = ms_protocols; |
f4ada568 | 211 | |
fd725bce | 212 | while (info) |
f6bcfd97 | 213 | { |
fd725bce WS |
214 | if (m_scheme == info->m_protoname) |
215 | { | |
216 | if (m_port.IsNull()) | |
217 | m_port = info->m_servname; | |
218 | m_protoinfo = info; | |
219 | m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject(); | |
220 | return true; | |
221 | } | |
222 | info = info->next; | |
f4ada568 | 223 | } |
fd725bce | 224 | return false; |
f4ada568 GL |
225 | } |
226 | ||
fae05df5 | 227 | // -------------------------------------------------------------- |
ce321570 | 228 | // GetInputStream |
fae05df5 GL |
229 | // -------------------------------------------------------------- |
230 | ||
58c837a4 | 231 | wxInputStream *wxURL::GetInputStream() |
f4ada568 | 232 | { |
fd725bce WS |
233 | if (!m_protocol) |
234 | { | |
235 | m_error = wxURL_NOPROTO; | |
236 | return NULL; | |
237 | } | |
238 | ||
239 | m_error = wxURL_NOERR; | |
240 | if (HasUserInfo()) | |
241 | { | |
242 | size_t dwPasswordPos = m_userinfo.find(':'); | |
243 | ||
244 | if (dwPasswordPos == wxString::npos) | |
e03e94b0 | 245 | m_protocol->SetUser(Unescape(m_userinfo)); |
fd725bce WS |
246 | else |
247 | { | |
e03e94b0 VZ |
248 | m_protocol->SetUser(Unescape(m_userinfo(0, dwPasswordPos))); |
249 | m_protocol->SetPassword(Unescape(m_userinfo(dwPasswordPos+1, m_userinfo.length() + 1))); | |
fd725bce WS |
250 | } |
251 | } | |
856d2e52 | 252 | |
25959b95 | 253 | #if wxUSE_URL_NATIVE |
fd725bce WS |
254 | // give the native implementation to return a better stream |
255 | // such as the native WinINet functionality under MS-Windows | |
256 | if (m_nativeImp) | |
257 | { | |
258 | wxInputStream *rc; | |
259 | rc = m_nativeImp->GetInputStream(this); | |
260 | if (rc != 0) | |
261 | return rc; | |
262 | } | |
263 | // else use the standard behaviour | |
25959b95 VZ |
264 | #endif // wxUSE_URL_NATIVE |
265 | ||
8a4df159 | 266 | #if wxUSE_SOCKETS |
19e0e04b RD |
267 | wxIPV4address addr; |
268 | ||
fd725bce | 269 | // m_protoinfo is NULL when we use a proxy |
283965f0 VZ |
270 | if ( |
271 | #if wxUSE_PROTOCOL_HTTP | |
272 | !m_useProxy && | |
273 | #endif // wxUSE_PROTOCOL_HTTP | |
274 | m_protoinfo->m_needhost ) | |
f6bcfd97 | 275 | { |
fd725bce WS |
276 | if (!addr.Hostname(m_server)) |
277 | { | |
278 | m_error = wxURL_NOHOST; | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | addr.Service(m_port); | |
283 | ||
284 | if (!m_protocol->Connect(addr, true)) // Watcom needs the 2nd arg for some reason | |
285 | { | |
286 | m_error = wxURL_CONNERR; | |
287 | return NULL; | |
288 | } | |
f4ada568 | 289 | } |
283965f0 | 290 | #endif // wxUSE_SOCKETS |
fd725bce WS |
291 | |
292 | wxString fullPath; | |
f4ada568 | 293 | |
283965f0 | 294 | #if wxUSE_PROTOCOL_HTTP |
fd725bce WS |
295 | // When we use a proxy, we have to pass the whole URL to it. |
296 | if (m_useProxy) | |
297 | fullPath += m_url; | |
283965f0 | 298 | #endif // wxUSE_PROTOCOL_HTTP |
f4ada568 | 299 | |
fd725bce WS |
300 | if(m_path.empty()) |
301 | fullPath += wxT("/"); | |
302 | else | |
303 | fullPath += m_path; | |
304 | ||
305 | if (HasQuery()) | |
306 | fullPath += wxT("?") + m_query; | |
307 | ||
308 | if (HasFragment()) | |
309 | fullPath += wxT("#") + m_fragment; | |
310 | ||
311 | wxInputStream *the_i_stream = m_protocol->GetInputStream(fullPath); | |
312 | ||
313 | if (!the_i_stream) | |
8a2c6ef8 | 314 | { |
fd725bce WS |
315 | m_error = wxURL_PROTOERR; |
316 | return NULL; | |
f4ada568 | 317 | } |
f4ada568 | 318 | |
fd725bce | 319 | return the_i_stream; |
f4ada568 GL |
320 | } |
321 | ||
ce195ee6 | 322 | #if wxUSE_PROTOCOL_HTTP |
f4ada568 GL |
323 | void wxURL::SetDefaultProxy(const wxString& url_proxy) |
324 | { | |
fd725bce WS |
325 | if ( !url_proxy ) |
326 | { | |
327 | if ( ms_proxyDefault ) | |
328 | { | |
329 | ms_proxyDefault->Close(); | |
330 | delete ms_proxyDefault; | |
331 | ms_proxyDefault = NULL; | |
332 | } | |
333 | } | |
334 | else | |
335 | { | |
336 | wxString tmp_str = url_proxy; | |
337 | int pos = tmp_str.Find(wxT(':')); | |
338 | if (pos == wxNOT_FOUND) | |
339 | return; | |
340 | ||
341 | wxString hostname = tmp_str(0, pos), | |
8ecff181 | 342 | port = tmp_str(pos+1, tmp_str.length()-pos); |
fd725bce WS |
343 | wxIPV4address addr; |
344 | ||
345 | if (!addr.Hostname(hostname)) | |
346 | return; | |
347 | if (!addr.Service(port)) | |
348 | return; | |
349 | ||
350 | if (ms_proxyDefault) | |
351 | // Finally, when all is right, we connect the new proxy. | |
352 | ms_proxyDefault->Close(); | |
353 | else | |
354 | ms_proxyDefault = new wxHTTP(); | |
355 | ms_proxyDefault->Connect(addr, true); // Watcom needs the 2nd arg for some reason | |
356 | } | |
f4ada568 GL |
357 | } |
358 | ||
359 | void wxURL::SetProxy(const wxString& url_proxy) | |
360 | { | |
b2b35524 VZ |
361 | if ( !url_proxy ) |
362 | { | |
363 | if ( m_proxy && m_proxy != ms_proxyDefault ) | |
364 | { | |
365 | m_proxy->Close(); | |
366 | delete m_proxy; | |
367 | } | |
f4ada568 | 368 | |
cb719f2e | 369 | m_useProxy = false; |
b2b35524 VZ |
370 | } |
371 | else | |
372 | { | |
373 | wxString tmp_str; | |
374 | wxString hostname, port; | |
375 | int pos; | |
376 | wxIPV4address addr; | |
377 | ||
378 | tmp_str = url_proxy; | |
379 | pos = tmp_str.Find(wxT(':')); | |
380 | // This is an invalid proxy name. | |
cb719f2e | 381 | if (pos == wxNOT_FOUND) |
b2b35524 VZ |
382 | return; |
383 | ||
384 | hostname = tmp_str(0, pos); | |
8ecff181 | 385 | port = tmp_str(pos+1, tmp_str.length()-pos); |
b2b35524 VZ |
386 | |
387 | addr.Hostname(hostname); | |
388 | addr.Service(port); | |
389 | ||
390 | // Finally, create the whole stuff. | |
391 | if (m_proxy && m_proxy != ms_proxyDefault) | |
392 | delete m_proxy; | |
393 | m_proxy = new wxHTTP(); | |
cb719f2e | 394 | m_proxy->Connect(addr, true); // Watcom needs the 2nd arg for some reason |
b2b35524 VZ |
395 | |
396 | CleanData(); | |
397 | // Reparse url. | |
cb719f2e | 398 | m_useProxy = true; |
b2b35524 VZ |
399 | ParseURL(); |
400 | } | |
f4ada568 | 401 | } |
ce195ee6 | 402 | #endif // wxUSE_PROTOCOL_HTTP |
35a4dab7 | 403 | |
b2b35524 | 404 | // ---------------------------------------------------------------------- |
ce321570 RN |
405 | // wxURLModule |
406 | // | |
b2b35524 VZ |
407 | // A module which deletes the default proxy if we created it |
408 | // ---------------------------------------------------------------------- | |
409 | ||
410 | #if wxUSE_SOCKETS | |
411 | ||
412 | class wxURLModule : public wxModule | |
413 | { | |
414 | public: | |
7921a093 VZ |
415 | wxURLModule(); |
416 | ||
b2b35524 VZ |
417 | virtual bool OnInit(); |
418 | virtual void OnExit(); | |
419 | ||
420 | private: | |
421 | DECLARE_DYNAMIC_CLASS(wxURLModule) | |
422 | }; | |
423 | ||
424 | IMPLEMENT_DYNAMIC_CLASS(wxURLModule, wxModule) | |
425 | ||
7921a093 VZ |
426 | wxURLModule::wxURLModule() |
427 | { | |
428 | // we must be cleaned up before wxSocketModule as otherwise deleting | |
429 | // ms_proxyDefault from our OnExit() won't work (and can actually crash) | |
430 | AddDependency(wxClassInfo::FindClass(_T("wxSocketModule"))); | |
431 | } | |
432 | ||
b2b35524 VZ |
433 | bool wxURLModule::OnInit() |
434 | { | |
ce195ee6 | 435 | #if wxUSE_PROTOCOL_HTTP |
b2b35524 VZ |
436 | // env var HTTP_PROXY contains the address of the default proxy to use if |
437 | // set, but don't try to create this proxy right now because it will slow | |
438 | // down the program startup (especially if there is no DNS server | |
439 | // available, in which case it may take up to 1 minute) | |
f6bcfd97 | 440 | |
67acaf80 | 441 | if ( wxGetenv(_T("HTTP_PROXY")) ) |
b2b35524 | 442 | { |
cb719f2e | 443 | wxURL::ms_useDefaultProxy = true; |
b2b35524 | 444 | } |
ce195ee6 | 445 | #endif // wxUSE_PROTOCOL_HTTP |
cb719f2e | 446 | return true; |
b2b35524 VZ |
447 | } |
448 | ||
449 | void wxURLModule::OnExit() | |
450 | { | |
ce195ee6 | 451 | #if wxUSE_PROTOCOL_HTTP |
b2b35524 VZ |
452 | delete wxURL::ms_proxyDefault; |
453 | wxURL::ms_proxyDefault = NULL; | |
ce195ee6 | 454 | #endif // wxUSE_PROTOCOL_HTTP |
b2b35524 VZ |
455 | } |
456 | ||
457 | #endif // wxUSE_SOCKETS | |
a5d46b73 | 458 | |
97d8fe87 | 459 | |
a5d46b73 | 460 | #endif // wxUSE_URL |