]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "url.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/string.h"
29 #include "wx/module.h"
32 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
33 IMPLEMENT_CLASS(wxURL
, wxObject
)
36 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
39 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
40 bool wxURL::ms_useDefaultProxy
= FALSE
;
43 // --------------------------------------------------------------
45 // --------------------------------------------------------------
47 // --------------------------------------------------------------
48 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
49 // --------------------------------------------------------------
51 wxURL::wxURL(const wxString
& url
)
54 m_error
= wxURL_NOERR
;
58 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
60 SetDefaultProxy(getenv("HTTP_PROXY"));
62 if ( !ms_proxyDefault
)
65 ms_useDefaultProxy
= FALSE
;
69 m_useProxy
= ms_proxyDefault
!= NULL
;
70 m_proxy
= ms_proxyDefault
;
71 #endif // wxUSE_SOCKETS
76 bool wxURL::ParseURL()
78 wxString last_url
= m_url
;
80 // If the URL was already parsed (so m_protocol != NULL), we pass this section.
86 // Extract protocol name
87 if (!PrepProto(last_url
)) {
88 m_error
= wxURL_SNTXERR
;
92 // Find and create the protocol object
93 if (!FetchProtocol()) {
94 m_error
= wxURL_NOPROTO
;
98 // Do we need a host name ?
99 if (m_protoinfo
->m_needhost
) {
101 if (!PrepHost(last_url
)) {
102 m_error
= wxURL_SNTXERR
;
108 if (!PrepPath(last_url
)) {
109 m_error
= wxURL_NOPATH
;
113 // URL parse finished.
117 // We destroy the newly created protocol.
120 // Third, we rebuild the URL.
121 m_url
= m_protoname
+ wxT(":");
122 if (m_protoinfo
->m_needhost
)
123 m_url
= m_url
+ wxT("//") + m_hostname
;
127 // We initialize specific variables.
128 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
132 m_error
= wxURL_NOERR
;
136 void wxURL::CleanData()
148 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
153 // --------------------------------------------------------------
154 // --------- wxURL urls decoders --------------------------------
155 // --------------------------------------------------------------
157 bool wxURL::PrepProto(wxString
& url
)
162 pos
= url
.Find(wxT(':'));
166 m_protoname
= url(0, pos
);
168 url
= url(pos
+1, url
.Length());
173 bool wxURL::PrepHost(wxString
& url
)
178 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
181 url
= url(2, url
.Length());
183 pos
= url
.Find(wxT('/'));
190 temp_url
= url(0, pos
);
191 url
= url(url
.Find(wxT('/')), url
.Length());
193 // Retrieve service number
194 pos2
= temp_url
.Find(wxT(':'), TRUE
);
195 if (pos2
!= -1 && pos2
< pos
) {
196 m_servname
= temp_url(pos2
+1, pos
);
197 if (!m_servname
.IsNumber())
199 temp_url
= temp_url(0, pos2
);
202 // Retrieve user and password.
203 pos2
= temp_url
.Find(wxT('@'));
204 // Even if pos2 equals -1, this code is right.
205 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
208 m_password
= wxT("");
213 temp_url
= temp_url(0, pos2
);
214 pos2
= temp_url
.Find(wxT(':'));
219 m_user
= temp_url(0, pos2
);
220 m_password
= temp_url(pos2
+1, url
.Length());
225 bool wxURL::PrepPath(wxString
& url
)
227 if (url
.Length() != 0)
228 m_path
= ConvertToValidURI(url
);
234 bool wxURL::FetchProtocol()
236 wxProtoInfo
*info
= ms_protocols
;
239 if (m_protoname
== info
->m_protoname
) {
240 if (m_servname
.IsNull())
241 m_servname
= info
->m_servname
;
244 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
252 // --------------------------------------------------------------
253 // --------- wxURL get ------------------------------------------
254 // --------------------------------------------------------------
256 wxInputStream
*wxURL::GetInputStream()
258 wxInputStream
*the_i_stream
= NULL
;
261 m_error
= wxURL_NOPROTO
;
265 m_error
= wxURL_NOERR
;
266 if (m_user
!= wxT("")) {
267 m_protocol
->SetUser(m_user
);
268 m_protocol
->SetPassword(m_password
);
274 // m_protoinfo is NULL when we use a proxy
275 if (!m_useProxy
&& m_protoinfo
->m_needhost
) {
276 if (!addr
.Hostname(m_hostname
)) {
277 m_error
= wxURL_NOHOST
;
281 addr
.Service(m_servname
);
283 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
285 m_error
= wxURL_CONNERR
;
291 // When we use a proxy, we have to pass the whole URL to it.
293 the_i_stream
= m_protocol
->GetInputStream(m_url
);
295 the_i_stream
= m_protocol
->GetInputStream(m_path
);
298 m_error
= wxURL_PROTOERR
;
306 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
310 if ( ms_proxyDefault
)
312 ms_proxyDefault
->Close();
313 delete ms_proxyDefault
;
314 ms_proxyDefault
= NULL
;
319 wxString tmp_str
= url_proxy
;
320 int pos
= tmp_str
.Find(wxT(':'));
324 wxString hostname
= tmp_str(0, pos
),
325 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
328 if (!addr
.Hostname(hostname
))
330 if (!addr
.Service(port
))
334 // Finally, when all is right, we connect the new proxy.
335 ms_proxyDefault
->Close();
337 ms_proxyDefault
= new wxHTTP();
338 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
342 void wxURL::SetProxy(const wxString
& url_proxy
)
346 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
357 wxString hostname
, port
;
362 pos
= tmp_str
.Find(wxT(':'));
363 // This is an invalid proxy name.
367 hostname
= tmp_str(0, pos
);
368 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
370 addr
.Hostname(hostname
);
373 // Finally, create the whole stuff.
374 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
376 m_proxy
= new wxHTTP();
377 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
385 #endif // wxUSE_SOCKETS
387 wxString
wxURL::ConvertToValidURI(const wxString
& uri
)
393 for (i
=0;i
<uri
.Len();i
++) {
394 wxChar c
= uri
.GetChar(i
);
399 if (!isalpha(c
) && c
!= wxT('.') && c
!= wxT('+') && c
!= wxT('/')) {
400 hexa_code
.Printf(wxT("%%%02X"), c
);
401 out_str
+= hexa_code
;
410 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
415 while (i
<uri
.Len()) {
417 if (uri
[i
] == wxT('%')) {
419 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
420 code
= (uri
[i
] - wxT('A') + 10) * 16;
422 code
= (uri
[i
] - wxT('0')) * 16;
424 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
425 code
+= (uri
[i
] - wxT('A')) + 10;
427 code
+= (uri
[i
] - wxT('0'));
429 new_uri
+= (wxChar
)code
;
438 // ----------------------------------------------------------------------
439 // A module which deletes the default proxy if we created it
440 // ----------------------------------------------------------------------
444 class wxURLModule
: public wxModule
447 virtual bool OnInit();
448 virtual void OnExit();
451 DECLARE_DYNAMIC_CLASS(wxURLModule
)
454 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
456 bool wxURLModule::OnInit()
458 // env var HTTP_PROXY contains the address of the default proxy to use if
459 // set, but don't try to create this proxy right now because it will slow
460 // down the program startup (especially if there is no DNS server
461 // available, in which case it may take up to 1 minute)
462 if ( getenv("HTTP_PROXY") )
464 wxURL::ms_useDefaultProxy
= TRUE
;
470 void wxURLModule::OnExit()
472 delete wxURL::ms_proxyDefault
;
473 wxURL::ms_proxyDefault
= NULL
;
476 #endif // wxUSE_SOCKETS