]>
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.
170 m_protocol
->Destroy();
171 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
173 #endif // wxUSE_PROTOCOL_HTTP
175 m_error
= wxURL_NOERR
;
179 // --------------------------------------------------------------
180 // Destruction/Cleanup
181 // --------------------------------------------------------------
183 void wxURL::CleanData()
185 #if wxUSE_PROTOCOL_HTTP
187 #endif // wxUSE_PROTOCOL_HTTP
191 // Need to safely delete the socket (pending events)
192 m_protocol
->Destroy();
201 #if wxUSE_PROTOCOL_HTTP
202 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
204 #endif // wxUSE_PROTOCOL_HTTP
210 // --------------------------------------------------------------
212 // --------------------------------------------------------------
214 bool wxURL::FetchProtocol()
216 wxProtoInfo
*info
= ms_protocols
;
220 if (m_scheme
== info
->m_protoname
)
223 m_port
= info
->m_servname
;
225 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
233 // --------------------------------------------------------------
235 // --------------------------------------------------------------
237 wxInputStream
*wxURL::GetInputStream()
241 m_error
= wxURL_NOPROTO
;
245 m_error
= wxURL_NOERR
;
248 size_t dwPasswordPos
= m_userinfo
.find(':');
250 if (dwPasswordPos
== wxString::npos
)
251 m_protocol
->SetUser(Unescape(m_userinfo
));
254 m_protocol
->SetUser(Unescape(m_userinfo(0, dwPasswordPos
)));
255 m_protocol
->SetPassword(Unescape(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1)));
260 // give the native implementation to return a better stream
261 // such as the native WinINet functionality under MS-Windows
265 rc
= m_nativeImp
->GetInputStream(this);
269 // else use the standard behaviour
270 #endif // wxUSE_URL_NATIVE
275 // m_protoinfo is NULL when we use a proxy
277 #if wxUSE_PROTOCOL_HTTP
279 #endif // wxUSE_PROTOCOL_HTTP
280 m_protoinfo
->m_needhost
)
282 if (!addr
.Hostname(m_server
))
284 m_error
= wxURL_NOHOST
;
288 addr
.Service(m_port
);
290 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
292 m_error
= wxURL_CONNERR
;
296 #endif // wxUSE_SOCKETS
300 #if wxUSE_PROTOCOL_HTTP
301 // When we use a proxy, we have to pass the whole URL to it.
304 #endif // wxUSE_PROTOCOL_HTTP
307 fullPath
+= wxT("/");
312 fullPath
+= wxT("?") + m_query
;
315 fullPath
+= wxT("#") + m_fragment
;
317 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
321 m_error
= wxURL_PROTOERR
;
328 #if wxUSE_PROTOCOL_HTTP
329 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
333 if ( ms_proxyDefault
)
335 ms_proxyDefault
->Close();
336 delete ms_proxyDefault
;
337 ms_proxyDefault
= NULL
;
342 wxString tmp_str
= url_proxy
;
343 int pos
= tmp_str
.Find(wxT(':'));
344 if (pos
== wxNOT_FOUND
)
347 wxString hostname
= tmp_str(0, pos
),
348 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
351 if (!addr
.Hostname(hostname
))
353 if (!addr
.Service(port
))
357 // Finally, when all is right, we connect the new proxy.
358 ms_proxyDefault
->Close();
360 ms_proxyDefault
= new wxHTTP();
361 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
365 void wxURL::SetProxy(const wxString
& url_proxy
)
369 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
380 wxString hostname
, port
;
385 pos
= tmp_str
.Find(wxT(':'));
386 // This is an invalid proxy name.
387 if (pos
== wxNOT_FOUND
)
390 hostname
= tmp_str(0, pos
);
391 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
393 addr
.Hostname(hostname
);
396 // Finally, create the whole stuff.
397 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
399 m_proxy
= new wxHTTP();
400 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
408 #endif // wxUSE_PROTOCOL_HTTP
410 // ----------------------------------------------------------------------
413 // A module which deletes the default proxy if we created it
414 // ----------------------------------------------------------------------
418 class wxURLModule
: public wxModule
423 virtual bool OnInit();
424 virtual void OnExit();
427 DECLARE_DYNAMIC_CLASS(wxURLModule
)
430 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
432 wxURLModule::wxURLModule()
434 // we must be cleaned up before wxSocketModule as otherwise deleting
435 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
436 AddDependency(wxClassInfo::FindClass(wxT("wxSocketModule")));
439 bool wxURLModule::OnInit()
441 #if wxUSE_PROTOCOL_HTTP
442 // env var HTTP_PROXY contains the address of the default proxy to use if
443 // set, but don't try to create this proxy right now because it will slow
444 // down the program startup (especially if there is no DNS server
445 // available, in which case it may take up to 1 minute)
447 if ( wxGetenv(wxT("HTTP_PROXY")) )
449 wxURL::ms_useDefaultProxy
= true;
451 #endif // wxUSE_PROTOCOL_HTTP
455 void wxURLModule::OnExit()
457 #if wxUSE_PROTOCOL_HTTP
458 delete wxURL::ms_proxyDefault
;
459 wxURL::ms_proxyDefault
= NULL
;
460 #endif // wxUSE_PROTOCOL_HTTP
463 #endif // wxUSE_SOCKETS