]>
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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "url.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/string.h"
28 #include "wx/module.h"
34 IMPLEMENT_CLASS(wxProtoInfo
, wxObject
)
35 IMPLEMENT_CLASS(wxURL
, wxObject
)
38 wxProtoInfo
*wxURL::ms_protocols
= NULL
;
40 // Enforce linking of protocol classes:
41 USE_PROTOCOL(wxFileProto
)
47 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
48 bool wxURL::ms_useDefaultProxy
= false;
51 // --------------------------------------------------------------
53 // --------------------------------------------------------------
55 // --------------------------------------------------------------
56 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
57 // --------------------------------------------------------------
59 wxURL::wxURL(const wxString
& url
)
62 m_error
= wxURL_NOERR
;
65 m_nativeImp
= CreateNativeImpObject();
69 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
71 SetDefaultProxy( wxGetenv(wxT("HTTP_PROXY")) );
73 if ( !ms_proxyDefault
)
76 ms_useDefaultProxy
= false;
80 m_useProxy
= ms_proxyDefault
!= NULL
;
81 m_proxy
= ms_proxyDefault
;
82 #endif // wxUSE_SOCKETS
87 bool wxURL::ParseURL()
89 wxString last_url
= m_url
;
91 // If the URL was already parsed (m_protocol != NULL), pass this section.
97 // Extract protocol name
98 if (!PrepProto(last_url
))
100 m_error
= wxURL_SNTXERR
;
104 // Find and create the protocol object
105 if (!FetchProtocol())
107 m_error
= wxURL_NOPROTO
;
111 // Do we need a host name ?
112 if (m_protoinfo
->m_needhost
)
115 if (!PrepHost(last_url
))
117 m_error
= wxURL_SNTXERR
;
123 if (!PrepPath(last_url
))
125 m_error
= wxURL_NOPATH
;
129 // URL parse finished.
134 // destroy the previously created protocol as we'll be using m_proxy
137 // Third, we rebuild the URL.
138 m_url
= m_protoname
+ wxT(":");
139 if (m_protoinfo
->m_needhost
)
140 m_url
= m_url
+ wxT("//") + m_hostname
;
144 // We initialize specific variables.
145 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
149 m_error
= wxURL_NOERR
;
153 void wxURL::CleanData()
165 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
173 // --------------------------------------------------------------
174 // --------- wxURL urls decoders --------------------------------
175 // --------------------------------------------------------------
177 bool wxURL::PrepProto(wxString
& url
)
182 pos
= url
.Find(wxT(':'));
183 if (pos
== wxNOT_FOUND
)
186 m_protoname
= url(0, pos
);
188 url
= url(pos
+1, url
.Length());
193 bool wxURL::PrepHost(wxString
& url
)
198 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
201 url
= url(2, url
.Length());
203 pos
= url
.Find(wxT('/'));
204 if (pos
== wxNOT_FOUND
)
210 temp_url
= url(0, pos
);
211 url
= url(url
.Find(wxT('/')), url
.Length());
213 // Retrieve service number
214 pos2
= temp_url
.Find(wxT(':'), true);
215 if (pos2
!= wxNOT_FOUND
&& pos2
< pos
)
217 m_servname
= temp_url(pos2
+1, pos
);
218 if (!m_servname
.IsNumber())
220 temp_url
= temp_url(0, pos2
);
223 // Retrieve user and password.
224 pos2
= temp_url
.Find(wxT('@'));
225 // Even if pos2 equals wxNOT_FOUND, this code is right.
226 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
229 m_password
= wxT("");
231 if (pos2
== wxNOT_FOUND
)
234 temp_url
= temp_url(0, pos2
);
235 pos2
= temp_url
.Find(wxT(':'));
237 if (pos2
== wxNOT_FOUND
)
240 m_user
= temp_url(0, pos2
);
241 m_password
= temp_url(pos2
+1, url
.Length());
246 bool wxURL::PrepPath(wxString
& url
)
248 if (url
.Length() != 0)
249 m_path
= ConvertToValidURI(url
);
255 bool wxURL::FetchProtocol()
257 wxProtoInfo
*info
= ms_protocols
;
261 if (m_protoname
== info
->m_protoname
)
263 if (m_servname
.IsNull())
264 m_servname
= info
->m_servname
;
267 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
275 // --------------------------------------------------------------
276 // --------- wxURL get ------------------------------------------
277 // --------------------------------------------------------------
279 wxInputStream
*wxURL::GetInputStream()
283 m_error
= wxURL_NOPROTO
;
287 m_error
= wxURL_NOERR
;
288 if (m_user
!= wxT(""))
290 m_protocol
->SetUser(m_user
);
291 m_protocol
->SetPassword(m_password
);
295 // give the native implementation to return a better stream
296 // such as the native WinINet functionality under MS-Windows
300 rc
= m_nativeImp
->GetInputStream(this);
304 // else use the standard behaviour
305 #endif // wxUSE_URL_NATIVE
310 // m_protoinfo is NULL when we use a proxy
311 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
313 if (!addr
.Hostname(m_hostname
))
315 m_error
= wxURL_NOHOST
;
319 addr
.Service(m_servname
);
321 if (!m_protocol
->Connect(addr
, true)) // Watcom needs the 2nd arg for some reason
323 m_error
= wxURL_CONNERR
;
329 // When we use a proxy, we have to pass the whole URL to it.
330 wxInputStream
*the_i_stream
=
331 (m_useProxy
) ? m_protocol
->GetInputStream(m_url
) :
332 m_protocol
->GetInputStream(m_path
);
336 m_error
= wxURL_PROTOERR
;
344 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
348 if ( ms_proxyDefault
)
350 ms_proxyDefault
->Close();
351 delete ms_proxyDefault
;
352 ms_proxyDefault
= NULL
;
357 wxString tmp_str
= url_proxy
;
358 int pos
= tmp_str
.Find(wxT(':'));
359 if (pos
== wxNOT_FOUND
)
362 wxString hostname
= tmp_str(0, pos
),
363 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
366 if (!addr
.Hostname(hostname
))
368 if (!addr
.Service(port
))
372 // Finally, when all is right, we connect the new proxy.
373 ms_proxyDefault
->Close();
375 ms_proxyDefault
= new wxHTTP();
376 ms_proxyDefault
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
380 void wxURL::SetProxy(const wxString
& url_proxy
)
384 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
395 wxString hostname
, port
;
400 pos
= tmp_str
.Find(wxT(':'));
401 // This is an invalid proxy name.
402 if (pos
== wxNOT_FOUND
)
405 hostname
= tmp_str(0, pos
);
406 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
408 addr
.Hostname(hostname
);
411 // Finally, create the whole stuff.
412 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
414 m_proxy
= new wxHTTP();
415 m_proxy
->Connect(addr
, true); // Watcom needs the 2nd arg for some reason
423 #endif // wxUSE_SOCKETS
425 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
431 for (i
= 0; i
< uri
.Len(); i
++)
433 wxChar c
= uri
.GetChar(i
);
437 // GRG, Apr/2000: changed to "%20" instead of '+'
439 out_str
+= wxT("%20");
443 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
445 // - Alphanumeric characters are never escaped
446 // - Unreserved marks are never escaped
447 // - Delimiters must be escaped if they appear within a component
448 // but not if they are used to separate components. Here we have
449 // no clear way to distinguish between these two cases, so they
450 // are escaped unless they are passed in the 'delims' parameter
451 // (allowed delimiters).
453 static const wxChar marks
[] = wxT("-_.!~*()'");
455 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
457 hexa_code
.Printf(wxT("%%%02X"), c
);
458 out_str
+= hexa_code
;
470 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
475 while (i
< uri
.Len())
478 if (uri
[i
] == wxT('%'))
481 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
482 code
= (uri
[i
] - wxT('A') + 10) * 16;
483 else if (uri
[i
] >= wxT('a') && uri
[i
] <= wxT('f'))
484 code
= (uri
[i
] - wxT('a') + 10) * 16;
486 code
= (uri
[i
] - wxT('0')) * 16;
489 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
490 code
+= (uri
[i
] - wxT('A')) + 10;
491 else if (uri
[i
] >= wxT('a') && uri
[i
] <= wxT('f'))
492 code
+= (uri
[i
] - wxT('a')) + 10;
494 code
+= (uri
[i
] - wxT('0'));
497 new_uri
+= (wxChar
)code
;
506 // ----------------------------------------------------------------------
507 // A module which deletes the default proxy if we created it
508 // ----------------------------------------------------------------------
512 class wxURLModule
: public wxModule
515 virtual bool OnInit();
516 virtual void OnExit();
519 DECLARE_DYNAMIC_CLASS(wxURLModule
)
522 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
524 bool wxURLModule::OnInit()
526 // env var HTTP_PROXY contains the address of the default proxy to use if
527 // set, but don't try to create this proxy right now because it will slow
528 // down the program startup (especially if there is no DNS server
529 // available, in which case it may take up to 1 minute)
531 if ( getenv("HTTP_PROXY") )
533 wxURL::ms_useDefaultProxy
= true;
539 void wxURLModule::OnExit()
541 delete wxURL::ms_proxyDefault
;
542 wxURL::ms_proxyDefault
= NULL
;
545 #endif // wxUSE_SOCKETS