]>
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
;
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
!= "proxy")
120 * --------------------------------------------------------------
121 * --------- wxURL urls decoders --------------------------------
122 * --------------------------------------------------------------
124 bool wxURL::PrepProto(wxString
& url
)
133 m_protoname
= url(0, pos
);
135 url
= url(pos
+1, url
.Length());
140 bool wxURL::PrepHost(wxString
& url
)
145 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
148 url
= url(2, url
.Length());
157 temp_url
= url(0, pos
);
158 url
= url(url
.Find('/'), url
.Length());
160 // Retrieve service number
161 pos2
= temp_url
.Find(':', TRUE
);
162 if (pos2
!= -1 && pos2
< pos
) {
163 m_servname
= url(pos2
, pos
);
164 if (!m_servname
.IsNumber())
167 temp_url
= temp_url(0, pos2
);
170 // Retrieve user and password.
171 pos2
= temp_url
.Find('@');
172 // Even if pos2 equals -1, this code is right.
173 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
181 temp_url
= temp_url(0, pos2
);
182 pos2
= temp_url
.Find(':');
187 m_user
= temp_url(0, pos2
);
188 m_password
= temp_url(pos2
+1, url
.Length());
193 bool wxURL::PrepPath(wxString
& url
)
195 if (url
.Length() != 0)
202 bool wxURL::FetchProtocol()
204 wxProtoInfo
*info
= g_protocols
;
207 if (m_protoname
== info
->m_protoname
) {
208 if (m_servname
.IsNull())
209 m_servname
= info
->m_servname
;
212 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
213 wxSocketHandler::Master().Register(m_protocol
);
222 * --------------------------------------------------------------
223 * --------- wxURL get ------------------------------------------
224 * --------------------------------------------------------------
226 wxInputStream
*wxURL::GetInputStream(void)
229 wxInputStream
*the_i_stream
= NULL
;
236 m_error
= wxURL_NOPROTO
;
240 m_error
= wxURL_NOERR
;
242 m_protocol
->SetUser(m_user
);
243 m_protocol
->SetPassword(m_password
);
246 if (m_protoinfo
->m_needhost
) {
247 if (!addr
.Hostname(m_hostname
)) {
248 m_error
= wxURL_NOHOST
;
252 addr
.Service(m_servname
);
254 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
256 m_error
= wxURL_CONNERR
;
261 the_i_stream
= m_protocol
->GetInputStream(m_path
);
263 m_error
= wxURL_PROTOERR
;
270 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
274 if (url_proxy
.IsNull())
277 wxString tmp_str
= url_proxy
;
278 int pos
= tmp_str
.Find(':');
279 wxString hostname
= tmp_str(0, pos
),
280 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
283 addr
.Hostname(hostname
);
286 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
289 void wxURL::SetProxy(const wxString
& url_proxy
)
291 if (url_proxy
.IsNull()) {
299 wxString hostname
, port
;
304 pos
= tmp_str
.Find(':');
305 hostname
= tmp_str(0, pos
);
306 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
308 addr
.Hostname(hostname
);
311 m_proxy
.Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
313 m_protocol
= &m_proxy
;
314 m_protoname
= "proxy";