]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "url.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include <wx/string.h>
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
35 IMPLEMENT_CLASS(wxURL
, wxObject
)
39 wxProtoInfo
*wxURL::g_protocols
= NULL
;
40 wxHTTP
*wxURL::g_proxy
= NULL
;
42 // --------------------------------------------------------------
44 // --------------------------------------------------------------
46 // --------------------------------------------------------------
47 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
48 // --------------------------------------------------------------
50 wxURL::wxURL(const wxString
& url
)
53 m_error
= wxURL_NOERR
;
55 m_useProxy
= (g_proxy
!= NULL
);
60 bool wxURL::ParseURL()
62 wxString last_url
= m_url
;
64 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
70 // Extract protocol name
71 if (!PrepProto(last_url
)) {
72 m_error
= wxURL_SNTXERR
;
76 // Find and create the protocol object
77 if (!FetchProtocol()) {
78 m_error
= wxURL_NOPROTO
;
82 // Do we need a host name ?
83 if (m_protoinfo
->m_needhost
) {
85 if (!PrepHost(last_url
)) {
86 m_error
= wxURL_SNTXERR
;
92 if (!PrepPath(last_url
)) {
93 m_error
= wxURL_NOPATH
;
97 // URL parse finished.
100 // We destroy the newly created protocol.
103 // Third, we rebuild the URL.
104 m_url
= m_protoname
+ _T(":");
105 if (m_protoinfo
->m_needhost
)
106 m_url
= m_url
+ _T("//") + m_hostname
;
110 // We initialize specific variables.
111 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
114 m_error
= wxURL_NOERR
;
118 void wxURL::CleanData()
127 if (m_proxy
&& m_proxy
!= g_proxy
)
131 // --------------------------------------------------------------
132 // --------- wxURL urls decoders --------------------------------
133 // --------------------------------------------------------------
135 bool wxURL::PrepProto(wxString
& url
)
140 pos
= url
.Find(_T(':'));
144 m_protoname
= url(0, pos
);
146 url
= url(pos
+1, url
.Length());
151 bool wxURL::PrepHost(wxString
& url
)
156 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
159 url
= url(2, url
.Length());
161 pos
= url
.Find(_T('/'));
168 temp_url
= url(0, pos
);
169 url
= url(url
.Find(_T('/')), url
.Length());
171 // Retrieve service number
172 pos2
= temp_url
.Find(_T(':'), TRUE
);
173 if (pos2
!= -1 && pos2
< pos
) {
174 m_servname
= temp_url(pos2
+1, pos
);
175 if (!m_servname
.IsNumber())
177 temp_url
= temp_url(0, pos2
);
180 // Retrieve user and password.
181 pos2
= temp_url
.Find(_T('@'));
182 // Even if pos2 equals -1, this code is right.
183 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
191 temp_url
= temp_url(0, pos2
);
192 pos2
= temp_url
.Find(_T(':'));
197 m_user
= temp_url(0, pos2
);
198 m_password
= temp_url(pos2
+1, url
.Length());
203 bool wxURL::PrepPath(wxString
& url
)
205 if (url
.Length() != 0)
206 m_path
= ConvertToValidURI(url
);
212 bool wxURL::FetchProtocol()
214 wxProtoInfo
*info
= g_protocols
;
217 if (m_protoname
== info
->m_protoname
) {
218 if (m_servname
.IsNull())
219 m_servname
= info
->m_servname
;
222 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
230 // --------------------------------------------------------------
231 // --------- wxURL get ------------------------------------------
232 // --------------------------------------------------------------
234 wxInputStream
*wxURL::GetInputStream(void)
237 wxInputStream
*the_i_stream
= NULL
;
240 m_error
= wxURL_NOPROTO
;
244 m_error
= wxURL_NOERR
;
245 if (m_user
!= _T("")) {
246 m_protocol
->SetUser(m_user
);
247 m_protocol
->SetPassword(m_password
);
250 // m_protoinfo is NULL when we use a proxy
251 if (!m_useProxy
&& m_protoinfo
->m_needhost
) {
252 if (!addr
.Hostname(m_hostname
)) {
253 m_error
= wxURL_NOHOST
;
257 addr
.Service(m_servname
);
259 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
261 m_error
= wxURL_CONNERR
;
266 // When we use a proxy, we have to pass the whole URL to it.
268 the_i_stream
= m_protocol
->GetInputStream(m_url
);
270 the_i_stream
= m_protocol
->GetInputStream(m_path
);
273 m_error
= wxURL_PROTOERR
;
280 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
282 if (url_proxy
.IsNull()) {
289 wxString tmp_str
= url_proxy
;
290 int pos
= tmp_str
.Find(_T(':'));
294 wxString hostname
= tmp_str(0, pos
),
295 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
298 if (!addr
.Hostname(hostname
))
300 if (!addr
.Service(port
))
304 // Finally, when all is right, we connect the new proxy.
307 g_proxy
= new wxHTTP();
308 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
311 void wxURL::SetProxy(const wxString
& url_proxy
)
313 if (url_proxy
.IsNull()) {
323 wxString hostname
, port
;
328 pos
= tmp_str
.Find(_T(':'));
329 // This is an invalid proxy name.
333 hostname
= tmp_str(0, pos
);
334 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
336 addr
.Hostname(hostname
);
339 // Finally, create the whole stuff.
340 if (m_proxy
&& m_proxy
!= g_proxy
)
342 m_proxy
= new wxHTTP();
343 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
351 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
357 for (i
=0;i
<uri
.Len();i
++) {
358 wxChar c
= uri
.GetChar(i
);
363 if (!isalpha(c
) && c
!= _T('.') && c
!= _T('+') && c
!= _T('/')) {
364 hexa_code
.Printf(_T("%%%02X"), c
);
365 out_str
+= hexa_code
;