]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
25268e90d4a75011648c7a6f0049d5ad6162cf01
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::ms_protocols
= NULL
;
38 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
39 bool wxURL::ms_useDefaultProxy
= FALSE
;
42 // --------------------------------------------------------------
44 // --------------------------------------------------------------
46 // --------------------------------------------------------------
47 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
48 // --------------------------------------------------------------
50 wxURL::wxURL(const wxString
& url
)
53 m_error
= wxURL_NOERR
;
57 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
59 SetDefaultProxy(getenv("HTTP_PROXY"));
61 if ( !ms_proxyDefault
)
64 ms_useDefaultProxy
= FALSE
;
68 m_useProxy
= ms_proxyDefault
!= NULL
;
69 m_proxy
= ms_proxyDefault
;
70 #endif // wxUSE_SOCKETS
75 bool wxURL::ParseURL()
77 wxString last_url
= m_url
;
79 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
85 // Extract protocol name
86 if (!PrepProto(last_url
)) {
87 m_error
= wxURL_SNTXERR
;
91 // Find and create the protocol object
92 if (!FetchProtocol()) {
93 m_error
= wxURL_NOPROTO
;
97 // Do we need a host name ?
98 if (m_protoinfo
->m_needhost
) {
100 if (!PrepHost(last_url
)) {
101 m_error
= wxURL_SNTXERR
;
107 if (!PrepPath(last_url
)) {
108 m_error
= wxURL_NOPATH
;
112 // URL parse finished.
116 // We destroy the newly created protocol.
119 // Third, we rebuild the URL.
120 m_url
= m_protoname
+ wxT(":");
121 if (m_protoinfo
->m_needhost
)
122 m_url
= m_url
+ wxT("//") + m_hostname
;
126 // We initialize specific variables.
127 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
131 m_error
= wxURL_NOERR
;
135 void wxURL::CleanData()
147 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
152 // --------------------------------------------------------------
153 // --------- wxURL urls decoders --------------------------------
154 // --------------------------------------------------------------
156 bool wxURL::PrepProto(wxString
& url
)
161 pos
= url
.Find(wxT(':'));
165 m_protoname
= url(0, pos
);
167 url
= url(pos
+1, url
.Length());
172 bool wxURL::PrepHost(wxString
& url
)
177 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
180 url
= url(2, url
.Length());
182 pos
= url
.Find(wxT('/'));
189 temp_url
= url(0, pos
);
190 url
= url(url
.Find(wxT('/')), url
.Length());
192 // Retrieve service number
193 pos2
= temp_url
.Find(wxT(':'), TRUE
);
194 if (pos2
!= -1 && pos2
< pos
) {
195 m_servname
= temp_url(pos2
+1, pos
);
196 if (!m_servname
.IsNumber())
198 temp_url
= temp_url(0, pos2
);
201 // Retrieve user and password.
202 pos2
= temp_url
.Find(wxT('@'));
203 // Even if pos2 equals -1, this code is right.
204 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
207 m_password
= wxT("");
212 temp_url
= temp_url(0, pos2
);
213 pos2
= temp_url
.Find(wxT(':'));
218 m_user
= temp_url(0, pos2
);
219 m_password
= temp_url(pos2
+1, url
.Length());
224 bool wxURL::PrepPath(wxString
& url
)
226 if (url
.Length() != 0)
227 m_path
= ConvertToValidURI(url
);
233 bool wxURL::FetchProtocol()
235 wxProtoInfo
*info
= ms_protocols
;
238 if (m_protoname
== info
->m_protoname
) {
239 if (m_servname
.IsNull())
240 m_servname
= info
->m_servname
;
243 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
251 // --------------------------------------------------------------
252 // --------- wxURL get ------------------------------------------
253 // --------------------------------------------------------------
255 wxInputStream
*wxURL::GetInputStream()
257 wxInputStream
*the_i_stream
= NULL
;
260 m_error
= wxURL_NOPROTO
;
264 m_error
= wxURL_NOERR
;
265 if (m_user
!= wxT("")) {
266 m_protocol
->SetUser(m_user
);
267 m_protocol
->SetPassword(m_password
);
273 // m_protoinfo is NULL when we use a proxy
274 if (!m_useProxy
&& m_protoinfo
->m_needhost
) {
275 if (!addr
.Hostname(m_hostname
)) {
276 m_error
= wxURL_NOHOST
;
280 addr
.Service(m_servname
);
282 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
284 m_error
= wxURL_CONNERR
;
290 // When we use a proxy, we have to pass the whole URL to it.
292 the_i_stream
= m_protocol
->GetInputStream(m_url
);
294 the_i_stream
= m_protocol
->GetInputStream(m_path
);
297 m_error
= wxURL_PROTOERR
;
305 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
309 if ( ms_proxyDefault
)
311 ms_proxyDefault
->Close();
312 delete ms_proxyDefault
;
313 ms_proxyDefault
= NULL
;
318 wxString tmp_str
= url_proxy
;
319 int pos
= tmp_str
.Find(wxT(':'));
323 wxString hostname
= tmp_str(0, pos
),
324 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
327 if (!addr
.Hostname(hostname
))
329 if (!addr
.Service(port
))
333 // Finally, when all is right, we connect the new proxy.
334 ms_proxyDefault
->Close();
336 ms_proxyDefault
= new wxHTTP();
337 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
341 void wxURL::SetProxy(const wxString
& url_proxy
)
345 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
356 wxString hostname
, port
;
361 pos
= tmp_str
.Find(wxT(':'));
362 // This is an invalid proxy name.
366 hostname
= tmp_str(0, pos
);
367 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
369 addr
.Hostname(hostname
);
372 // Finally, create the whole stuff.
373 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
375 m_proxy
= new wxHTTP();
376 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
384 #endif // wxUSE_SOCKETS
386 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
392 for (i
=0;i
<uri
.Len();i
++) {
393 wxChar c
= uri
.GetChar(i
);
398 if (!isalpha(c
) && c
!= wxT('.') && c
!= wxT('+') && c
!= wxT('/')) {
399 hexa_code
.Printf(wxT("%%%02X"), c
);
400 out_str
+= hexa_code
;
409 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
414 while (i
<uri
.Len()) {
416 if (uri
[i
] == wxT('%')) {
418 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
419 code
= (uri
[i
] - wxT('A') + 10) * 16;
421 code
= (uri
[i
] - wxT('0')) * 16;
423 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
424 code
+= (uri
[i
] - wxT('A')) + 10;
426 code
+= (uri
[i
] - wxT('0'));
428 new_uri
+= (wxChar
)code
;
437 // ----------------------------------------------------------------------
438 // A module which deletes the default proxy if we created it
439 // ----------------------------------------------------------------------
443 class wxURLModule
: public wxModule
446 virtual bool OnInit();
447 virtual void OnExit();
450 DECLARE_DYNAMIC_CLASS(wxURLModule
)
453 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
455 bool wxURLModule::OnInit()
457 // env var HTTP_PROXY contains the address of the default proxy to use if
458 // set, but don't try to create this proxy right now because it will slow
459 // down the program startup (especially if there is no DNS server
460 // available, in which case it may take up to 1 minute)
461 if ( getenv("HTTP_PROXY") )
463 wxURL::ms_useDefaultProxy
= TRUE
;
469 void wxURLModule::OnExit()
471 delete wxURL::ms_proxyDefault
;
472 wxURL::ms_proxyDefault
= NULL
;
475 #endif // wxUSE_SOCKETS