]>
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"
30 #include <wx/string.h>
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
39 IMPLEMENT_CLASS(wxURL
, wxObject
)
43 wxProtoInfo
*wxURL::g_protocols
= NULL
;
44 wxHTTP
*wxURL::g_proxy
;
46 /////////////////////////////////////////////////////////////////
47 // wxURL ////////////////////////////////////////////////////////
48 /////////////////////////////////////////////////////////////////
51 * --------------------------------------------------------------
52 * --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
53 * --------------------------------------------------------------
56 wxURL::wxURL(const wxString
& url
)
59 if (g_proxy
->IsConnected()) {
61 m_protoname
= "proxy";
66 m_error
= wxURL_NOERR
;
69 bool wxURL::ParseURL()
71 wxString last_url
= m_url
;
76 // Extract protocol name
77 if (!PrepProto(last_url
)) {
78 m_error
= wxURL_SNTXERR
;
82 // Find and create the protocol object
83 if (!FetchProtocol()) {
84 m_error
= wxURL_NOPROTO
;
88 // Do we need a host name ?
89 if (m_protoinfo
->m_needhost
) {
91 if (!PrepHost(last_url
)) {
92 m_error
= wxURL_SNTXERR
;
98 if (!PrepPath(last_url
)) {
99 m_error
= wxURL_NOPATH
;
103 m_error
= wxURL_NOERR
;
107 void wxURL::CleanData()
109 if (m_protoname
!= "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
= url(pos2
, pos
);
163 if (!m_servname
.IsNumber())
166 temp_url
= temp_url(0, pos2
);
169 // Retrieve user and password.
170 pos2
= temp_url
.Find('@');
171 // Even if pos2 equals -1, this code is right.
172 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
180 temp_url
= temp_url(0, pos2
);
181 pos2
= temp_url
.Find(':');
186 m_user
= temp_url(0, pos2
);
187 m_password
= temp_url(pos2
+1, url
.Length());
192 bool wxURL::PrepPath(wxString
& url
)
194 if (url
.Length() != 0)
201 bool wxURL::FetchProtocol()
203 wxProtoInfo
*info
= g_protocols
;
206 if (m_protoname
== info
->m_protoname
) {
207 if (m_servname
.IsNull())
208 m_servname
= info
->m_servname
;
211 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
212 wxSocketHandler::Master().Register(m_protocol
);
221 * --------------------------------------------------------------
222 * --------- wxURL get ------------------------------------------
223 * --------------------------------------------------------------
225 wxInputStream
*wxURL::GetInputStream(void)
228 wxInputStream
*the_i_stream
= NULL
;
235 m_error
= wxURL_NOPROTO
;
239 m_error
= wxURL_NOERR
;
241 m_protocol
->SetUser(m_user
);
242 m_protocol
->SetPassword(m_password
);
245 if (m_protoinfo
->m_needhost
) {
246 if (!addr
.Hostname(m_hostname
)) {
247 m_error
= wxURL_NOHOST
;
251 addr
.Service(m_servname
);
253 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
255 m_error
= wxURL_CONNERR
;
260 the_i_stream
= m_protocol
->GetInputStream(m_path
);
262 m_error
= wxURL_PROTOERR
;
269 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
273 if (url_proxy
.IsNull())
276 wxString tmp_str
= url_proxy
;
277 int pos
= tmp_str
.Find(':');
278 wxString hostname
= tmp_str(0, pos
),
279 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
282 addr
.Hostname(hostname
);
285 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
288 void wxURL::SetProxy(const wxString
& url_proxy
)
290 if (url_proxy
.IsNull()) {
298 wxString hostname
, port
;
303 pos
= tmp_str
.Find(':');
304 hostname
= tmp_str(0, pos
);
305 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
307 addr
.Hostname(hostname
);
310 m_proxy
.Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
312 m_protocol
= &m_proxy
;
313 m_protoname
= "proxy";