]>
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
& uri
) : wxURI(uri
)
74 wxURL::wxURL(const wxURL
& url
) : wxURI(url
)
80 void wxURL::Init(const wxString
& url
)
83 m_error
= wxURL_NOERR
;
86 m_nativeImp
= CreateNativeImpObject();
89 #if wxUSE_PROTOCOL_HTTP
90 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
92 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
94 if ( !ms_proxyDefault
)
97 ms_useDefaultProxy
= false;
101 m_useProxy
= ms_proxyDefault
!= NULL
;
102 m_proxy
= ms_proxyDefault
;
103 #endif // wxUSE_PROTOCOL_HTTP
107 // --------------------------------------------------------------
109 // --------------------------------------------------------------
111 wxURL
& wxURL::operator = (const wxString
& url
)
113 wxURI::operator = (url
);
121 wxURL
& wxURL::operator = (const wxURI
& uri
)
125 wxURI::operator = (uri
);
127 Init(uri
.BuildURI());
134 wxURL
& wxURL::operator = (const wxURL
& url
)
138 wxURI::operator = (url
);
147 // --------------------------------------------------------------
150 // Builds the URL and takes care of the old protocol stuff
151 // --------------------------------------------------------------
153 bool wxURL::ParseURL()
155 // If the URL was already parsed (m_protocol != NULL), pass this section.
161 // Make sure we have a protocol/scheme
164 m_error
= wxURL_SNTXERR
;
168 // Find and create the protocol object
169 if (!FetchProtocol())
171 m_error
= wxURL_NOPROTO
;
175 // Do we need a host name ?
176 if (m_protoinfo
->m_needhost
)
178 // Make sure we have one, then
181 m_error
= wxURL_SNTXERR
;
187 #if wxUSE_PROTOCOL_HTTP
190 // Third, we rebuild the URL.
191 m_url
= m_scheme
+ wxT(":");
192 if (m_protoinfo
->m_needhost
)
193 m_url
= m_url
+ wxT("//") + m_server
;
195 // We initialize specific variables.
197 m_protocol
->Destroy();
198 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
200 #endif // wxUSE_PROTOCOL_HTTP
202 m_error
= wxURL_NOERR
;
206 // --------------------------------------------------------------
207 // Destruction/Cleanup
208 // --------------------------------------------------------------
210 void wxURL::CleanData()
212 #if wxUSE_PROTOCOL_HTTP
214 #endif // wxUSE_PROTOCOL_HTTP
218 // Need to safely delete the socket (pending events)
219 m_protocol
->Destroy();
228 #if wxUSE_PROTOCOL_HTTP
229 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
231 #endif // wxUSE_PROTOCOL_HTTP
242 // --------------------------------------------------------------
244 // --------------------------------------------------------------
246 bool wxURL::FetchProtocol()
248 wxProtoInfo
*info
= ms_protocols
;
252 if (m_scheme
== info
->m_protoname
)
254 if ( m_port
.empty() )
255 m_port
= info
->m_servname
;
257 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
265 // --------------------------------------------------------------
267 // --------------------------------------------------------------
269 wxInputStream
*wxURL::GetInputStream()
273 m_error
= wxURL_NOPROTO
;
277 m_error
= wxURL_NOERR
;
280 size_t dwPasswordPos
= m_userinfo
.find(':');
282 if (dwPasswordPos
== wxString::npos
)
283 m_protocol
->SetUser(Unescape(m_userinfo
));
286 m_protocol
->SetUser(Unescape(m_userinfo(0, dwPasswordPos
)));
287 m_protocol
->SetPassword(Unescape(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1)));
292 // give the native implementation to return a better stream
293 // such as the native WinINet functionality under MS-Windows
297 rc
= m_nativeImp
->GetInputStream(this);
301 // else use the standard behaviour
302 #endif // wxUSE_URL_NATIVE
307 // m_protoinfo is NULL when we use a proxy
309 #if wxUSE_PROTOCOL_HTTP
311 #endif // wxUSE_PROTOCOL_HTTP
312 m_protoinfo
->m_needhost
)
314 if (!addr
.Hostname(m_server
))
316 m_error
= wxURL_NOHOST
;
320 addr
.Service(m_port
);
322 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
324 m_error
= wxURL_CONNERR
;
328 #endif // wxUSE_SOCKETS
332 #if wxUSE_PROTOCOL_HTTP
333 // When we use a proxy, we have to pass the whole URL to it.
336 #endif // wxUSE_PROTOCOL_HTTP
339 fullPath
+= wxT("/");
344 fullPath
+= wxT("?") + m_query
;
347 fullPath
+= wxT("#") + m_fragment
;
349 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
353 m_error
= wxURL_PROTOERR
;
360 #if wxUSE_PROTOCOL_HTTP
361 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
365 if ( ms_proxyDefault
)
367 ms_proxyDefault
->Close();
368 wxDELETE(ms_proxyDefault
);
373 wxString tmp_str
= url_proxy
;
374 int pos
= tmp_str
.Find(wxT(':'));
375 if (pos
== wxNOT_FOUND
)
378 wxString hostname
= tmp_str(0, pos
),
379 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
382 if (!addr
.Hostname(hostname
))
384 if (!addr
.Service(port
))
388 // Finally, when all is right, we connect the new proxy.
389 ms_proxyDefault
->Close();
391 ms_proxyDefault
= new wxHTTP();
392 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
396 void wxURL::SetProxy(const wxString
& url_proxy
)
400 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
411 wxString hostname
, port
;
416 pos
= tmp_str
.Find(wxT(':'));
417 // This is an invalid proxy name.
418 if (pos
== wxNOT_FOUND
)
421 hostname
= tmp_str(0, pos
);
422 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
424 addr
.Hostname(hostname
);
427 // Finally, create the whole stuff.
428 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
430 m_proxy
= new wxHTTP();
431 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
439 #endif // wxUSE_PROTOCOL_HTTP
441 // ----------------------------------------------------------------------
444 // A module which deletes the default proxy if we created it
445 // ----------------------------------------------------------------------
449 class wxURLModule
: public wxModule
454 virtual bool OnInit();
455 virtual void OnExit();
458 DECLARE_DYNAMIC_CLASS(wxURLModule
)
461 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
463 wxURLModule::wxURLModule()
465 // we must be cleaned up before wxSocketModule as otherwise deleting
466 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
467 AddDependency(wxClassInfo::FindClass(wxT("wxSocketModule")));
470 bool wxURLModule::OnInit()
472 #if wxUSE_PROTOCOL_HTTP
473 // env var HTTP_PROXY contains the address of the default proxy to use if
474 // set, but don't try to create this proxy right now because it will slow
475 // down the program startup (especially if there is no DNS server
476 // available, in which case it may take up to 1 minute)
478 if ( wxGetenv(wxT("HTTP_PROXY")) )
480 wxURL::ms_useDefaultProxy
= true;
482 #endif // wxUSE_PROTOCOL_HTTP
486 void wxURLModule::OnExit()
488 #if wxUSE_PROTOCOL_HTTP
489 wxDELETE(wxURL::ms_proxyDefault
);
490 #endif // wxUSE_PROTOCOL_HTTP
493 #endif // wxUSE_SOCKETS