]>
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"
26 #include <wx/string.h>
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
33 IMPLEMENT_CLASS(wxURL
, wxObject
)
37 wxProtoInfo
*wxURL::g_protocols
= NULL
;
40 wxHTTP
*wxURL::g_proxy
= NULL
;
43 // --------------------------------------------------------------
45 // --------------------------------------------------------------
47 // --------------------------------------------------------------
48 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
49 // --------------------------------------------------------------
51 wxURL::wxURL(const wxString
& url
)
54 m_error
= wxURL_NOERR
;
57 m_useProxy
= (g_proxy
!= NULL
);
63 bool wxURL::ParseURL()
65 wxString last_url
= m_url
;
67 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
73 // Extract protocol name
74 if (!PrepProto(last_url
)) {
75 m_error
= wxURL_SNTXERR
;
79 // Find and create the protocol object
80 if (!FetchProtocol()) {
81 m_error
= wxURL_NOPROTO
;
85 // Do we need a host name ?
86 if (m_protoinfo
->m_needhost
) {
88 if (!PrepHost(last_url
)) {
89 m_error
= wxURL_SNTXERR
;
95 if (!PrepPath(last_url
)) {
96 m_error
= wxURL_NOPATH
;
100 // URL parse finished.
104 // We destroy the newly created protocol.
107 // Third, we rebuild the URL.
108 m_url
= m_protoname
+ _T(":");
109 if (m_protoinfo
->m_needhost
)
110 m_url
= m_url
+ _T("//") + m_hostname
;
114 // We initialize specific variables.
115 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
119 m_error
= wxURL_NOERR
;
123 void wxURL::CleanData()
135 if (m_proxy
&& m_proxy
!= g_proxy
)
140 // --------------------------------------------------------------
141 // --------- wxURL urls decoders --------------------------------
142 // --------------------------------------------------------------
144 bool wxURL::PrepProto(wxString
& url
)
149 pos
= url
.Find(_T(':'));
153 m_protoname
= url(0, pos
);
155 url
= url(pos
+1, url
.Length());
160 bool wxURL::PrepHost(wxString
& url
)
165 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
168 url
= url(2, url
.Length());
170 pos
= url
.Find(_T('/'));
177 temp_url
= url(0, pos
);
178 url
= url(url
.Find(_T('/')), url
.Length());
180 // Retrieve service number
181 pos2
= temp_url
.Find(_T(':'), TRUE
);
182 if (pos2
!= -1 && pos2
< pos
) {
183 m_servname
= temp_url(pos2
+1, pos
);
184 if (!m_servname
.IsNumber())
186 temp_url
= temp_url(0, pos2
);
189 // Retrieve user and password.
190 pos2
= temp_url
.Find(_T('@'));
191 // Even if pos2 equals -1, this code is right.
192 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
200 temp_url
= temp_url(0, pos2
);
201 pos2
= temp_url
.Find(_T(':'));
206 m_user
= temp_url(0, pos2
);
207 m_password
= temp_url(pos2
+1, url
.Length());
212 bool wxURL::PrepPath(wxString
& url
)
214 if (url
.Length() != 0)
215 m_path
= ConvertToValidURI(url
);
221 bool wxURL::FetchProtocol()
223 wxProtoInfo
*info
= g_protocols
;
226 if (m_protoname
== info
->m_protoname
) {
227 if (m_servname
.IsNull())
228 m_servname
= info
->m_servname
;
231 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
239 // --------------------------------------------------------------
240 // --------- wxURL get ------------------------------------------
241 // --------------------------------------------------------------
243 wxInputStream
*wxURL::GetInputStream(void)
245 wxInputStream
*the_i_stream
= NULL
;
248 m_error
= wxURL_NOPROTO
;
252 m_error
= wxURL_NOERR
;
253 if (m_user
!= _T("")) {
254 m_protocol
->SetUser(m_user
);
255 m_protocol
->SetPassword(m_password
);
261 // m_protoinfo is NULL when we use a proxy
262 if (!m_useProxy
&& m_protoinfo
->m_needhost
) {
263 if (!addr
.Hostname(m_hostname
)) {
264 m_error
= wxURL_NOHOST
;
268 addr
.Service(m_servname
);
270 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
272 m_error
= wxURL_CONNERR
;
278 // When we use a proxy, we have to pass the whole URL to it.
280 the_i_stream
= m_protocol
->GetInputStream(m_url
);
282 the_i_stream
= m_protocol
->GetInputStream(m_path
);
285 m_error
= wxURL_PROTOERR
;
293 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
295 if (url_proxy
.IsNull()) {
302 wxString tmp_str
= url_proxy
;
303 int pos
= tmp_str
.Find(_T(':'));
307 wxString hostname
= tmp_str(0, pos
),
308 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
311 if (!addr
.Hostname(hostname
))
313 if (!addr
.Service(port
))
317 // Finally, when all is right, we connect the new proxy.
320 g_proxy
= new wxHTTP();
321 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
324 void wxURL::SetProxy(const wxString
& url_proxy
)
326 if (url_proxy
.IsNull()) {
336 wxString hostname
, port
;
341 pos
= tmp_str
.Find(_T(':'));
342 // This is an invalid proxy name.
346 hostname
= tmp_str(0, pos
);
347 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
349 addr
.Hostname(hostname
);
352 // Finally, create the whole stuff.
353 if (m_proxy
&& m_proxy
!= g_proxy
)
355 m_proxy
= new wxHTTP();
356 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
365 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
371 for (i
=0;i
<uri
.Len();i
++) {
372 wxChar c
= uri
.GetChar(i
);
377 if (!isalpha(c
) && c
!= _T('.') && c
!= _T('+') && c
!= _T('/')) {
378 hexa_code
.Printf(_T("%%%02X"), c
);
379 out_str
+= hexa_code
;
388 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
397 while (i
<uri
.Len()) {
398 if (uri
[i
] == _T('%')) {
400 if (uri
[i
] >= _T('A') && uri
[i
] <= _T('F'))
401 code
= (uri
[i
] - _T('A') + 10) * 16;
403 code
= (uri
[i
] - _T('0')) * 16;
405 if (uri
[i
] >= _T('A') && uri
[i
] <= _T('F'))
406 code
+= (uri
[i
] - _T('A')) + 10;
408 code
+= (uri
[i
] - _T('0'));
410 new_uri
+= (wxChar
)code
;