]>
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"
19 #include <wx/string.h>
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
32 IMPLEMENT_CLASS(wxURL
, wxObject
)
36 wxProtoInfo
*wxURL::g_protocols
= NULL
;
37 wxHTTP
wxURL::g_proxy
;
39 /////////////////////////////////////////////////////////////////
40 // wxURL ////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////
44 * --------------------------------------------------------------
45 * --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
46 * --------------------------------------------------------------
49 wxURL::wxURL(const wxString
& url
)
52 if (g_proxy
.IsConnected()) {
53 m_protocol
= &g_proxy
;
54 m_protoname
= "proxy";
59 m_error
= wxURL_NOERR
;
62 bool wxURL::ParseURL()
64 wxString last_url
= m_url
;
69 // Extract protocol name
70 if (!PrepProto(last_url
)) {
71 m_error
= wxURL_SNTXERR
;
75 // Find and create the protocol object
76 if (!FetchProtocol()) {
77 m_error
= wxURL_NOPROTO
;
81 // Do we need a host name ?
82 if (m_protoinfo
->m_needhost
) {
84 if (!PrepHost(last_url
)) {
85 m_error
= wxURL_SNTXERR
;
91 if (!PrepPath(last_url
)) {
92 m_error
= wxURL_NOPATH
;
96 m_error
= wxURL_NOERR
;
100 void wxURL::CleanData()
102 if (m_protoname
!= "proxy")
112 * --------------------------------------------------------------
113 * --------- wxURL urls decoders --------------------------------
114 * --------------------------------------------------------------
116 bool wxURL::PrepProto(wxString
& url
)
125 m_protoname
= url(0, pos
);
127 url
= url(pos
+1, url
.Length());
132 bool wxURL::PrepHost(wxString
& url
)
136 if ((url
[0UL] != '/') || (url
[1UL] != '/'))
139 url
= url(2, url
.Length());
145 pos2
= url
.Find(':');
146 if (pos2
!= -1 && pos2
< pos
) {
147 m_servname
= url(pos2
, pos
);
148 if (!m_servname
.IsNumber())
153 m_hostname
= url(0, pos
);
155 url
= url(url
.Find('/'), url
.Length());
160 bool wxURL::PrepPath(wxString
& url
)
162 if (url
.Length() != 0)
169 bool wxURL::FetchProtocol()
171 wxProtoInfo
*info
= g_protocols
;
174 if (m_protoname
== info
->m_protoname
) {
175 if (m_servname
.IsNull())
176 m_servname
= info
->m_servname
;
179 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
180 wxSocketHandler::Master().Register(m_protocol
);
189 * --------------------------------------------------------------
190 * --------- wxURL get ------------------------------------------
191 * --------------------------------------------------------------
193 wxInputStream
*wxURL::GetInputStream(void)
196 wxInputStream
*the_i_stream
= NULL
;
203 m_error
= wxURL_NOPROTO
;
207 m_error
= wxURL_NOERR
;
208 if (m_protoinfo
->m_needhost
) {
209 if (!addr
.Hostname(m_hostname
)) {
210 m_error
= wxURL_NOHOST
;
214 addr
.Service(m_servname
);
216 if (!m_protocol
->Connect(addr
)) {
217 m_error
= wxURL_CONNERR
;
222 the_i_stream
= m_protocol
->GetInputStream(m_path
);
224 m_error
= wxURL_PROTOERR
;
231 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
235 if (url_proxy
.IsNull())
238 wxString tmp_str
= url_proxy
;
239 int pos
= tmp_str
.Find(':');
240 wxString hostname
= tmp_str(0, pos
),
241 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
244 addr
.Hostname(hostname
);
247 g_proxy
.Connect(addr
);
250 void wxURL::SetProxy(const wxString
& url_proxy
)
252 if (url_proxy
.IsNull()) {
260 wxString hostname
, port
;
265 pos
= tmp_str
.Find(':');
266 hostname
= tmp_str(0, pos
);
267 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
269 addr
.Hostname(hostname
);
272 m_proxy
.Connect(addr
);
274 m_protocol
= &m_proxy
;
275 m_protoname
= "proxy";