]>
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"
32 #include <wx/string.h>
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
41 IMPLEMENT_CLASS(wxURL
, wxObject
)
45 wxProtoInfo
*wxURL::g_protocols
= NULL
;
46 wxHTTP
*wxURL::g_proxy
;
48 // --------------------------------------------------------------
50 // --------------------------------------------------------------
52 // --------------------------------------------------------------
53 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
54 // --------------------------------------------------------------
56 wxURL::wxURL(const wxString
& url
)
59 if (g_proxy
->IsConnected()) {
61 m_protoname
= "proxy";
66 m_error
= wxURL_NOERR
;
70 bool wxURL::ParseURL()
72 wxString last_url
= m_url
;
77 // Extract protocol name
78 if (!PrepProto(last_url
)) {
79 m_error
= wxURL_SNTXERR
;
83 // Find and create the protocol object
84 if (!FetchProtocol()) {
85 m_error
= wxURL_NOPROTO
;
89 // Do we need a host name ?
90 if (m_protoinfo
->m_needhost
) {
92 if (!PrepHost(last_url
)) {
93 m_error
= wxURL_SNTXERR
;
99 if (!PrepPath(last_url
)) {
100 m_error
= wxURL_NOPATH
;
104 m_error
= wxURL_NOERR
;
108 void wxURL::CleanData()
110 if (m_protoname
!= _T("proxy"))
119 // --------------------------------------------------------------
120 // --------- wxURL urls decoders --------------------------------
121 // --------------------------------------------------------------
123 bool wxURL::PrepProto(wxString
& url
)
132 m_protoname
= url(0, pos
);
134 url
= url(pos
+1, url
.Length());
139 bool wxURL::PrepHost(wxString
& url
)
144 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
147 url
= url(2, url
.Length());
156 temp_url
= url(0, pos
);
157 url
= url(url
.Find('/'), url
.Length());
159 // Retrieve service number
160 pos2
= temp_url
.Find(':', TRUE
);
161 if (pos2
!= -1 && pos2
< pos
) {
162 m_servname
= temp_url(pos2
+1, pos
);
163 if (!m_servname
.IsNumber())
165 temp_url
= temp_url(0, pos2
);
168 // Retrieve user and password.
169 pos2
= temp_url
.Find('@');
170 // Even if pos2 equals -1, this code is right.
171 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
179 temp_url
= temp_url(0, pos2
);
180 pos2
= temp_url
.Find(':');
185 m_user
= temp_url(0, pos2
);
186 m_password
= temp_url(pos2
+1, url
.Length());
191 bool wxURL::PrepPath(wxString
& url
)
193 if (url
.Length() != 0)
200 bool wxURL::FetchProtocol()
202 wxProtoInfo
*info
= g_protocols
;
205 if (m_protoname
== info
->m_protoname
) {
206 if (m_servname
.IsNull())
207 m_servname
= info
->m_servname
;
210 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
211 wxSocketHandler::Master().Register(m_protocol
);
219 // --------------------------------------------------------------
220 // --------- wxURL get ------------------------------------------
221 // --------------------------------------------------------------
223 wxInputStream
*wxURL::GetInputStream(void)
226 wxInputStream
*the_i_stream
= NULL
;
233 m_error
= wxURL_NOPROTO
;
237 m_error
= wxURL_NOERR
;
238 if (m_user
!= _T("")) {
239 m_protocol
->SetUser(m_user
);
240 m_protocol
->SetPassword(m_password
);
243 if (m_protoinfo
->m_needhost
) {
244 if (!addr
.Hostname(m_hostname
)) {
245 m_error
= wxURL_NOHOST
;
249 addr
.Service(m_servname
);
251 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
253 m_error
= wxURL_CONNERR
;
258 the_i_stream
= m_protocol
->GetInputStream(m_path
);
260 m_error
= wxURL_PROTOERR
;
267 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
271 if (url_proxy
.IsNull())
274 wxString tmp_str
= url_proxy
;
275 int pos
= tmp_str
.Find(':');
276 wxString hostname
= tmp_str(0, pos
),
277 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
280 addr
.Hostname(hostname
);
283 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
286 void wxURL::SetProxy(const wxString
& url_proxy
)
288 if (url_proxy
.IsNull()) {
296 wxString hostname
, port
;
301 pos
= tmp_str
.Find(':');
302 hostname
= tmp_str(0, pos
);
303 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
305 addr
.Hostname(hostname
);
308 m_proxy
.Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
310 m_protocol
= &m_proxy
;
311 m_protoname
= "proxy";
315 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
321 for (i
=0;i
<uri
.Len();i
++) {
322 wxChar c
= uri
.GetChar(i
);
324 if (!isalpha(c
) && c
!= _T('.') && c
!= _T('+') && c
!= _T('.') &&
326 hexa_code
.Printf(_T("%02X"), c
);
327 out_str
+= hexa_code
;