]>
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"
26 #include "wx/string.h"
31 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
32 IMPLEMENT_CLASS(wxURL
, wxObject
)
35 wxProtoInfo
*wxURL::g_protocols
= NULL
;
38 wxHTTP
*wxURL::g_proxy
= NULL
;
41 // --------------------------------------------------------------
43 // --------------------------------------------------------------
45 // --------------------------------------------------------------
46 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
47 // --------------------------------------------------------------
49 wxURL::wxURL(const wxString
& url
)
52 m_error
= wxURL_NOERR
;
55 m_useProxy
= (g_proxy
!= NULL
);
61 bool wxURL::ParseURL()
63 wxString last_url
= m_url
;
65 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
71 // Extract protocol name
72 if (!PrepProto(last_url
)) {
73 m_error
= wxURL_SNTXERR
;
77 // Find and create the protocol object
78 if (!FetchProtocol()) {
79 m_error
= wxURL_NOPROTO
;
83 // Do we need a host name ?
84 if (m_protoinfo
->m_needhost
) {
86 if (!PrepHost(last_url
)) {
87 m_error
= wxURL_SNTXERR
;
93 if (!PrepPath(last_url
)) {
94 m_error
= wxURL_NOPATH
;
98 // URL parse finished.
102 // We destroy the newly created protocol.
105 // Third, we rebuild the URL.
106 m_url
= m_protoname
+ wxT(":");
107 if (m_protoinfo
->m_needhost
)
108 m_url
= m_url
+ wxT("//") + m_hostname
;
112 // We initialize specific variables.
113 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
117 m_error
= wxURL_NOERR
;
121 void wxURL::CleanData()
133 if (m_proxy
&& m_proxy
!= g_proxy
)
138 // --------------------------------------------------------------
139 // --------- wxURL urls decoders --------------------------------
140 // --------------------------------------------------------------
142 bool wxURL::PrepProto(wxString
& url
)
147 pos
= url
.Find(wxT(':'));
151 m_protoname
= url(0, pos
);
153 url
= url(pos
+1, url
.Length());
158 bool wxURL::PrepHost(wxString
& url
)
163 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
166 url
= url(2, url
.Length());
168 pos
= url
.Find(wxT('/'));
175 temp_url
= url(0, pos
);
176 url
= url(url
.Find(wxT('/')), url
.Length());
178 // Retrieve service number
179 pos2
= temp_url
.Find(wxT(':'), TRUE
);
180 if (pos2
!= -1 && pos2
< pos
) {
181 m_servname
= temp_url(pos2
+1, pos
);
182 if (!m_servname
.IsNumber())
184 temp_url
= temp_url(0, pos2
);
187 // Retrieve user and password.
188 pos2
= temp_url
.Find(wxT('@'));
189 // Even if pos2 equals -1, this code is right.
190 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
193 m_password
= wxT("");
198 temp_url
= temp_url(0, pos2
);
199 pos2
= temp_url
.Find(wxT(':'));
204 m_user
= temp_url(0, pos2
);
205 m_password
= temp_url(pos2
+1, url
.Length());
210 bool wxURL::PrepPath(wxString
& url
)
212 if (url
.Length() != 0)
213 m_path
= ConvertToValidURI(url
);
219 bool wxURL::FetchProtocol()
221 wxProtoInfo
*info
= g_protocols
;
224 if (m_protoname
== info
->m_protoname
) {
225 if (m_servname
.IsNull())
226 m_servname
= info
->m_servname
;
229 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
237 // --------------------------------------------------------------
238 // --------- wxURL get ------------------------------------------
239 // --------------------------------------------------------------
241 wxInputStream
*wxURL::GetInputStream()
243 wxInputStream
*the_i_stream
= NULL
;
246 m_error
= wxURL_NOPROTO
;
250 m_error
= wxURL_NOERR
;
251 if (m_user
!= wxT("")) {
252 m_protocol
->SetUser(m_user
);
253 m_protocol
->SetPassword(m_password
);
259 // m_protoinfo is NULL when we use a proxy
260 if (!m_useProxy
&& m_protoinfo
->m_needhost
) {
261 if (!addr
.Hostname(m_hostname
)) {
262 m_error
= wxURL_NOHOST
;
266 addr
.Service(m_servname
);
268 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
270 m_error
= wxURL_CONNERR
;
276 // When we use a proxy, we have to pass the whole URL to it.
278 the_i_stream
= m_protocol
->GetInputStream(m_url
);
280 the_i_stream
= m_protocol
->GetInputStream(m_path
);
283 m_error
= wxURL_PROTOERR
;
291 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
293 if (url_proxy
.IsNull()) {
300 wxString tmp_str
= url_proxy
;
301 int pos
= tmp_str
.Find(wxT(':'));
305 wxString hostname
= tmp_str(0, pos
),
306 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
309 if (!addr
.Hostname(hostname
))
311 if (!addr
.Service(port
))
315 // Finally, when all is right, we connect the new proxy.
318 g_proxy
= new wxHTTP();
319 g_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
322 void wxURL::SetProxy(const wxString
& url_proxy
)
324 if (url_proxy
.IsNull()) {
334 wxString hostname
, port
;
339 pos
= tmp_str
.Find(wxT(':'));
340 // This is an invalid proxy name.
344 hostname
= tmp_str(0, pos
);
345 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
347 addr
.Hostname(hostname
);
350 // Finally, create the whole stuff.
351 if (m_proxy
&& m_proxy
!= g_proxy
)
353 m_proxy
= new wxHTTP();
354 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
363 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
369 for (i
=0;i
<uri
.Len();i
++) {
370 wxChar c
= uri
.GetChar(i
);
375 if (!isalpha(c
) && c
!= wxT('.') && c
!= wxT('+') && c
!= wxT('/')) {
376 hexa_code
.Printf(wxT("%%%02X"), c
);
377 out_str
+= hexa_code
;
386 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
391 while (i
<uri
.Len()) {
393 if (uri
[i
] == wxT('%')) {
395 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
396 code
= (uri
[i
] - wxT('A') + 10) * 16;
398 code
= (uri
[i
] - wxT('0')) * 16;
400 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
401 code
+= (uri
[i
] - wxT('A')) + 10;
403 code
+= (uri
[i
] - wxT('0'));
405 new_uri
+= (wxChar
)code
;