]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/url.cpp
4 // Author: Guilhem Lavaux
7 // Copyright: (c) 1997, 1998 Guilhem Lavaux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
24 #include "wx/string.h"
26 #include "wx/module.h"
32 IMPLEMENT_CLASS(wxURL
, wxURI
)
35 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
37 // Enforce linking of protocol classes:
38 USE_PROTOCOL(wxFileProto
)
40 #if wxUSE_PROTOCOL_HTTP
43 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
44 bool wxURL::ms_useDefaultProxy
= false;
47 #if wxUSE_PROTOCOL_FTP
51 // --------------------------------------------------------------
55 // --------------------------------------------------------------
57 // --------------------------------------------------------------
59 // --------------------------------------------------------------
61 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
67 wxURL::wxURL(const wxURI
& uri
) : wxURI(uri
)
73 wxURL::wxURL(const wxURL
& url
) : wxURI(url
)
79 void wxURL::Init(const wxString
& url
)
82 m_error
= wxURL_NOERR
;
85 m_nativeImp
= CreateNativeImpObject();
88 #if wxUSE_PROTOCOL_HTTP
89 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
91 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
93 if ( !ms_proxyDefault
)
96 ms_useDefaultProxy
= false;
100 m_useProxy
= ms_proxyDefault
!= NULL
;
101 m_proxy
= ms_proxyDefault
;
102 #endif // wxUSE_PROTOCOL_HTTP
106 // --------------------------------------------------------------
108 // --------------------------------------------------------------
110 wxURL
& wxURL::operator = (const wxString
& url
)
112 wxURI::operator = (url
);
120 wxURL
& wxURL::operator = (const wxURI
& uri
)
124 wxURI::operator = (uri
);
126 Init(uri
.BuildURI());
133 wxURL
& wxURL::operator = (const wxURL
& url
)
137 wxURI::operator = (url
);
146 // --------------------------------------------------------------
149 // Builds the URL and takes care of the old protocol stuff
150 // --------------------------------------------------------------
152 bool wxURL::ParseURL()
154 // If the URL was already parsed (m_protocol != NULL), pass this section.
160 // Make sure we have a protocol/scheme
163 m_error
= wxURL_SNTXERR
;
167 // Find and create the protocol object
168 if (!FetchProtocol())
170 m_error
= wxURL_NOPROTO
;
174 // Do we need a host name ?
175 if (m_protoinfo
->m_needhost
)
177 // Make sure we have one, then
180 m_error
= wxURL_SNTXERR
;
186 #if wxUSE_PROTOCOL_HTTP
189 // Third, we rebuild the URL.
190 m_url
= m_scheme
+ wxT(":");
191 if (m_protoinfo
->m_needhost
)
192 m_url
= m_url
+ wxT("//") + m_server
;
194 // We initialize specific variables.
196 m_protocol
->Destroy();
197 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
199 #endif // wxUSE_PROTOCOL_HTTP
201 m_error
= wxURL_NOERR
;
205 // --------------------------------------------------------------
206 // Destruction/Cleanup
207 // --------------------------------------------------------------
209 void wxURL::CleanData()
211 #if wxUSE_PROTOCOL_HTTP
213 #endif // wxUSE_PROTOCOL_HTTP
217 // Need to safely delete the socket (pending events)
218 m_protocol
->Destroy();
227 #if wxUSE_PROTOCOL_HTTP
228 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
230 #endif // wxUSE_PROTOCOL_HTTP
241 // --------------------------------------------------------------
243 // --------------------------------------------------------------
245 bool wxURL::FetchProtocol()
247 wxProtoInfo
*info
= ms_protocols
;
251 if (m_scheme
== info
->m_protoname
)
253 if ( m_port
.empty() )
254 m_port
= info
->m_servname
;
256 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
264 // --------------------------------------------------------------
266 // --------------------------------------------------------------
268 wxInputStream
*wxURL::GetInputStream()
272 m_error
= wxURL_NOPROTO
;
276 m_error
= wxURL_NOERR
;
279 size_t dwPasswordPos
= m_userinfo
.find(':');
281 if (dwPasswordPos
== wxString::npos
)
282 m_protocol
->SetUser(Unescape(m_userinfo
));
285 m_protocol
->SetUser(Unescape(m_userinfo(0, dwPasswordPos
)));
286 m_protocol
->SetPassword(Unescape(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1)));
291 // give the native implementation to return a better stream
292 // such as the native WinINet functionality under MS-Windows
296 rc
= m_nativeImp
->GetInputStream(this);
300 // else use the standard behaviour
301 #endif // wxUSE_URL_NATIVE
306 // m_protoinfo is NULL when we use a proxy
308 #if wxUSE_PROTOCOL_HTTP
310 #endif // wxUSE_PROTOCOL_HTTP
311 m_protoinfo
->m_needhost
)
313 if (!addr
.Hostname(m_server
))
315 m_error
= wxURL_NOHOST
;
319 addr
.Service(m_port
);
321 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
323 m_error
= wxURL_CONNERR
;
327 #endif // wxUSE_SOCKETS
331 #if wxUSE_PROTOCOL_HTTP
332 // When we use a proxy, we have to pass the whole URL to it.
335 #endif // wxUSE_PROTOCOL_HTTP
338 fullPath
+= wxT("/");
343 fullPath
+= wxT("?") + m_query
;
346 fullPath
+= wxT("#") + m_fragment
;
348 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
352 m_error
= wxURL_PROTOERR
;
359 #if wxUSE_PROTOCOL_HTTP
360 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
364 if ( ms_proxyDefault
)
366 ms_proxyDefault
->Close();
367 wxDELETE(ms_proxyDefault
);
372 wxString tmp_str
= url_proxy
;
373 int pos
= tmp_str
.Find(wxT(':'));
374 if (pos
== wxNOT_FOUND
)
377 wxString hostname
= tmp_str(0, pos
),
378 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
381 if (!addr
.Hostname(hostname
))
383 if (!addr
.Service(port
))
387 // Finally, when all is right, we connect the new proxy.
388 ms_proxyDefault
->Close();
390 ms_proxyDefault
= new wxHTTP();
391 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
395 void wxURL::SetProxy(const wxString
& url_proxy
)
399 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
410 wxString hostname
, port
;
415 pos
= tmp_str
.Find(wxT(':'));
416 // This is an invalid proxy name.
417 if (pos
== wxNOT_FOUND
)
420 hostname
= tmp_str(0, pos
);
421 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
423 addr
.Hostname(hostname
);
426 // Finally, create the whole stuff.
427 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
429 m_proxy
= new wxHTTP();
430 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
438 #endif // wxUSE_PROTOCOL_HTTP
440 // ----------------------------------------------------------------------
443 // A module which deletes the default proxy if we created it
444 // ----------------------------------------------------------------------
448 class wxURLModule
: public wxModule
453 virtual bool OnInit();
454 virtual void OnExit();
457 DECLARE_DYNAMIC_CLASS(wxURLModule
)
460 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
462 wxURLModule::wxURLModule()
464 // we must be cleaned up before wxSocketModule as otherwise deleting
465 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
466 AddDependency(wxClassInfo::FindClass(wxT("wxSocketModule")));
469 bool wxURLModule::OnInit()
471 #if wxUSE_PROTOCOL_HTTP
472 // env var HTTP_PROXY contains the address of the default proxy to use if
473 // set, but don't try to create this proxy right now because it will slow
474 // down the program startup (especially if there is no DNS server
475 // available, in which case it may take up to 1 minute)
477 if ( wxGetenv(wxT("HTTP_PROXY")) )
479 wxURL::ms_useDefaultProxy
= true;
481 #endif // wxUSE_PROTOCOL_HTTP
485 void wxURLModule::OnExit()
487 #if wxUSE_PROTOCOL_HTTP
488 wxDELETE(wxURL::ms_proxyDefault
);
489 #endif // wxUSE_PROTOCOL_HTTP
492 #endif // wxUSE_SOCKETS