]>
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(wxProtoInfo
, wxObject
)
34 IMPLEMENT_CLASS(wxURL
, wxURI
)
37 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
39 // Enforce linking of protocol classes:
40 USE_PROTOCOL(wxFileProto
)
42 #if wxUSE_PROTOCOL_HTTP
45 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
46 bool wxURL::ms_useDefaultProxy
= false;
49 #if wxUSE_PROTOCOL_FTP
53 // --------------------------------------------------------------
57 // --------------------------------------------------------------
59 // --------------------------------------------------------------
61 // --------------------------------------------------------------
63 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
69 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
75 void wxURL::Init(const wxString
& url
)
78 m_error
= wxURL_NOERR
;
81 m_nativeImp
= CreateNativeImpObject();
84 #if wxUSE_PROTOCOL_HTTP
85 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
87 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
89 if ( !ms_proxyDefault
)
92 ms_useDefaultProxy
= false;
96 m_useProxy
= ms_proxyDefault
!= NULL
;
97 m_proxy
= ms_proxyDefault
;
98 #endif // wxUSE_PROTOCOL_HTTP
102 // --------------------------------------------------------------
104 // --------------------------------------------------------------
106 wxURL
& wxURL::operator = (const wxURI
& url
)
108 wxURI::operator = (url
);
109 Init(url
.BuildURI());
113 wxURL
& wxURL::operator = (const wxString
& url
)
115 wxURI::operator = (url
);
121 // --------------------------------------------------------------
124 // Builds the URL and takes care of the old protocol stuff
125 // --------------------------------------------------------------
127 bool wxURL::ParseURL()
129 // If the URL was already parsed (m_protocol != NULL), pass this section.
135 // Make sure we have a protocol/scheme
138 m_error
= wxURL_SNTXERR
;
142 // Find and create the protocol object
143 if (!FetchProtocol())
145 m_error
= wxURL_NOPROTO
;
149 // Do we need a host name ?
150 if (m_protoinfo
->m_needhost
)
152 // Make sure we have one, then
155 m_error
= wxURL_SNTXERR
;
161 #if wxUSE_PROTOCOL_HTTP
164 // Third, we rebuild the URL.
165 m_url
= m_scheme
+ wxT(":");
166 if (m_protoinfo
->m_needhost
)
167 m_url
= m_url
+ wxT("//") + m_server
;
169 // We initialize specific variables.
170 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
172 #endif // wxUSE_PROTOCOL_HTTP
174 m_error
= wxURL_NOERR
;
178 // --------------------------------------------------------------
179 // Destruction/Cleanup
180 // --------------------------------------------------------------
182 void wxURL::CleanData()
184 #if wxUSE_PROTOCOL_HTTP
186 #endif // wxUSE_PROTOCOL_HTTP
188 // Need to safely delete the socket (pending events)
189 m_protocol
->Destroy();
195 #if wxUSE_PROTOCOL_HTTP
196 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
198 #endif // wxUSE_PROTOCOL_HTTP
204 // --------------------------------------------------------------
206 // --------------------------------------------------------------
208 bool wxURL::FetchProtocol()
210 wxProtoInfo
*info
= ms_protocols
;
214 if (m_scheme
== info
->m_protoname
)
217 m_port
= info
->m_servname
;
219 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
227 // --------------------------------------------------------------
229 // --------------------------------------------------------------
231 wxInputStream
*wxURL::GetInputStream()
235 m_error
= wxURL_NOPROTO
;
239 m_error
= wxURL_NOERR
;
242 size_t dwPasswordPos
= m_userinfo
.find(':');
244 if (dwPasswordPos
== wxString::npos
)
245 m_protocol
->SetUser(Unescape(m_userinfo
));
248 m_protocol
->SetUser(Unescape(m_userinfo(0, dwPasswordPos
)));
249 m_protocol
->SetPassword(Unescape(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1)));
254 // give the native implementation to return a better stream
255 // such as the native WinINet functionality under MS-Windows
259 rc
= m_nativeImp
->GetInputStream(this);
263 // else use the standard behaviour
264 #endif // wxUSE_URL_NATIVE
269 // m_protoinfo is NULL when we use a proxy
271 #if wxUSE_PROTOCOL_HTTP
273 #endif // wxUSE_PROTOCOL_HTTP
274 m_protoinfo
->m_needhost
)
276 if (!addr
.Hostname(m_server
))
278 m_error
= wxURL_NOHOST
;
282 addr
.Service(m_port
);
284 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
286 m_error
= wxURL_CONNERR
;
290 #endif // wxUSE_SOCKETS
294 #if wxUSE_PROTOCOL_HTTP
295 // When we use a proxy, we have to pass the whole URL to it.
298 #endif // wxUSE_PROTOCOL_HTTP
301 fullPath
+= wxT("/");
306 fullPath
+= wxT("?") + m_query
;
309 fullPath
+= wxT("#") + m_fragment
;
311 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
315 m_error
= wxURL_PROTOERR
;
322 #if wxUSE_PROTOCOL_HTTP
323 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
327 if ( ms_proxyDefault
)
329 ms_proxyDefault
->Close();
330 delete ms_proxyDefault
;
331 ms_proxyDefault
= NULL
;
336 wxString tmp_str
= url_proxy
;
337 int pos
= tmp_str
.Find(wxT(':'));
338 if (pos
== wxNOT_FOUND
)
341 wxString hostname
= tmp_str(0, pos
),
342 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
345 if (!addr
.Hostname(hostname
))
347 if (!addr
.Service(port
))
351 // Finally, when all is right, we connect the new proxy.
352 ms_proxyDefault
->Close();
354 ms_proxyDefault
= new wxHTTP();
355 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
359 void wxURL::SetProxy(const wxString
& url_proxy
)
363 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
374 wxString hostname
, port
;
379 pos
= tmp_str
.Find(wxT(':'));
380 // This is an invalid proxy name.
381 if (pos
== wxNOT_FOUND
)
384 hostname
= tmp_str(0, pos
);
385 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
387 addr
.Hostname(hostname
);
390 // Finally, create the whole stuff.
391 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
393 m_proxy
= new wxHTTP();
394 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
402 #endif // wxUSE_PROTOCOL_HTTP
404 // ----------------------------------------------------------------------
407 // A module which deletes the default proxy if we created it
408 // ----------------------------------------------------------------------
412 class wxURLModule
: public wxModule
417 virtual bool OnInit();
418 virtual void OnExit();
421 DECLARE_DYNAMIC_CLASS(wxURLModule
)
424 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
426 wxURLModule::wxURLModule()
428 // we must be cleaned up before wxSocketModule as otherwise deleting
429 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
430 AddDependency(wxClassInfo::FindClass(_T("wxSocketModule")));
433 bool wxURLModule::OnInit()
435 #if wxUSE_PROTOCOL_HTTP
436 // env var HTTP_PROXY contains the address of the default proxy to use if
437 // set, but don't try to create this proxy right now because it will slow
438 // down the program startup (especially if there is no DNS server
439 // available, in which case it may take up to 1 minute)
441 if ( wxGetenv(_T("HTTP_PROXY")) )
443 wxURL::ms_useDefaultProxy
= true;
445 #endif // wxUSE_PROTOCOL_HTTP
449 void wxURLModule::OnExit()
451 #if wxUSE_PROTOCOL_HTTP
452 delete wxURL::ms_proxyDefault
;
453 wxURL::ms_proxyDefault
= NULL
;
454 #endif // wxUSE_PROTOCOL_HTTP
457 #endif // wxUSE_SOCKETS