]>
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 /////////////////////////////////////////////////////////////////
49 // wxURL ////////////////////////////////////////////////////////
50 /////////////////////////////////////////////////////////////////
53 * --------------------------------------------------------------
54 * --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
55 * --------------------------------------------------------------
58 wxURL::wxURL(const wxString
& url
)
61 if (g_proxy
->IsConnected()) {
63 m_protoname
= "proxy";
68 m_error
= wxURL_NOERR
;
72 bool wxURL::ParseURL()
74 wxString last_url
= m_url
;
79 // Extract protocol name
80 if (!PrepProto(last_url
)) {
81 m_error
= wxURL_SNTXERR
;
85 // Find and create the protocol object
86 if (!FetchProtocol()) {
87 m_error
= wxURL_NOPROTO
;
91 // Do we need a host name ?
92 if (m_protoinfo
->m_needhost
) {
94 if (!PrepHost(last_url
)) {
95 m_error
= wxURL_SNTXERR
;
101 if (!PrepPath(last_url
)) {
102 m_error
= wxURL_NOPATH
;
106 m_error
= wxURL_NOERR
;
110 void wxURL::CleanData()
112 if (m_protoname
!= _T("proxy"))
122 * --------------------------------------------------------------
123 * --------- wxURL urls decoders --------------------------------
124 * --------------------------------------------------------------
126 bool wxURL::PrepProto(wxString
& url
)
135 m_protoname
= url(0, pos
);
137 url
= url(pos
+1, url
.Length());
142 bool wxURL::PrepHost(wxString
& url
)
147 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
150 url
= url(2, url
.Length());
159 temp_url
= url(0, pos
);
160 url
= url(url
.Find('/'), url
.Length());
162 // Retrieve service number
163 pos2
= temp_url
.Find(':', TRUE
);
164 if (pos2
!= -1 && pos2
< pos
) {
165 m_servname
= temp_url(pos2
+1, pos
);
166 if (!m_servname
.IsNumber())
168 temp_url
= temp_url(0, pos2
);
171 // Retrieve user and password.
172 pos2
= temp_url
.Find('@');
173 // Even if pos2 equals -1, this code is right.
174 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
182 temp_url
= temp_url(0, pos2
);
183 pos2
= temp_url
.Find(':');
188 m_user
= temp_url(0, pos2
);
189 m_password
= temp_url(pos2
+1, url
.Length());
194 bool wxURL::PrepPath(wxString
& url
)
196 if (url
.Length() != 0)
203 bool wxURL::FetchProtocol()
205 wxProtoInfo
*info
= g_protocols
;
208 if (m_protoname
== info
->m_protoname
) {
209 if (m_servname
.IsNull())
210 m_servname
= info
->m_servname
;
213 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
214 wxSocketHandler::Master().Register(m_protocol
);
223 * --------------------------------------------------------------
224 * --------- wxURL get ------------------------------------------
225 * --------------------------------------------------------------
227 wxInputStream
*wxURL::GetInputStream(void)
230 wxInputStream
*the_i_stream
= NULL
;
237 m_error
= wxURL_NOPROTO
;
241 m_error
= wxURL_NOERR
;
242 if (m_user
!= _T("")) {
243 m_protocol
->SetUser(m_user
);
244 m_protocol
->SetPassword(m_password
);
247 if (m_protoinfo
->m_needhost
) {
248 if (!addr
.Hostname(m_hostname
)) {
249 m_error
= wxURL_NOHOST
;
253 addr
.Service(m_servname
);
255 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
257 m_error
= wxURL_CONNERR
;
262 the_i_stream
= m_protocol
->GetInputStream(m_path
);
264 m_error
= wxURL_PROTOERR
;
271 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
275 if (url_proxy
.IsNull())
278 wxString tmp_str
= url_proxy
;
279 int pos
= tmp_str
.Find(':');
280 wxString hostname
= tmp_str(0, pos
),
281 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
284 addr
.Hostname(hostname
);
287 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
290 void wxURL::SetProxy(const wxString
& url_proxy
)
292 if (url_proxy
.IsNull()) {
300 wxString hostname
, port
;
305 pos
= tmp_str
.Find(':');
306 hostname
= tmp_str(0, pos
);
307 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
309 addr
.Hostname(hostname
);
312 m_proxy
.Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
314 m_protocol
= &m_proxy
;
315 m_protoname
= "proxy";