]>
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()) {
60 m_protocol
= &g_proxy
;
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
)
143 if ((url
.GetChar(0) != '/') || (url
.GetChar(1) != '/'))
146 url
= url(2, url
.Length());
152 pos2
= url
.Find(':');
153 if (pos2
!= -1 && pos2
< pos
) {
154 m_servname
= url(pos2
, pos
);
155 if (!m_servname
.IsNumber())
160 m_hostname
= url(0, pos
);
162 url
= url(url
.Find('/'), url
.Length());
167 bool wxURL::PrepPath(wxString
& url
)
169 if (url
.Length() != 0)
176 bool wxURL::FetchProtocol()
178 wxProtoInfo
*info
= g_protocols
;
181 if (m_protoname
== info
->m_protoname
) {
182 if (m_servname
.IsNull())
183 m_servname
= info
->m_servname
;
186 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
187 wxSocketHandler::Master().Register(m_protocol
);
196 * --------------------------------------------------------------
197 * --------- wxURL get ------------------------------------------
198 * --------------------------------------------------------------
200 wxInputStream
*wxURL::GetInputStream(void)
203 wxInputStream
*the_i_stream
= NULL
;
210 m_error
= wxURL_NOPROTO
;
214 m_error
= wxURL_NOERR
;
215 if (m_protoinfo
->m_needhost
) {
216 if (!addr
.Hostname(m_hostname
)) {
217 m_error
= wxURL_NOHOST
;
221 addr
.Service(m_servname
);
223 if (!m_protocol
->Connect(addr
)) {
224 m_error
= wxURL_CONNERR
;
229 the_i_stream
= m_protocol
->GetInputStream(m_path
);
231 m_error
= wxURL_PROTOERR
;
238 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
242 if (url_proxy
.IsNull())
245 wxString tmp_str
= url_proxy
;
246 int pos
= tmp_str
.Find(':');
247 wxString hostname
= tmp_str(0, pos
),
248 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
251 addr
.Hostname(hostname
);
254 g_proxy
.Connect(addr
);
257 void wxURL::SetProxy(const wxString
& url_proxy
)
259 if (url_proxy
.IsNull()) {
267 wxString hostname
, port
;
272 pos
= tmp_str
.Find(':');
273 hostname
= tmp_str(0, pos
);
274 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
276 addr
.Hostname(hostname
);
279 m_proxy
.Connect(addr
);
281 m_protocol
= &m_proxy
;
282 m_protoname
= "proxy";