* wxSocket fixes: FTP, HTTP works really now. GTK fixes to prevent infinite loop.
[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 #ifndef WX_PRECOMP
24 #endif
25
26 #include <string.h>
27 #include <ctype.h>
28
29 // wxWindows headers
30 #include <wx/string.h>
31 #include <wx/list.h>
32 #include <wx/utils.h>
33
34 // wxSocket header
35 #include "wx/url.h"
36
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_CLASS(wxProtoInfo, wxObject)
39 IMPLEMENT_CLASS(wxURL, wxObject)
40 #endif
41
42 // Protocols list
43 wxProtoInfo *wxURL::g_protocols = NULL;
44 wxHTTP *wxURL::g_proxy;
45
46 /////////////////////////////////////////////////////////////////
47 // wxURL ////////////////////////////////////////////////////////
48 /////////////////////////////////////////////////////////////////
49
50 /*
51 * --------------------------------------------------------------
52 * --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
53 * --------------------------------------------------------------
54 */
55
56 wxURL::wxURL(const wxString& url)
57 {
58 m_protocol = NULL;
59 if (g_proxy->IsConnected()) {
60 m_protocol = g_proxy;
61 m_protoname = "proxy";
62 m_path = url;
63 return;
64 }
65 m_url = url;
66 m_error = wxURL_NOERR;
67 ParseURL();
68 }
69
70 bool wxURL::ParseURL()
71 {
72 wxString last_url = m_url;
73
74 // Clean up
75 CleanData();
76
77 // Extract protocol name
78 if (!PrepProto(last_url)) {
79 m_error = wxURL_SNTXERR;
80 return FALSE;
81 }
82
83 // Find and create the protocol object
84 if (!FetchProtocol()) {
85 m_error = wxURL_NOPROTO;
86 return FALSE;
87 }
88
89 // Do we need a host name ?
90 if (m_protoinfo->m_needhost) {
91 // Extract it
92 if (!PrepHost(last_url)) {
93 m_error = wxURL_SNTXERR;
94 return FALSE;
95 }
96 }
97
98 // Extract full path
99 if (!PrepPath(last_url)) {
100 m_error = wxURL_NOPATH;
101 return FALSE;
102 }
103
104 m_error = wxURL_NOERR;
105 return TRUE;
106 }
107
108 void wxURL::CleanData()
109 {
110 if (m_protoname != "proxy")
111 delete m_protocol;
112 }
113
114 wxURL::~wxURL()
115 {
116 CleanData();
117 }
118
119 /*
120 * --------------------------------------------------------------
121 * --------- wxURL urls decoders --------------------------------
122 * --------------------------------------------------------------
123 */
124 bool wxURL::PrepProto(wxString& url)
125 {
126 int pos;
127
128 // Find end
129 pos = url.Find(':');
130 if (pos == -1)
131 return FALSE;
132
133 m_protoname = url(0, pos);
134
135 url = url(pos+1, url.Length());
136
137 return TRUE;
138 }
139
140 bool wxURL::PrepHost(wxString& url)
141 {
142 wxString temp_url;
143 int pos, pos2;
144
145 if ((url.GetChar(0) != '/') || (url.GetChar(1) != '/'))
146 return FALSE;
147
148 url = url(2, url.Length());
149
150 pos = url.Find('/');
151 if (pos == -1)
152 pos = url.Length();
153
154 if (pos == 0)
155 return FALSE;
156
157 temp_url = url(0, pos);
158 url = url(url.Find('/'), url.Length());
159
160 // Retrieve service number
161 pos2 = temp_url.Find(':', TRUE);
162 if (pos2 != -1 && pos2 < pos) {
163 m_servname = temp_url(pos2+1, pos);
164 if (!m_servname.IsNumber())
165 return FALSE;
166 temp_url = temp_url(0, pos2);
167 }
168
169 // Retrieve user and password.
170 pos2 = temp_url.Find('@');
171 // Even if pos2 equals -1, this code is right.
172 m_hostname = temp_url(pos2+1, temp_url.Length());
173
174 m_user = "";
175 m_password = "";
176
177 if (pos2 == -1)
178 return TRUE;
179
180 temp_url = temp_url(0, pos2);
181 pos2 = temp_url.Find(':');
182
183 if (pos2 == -1)
184 return FALSE;
185
186 m_user = temp_url(0, pos2);
187 m_password = temp_url(pos2+1, url.Length());
188
189 return TRUE;
190 }
191
192 bool wxURL::PrepPath(wxString& url)
193 {
194 if (url.Length() != 0)
195 m_path = url;
196 else
197 m_path = "/";
198 return TRUE;
199 }
200
201 bool wxURL::FetchProtocol()
202 {
203 wxProtoInfo *info = g_protocols;
204
205 while (info) {
206 if (m_protoname == info->m_protoname) {
207 if (m_servname.IsNull())
208 m_servname = info->m_servname;
209
210 m_protoinfo = info;
211 m_protocol = (wxProtocol *)m_protoinfo->m_cinfo->CreateObject();
212 wxSocketHandler::Master().Register(m_protocol);
213 return TRUE;
214 }
215 info = info->next;
216 }
217 return FALSE;
218 }
219
220 /*
221 * --------------------------------------------------------------
222 * --------- wxURL get ------------------------------------------
223 * --------------------------------------------------------------
224 */
225 wxInputStream *wxURL::GetInputStream(void)
226 {
227 wxIPV4address addr;
228 wxInputStream *the_i_stream = NULL;
229
230 if (!m_protocol)
231 if (!ParseURL())
232 return NULL;
233
234 if (!m_protocol) {
235 m_error = wxURL_NOPROTO;
236 return NULL;
237 }
238
239 m_error = wxURL_NOERR;
240 if (m_user != "") {
241 m_protocol->SetUser(m_user);
242 m_protocol->SetPassword(m_password);
243 }
244
245 if (m_protoinfo->m_needhost) {
246 if (!addr.Hostname(m_hostname)) {
247 m_error = wxURL_NOHOST;
248 return NULL;
249 }
250
251 addr.Service(m_servname);
252
253 if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason
254 {
255 m_error = wxURL_CONNERR;
256 return NULL;
257 }
258 }
259
260 the_i_stream = m_protocol->GetInputStream(m_path);
261 if (!the_i_stream) {
262 m_error = wxURL_PROTOERR;
263 return NULL;
264 }
265
266 return the_i_stream;
267 }
268
269 void wxURL::SetDefaultProxy(const wxString& url_proxy)
270 {
271 g_proxy->Close();
272
273 if (url_proxy.IsNull())
274 return;
275
276 wxString tmp_str = url_proxy;
277 int pos = tmp_str.Find(':');
278 wxString hostname = tmp_str(0, pos),
279 port = tmp_str(pos, tmp_str.Length()-pos);
280 wxIPV4address addr;
281
282 addr.Hostname(hostname);
283 addr.Service(port);
284
285 g_proxy->Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
286 }
287
288 void wxURL::SetProxy(const wxString& url_proxy)
289 {
290 if (url_proxy.IsNull()) {
291 m_proxy.Close();
292 return;
293 }
294
295 CleanData();
296
297 wxString tmp_str;
298 wxString hostname, port;
299 int pos;
300 wxIPV4address addr;
301
302 tmp_str = url_proxy;
303 pos = tmp_str.Find(':');
304 hostname = tmp_str(0, pos);
305 port = tmp_str(pos, tmp_str.Length()-pos);
306
307 addr.Hostname(hostname);
308 addr.Service(port);
309
310 m_proxy.Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
311
312 m_protocol = &m_proxy;
313 m_protoname = "proxy";
314 m_path = url_proxy;
315 }