]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
7d2da23724363c3d89a772dd88e9dca89af9f70c
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "url.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/string.h"
28 #include "wx/module.h"
34 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
35 IMPLEMENT_CLASS(wxURL
, wxURI
)
38 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
40 // Enforce linking of protocol classes:
41 USE_PROTOCOL(wxFileProto
)
47 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
48 bool wxURL::ms_useDefaultProxy
= false;
51 // --------------------------------------------------------------
55 // --------------------------------------------------------------
57 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
63 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
69 wxURL
& wxURL::operator = (const wxURI
& url
)
71 wxURI::operator = (url
);
76 wxURL
& wxURL::operator = (const wxString
& url
)
78 wxURI::operator = (url
);
84 void wxURL::Init(const wxString
& url
)
87 m_error
= wxURL_NOERR
;
90 m_nativeImp
= CreateNativeImpObject();
94 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
96 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
98 if ( !ms_proxyDefault
)
101 ms_useDefaultProxy
= false;
105 m_useProxy
= ms_proxyDefault
!= NULL
;
106 m_proxy
= ms_proxyDefault
;
107 #endif // wxUSE_SOCKETS
110 // --------------------------------------------------------------
113 // Builds the URL and takes care of the old protocol stuff
114 // --------------------------------------------------------------
116 bool wxURL::ParseURL()
118 // If the URL was already parsed (m_protocol != NULL), pass this section.
124 // Make sure we have a protocol/scheme
127 m_error
= wxURL_SNTXERR
;
131 // Find and create the protocol object
132 if (!FetchProtocol())
134 m_error
= wxURL_NOPROTO
;
138 // Do we need a host name ?
139 if (m_protoinfo
->m_needhost
)
141 // Make sure we have one, then
144 m_error
= wxURL_SNTXERR
;
153 // destroy the previously created protocol as we'll be using m_proxy
156 // Third, we rebuild the URL.
157 m_url
= m_scheme
+ wxT(":");
158 if (m_protoinfo
->m_needhost
)
159 m_url
= m_url
+ wxT("//") + m_server
;
161 // We initialize specific variables.
162 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
166 m_error
= wxURL_NOERR
;
170 void wxURL::CleanData()
182 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
191 bool wxURL::FetchProtocol()
193 wxProtoInfo
*info
= ms_protocols
;
197 if (m_scheme
== info
->m_protoname
)
200 m_port
= info
->m_servname
;
202 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
210 // --------------------------------------------------------------
211 // --------- wxURL get ------------------------------------------
212 // --------------------------------------------------------------
214 wxInputStream
*wxURL::GetInputStream()
218 m_error
= wxURL_NOPROTO
;
222 m_error
= wxURL_NOERR
;
225 size_t dwPasswordPos
= m_user
.find(':');
227 if (dwPasswordPos
== wxString::npos
)
228 m_protocol
->SetUser(m_user
);
231 m_protocol
->SetUser(m_user(0, dwPasswordPos
));
232 m_protocol
->SetPassword(m_user(dwPasswordPos
+1, m_user
.length() + 1));
237 // give the native implementation to return a better stream
238 // such as the native WinINet functionality under MS-Windows
242 rc
= m_nativeImp
->GetInputStream(this);
246 // else use the standard behaviour
247 #endif // wxUSE_URL_NATIVE
252 // m_protoinfo is NULL when we use a proxy
253 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
255 if (!addr
.Hostname(m_server
))
257 m_error
= wxURL_NOHOST
;
261 addr
.Service(m_port
);
263 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
265 m_error
= wxURL_CONNERR
;
271 // When we use a proxy, we have to pass the whole URL to it.
272 wxInputStream
*the_i_stream
=
273 (m_useProxy
) ? m_protocol
->GetInputStream(m_url
) :
274 m_protocol
->GetInputStream(m_path
);
278 m_error
= wxURL_PROTOERR
;
286 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
290 if ( ms_proxyDefault
)
292 ms_proxyDefault
->Close();
293 delete ms_proxyDefault
;
294 ms_proxyDefault
= NULL
;
299 wxString tmp_str
= url_proxy
;
300 int pos
= tmp_str
.Find(wxT(':'));
301 if (pos
== wxNOT_FOUND
)
304 wxString hostname
= tmp_str(0, pos
),
305 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
308 if (!addr
.Hostname(hostname
))
310 if (!addr
.Service(port
))
314 // Finally, when all is right, we connect the new proxy.
315 ms_proxyDefault
->Close();
317 ms_proxyDefault
= new wxHTTP();
318 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
322 void wxURL::SetProxy(const wxString
& url_proxy
)
326 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
337 wxString hostname
, port
;
342 pos
= tmp_str
.Find(wxT(':'));
343 // This is an invalid proxy name.
344 if (pos
== wxNOT_FOUND
)
347 hostname
= tmp_str(0, pos
);
348 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
350 addr
.Hostname(hostname
);
353 // Finally, create the whole stuff.
354 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
356 m_proxy
= new wxHTTP();
357 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
365 #endif // wxUSE_SOCKETS
367 // ----------------------------------------------------------------------
368 // A module which deletes the default proxy if we created it
369 // ----------------------------------------------------------------------
373 class wxURLModule
: public wxModule
376 virtual bool OnInit();
377 virtual void OnExit();
380 DECLARE_DYNAMIC_CLASS(wxURLModule
)
383 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
385 bool wxURLModule::OnInit()
387 // env var HTTP_PROXY contains the address of the default proxy to use if
388 // set, but don't try to create this proxy right now because it will slow
389 // down the program startup (especially if there is no DNS server
390 // available, in which case it may take up to 1 minute)
392 if ( getenv("HTTP_PROXY") )
394 wxURL::ms_useDefaultProxy
= true;
400 void wxURLModule::OnExit()
402 delete wxURL::ms_proxyDefault
;
403 wxURL::ms_proxyDefault
= NULL
;
406 #endif // wxUSE_SOCKETS