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