]>
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 (m_protocol != NULL), pass this section.
86 // Extract protocol name
87 if (!PrepProto(last_url
))
89 m_error
= wxURL_SNTXERR
;
93 // Find and create the protocol object
96 m_error
= wxURL_NOPROTO
;
100 // Do we need a host name ?
101 if (m_protoinfo
->m_needhost
)
104 if (!PrepHost(last_url
))
106 m_error
= wxURL_SNTXERR
;
112 if (!PrepPath(last_url
))
114 m_error
= wxURL_NOPATH
;
118 // URL parse finished.
123 // We destroy the newly created protocol.
126 // Third, we rebuild the URL.
127 m_url
= m_protoname
+ wxT(":");
128 if (m_protoinfo
->m_needhost
)
129 m_url
= m_url
+ wxT("//") + m_hostname
;
133 // We initialize specific variables.
134 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
138 m_error
= wxURL_NOERR
;
142 void wxURL::CleanData()
154 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
159 // --------------------------------------------------------------
160 // --------- wxURL urls decoders --------------------------------
161 // --------------------------------------------------------------
163 bool wxURL::PrepProto(wxString
& url
)
168 pos
= url
.Find(wxT(':'));
172 m_protoname
= url(0, pos
);
174 url
= url(pos
+1, url
.Length());
179 bool wxURL::PrepHost(wxString
& url
)
184 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
187 url
= url(2, url
.Length());
189 pos
= url
.Find(wxT('/'));
196 temp_url
= url(0, pos
);
197 url
= url(url
.Find(wxT('/')), url
.Length());
199 // Retrieve service number
200 pos2
= temp_url
.Find(wxT(':'), TRUE
);
201 if (pos2
!= -1 && pos2
< pos
)
203 m_servname
= temp_url(pos2
+1, pos
);
204 if (!m_servname
.IsNumber())
206 temp_url
= temp_url(0, pos2
);
209 // Retrieve user and password.
210 pos2
= temp_url
.Find(wxT('@'));
211 // Even if pos2 equals -1, this code is right.
212 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
215 m_password
= wxT("");
220 temp_url
= temp_url(0, pos2
);
221 pos2
= temp_url
.Find(wxT(':'));
226 m_user
= temp_url(0, pos2
);
227 m_password
= temp_url(pos2
+1, url
.Length());
232 bool wxURL::PrepPath(wxString
& url
)
234 if (url
.Length() != 0)
235 m_path
= ConvertToValidURI(url
);
241 bool wxURL::FetchProtocol()
243 wxProtoInfo
*info
= ms_protocols
;
247 if (m_protoname
== info
->m_protoname
)
249 if (m_servname
.IsNull())
250 m_servname
= info
->m_servname
;
253 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
261 // --------------------------------------------------------------
262 // --------- wxURL get ------------------------------------------
263 // --------------------------------------------------------------
265 wxInputStream
*wxURL::GetInputStream()
267 wxInputStream
*the_i_stream
= NULL
;
271 m_error
= wxURL_NOPROTO
;
275 m_error
= wxURL_NOERR
;
276 if (m_user
!= wxT(""))
278 m_protocol
->SetUser(m_user
);
279 m_protocol
->SetPassword(m_password
);
285 // m_protoinfo is NULL when we use a proxy
286 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
288 if (!addr
.Hostname(m_hostname
))
290 m_error
= wxURL_NOHOST
;
294 addr
.Service(m_servname
);
296 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
298 m_error
= wxURL_CONNERR
;
304 // When we use a proxy, we have to pass the whole URL to it.
306 the_i_stream
= m_protocol
->GetInputStream(m_url
);
308 the_i_stream
= m_protocol
->GetInputStream(m_path
);
312 m_error
= wxURL_PROTOERR
;
320 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
324 if ( ms_proxyDefault
)
326 ms_proxyDefault
->Close();
327 delete ms_proxyDefault
;
328 ms_proxyDefault
= NULL
;
333 wxString tmp_str
= url_proxy
;
334 int pos
= tmp_str
.Find(wxT(':'));
338 wxString hostname
= tmp_str(0, pos
),
339 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
342 if (!addr
.Hostname(hostname
))
344 if (!addr
.Service(port
))
348 // Finally, when all is right, we connect the new proxy.
349 ms_proxyDefault
->Close();
351 ms_proxyDefault
= new wxHTTP();
352 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
356 void wxURL::SetProxy(const wxString
& url_proxy
)
360 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
371 wxString hostname
, port
;
376 pos
= tmp_str
.Find(wxT(':'));
377 // This is an invalid proxy name.
381 hostname
= tmp_str(0, pos
);
382 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
384 addr
.Hostname(hostname
);
387 // Finally, create the whole stuff.
388 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
390 m_proxy
= new wxHTTP();
391 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
399 #endif // wxUSE_SOCKETS
401 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
407 for (i
= 0; i
< uri
.Len(); i
++)
409 wxChar c
= uri
.GetChar(i
);
413 // GRG, Apr/2000: changed to "%20" instead of '+'
415 out_str
+= wxT("%20");
419 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
421 // - Alphanumeric characters are never escaped
422 // - Unreserved marks are never escaped
423 // - Delimiters must be escaped if they appear within a component
424 // but not if they are used to separate components. Here we have
425 // no clear way to distinguish between these two cases, so they
426 // are escaped unless they are passed in the 'delims' parameter
427 // (allowed delimiters).
429 static const wxChar marks
[] = wxT("-_.!~*()'");
431 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
433 hexa_code
.Printf(wxT("%%%02X"), c
);
434 out_str
+= hexa_code
;
446 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
451 while (i
< uri
.Len())
454 if (uri
[i
] == wxT('%'))
457 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
458 code
= (uri
[i
] - wxT('A') + 10) * 16;
460 code
= (uri
[i
] - wxT('0')) * 16;
463 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
464 code
+= (uri
[i
] - wxT('A')) + 10;
466 code
+= (uri
[i
] - wxT('0'));
469 new_uri
+= (wxChar
)code
;
478 // ----------------------------------------------------------------------
479 // A module which deletes the default proxy if we created it
480 // ----------------------------------------------------------------------
484 class wxURLModule
: public wxModule
487 virtual bool OnInit();
488 virtual void OnExit();
491 DECLARE_DYNAMIC_CLASS(wxURLModule
)
494 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
496 bool wxURLModule::OnInit()
498 // env var HTTP_PROXY contains the address of the default proxy to use if
499 // set, but don't try to create this proxy right now because it will slow
500 // down the program startup (especially if there is no DNS server
501 // available, in which case it may take up to 1 minute)
503 if ( getenv("HTTP_PROXY") )
505 wxURL::ms_useDefaultProxy
= TRUE
;
511 void wxURLModule::OnExit()
513 delete wxURL::ms_proxyDefault
;
514 wxURL::ms_proxyDefault
= NULL
;
517 #endif // wxUSE_SOCKETS