]>
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(m_userinfo
);
248 m_protocol
->SetUser(m_userinfo(0, dwPasswordPos
));
249 m_protocol
->SetPassword(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
270 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
272 if (!addr
.Hostname(m_server
))
274 m_error
= wxURL_NOHOST
;
278 addr
.Service(m_port
);
280 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
282 m_error
= wxURL_CONNERR
;
290 // When we use a proxy, we have to pass the whole URL to it.
295 fullPath
+= wxT("/");
300 fullPath
+= wxT("?") + m_query
;
303 fullPath
+= wxT("#") + m_fragment
;
305 wxInputStream
*the_i_stream
= m_protocol
->GetInputStream(fullPath
);
309 m_error
= wxURL_PROTOERR
;
316 #if wxUSE_PROTOCOL_HTTP
317 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
321 if ( ms_proxyDefault
)
323 ms_proxyDefault
->Close();
324 delete ms_proxyDefault
;
325 ms_proxyDefault
= NULL
;
330 wxString tmp_str
= url_proxy
;
331 int pos
= tmp_str
.Find(wxT(':'));
332 if (pos
== wxNOT_FOUND
)
335 wxString hostname
= tmp_str(0, pos
),
336 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
339 if (!addr
.Hostname(hostname
))
341 if (!addr
.Service(port
))
345 // Finally, when all is right, we connect the new proxy.
346 ms_proxyDefault
->Close();
348 ms_proxyDefault
= new wxHTTP();
349 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
353 void wxURL::SetProxy(const wxString
& url_proxy
)
357 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
368 wxString hostname
, port
;
373 pos
= tmp_str
.Find(wxT(':'));
374 // This is an invalid proxy name.
375 if (pos
== wxNOT_FOUND
)
378 hostname
= tmp_str(0, pos
);
379 port
= tmp_str(pos
+1, tmp_str
.length()-pos
);
381 addr
.Hostname(hostname
);
384 // Finally, create the whole stuff.
385 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
387 m_proxy
= new wxHTTP();
388 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
396 #endif // wxUSE_PROTOCOL_HTTP
398 // ----------------------------------------------------------------------
401 // A module which deletes the default proxy if we created it
402 // ----------------------------------------------------------------------
406 class wxURLModule
: public wxModule
409 virtual bool OnInit();
410 virtual void OnExit();
413 DECLARE_DYNAMIC_CLASS(wxURLModule
)
416 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
418 bool wxURLModule::OnInit()
420 #if wxUSE_PROTOCOL_HTTP
421 // env var HTTP_PROXY contains the address of the default proxy to use if
422 // set, but don't try to create this proxy right now because it will slow
423 // down the program startup (especially if there is no DNS server
424 // available, in which case it may take up to 1 minute)
426 if ( wxGetenv(_T("HTTP_PROXY")) )
428 wxURL::ms_useDefaultProxy
= true;
430 #endif // wxUSE_PROTOCOL_HTTP
434 void wxURLModule::OnExit()
436 #if wxUSE_PROTOCOL_HTTP
437 delete wxURL::ms_proxyDefault
;
438 wxURL::ms_proxyDefault
= NULL
;
439 #endif // wxUSE_PROTOCOL_HTTP
442 #endif // wxUSE_SOCKETS