]>
Commit | Line | Data |
---|---|---|
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 | } | |
68 | ||
69 | bool wxURL::ParseURL() | |
70 | { | |
71 | wxString last_url = m_url; | |
72 | ||
73 | // Clean up | |
74 | CleanData(); | |
75 | ||
76 | // Extract protocol name | |
77 | if (!PrepProto(last_url)) { | |
78 | m_error = wxURL_SNTXERR; | |
79 | return FALSE; | |
80 | } | |
81 | ||
82 | // Find and create the protocol object | |
83 | if (!FetchProtocol()) { | |
84 | m_error = wxURL_NOPROTO; | |
85 | return FALSE; | |
86 | } | |
87 | ||
88 | // Do we need a host name ? | |
89 | if (m_protoinfo->m_needhost) { | |
90 | // Extract it | |
91 | if (!PrepHost(last_url)) { | |
92 | m_error = wxURL_SNTXERR; | |
93 | return FALSE; | |
94 | } | |
95 | } | |
96 | ||
97 | // Extract full path | |
98 | if (!PrepPath(last_url)) { | |
99 | m_error = wxURL_NOPATH; | |
100 | return FALSE; | |
101 | } | |
102 | ||
103 | m_error = wxURL_NOERR; | |
104 | return TRUE; | |
105 | } | |
106 | ||
107 | void wxURL::CleanData() | |
108 | { | |
109 | if (m_protoname != "proxy") | |
110 | delete m_protocol; | |
111 | } | |
112 | ||
113 | wxURL::~wxURL() | |
114 | { | |
115 | CleanData(); | |
116 | } | |
117 | ||
118 | /* | |
119 | * -------------------------------------------------------------- | |
120 | * --------- wxURL urls decoders -------------------------------- | |
121 | * -------------------------------------------------------------- | |
122 | */ | |
123 | bool wxURL::PrepProto(wxString& url) | |
124 | { | |
125 | int pos; | |
126 | ||
127 | // Find end | |
128 | pos = url.Find(':'); | |
129 | if (pos == -1) | |
130 | return FALSE; | |
131 | ||
132 | m_protoname = url(0, pos); | |
133 | ||
134 | url = url(pos+1, url.Length()); | |
135 | ||
136 | return TRUE; | |
137 | } | |
138 | ||
139 | bool wxURL::PrepHost(wxString& url) | |
140 | { | |
141 | wxString temp_url; | |
142 | int pos, pos2; | |
143 | ||
144 | if ((url.GetChar(0) != '/') || (url.GetChar(1) != '/')) | |
145 | return FALSE; | |
146 | ||
147 | url = url(2, url.Length()); | |
148 | ||
149 | pos = url.Find('/'); | |
150 | if (pos == -1) | |
151 | pos = url.Length(); | |
152 | ||
153 | if (pos == 0) | |
154 | return FALSE; | |
155 | ||
156 | temp_url = url(0, pos); | |
157 | url = url(url.Find('/'), url.Length()); | |
158 | ||
159 | // Retrieve service number | |
160 | pos2 = temp_url.Find(':', TRUE); | |
161 | if (pos2 != -1 && pos2 < pos) { | |
162 | m_servname = url(pos2, pos); | |
163 | if (!m_servname.IsNumber()) | |
164 | return FALSE; | |
165 | pos2 = pos; | |
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)) { | |
254 | m_error = wxURL_CONNERR; | |
255 | return NULL; | |
256 | } | |
257 | } | |
258 | ||
259 | the_i_stream = m_protocol->GetInputStream(m_path); | |
260 | if (!the_i_stream) { | |
261 | m_error = wxURL_PROTOERR; | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | return the_i_stream; | |
266 | } | |
267 | ||
268 | void wxURL::SetDefaultProxy(const wxString& url_proxy) | |
269 | { | |
270 | g_proxy.Close(); | |
271 | ||
272 | if (url_proxy.IsNull()) | |
273 | return; | |
274 | ||
275 | wxString tmp_str = url_proxy; | |
276 | int pos = tmp_str.Find(':'); | |
277 | wxString hostname = tmp_str(0, pos), | |
278 | port = tmp_str(pos, tmp_str.Length()-pos); | |
279 | wxIPV4address addr; | |
280 | ||
281 | addr.Hostname(hostname); | |
282 | addr.Service(port); | |
283 | ||
284 | g_proxy.Connect(addr); | |
285 | } | |
286 | ||
287 | void wxURL::SetProxy(const wxString& url_proxy) | |
288 | { | |
289 | if (url_proxy.IsNull()) { | |
290 | m_proxy.Close(); | |
291 | return; | |
292 | } | |
293 | ||
294 | CleanData(); | |
295 | ||
296 | wxString tmp_str; | |
297 | wxString hostname, port; | |
298 | int pos; | |
299 | wxIPV4address addr; | |
300 | ||
301 | tmp_str = url_proxy; | |
302 | pos = tmp_str.Find(':'); | |
303 | hostname = tmp_str(0, pos); | |
304 | port = tmp_str(pos, tmp_str.Length()-pos); | |
305 | ||
306 | addr.Hostname(hostname); | |
307 | addr.Service(port); | |
308 | ||
309 | m_proxy.Connect(addr); | |
310 | ||
311 | m_protocol = &m_proxy; | |
312 | m_protoname = "proxy"; | |
313 | m_path = url_proxy; | |
314 | } |