]>
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
;
38 // Enforce linking of protocol classes:
39 USE_PROTOCOL(wxFileProto
)
45 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
46 bool wxURL::ms_useDefaultProxy
= FALSE
;
49 // --------------------------------------------------------------
51 // --------------------------------------------------------------
53 // --------------------------------------------------------------
54 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
55 // --------------------------------------------------------------
57 wxURL::wxURL(const wxString
& url
)
60 m_error
= wxURL_NOERR
;
64 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
66 SetDefaultProxy(getenv("HTTP_PROXY"));
68 if ( !ms_proxyDefault
)
71 ms_useDefaultProxy
= FALSE
;
75 m_useProxy
= ms_proxyDefault
!= NULL
;
76 m_proxy
= ms_proxyDefault
;
77 #endif // wxUSE_SOCKETS
82 bool wxURL::ParseURL()
84 wxString last_url
= m_url
;
86 // If the URL was already parsed (m_protocol != NULL), pass this section.
92 // Extract protocol name
93 if (!PrepProto(last_url
))
95 m_error
= wxURL_SNTXERR
;
99 // Find and create the protocol object
100 if (!FetchProtocol())
102 m_error
= wxURL_NOPROTO
;
106 // Do we need a host name ?
107 if (m_protoinfo
->m_needhost
)
110 if (!PrepHost(last_url
))
112 m_error
= wxURL_SNTXERR
;
118 if (!PrepPath(last_url
))
120 m_error
= wxURL_NOPATH
;
124 // URL parse finished.
129 // We destroy the newly created protocol.
132 // Third, we rebuild the URL.
133 m_url
= m_protoname
+ wxT(":");
134 if (m_protoinfo
->m_needhost
)
135 m_url
= m_url
+ wxT("//") + m_hostname
;
139 // We initialize specific variables.
140 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
144 m_error
= wxURL_NOERR
;
148 void wxURL::CleanData()
160 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
165 // --------------------------------------------------------------
166 // --------- wxURL urls decoders --------------------------------
167 // --------------------------------------------------------------
169 bool wxURL::PrepProto(wxString
& url
)
174 pos
= url
.Find(wxT(':'));
178 m_protoname
= url(0, pos
);
180 url
= url(pos
+1, url
.Length());
185 bool wxURL::PrepHost(wxString
& url
)
190 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
193 url
= url(2, url
.Length());
195 pos
= url
.Find(wxT('/'));
202 temp_url
= url(0, pos
);
203 url
= url(url
.Find(wxT('/')), url
.Length());
205 // Retrieve service number
206 pos2
= temp_url
.Find(wxT(':'), TRUE
);
207 if (pos2
!= -1 && pos2
< pos
)
209 m_servname
= temp_url(pos2
+1, pos
);
210 if (!m_servname
.IsNumber())
212 temp_url
= temp_url(0, pos2
);
215 // Retrieve user and password.
216 pos2
= temp_url
.Find(wxT('@'));
217 // Even if pos2 equals -1, this code is right.
218 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
221 m_password
= wxT("");
226 temp_url
= temp_url(0, pos2
);
227 pos2
= temp_url
.Find(wxT(':'));
232 m_user
= temp_url(0, pos2
);
233 m_password
= temp_url(pos2
+1, url
.Length());
238 bool wxURL::PrepPath(wxString
& url
)
240 if (url
.Length() != 0)
241 m_path
= ConvertToValidURI(url
);
247 bool wxURL::FetchProtocol()
249 wxProtoInfo
*info
= ms_protocols
;
253 if (m_protoname
== info
->m_protoname
)
255 if (m_servname
.IsNull())
256 m_servname
= info
->m_servname
;
259 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
267 // --------------------------------------------------------------
268 // --------- wxURL get ------------------------------------------
269 // --------------------------------------------------------------
271 wxInputStream
*wxURL::GetInputStream()
273 wxInputStream
*the_i_stream
= NULL
;
277 m_error
= wxURL_NOPROTO
;
281 m_error
= wxURL_NOERR
;
282 if (m_user
!= wxT(""))
284 m_protocol
->SetUser(m_user
);
285 m_protocol
->SetPassword(m_password
);
291 // m_protoinfo is NULL when we use a proxy
292 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
294 if (!addr
.Hostname(m_hostname
))
296 m_error
= wxURL_NOHOST
;
300 addr
.Service(m_servname
);
302 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
304 m_error
= wxURL_CONNERR
;
310 // When we use a proxy, we have to pass the whole URL to it.
312 the_i_stream
= m_protocol
->GetInputStream(m_url
);
314 the_i_stream
= m_protocol
->GetInputStream(m_path
);
318 m_error
= wxURL_PROTOERR
;
326 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
330 if ( ms_proxyDefault
)
332 ms_proxyDefault
->Close();
333 delete ms_proxyDefault
;
334 ms_proxyDefault
= NULL
;
339 wxString tmp_str
= url_proxy
;
340 int pos
= tmp_str
.Find(wxT(':'));
344 wxString hostname
= tmp_str(0, pos
),
345 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
348 if (!addr
.Hostname(hostname
))
350 if (!addr
.Service(port
))
354 // Finally, when all is right, we connect the new proxy.
355 ms_proxyDefault
->Close();
357 ms_proxyDefault
= new wxHTTP();
358 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
362 void wxURL::SetProxy(const wxString
& url_proxy
)
366 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
377 wxString hostname
, port
;
382 pos
= tmp_str
.Find(wxT(':'));
383 // This is an invalid proxy name.
387 hostname
= tmp_str(0, pos
);
388 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
390 addr
.Hostname(hostname
);
393 // Finally, create the whole stuff.
394 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
396 m_proxy
= new wxHTTP();
397 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
405 #endif // wxUSE_SOCKETS
407 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
413 for (i
= 0; i
< uri
.Len(); i
++)
415 wxChar c
= uri
.GetChar(i
);
419 // GRG, Apr/2000: changed to "%20" instead of '+'
421 out_str
+= wxT("%20");
425 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
427 // - Alphanumeric characters are never escaped
428 // - Unreserved marks are never escaped
429 // - Delimiters must be escaped if they appear within a component
430 // but not if they are used to separate components. Here we have
431 // no clear way to distinguish between these two cases, so they
432 // are escaped unless they are passed in the 'delims' parameter
433 // (allowed delimiters).
435 static const wxChar marks
[] = wxT("-_.!~*()'");
437 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
439 hexa_code
.Printf(wxT("%%%02X"), c
);
440 out_str
+= hexa_code
;
452 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
457 while (i
< uri
.Len())
460 if (uri
[i
] == wxT('%'))
463 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
464 code
= (uri
[i
] - wxT('A') + 10) * 16;
466 code
= (uri
[i
] - wxT('0')) * 16;
469 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
470 code
+= (uri
[i
] - wxT('A')) + 10;
472 code
+= (uri
[i
] - wxT('0'));
475 new_uri
+= (wxChar
)code
;
484 // ----------------------------------------------------------------------
485 // A module which deletes the default proxy if we created it
486 // ----------------------------------------------------------------------
490 class wxURLModule
: public wxModule
493 virtual bool OnInit();
494 virtual void OnExit();
497 DECLARE_DYNAMIC_CLASS(wxURLModule
)
500 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
502 bool wxURLModule::OnInit()
504 // env var HTTP_PROXY contains the address of the default proxy to use if
505 // set, but don't try to create this proxy right now because it will slow
506 // down the program startup (especially if there is no DNS server
507 // available, in which case it may take up to 1 minute)
509 if ( getenv("HTTP_PROXY") )
511 wxURL::ms_useDefaultProxy
= TRUE
;
517 void wxURLModule::OnExit()
519 delete wxURL::ms_proxyDefault
;
520 wxURL::ms_proxyDefault
= NULL
;
523 #endif // wxUSE_SOCKETS