]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/url.cpp
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
25 #include "wx/string.h"
27 #include "wx/module.h"
33 IMPLEMENT_CLASS(wxURL
, wxURI
)
36 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
38 // Enforce linking of protocol classes:
39 USE_PROTOCOL(wxFileProto
)
41 #if wxUSE_PROTOCOL_HTTP
44 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
45 bool wxURL::ms_useDefaultProxy
= false;
48 #if wxUSE_PROTOCOL_FTP
52 // --------------------------------------------------------------
56 // --------------------------------------------------------------
58 // --------------------------------------------------------------
60 // --------------------------------------------------------------
62 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
68 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
74 void wxURL::Init(const wxString
& url
)
77 m_error
= wxURL_NOERR
;
80 m_nativeImp
= CreateNativeImpObject();
83 #if wxUSE_PROTOCOL_HTTP
84 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
86 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
88 if ( !ms_proxyDefault
)
91 ms_useDefaultProxy
= false;
95 m_useProxy
= ms_proxyDefault
!= NULL
;
96 m_proxy
= ms_proxyDefault
;
97 #endif // wxUSE_PROTOCOL_HTTP
101 // --------------------------------------------------------------
103 // --------------------------------------------------------------
105 wxURL
& wxURL::operator = (const wxURI
& url
)
107 wxURI::operator = (url
);
108 Init(url
.BuildURI());
112 wxURL
& wxURL::operator = (const wxString
& url
)
114 wxURI::operator = (url
);
120 // --------------------------------------------------------------
123 // Builds the URL and takes care of the old protocol stuff
124 // --------------------------------------------------------------
126 bool wxURL::ParseURL()
128 // If the URL was already parsed (m_protocol != NULL), pass this section.
134 // Make sure we have a protocol/scheme
137 m_error
= wxURL_SNTXERR
;
141 // Find and create the protocol object
142 if (!FetchProtocol())
144 m_error
= wxURL_NOPROTO
;
148 // Do we need a host name ?
149 if (m_protoinfo
->m_needhost
)
151 // Make sure we have one, then
154 m_error
= wxURL_SNTXERR
;
160 #if wxUSE_PROTOCOL_HTTP
163 // Third, we rebuild the URL.
164 m_url
= m_scheme
+ wxT(":");
165 if (m_protoinfo
->m_needhost
)
166 m_url
= m_url
+ wxT("//") + m_server
;
168 // We initialize specific variables.
169 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
171 #endif // wxUSE_PROTOCOL_HTTP
173 m_error
= wxURL_NOERR
;
177 // --------------------------------------------------------------
178 // Destruction/Cleanup
179 // --------------------------------------------------------------
181 void wxURL::CleanData()
183 #if wxUSE_PROTOCOL_HTTP
185 #endif // wxUSE_PROTOCOL_HTTP
187 // Need to safely delete the socket (pending events)
188 m_protocol
->Destroy();
194 #if wxUSE_PROTOCOL_HTTP
195 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
197 #endif // wxUSE_PROTOCOL_HTTP
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_userinfo
.find(':');
243 if (dwPasswordPos
== wxString::npos
)
244 m_protocol
->SetUser(Unescape(m_userinfo
));
247 m_protocol
->SetUser(Unescape(m_userinfo(0, dwPasswordPos
)));
248 m_protocol
->SetPassword(Unescape(m_userinfo(dwPasswordPos
+1, m_userinfo
.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
270 #if wxUSE_PROTOCOL_HTTP
272 #endif // wxUSE_PROTOCOL_HTTP
273 m_protoinfo
->m_needhost
)
275 if (!addr
.Hostname(m_server
))
277 m_error
= wxURL_NOHOST
;
281 addr
.Service(m_port
);
283 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
285 m_error
= wxURL_CONNERR
;
289 #endif // wxUSE_SOCKETS
293 #if wxUSE_PROTOCOL_HTTP
294 // When we use a proxy, we have to pass the whole URL to it.
297 #endif // wxUSE_PROTOCOL_HTTP
300 fullPath
+= wxT("/");
305 fullPath
+= wxT("?") + m_query
;
308 fullPath
+= wxT("#") + m_fragment
;
310 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
314 m_error
= wxURL_PROTOERR
;
321 #if wxUSE_PROTOCOL_HTTP
322 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
326 if ( ms_proxyDefault
)
328 ms_proxyDefault
->Close();
329 delete ms_proxyDefault
;
330 ms_proxyDefault
= NULL
;
335 wxString tmp_str
= url_proxy
;
336 int pos
= tmp_str
.Find(wxT(':'));
337 if (pos
== wxNOT_FOUND
)
340 wxString hostname
= tmp_str(0, pos
),
341 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
344 if (!addr
.Hostname(hostname
))
346 if (!addr
.Service(port
))
350 // Finally, when all is right, we connect the new proxy.
351 ms_proxyDefault
->Close();
353 ms_proxyDefault
= new wxHTTP();
354 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
358 void wxURL::SetProxy(const wxString
& url_proxy
)
362 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
373 wxString hostname
, port
;
378 pos
= tmp_str
.Find(wxT(':'));
379 // This is an invalid proxy name.
380 if (pos
== wxNOT_FOUND
)
383 hostname
= tmp_str(0, pos
);
384 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
386 addr
.Hostname(hostname
);
389 // Finally, create the whole stuff.
390 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
392 m_proxy
= new wxHTTP();
393 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
401 #endif // wxUSE_PROTOCOL_HTTP
403 // ----------------------------------------------------------------------
406 // A module which deletes the default proxy if we created it
407 // ----------------------------------------------------------------------
411 class wxURLModule
: public wxModule
416 virtual bool OnInit();
417 virtual void OnExit();
420 DECLARE_DYNAMIC_CLASS(wxURLModule
)
423 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
425 wxURLModule::wxURLModule()
427 // we must be cleaned up before wxSocketModule as otherwise deleting
428 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
429 AddDependency(wxClassInfo::FindClass(wxT("wxSocketModule")));
432 bool wxURLModule::OnInit()
434 #if wxUSE_PROTOCOL_HTTP
435 // env var HTTP_PROXY contains the address of the default proxy to use if
436 // set, but don't try to create this proxy right now because it will slow
437 // down the program startup (especially if there is no DNS server
438 // available, in which case it may take up to 1 minute)
440 if ( wxGetenv(wxT("HTTP_PROXY")) )
442 wxURL::ms_useDefaultProxy
= true;
444 #endif // wxUSE_PROTOCOL_HTTP
448 void wxURLModule::OnExit()
450 #if wxUSE_PROTOCOL_HTTP
451 delete wxURL::ms_proxyDefault
;
452 wxURL::ms_proxyDefault
= NULL
;
453 #endif // wxUSE_PROTOCOL_HTTP
456 #endif // wxUSE_SOCKETS