]>
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();
218 // --------------------------------------------------------------
219 // --------- wxURL get ------------------------------------------
220 // --------------------------------------------------------------
222 wxInputStream
*wxURL::GetInputStream(void)
225 wxInputStream
*the_i_stream
= NULL
;
232 m_error
= wxURL_NOPROTO
;
236 m_error
= wxURL_NOERR
;
237 if (m_user
!= _T("")) {
238 m_protocol
->SetUser(m_user
);
239 m_protocol
->SetPassword(m_password
);
242 if (m_protoinfo
->m_needhost
) {
243 if (!addr
.Hostname(m_hostname
)) {
244 m_error
= wxURL_NOHOST
;
248 addr
.Service(m_servname
);
250 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
252 m_error
= wxURL_CONNERR
;
257 the_i_stream
= m_protocol
->GetInputStream(m_path
);
259 m_error
= wxURL_PROTOERR
;
266 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
270 if (url_proxy
.IsNull())
273 wxString tmp_str
= url_proxy
;
274 int pos
= tmp_str
.Find(':');
275 wxString hostname
= tmp_str(0, pos
),
276 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
279 addr
.Hostname(hostname
);
282 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
285 void wxURL::SetProxy(const wxString
& url_proxy
)
287 if (url_proxy
.IsNull()) {
295 wxString hostname
, port
;
300 pos
= tmp_str
.Find(':');
301 hostname
= tmp_str(0, pos
);
302 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
304 addr
.Hostname(hostname
);
307 m_proxy
.Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
309 m_protocol
= &m_proxy
;
310 m_protoname
= "proxy";
314 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
320 for (i
=0;i
<uri
.Len();i
++) {
321 wxChar c
= uri
.GetChar(i
);
326 if (!isalpha(c
) && c
!= _T('.') && c
!= _T('+') && c
!= _T('.') &&
328 hexa_code
.Printf(_T("%%%02X"), c
);
329 out_str
+= hexa_code
;