]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
First set of changes toward wxBase compliance :-)
[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
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"
29#include "wx/url.h"
f4ada568 30
f4ada568
GL
31IMPLEMENT_CLASS(wxProtoInfo, wxObject)
32IMPLEMENT_CLASS(wxURL, wxObject)
f4ada568
GL
33
34// Protocols list
35wxProtoInfo *wxURL::g_protocols = NULL;
8a4df159
RR
36
37#if wxUSE_SOCKETS
f61815af 38wxHTTP *wxURL::g_proxy = NULL;
8a4df159 39#endif
f4ada568 40
fae05df5
GL
41// --------------------------------------------------------------
42// wxURL
43// --------------------------------------------------------------
f4ada568 44
fae05df5
GL
45// --------------------------------------------------------------
46// --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
47// --------------------------------------------------------------
f4ada568
GL
48
49wxURL::wxURL(const wxString& url)
50{
51 m_protocol = NULL;
f4ada568 52 m_error = wxURL_NOERR;
f61815af 53 m_url = url;
8a4df159 54#if wxUSE_SOCKETS
f61815af
GL
55 m_useProxy = (g_proxy != NULL);
56 m_proxy = g_proxy;
8a4df159 57#endif
41895a05 58 ParseURL();
f4ada568
GL
59}
60
61bool wxURL::ParseURL()
62{
63 wxString last_url = m_url;
64
f61815af
GL
65 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
66 if (!m_protocol) {
f4ada568 67
f61815af
GL
68 // Clean up
69 CleanData();
f4ada568 70
f61815af
GL
71 // Extract protocol name
72 if (!PrepProto(last_url)) {
f4ada568
GL
73 m_error = wxURL_SNTXERR;
74 return FALSE;
75 }
f61815af
GL
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 }
f4ada568 97 }
f61815af 98 // URL parse finished.
f4ada568 99
8a4df159 100#if wxUSE_SOCKETS
f61815af
GL
101 if (m_useProxy) {
102 // We destroy the newly created protocol.
103 CleanData();
104
105 // Third, we rebuild the URL.
223d09f6 106 m_url = m_protoname + wxT(":");
f61815af 107 if (m_protoinfo->m_needhost)
223d09f6 108 m_url = m_url + wxT("//") + m_hostname;
f61815af
GL
109
110 m_url += m_path;
111
112 // We initialize specific variables.
113 m_protocol = m_proxy; // FIXME: we should clone the protocol
f4ada568 114 }
8a4df159 115#endif
f4ada568
GL
116
117 m_error = wxURL_NOERR;
118 return TRUE;
119}
120
121void wxURL::CleanData()
122{
8a4df159 123#if wxUSE_SOCKETS
f61815af 124 if (!m_useProxy)
8a4df159 125#endif
f4ada568
GL
126 delete m_protocol;
127}
128
129wxURL::~wxURL()
130{
131 CleanData();
8a4df159 132#if wxUSE_SOCKETS
f61815af
GL
133 if (m_proxy && m_proxy != g_proxy)
134 delete m_proxy;
8a4df159 135#endif
f4ada568
GL
136}
137
fae05df5
GL
138// --------------------------------------------------------------
139// --------- wxURL urls decoders --------------------------------
140// --------------------------------------------------------------
141
f4ada568
GL
142bool wxURL::PrepProto(wxString& url)
143{
144 int pos;
145
146 // Find end
223d09f6 147 pos = url.Find(wxT(':'));
f4ada568
GL
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
158bool wxURL::PrepHost(wxString& url)
159{
856d2e52 160 wxString temp_url;
f4ada568
GL
161 int pos, pos2;
162
58c837a4 163 if ((url.GetChar(0) != wxT('/')) || (url.GetChar(1) != wxT('/')))
f4ada568
GL
164 return FALSE;
165
166 url = url(2, url.Length());
167
223d09f6 168 pos = url.Find(wxT('/'));
f4ada568 169 if (pos == -1)
b7db6f0b 170 pos = url.Length();
f4ada568 171
856d2e52
GL
172 if (pos == 0)
173 return FALSE;
174
175 temp_url = url(0, pos);
223d09f6 176 url = url(url.Find(wxT('/')), url.Length());
856d2e52
GL
177
178 // Retrieve service number
223d09f6 179 pos2 = temp_url.Find(wxT(':'), TRUE);
f4ada568 180 if (pos2 != -1 && pos2 < pos) {
375abe3d 181 m_servname = temp_url(pos2+1, pos);
f4ada568
GL
182 if (!m_servname.IsNumber())
183 return FALSE;
856d2e52 184 temp_url = temp_url(0, pos2);
f4ada568
GL
185 }
186
856d2e52 187 // Retrieve user and password.
223d09f6 188 pos2 = temp_url.Find(wxT('@'));
856d2e52
GL
189 // Even if pos2 equals -1, this code is right.
190 m_hostname = temp_url(pos2+1, temp_url.Length());
f4ada568 191
223d09f6
KB
192 m_user = wxT("");
193 m_password = wxT("");
856d2e52
GL
194
195 if (pos2 == -1)
196 return TRUE;
197
198 temp_url = temp_url(0, pos2);
223d09f6 199 pos2 = temp_url.Find(wxT(':'));
856d2e52
GL
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());
f4ada568
GL
206
207 return TRUE;
208}
209
210bool wxURL::PrepPath(wxString& url)
211{
212 if (url.Length() != 0)
f61815af 213 m_path = ConvertToValidURI(url);
f4ada568 214 else
223d09f6 215 m_path = wxT("/");
f4ada568
GL
216 return TRUE;
217}
218
219bool 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();
f4ada568
GL
230 return TRUE;
231 }
232 info = info->next;
233 }
234 return FALSE;
235}
236
fae05df5
GL
237// --------------------------------------------------------------
238// --------- wxURL get ------------------------------------------
239// --------------------------------------------------------------
240
58c837a4 241wxInputStream *wxURL::GetInputStream()
f4ada568 242{
f4ada568
GL
243 wxInputStream *the_i_stream = NULL;
244
f4ada568
GL
245 if (!m_protocol) {
246 m_error = wxURL_NOPROTO;
247 return NULL;
248 }
249
250 m_error = wxURL_NOERR;
223d09f6 251 if (m_user != wxT("")) {
856d2e52
GL
252 m_protocol->SetUser(m_user);
253 m_protocol->SetPassword(m_password);
254 }
255
8a4df159 256#if wxUSE_SOCKETS
19e0e04b
RD
257 wxIPV4address addr;
258
f61815af
GL
259 // m_protoinfo is NULL when we use a proxy
260 if (!m_useProxy && m_protoinfo->m_needhost) {
f4ada568
GL
261 if (!addr.Hostname(m_hostname)) {
262 m_error = wxURL_NOHOST;
263 return NULL;
264 }
265
266 addr.Service(m_servname);
267
8a2c6ef8
JS
268 if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason
269 {
f4ada568
GL
270 m_error = wxURL_CONNERR;
271 return NULL;
272 }
273 }
8a4df159 274#endif
f4ada568 275
f61815af
GL
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
f4ada568
GL
282 if (!the_i_stream) {
283 m_error = wxURL_PROTOERR;
284 return NULL;
285 }
286
287 return the_i_stream;
288}
289
8a4df159 290#if wxUSE_SOCKETS
f4ada568
GL
291void wxURL::SetDefaultProxy(const wxString& url_proxy)
292{
f61815af
GL
293 if (url_proxy.IsNull()) {
294 g_proxy->Close();
295 delete g_proxy;
296 g_proxy = NULL;
f4ada568 297 return;
f61815af 298 }
f4ada568
GL
299
300 wxString tmp_str = url_proxy;
223d09f6 301 int pos = tmp_str.Find(wxT(':'));
f61815af
GL
302 if (pos == -1)
303 return;
304
f4ada568 305 wxString hostname = tmp_str(0, pos),
f61815af 306 port = tmp_str(pos+1, tmp_str.Length()-pos);
f4ada568
GL
307 wxIPV4address addr;
308
f61815af
GL
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();
8a2c6ef8 319 g_proxy->Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
f4ada568
GL
320}
321
322void wxURL::SetProxy(const wxString& url_proxy)
323{
324 if (url_proxy.IsNull()) {
f61815af
GL
325 if (m_proxy) {
326 m_proxy->Close();
327 delete m_proxy;
328 }
329 m_useProxy = FALSE;
f4ada568
GL
330 return;
331 }
332
f4ada568
GL
333 wxString tmp_str;
334 wxString hostname, port;
335 int pos;
336 wxIPV4address addr;
337
338 tmp_str = url_proxy;
223d09f6 339 pos = tmp_str.Find(wxT(':'));
f61815af
GL
340 // This is an invalid proxy name.
341 if (pos == -1)
342 return;
343
f4ada568
GL
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
f61815af
GL
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
f4ada568 355
f61815af
GL
356 CleanData();
357 // Reparse url.
358 m_useProxy = TRUE;
359 ParseURL();
f4ada568 360}
8a4df159 361#endif
35a4dab7 362
14906731
GL
363wxString wxURL::ConvertToValidURI(const wxString& uri)
364{
fae05df5
GL
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
223d09f6
KB
372 if (c == wxT(' '))
373 out_str += wxT('+');
5a96d2f4 374 else {
223d09f6
KB
375 if (!isalpha(c) && c != wxT('.') && c != wxT('+') && c != wxT('/')) {
376 hexa_code.Printf(wxT("%%%02X"), c);
5a96d2f4
GL
377 out_str += hexa_code;
378 } else
379 out_str += c;
380 }
fae05df5 381 }
19e0e04b 382
fae05df5 383 return out_str;
14906731
GL
384}
385
aa6d9706
GL
386wxString wxURL::ConvertFromURI(const wxString& uri)
387{
aa6d9706
GL
388 wxString new_uri;
389
74b31181 390 size_t i = 0;
aa6d9706 391 while (i<uri.Len()) {
74b31181 392 int code;
223d09f6 393 if (uri[i] == wxT('%')) {
aa6d9706 394 i++;
223d09f6
KB
395 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
396 code = (uri[i] - wxT('A') + 10) * 16;
aa6d9706 397 else
223d09f6 398 code = (uri[i] - wxT('0')) * 16;
aa6d9706 399 i++;
223d09f6
KB
400 if (uri[i] >= wxT('A') && uri[i] <= wxT('F'))
401 code += (uri[i] - wxT('A')) + 10;
aa6d9706 402 else
223d09f6 403 code += (uri[i] - wxT('0'));
aa6d9706
GL
404 i++;
405 new_uri += (wxChar)code;
406 continue;
407 }
408 new_uri += uri[i];
409 i++;
410 }
411 return new_uri;
412}