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