]> git.saurik.com Git - wxWidgets.git/blame - src/common/url.cpp
1. C++ comments removed from setup.h
[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
23#ifndef WX_PRECOMP
24#endif
25
f4ada568
GL
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
f4ada568
GL
37#if !USE_SHARED_LIBRARY
38IMPLEMENT_CLASS(wxProtoInfo, wxObject)
39IMPLEMENT_CLASS(wxURL, wxObject)
40#endif
41
42// Protocols list
43wxProtoInfo *wxURL::g_protocols = NULL;
3b4183d8 44wxHTTP *wxURL::g_proxy;
f4ada568
GL
45
46/////////////////////////////////////////////////////////////////
47// wxURL ////////////////////////////////////////////////////////
48/////////////////////////////////////////////////////////////////
49
50/*
51 * --------------------------------------------------------------
52 * --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
53 * --------------------------------------------------------------
54 */
55
56wxURL::wxURL(const wxString& url)
57{
58 m_protocol = NULL;
3b4183d8
GL
59 if (g_proxy->IsConnected()) {
60 m_protocol = g_proxy;
f4ada568
GL
61 m_protoname = "proxy";
62 m_path = url;
63 return;
64 }
65 m_url = url;
66 m_error = wxURL_NOERR;
67}
68
69bool 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
107void wxURL::CleanData()
108{
109 if (m_protoname != "proxy")
110 delete m_protocol;
111}
112
113wxURL::~wxURL()
114{
115 CleanData();
116}
117
118/*
119 * --------------------------------------------------------------
120 * --------- wxURL urls decoders --------------------------------
121 * --------------------------------------------------------------
122 */
123bool 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
139bool wxURL::PrepHost(wxString& url)
140{
856d2e52 141 wxString temp_url;
f4ada568
GL
142 int pos, pos2;
143
fcc6dddd 144 if ((url.GetChar(0) != '/') || (url.GetChar(1) != '/'))
f4ada568
GL
145 return FALSE;
146
147 url = url(2, url.Length());
148
149 pos = url.Find('/');
150 if (pos == -1)
b7db6f0b 151 pos = url.Length();
f4ada568 152
856d2e52
GL
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);
f4ada568
GL
161 if (pos2 != -1 && pos2 < pos) {
162 m_servname = url(pos2, pos);
163 if (!m_servname.IsNumber())
164 return FALSE;
165 pos2 = pos;
856d2e52 166 temp_url = temp_url(0, pos2);
f4ada568
GL
167 }
168
856d2e52
GL
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());
f4ada568 173
856d2e52
GL
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());
f4ada568
GL
188
189 return TRUE;
190}
191
192bool wxURL::PrepPath(wxString& url)
193{
194 if (url.Length() != 0)
195 m_path = url;
196 else
197 m_path = "/";
198 return TRUE;
199}
200
201bool 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 */
225wxInputStream *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;
856d2e52
GL
240 if (m_user != "") {
241 m_protocol->SetUser(m_user);
242 m_protocol->SetPassword(m_password);
243 }
244
f4ada568
GL
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
8a2c6ef8
JS
253 if (!m_protocol->Connect(addr, TRUE)) // Watcom needs the 2nd arg for some reason
254 {
f4ada568
GL
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
269void wxURL::SetDefaultProxy(const wxString& url_proxy)
270{
3b4183d8 271 g_proxy->Close();
f4ada568
GL
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
8a2c6ef8 285 g_proxy->Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
f4ada568
GL
286}
287
288void 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
8a2c6ef8 310 m_proxy.Connect(addr, TRUE); // Watcom needs the 2nd arg for some reason
f4ada568
GL
311
312 m_protocol = &m_proxy;
313 m_protoname = "proxy";
314 m_path = url_proxy;
315}