]>
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 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
)
43 #if wxUSE_PROTOCOL_HTTP
46 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
47 bool wxURL::ms_useDefaultProxy
= false;
50 #if wxUSE_PROTOCOL_FTP
54 // --------------------------------------------------------------
58 // --------------------------------------------------------------
60 // --------------------------------------------------------------
62 // --------------------------------------------------------------
64 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
70 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
76 void wxURL::Init(const wxString
& url
)
79 m_error
= wxURL_NOERR
;
82 m_nativeImp
= CreateNativeImpObject();
85 #if wxUSE_PROTOCOL_HTTP
86 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
88 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
90 if ( !ms_proxyDefault
)
93 ms_useDefaultProxy
= false;
97 m_useProxy
= ms_proxyDefault
!= NULL
;
98 m_proxy
= ms_proxyDefault
;
99 #endif // wxUSE_PROTOCOL_HTTP
103 // --------------------------------------------------------------
105 // --------------------------------------------------------------
107 wxURL
& wxURL::operator = (const wxURI
& url
)
109 wxURI::operator = (url
);
110 Init(url
.BuildURI());
114 wxURL
& wxURL::operator = (const wxString
& url
)
116 wxURI::operator = (url
);
122 // --------------------------------------------------------------
125 // Builds the URL and takes care of the old protocol stuff
126 // --------------------------------------------------------------
128 bool wxURL::ParseURL()
130 // If the URL was already parsed (m_protocol != NULL), pass this section.
136 // Make sure we have a protocol/scheme
139 m_error
= wxURL_SNTXERR
;
143 // Find and create the protocol object
144 if (!FetchProtocol())
146 m_error
= wxURL_NOPROTO
;
150 // Do we need a host name ?
151 if (m_protoinfo
->m_needhost
)
153 // Make sure we have one, then
156 m_error
= wxURL_SNTXERR
;
162 #if wxUSE_PROTOCOL_HTTP
165 // destroy the previously created protocol as we'll be using m_proxy
168 // Third, we rebuild the URL.
169 m_url
= m_scheme
+ wxT(":");
170 if (m_protoinfo
->m_needhost
)
171 m_url
= m_url
+ wxT("//") + m_server
;
173 // We initialize specific variables.
174 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
176 #endif // wxUSE_PROTOCOL_HTTP
178 m_error
= wxURL_NOERR
;
182 // --------------------------------------------------------------
183 // Destruction/Cleanup
184 // --------------------------------------------------------------
186 void wxURL::CleanData()
188 #if wxUSE_PROTOCOL_HTTP
190 #endif // wxUSE_PROTOCOL_HTTP
197 #if wxUSE_PROTOCOL_HTTP
198 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
200 #endif // wxUSE_PROTOCOL_HTTP
206 // --------------------------------------------------------------
208 // --------------------------------------------------------------
210 bool wxURL::FetchProtocol()
212 wxProtoInfo
*info
= ms_protocols
;
216 if (m_scheme
== info
->m_protoname
)
219 m_port
= info
->m_servname
;
221 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
229 // --------------------------------------------------------------
231 // --------------------------------------------------------------
233 wxInputStream
*wxURL::GetInputStream()
237 m_error
= wxURL_NOPROTO
;
241 m_error
= wxURL_NOERR
;
244 size_t dwPasswordPos
= m_user
.find(':');
246 if (dwPasswordPos
== wxString::npos
)
247 m_protocol
->SetUser(m_user
);
250 m_protocol
->SetUser(m_user(0, dwPasswordPos
));
251 m_protocol
->SetPassword(m_user(dwPasswordPos
+1, m_user
.length() + 1));
256 // give the native implementation to return a better stream
257 // such as the native WinINet functionality under MS-Windows
261 rc
= m_nativeImp
->GetInputStream(this);
265 // else use the standard behaviour
266 #endif // wxUSE_URL_NATIVE
271 // m_protoinfo is NULL when we use a proxy
272 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
274 if (!addr
.Hostname(m_server
))
276 m_error
= wxURL_NOHOST
;
280 addr
.Service(m_port
);
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.
291 wxInputStream
*the_i_stream
;
295 the_i_stream
= m_protocol
->GetInputStream(m_url
);
299 wxString fullPath
= m_path
;
302 fullPath
+= wxT("?") + m_query
;
305 fullPath
+= wxT("#") + m_fragment
;
307 the_i_stream
= m_protocol
->GetInputStream(fullPath
);
312 m_error
= wxURL_PROTOERR
;
319 #if wxUSE_PROTOCOL_HTTP
320 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
324 if ( ms_proxyDefault
)
326 ms_proxyDefault
->Close();
327 delete ms_proxyDefault
;
328 ms_proxyDefault
= NULL
;
333 wxString tmp_str
= url_proxy
;
334 int pos
= tmp_str
.Find(wxT(':'));
335 if (pos
== wxNOT_FOUND
)
338 wxString hostname
= tmp_str(0, pos
),
339 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
342 if (!addr
.Hostname(hostname
))
344 if (!addr
.Service(port
))
348 // Finally, when all is right, we connect the new proxy.
349 ms_proxyDefault
->Close();
351 ms_proxyDefault
= new wxHTTP();
352 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
356 void wxURL::SetProxy(const wxString
& url_proxy
)
360 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
371 wxString hostname
, port
;
376 pos
= tmp_str
.Find(wxT(':'));
377 // This is an invalid proxy name.
378 if (pos
== wxNOT_FOUND
)
381 hostname
= tmp_str(0, pos
);
382 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
384 addr
.Hostname(hostname
);
387 // Finally, create the whole stuff.
388 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
390 m_proxy
= new wxHTTP();
391 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
399 #endif // wxUSE_PROTOCOL_HTTP
401 // ----------------------------------------------------------------------
404 // A module which deletes the default proxy if we created it
405 // ----------------------------------------------------------------------
409 class wxURLModule
: public wxModule
412 virtual bool OnInit();
413 virtual void OnExit();
416 DECLARE_DYNAMIC_CLASS(wxURLModule
)
419 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
421 bool wxURLModule::OnInit()
423 #if wxUSE_PROTOCOL_HTTP
424 // env var HTTP_PROXY contains the address of the default proxy to use if
425 // set, but don't try to create this proxy right now because it will slow
426 // down the program startup (especially if there is no DNS server
427 // available, in which case it may take up to 1 minute)
429 if ( getenv("HTTP_PROXY") )
431 wxURL::ms_useDefaultProxy
= true;
433 #endif // wxUSE_PROTOCOL_HTTP
437 void wxURLModule::OnExit()
439 #if wxUSE_PROTOCOL_HTTP
440 delete wxURL::ms_proxyDefault
;
441 wxURL::ms_proxyDefault
= NULL
;
442 #endif // wxUSE_PROTOCOL_HTTP
445 #endif // wxUSE_SOCKETS