]>
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"
29 #include "wx/module.h"
34 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
35 IMPLEMENT_CLASS(wxURL
, wxURI
)
38 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
40 // Enforce linking of protocol classes:
41 USE_PROTOCOL(wxFileProto
)
43 #if wxUSE_PROTOCOL_HTTP
46 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
47 bool wxURL::ms_useDefaultProxy
= false;
50 #if wxUSE_PROTOCOL_FTP
54 // --------------------------------------------------------------
58 // --------------------------------------------------------------
60 // --------------------------------------------------------------
62 // --------------------------------------------------------------
64 wxURL::wxURL(const wxString
& url
) : wxURI(url
)
70 wxURL::wxURL(const wxURI
& url
) : wxURI(url
)
76 void wxURL::Init(const wxString
& url
)
79 m_error
= wxURL_NOERR
;
82 m_nativeImp
= CreateNativeImpObject();
85 #if wxUSE_PROTOCOL_HTTP
86 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
88 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
90 if ( !ms_proxyDefault
)
93 ms_useDefaultProxy
= false;
97 m_useProxy
= ms_proxyDefault
!= NULL
;
98 m_proxy
= ms_proxyDefault
;
99 #endif // wxUSE_PROTOCOL_HTTP
103 // --------------------------------------------------------------
105 // --------------------------------------------------------------
107 wxURL
& wxURL::operator = (const wxURI
& url
)
109 wxURI::operator = (url
);
110 Init(url
.BuildURI());
114 wxURL
& wxURL::operator = (const wxString
& url
)
116 wxURI::operator = (url
);
122 // --------------------------------------------------------------
125 // Builds the URL and takes care of the old protocol stuff
126 // --------------------------------------------------------------
128 bool wxURL::ParseURL()
130 // If the URL was already parsed (m_protocol != NULL), pass this section.
136 // Make sure we have a protocol/scheme
139 m_error
= wxURL_SNTXERR
;
143 // Find and create the protocol object
144 if (!FetchProtocol())
146 m_error
= wxURL_NOPROTO
;
150 // Do we need a host name ?
151 if (m_protoinfo
->m_needhost
)
153 // Make sure we have one, then
156 m_error
= wxURL_SNTXERR
;
162 #if wxUSE_PROTOCOL_HTTP
165 // Third, we rebuild the URL.
166 m_url
= m_scheme
+ wxT(":");
167 if (m_protoinfo
->m_needhost
)
168 m_url
= m_url
+ wxT("//") + m_server
;
170 // We initialize specific variables.
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
189 // Need to safely delete the socket (pending events)
190 m_protocol
->Destroy();
196 #if wxUSE_PROTOCOL_HTTP
197 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
199 #endif // wxUSE_PROTOCOL_HTTP
205 // --------------------------------------------------------------
207 // --------------------------------------------------------------
209 bool wxURL::FetchProtocol()
211 wxProtoInfo
*info
= ms_protocols
;
215 if (m_scheme
== info
->m_protoname
)
218 m_port
= info
->m_servname
;
220 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
228 // --------------------------------------------------------------
230 // --------------------------------------------------------------
232 wxInputStream
*wxURL::GetInputStream()
236 m_error
= wxURL_NOPROTO
;
240 m_error
= wxURL_NOERR
;
243 size_t dwPasswordPos
= m_userinfo
.find(':');
245 if (dwPasswordPos
== wxString::npos
)
246 m_protocol
->SetUser(m_userinfo
);
249 m_protocol
->SetUser(m_userinfo(0, dwPasswordPos
));
250 m_protocol
->SetPassword(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1));
255 // give the native implementation to return a better stream
256 // such as the native WinINet functionality under MS-Windows
260 rc
= m_nativeImp
->GetInputStream(this);
264 // else use the standard behaviour
265 #endif // wxUSE_URL_NATIVE
270 // m_protoinfo is NULL when we use a proxy
271 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
273 if (!addr
.Hostname(m_server
))
275 m_error
= wxURL_NOHOST
;
279 addr
.Service(m_port
);
281 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
283 m_error
= wxURL_CONNERR
;
291 // When we use a proxy, we have to pass the whole URL to it.
296 fullPath
+= wxT("/");
301 fullPath
+= wxT("?") + m_query
;
304 fullPath
+= wxT("#") + m_fragment
;
306 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
310 m_error
= wxURL_PROTOERR
;
317 #if wxUSE_PROTOCOL_HTTP
318 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
322 if ( ms_proxyDefault
)
324 ms_proxyDefault
->Close();
325 delete ms_proxyDefault
;
326 ms_proxyDefault
= NULL
;
331 wxString tmp_str
= url_proxy
;
332 int pos
= tmp_str
.Find(wxT(':'));
333 if (pos
== wxNOT_FOUND
)
336 wxString hostname
= tmp_str(0, pos
),
337 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
340 if (!addr
.Hostname(hostname
))
342 if (!addr
.Service(port
))
346 // Finally, when all is right, we connect the new proxy.
347 ms_proxyDefault
->Close();
349 ms_proxyDefault
= new wxHTTP();
350 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
354 void wxURL::SetProxy(const wxString
& url_proxy
)
358 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
369 wxString hostname
, port
;
374 pos
= tmp_str
.Find(wxT(':'));
375 // This is an invalid proxy name.
376 if (pos
== wxNOT_FOUND
)
379 hostname
= tmp_str(0, pos
);
380 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
382 addr
.Hostname(hostname
);
385 // Finally, create the whole stuff.
386 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
388 m_proxy
= new wxHTTP();
389 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
397 #endif // wxUSE_PROTOCOL_HTTP
399 // ----------------------------------------------------------------------
402 // A module which deletes the default proxy if we created it
403 // ----------------------------------------------------------------------
407 class wxURLModule
: public wxModule
410 virtual bool OnInit();
411 virtual void OnExit();
414 DECLARE_DYNAMIC_CLASS(wxURLModule
)
417 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
419 bool wxURLModule::OnInit()
421 #if wxUSE_PROTOCOL_HTTP
422 // env var HTTP_PROXY contains the address of the default proxy to use if
423 // set, but don't try to create this proxy right now because it will slow
424 // down the program startup (especially if there is no DNS server
425 // available, in which case it may take up to 1 minute)
427 if ( wxGetenv(_T("HTTP_PROXY")) )
429 wxURL::ms_useDefaultProxy
= true;
431 #endif // wxUSE_PROTOCOL_HTTP
435 void wxURLModule::OnExit()
437 #if wxUSE_PROTOCOL_HTTP
438 delete wxURL::ms_proxyDefault
;
439 wxURL::ms_proxyDefault
= NULL
;
440 #endif // wxUSE_PROTOCOL_HTTP
443 #endif // wxUSE_SOCKETS
445 // ---------------------------------------------------------------------------
447 // wxURL Compatibility
449 // ---------------------------------------------------------------------------
451 #if WXWIN_COMPATIBILITY_2_4
455 wxString
wxURL::GetProtocolName() const
460 wxString
wxURL::GetHostName() const
465 wxString
wxURL::GetPath() const
470 //Note that this old code really doesn't convert to a URI that well and looks
471 //more like a dirty hack than anything else...
473 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
479 for (i
= 0; i
< uri
.Len(); i
++)
481 wxChar c
= uri
.GetChar(i
);
485 // GRG, Apr/2000: changed to "%20" instead of '+'
487 out_str
+= wxT("%20");
491 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
493 // - Alphanumeric characters are never escaped
494 // - Unreserved marks are never escaped
495 // - Delimiters must be escaped if they appear within a component
496 // but not if they are used to separate components. Here we have
497 // no clear way to distinguish between these two cases, so they
498 // are escaped unless they are passed in the 'delims' parameter
499 // (allowed delimiters).
501 static const wxChar marks
[] = wxT("-_.!~*()'");
503 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
505 hexa_code
.Printf(wxT("%%%02X"), c
);
506 out_str
+= hexa_code
;
518 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
520 return wxURI::Unescape(uri
);
523 #endif //WXWIN_COMPATIBILITY_2_4