]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
65f2fe726511259b567b42855c4954a8af280092
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
193 #if wxUSE_PROTOCOL_HTTP
194 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
196 #endif // wxUSE_PROTOCOL_HTTP
202 // --------------------------------------------------------------
204 // --------------------------------------------------------------
206 bool wxURL::FetchProtocol()
208 wxProtoInfo
*info
= ms_protocols
;
212 if (m_scheme
== info
->m_protoname
)
215 m_port
= info
->m_servname
;
217 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
225 // --------------------------------------------------------------
227 // --------------------------------------------------------------
229 wxInputStream
*wxURL::GetInputStream()
233 m_error
= wxURL_NOPROTO
;
237 m_error
= wxURL_NOERR
;
240 size_t dwPasswordPos
= m_userinfo
.find(':');
242 if (dwPasswordPos
== wxString::npos
)
243 m_protocol
->SetUser(m_userinfo
);
246 m_protocol
->SetUser(m_userinfo(0, dwPasswordPos
));
247 m_protocol
->SetPassword(m_userinfo(dwPasswordPos
+1, m_userinfo
.length() + 1));
252 // give the native implementation to return a better stream
253 // such as the native WinINet functionality under MS-Windows
257 rc
= m_nativeImp
->GetInputStream(this);
261 // else use the standard behaviour
262 #endif // wxUSE_URL_NATIVE
267 // m_protoinfo is NULL when we use a proxy
268 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
270 if (!addr
.Hostname(m_server
))
272 m_error
= wxURL_NOHOST
;
276 addr
.Service(m_port
);
278 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
280 m_error
= wxURL_CONNERR
;
288 // When we use a proxy, we have to pass the whole URL to it.
293 fullPath
+= wxT("/");
298 fullPath
+= wxT("?") + m_query
;
301 fullPath
+= wxT("#") + m_fragment
;
303 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
307 m_error
= wxURL_PROTOERR
;
314 #if wxUSE_PROTOCOL_HTTP
315 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
319 if ( ms_proxyDefault
)
321 ms_proxyDefault
->Close();
322 delete ms_proxyDefault
;
323 ms_proxyDefault
= NULL
;
328 wxString tmp_str
= url_proxy
;
329 int pos
= tmp_str
.Find(wxT(':'));
330 if (pos
== wxNOT_FOUND
)
333 wxString hostname
= tmp_str(0, pos
),
334 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
337 if (!addr
.Hostname(hostname
))
339 if (!addr
.Service(port
))
343 // Finally, when all is right, we connect the new proxy.
344 ms_proxyDefault
->Close();
346 ms_proxyDefault
= new wxHTTP();
347 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
351 void wxURL::SetProxy(const wxString
& url_proxy
)
355 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
366 wxString hostname
, port
;
371 pos
= tmp_str
.Find(wxT(':'));
372 // This is an invalid proxy name.
373 if (pos
== wxNOT_FOUND
)
376 hostname
= tmp_str(0, pos
);
377 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
379 addr
.Hostname(hostname
);
382 // Finally, create the whole stuff.
383 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
385 m_proxy
= new wxHTTP();
386 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
394 #endif // wxUSE_PROTOCOL_HTTP
396 // ----------------------------------------------------------------------
399 // A module which deletes the default proxy if we created it
400 // ----------------------------------------------------------------------
404 class wxURLModule
: public wxModule
407 virtual bool OnInit();
408 virtual void OnExit();
411 DECLARE_DYNAMIC_CLASS(wxURLModule
)
414 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
416 bool wxURLModule::OnInit()
418 #if wxUSE_PROTOCOL_HTTP
419 // env var HTTP_PROXY contains the address of the default proxy to use if
420 // set, but don't try to create this proxy right now because it will slow
421 // down the program startup (especially if there is no DNS server
422 // available, in which case it may take up to 1 minute)
424 if ( wxGetenv(_T("HTTP_PROXY")) )
426 wxURL::ms_useDefaultProxy
= true;
428 #endif // wxUSE_PROTOCOL_HTTP
432 void wxURLModule::OnExit()
434 #if wxUSE_PROTOCOL_HTTP
435 delete wxURL::ms_proxyDefault
;
436 wxURL::ms_proxyDefault
= NULL
;
437 #endif // wxUSE_PROTOCOL_HTTP
440 #endif // wxUSE_SOCKETS
442 // ---------------------------------------------------------------------------
444 // wxURL Compatibility
446 // ---------------------------------------------------------------------------
448 #if WXWIN_COMPATIBILITY_2_4
452 wxString
wxURL::GetProtocolName() const
457 wxString
wxURL::GetHostName() const
462 wxString
wxURL::GetPath() const
467 //Note that this old code really doesn't convert to a URI that well and looks
468 //more like a dirty hack than anything else...
470 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
476 for (i
= 0; i
< uri
.Len(); i
++)
478 wxChar c
= uri
.GetChar(i
);
482 // GRG, Apr/2000: changed to "%20" instead of '+'
484 out_str
+= wxT("%20");
488 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
490 // - Alphanumeric characters are never escaped
491 // - Unreserved marks are never escaped
492 // - Delimiters must be escaped if they appear within a component
493 // but not if they are used to separate components. Here we have
494 // no clear way to distinguish between these two cases, so they
495 // are escaped unless they are passed in the 'delims' parameter
496 // (allowed delimiters).
498 static const wxChar marks
[] = wxT("-_.!~*()'");
500 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
502 hexa_code
.Printf(wxT("%%%02X"), c
);
503 out_str
+= hexa_code
;
515 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
517 return wxURI::Unescape(uri
);
520 #endif //WXWIN_COMPATIBILITY_2_4