]>
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
)
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 delete ms_proxyDefault
;
369 ms_proxyDefault
= NULL
;
374 wxString tmp_str
= url_proxy
;
375 int pos
= tmp_str
.Find(wxT(':'));
376 if (pos
== wxNOT_FOUND
)
379 wxString hostname
= tmp_str(0, pos
),
380 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
383 if (!addr
.Hostname(hostname
))
385 if (!addr
.Service(port
))
389 // Finally, when all is right, we connect the new proxy.
390 ms_proxyDefault
->Close();
392 ms_proxyDefault
= new wxHTTP();
393 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
397 void wxURL::SetProxy(const wxString
& url_proxy
)
401 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
412 wxString hostname
, port
;
417 pos
= tmp_str
.Find(wxT(':'));
418 // This is an invalid proxy name.
419 if (pos
== wxNOT_FOUND
)
422 hostname
= tmp_str(0, pos
);
423 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
425 addr
.Hostname(hostname
);
428 // Finally, create the whole stuff.
429 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
431 m_proxy
= new wxHTTP();
432 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
440 #endif // wxUSE_PROTOCOL_HTTP
442 // ----------------------------------------------------------------------
445 // A module which deletes the default proxy if we created it
446 // ----------------------------------------------------------------------
450 class wxURLModule
: public wxModule
455 virtual bool OnInit();
456 virtual void OnExit();
459 DECLARE_DYNAMIC_CLASS(wxURLModule
)
462 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
464 wxURLModule::wxURLModule()
466 // we must be cleaned up before wxSocketModule as otherwise deleting
467 // ms_proxyDefault from our OnExit() won't work (and can actually crash)
468 AddDependency(wxClassInfo::FindClass(wxT("wxSocketModule")));
471 bool wxURLModule::OnInit()
473 #if wxUSE_PROTOCOL_HTTP
474 // env var HTTP_PROXY contains the address of the default proxy to use if
475 // set, but don't try to create this proxy right now because it will slow
476 // down the program startup (especially if there is no DNS server
477 // available, in which case it may take up to 1 minute)
479 if ( wxGetenv(wxT("HTTP_PROXY")) )
481 wxURL::ms_useDefaultProxy
= true;
483 #endif // wxUSE_PROTOCOL_HTTP
487 void wxURLModule::OnExit()
489 #if wxUSE_PROTOCOL_HTTP
490 delete wxURL::ms_proxyDefault
;
491 wxURL::ms_proxyDefault
= NULL
;
492 #endif // wxUSE_PROTOCOL_HTTP
495 #endif // wxUSE_SOCKETS