]>
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
)
47 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
48 bool wxURL::ms_useDefaultProxy
= false;
51 // --------------------------------------------------------------
55 // --------------------------------------------------------------
57 // --------------------------------------------------------------
59 // --------------------------------------------------------------
61 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
67 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
73 void wxURL::Init(const wxString
& url
)
76 m_error
= wxURL_NOERR
;
79 m_nativeImp
= CreateNativeImpObject();
83 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
85 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
87 if ( !ms_proxyDefault
)
90 ms_useDefaultProxy
= false;
94 m_useProxy
= ms_proxyDefault
!= NULL
;
95 m_proxy
= ms_proxyDefault
;
96 #endif // wxUSE_SOCKETS
100 // --------------------------------------------------------------
102 // --------------------------------------------------------------
104 wxURL
& wxURL::operator = (const wxURI
& url
)
106 wxURI::operator = (url
);
107 Init(url
.BuildURI());
111 wxURL
& wxURL::operator = (const wxString
& url
)
113 wxURI::operator = (url
);
119 // --------------------------------------------------------------
122 // Builds the URL and takes care of the old protocol stuff
123 // --------------------------------------------------------------
125 bool wxURL::ParseURL()
127 // If the URL was already parsed (m_protocol != NULL), pass this section.
133 // Make sure we have a protocol/scheme
136 m_error
= wxURL_SNTXERR
;
140 // Find and create the protocol object
141 if (!FetchProtocol())
143 m_error
= wxURL_NOPROTO
;
147 // Do we need a host name ?
148 if (m_protoinfo
->m_needhost
)
150 // Make sure we have one, then
153 m_error
= wxURL_SNTXERR
;
162 // destroy the previously created protocol as we'll be using m_proxy
165 // Third, we rebuild the URL.
166 m_url
= m_scheme
+ wxT(":");
167 if (m_protoinfo
->m_needhost
)
168 m_url
= m_url
+ wxT("//") + m_server
;
170 // We initialize specific variables.
171 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
175 m_error
= wxURL_NOERR
;
179 // --------------------------------------------------------------
180 // Destruction/Cleanup
181 // --------------------------------------------------------------
183 void wxURL::CleanData()
195 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
203 // --------------------------------------------------------------
205 // --------------------------------------------------------------
207 bool wxURL::FetchProtocol()
209 wxProtoInfo
*info
= ms_protocols
;
213 if (m_scheme
== info
->m_protoname
)
216 m_port
= info
->m_servname
;
218 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
226 // --------------------------------------------------------------
228 // --------------------------------------------------------------
230 wxInputStream
*wxURL::GetInputStream()
234 m_error
= wxURL_NOPROTO
;
238 m_error
= wxURL_NOERR
;
241 size_t dwPasswordPos
= m_user
.find(':');
243 if (dwPasswordPos
== wxString::npos
)
244 m_protocol
->SetUser(m_user
);
247 m_protocol
->SetUser(m_user(0, dwPasswordPos
));
248 m_protocol
->SetPassword(m_user(dwPasswordPos
+1, m_user
.length() + 1));
253 // give the native implementation to return a better stream
254 // such as the native WinINet functionality under MS-Windows
258 rc
= m_nativeImp
->GetInputStream(this);
262 // else use the standard behaviour
263 #endif // wxUSE_URL_NATIVE
268 // m_protoinfo is NULL when we use a proxy
269 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
271 if (!addr
.Hostname(m_server
))
273 m_error
= wxURL_NOHOST
;
277 addr
.Service(m_port
);
279 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
281 m_error
= wxURL_CONNERR
;
287 // When we use a proxy, we have to pass the whole URL to it.
288 wxInputStream
*the_i_stream
;
292 the_i_stream
= m_protocol
->GetInputStream(m_url
);
296 wxString fullPath
= m_path
;
299 fullPath
+= wxT("?") + m_query
;
302 fullPath
+= wxT("#") + m_fragment
;
304 the_i_stream
= m_protocol
->GetInputStream(fullPath
);
309 m_error
= wxURL_PROTOERR
;
317 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
321 if ( ms_proxyDefault
)
323 ms_proxyDefault
->Close();
324 delete ms_proxyDefault
;
325 ms_proxyDefault
= NULL
;
330 wxString tmp_str
= url_proxy
;
331 int pos
= tmp_str
.Find(wxT(':'));
332 if (pos
== wxNOT_FOUND
)
335 wxString hostname
= tmp_str(0, pos
),
336 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
339 if (!addr
.Hostname(hostname
))
341 if (!addr
.Service(port
))
345 // Finally, when all is right, we connect the new proxy.
346 ms_proxyDefault
->Close();
348 ms_proxyDefault
= new wxHTTP();
349 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
353 void wxURL::SetProxy(const wxString
& url_proxy
)
357 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
368 wxString hostname
, port
;
373 pos
= tmp_str
.Find(wxT(':'));
374 // This is an invalid proxy name.
375 if (pos
== wxNOT_FOUND
)
378 hostname
= tmp_str(0, pos
);
379 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
381 addr
.Hostname(hostname
);
384 // Finally, create the whole stuff.
385 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
387 m_proxy
= new wxHTTP();
388 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
396 #endif // wxUSE_SOCKETS
398 // ----------------------------------------------------------------------
401 // A module which deletes the default proxy if we created it
402 // ----------------------------------------------------------------------
406 class wxURLModule
: public wxModule
409 virtual bool OnInit();
410 virtual void OnExit();
413 DECLARE_DYNAMIC_CLASS(wxURLModule
)
416 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
418 bool wxURLModule::OnInit()
420 // env var HTTP_PROXY contains the address of the default proxy to use if
421 // set, but don't try to create this proxy right now because it will slow
422 // down the program startup (especially if there is no DNS server
423 // available, in which case it may take up to 1 minute)
425 if ( getenv("HTTP_PROXY") )
427 wxURL::ms_useDefaultProxy
= true;
433 void wxURLModule::OnExit()
435 delete wxURL::ms_proxyDefault
;
436 wxURL::ms_proxyDefault
= NULL
;
439 #endif // wxUSE_SOCKETS