]>
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:
41 USE_PROTOCOL(wxFileProto
)
44 wxHTTP
*wxURL::ms_proxyDefault
= NULL
;
45 bool wxURL::ms_useDefaultProxy
= FALSE
;
48 // --------------------------------------------------------------
50 // --------------------------------------------------------------
52 // --------------------------------------------------------------
53 // --------- wxURL CONSTRUCTOR DESTRUCTOR -----------------------
54 // --------------------------------------------------------------
56 wxURL::wxURL(const wxString
& url
)
59 m_error
= wxURL_NOERR
;
63 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
65 SetDefaultProxy(getenv("HTTP_PROXY"));
67 if ( !ms_proxyDefault
)
70 ms_useDefaultProxy
= FALSE
;
74 m_useProxy
= ms_proxyDefault
!= NULL
;
75 m_proxy
= ms_proxyDefault
;
76 #endif // wxUSE_SOCKETS
81 bool wxURL::ParseURL()
83 wxString last_url
= m_url
;
85 // If the URL was already parsed (m_protocol != NULL), pass this section.
91 // Extract protocol name
92 if (!PrepProto(last_url
))
94 m_error
= wxURL_SNTXERR
;
98 // Find and create the protocol object
101 m_error
= wxURL_NOPROTO
;
105 // Do we need a host name ?
106 if (m_protoinfo
->m_needhost
)
109 if (!PrepHost(last_url
))
111 m_error
= wxURL_SNTXERR
;
117 if (!PrepPath(last_url
))
119 m_error
= wxURL_NOPATH
;
123 // URL parse finished.
128 // We destroy the newly created protocol.
131 // Third, we rebuild the URL.
132 m_url
= m_protoname
+ wxT(":");
133 if (m_protoinfo
->m_needhost
)
134 m_url
= m_url
+ wxT("//") + m_hostname
;
138 // We initialize specific variables.
139 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
143 m_error
= wxURL_NOERR
;
147 void wxURL::CleanData()
159 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
164 // --------------------------------------------------------------
165 // --------- wxURL urls decoders --------------------------------
166 // --------------------------------------------------------------
168 bool wxURL::PrepProto(wxString
& url
)
173 pos
= url
.Find(wxT(':'));
177 m_protoname
= url(0, pos
);
179 url
= url(pos
+1, url
.Length());
184 bool wxURL::PrepHost(wxString
& url
)
189 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
192 url
= url(2, url
.Length());
194 pos
= url
.Find(wxT('/'));
201 temp_url
= url(0, pos
);
202 url
= url(url
.Find(wxT('/')), url
.Length());
204 // Retrieve service number
205 pos2
= temp_url
.Find(wxT(':'), TRUE
);
206 if (pos2
!= -1 && pos2
< pos
)
208 m_servname
= temp_url(pos2
+1, pos
);
209 if (!m_servname
.IsNumber())
211 temp_url
= temp_url(0, pos2
);
214 // Retrieve user and password.
215 pos2
= temp_url
.Find(wxT('@'));
216 // Even if pos2 equals -1, this code is right.
217 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
220 m_password
= wxT("");
225 temp_url
= temp_url(0, pos2
);
226 pos2
= temp_url
.Find(wxT(':'));
231 m_user
= temp_url(0, pos2
);
232 m_password
= temp_url(pos2
+1, url
.Length());
237 bool wxURL::PrepPath(wxString
& url
)
239 if (url
.Length() != 0)
240 m_path
= ConvertToValidURI(url
);
246 bool wxURL::FetchProtocol()
248 wxProtoInfo
*info
= ms_protocols
;
252 if (m_protoname
== info
->m_protoname
)
254 if (m_servname
.IsNull())
255 m_servname
= info
->m_servname
;
258 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
266 // --------------------------------------------------------------
267 // --------- wxURL get ------------------------------------------
268 // --------------------------------------------------------------
270 wxInputStream
*wxURL::GetInputStream()
272 wxInputStream
*the_i_stream
= NULL
;
276 m_error
= wxURL_NOPROTO
;
280 m_error
= wxURL_NOERR
;
281 if (m_user
!= wxT(""))
283 m_protocol
->SetUser(m_user
);
284 m_protocol
->SetPassword(m_password
);
290 // m_protoinfo is NULL when we use a proxy
291 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
293 if (!addr
.Hostname(m_hostname
))
295 m_error
= wxURL_NOHOST
;
299 addr
.Service(m_servname
);
301 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
303 m_error
= wxURL_CONNERR
;
309 // When we use a proxy, we have to pass the whole URL to it.
311 the_i_stream
= m_protocol
->GetInputStream(m_url
);
313 the_i_stream
= m_protocol
->GetInputStream(m_path
);
317 m_error
= wxURL_PROTOERR
;
325 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
329 if ( ms_proxyDefault
)
331 ms_proxyDefault
->Close();
332 delete ms_proxyDefault
;
333 ms_proxyDefault
= NULL
;
338 wxString tmp_str
= url_proxy
;
339 int pos
= tmp_str
.Find(wxT(':'));
343 wxString hostname
= tmp_str(0, pos
),
344 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
347 if (!addr
.Hostname(hostname
))
349 if (!addr
.Service(port
))
353 // Finally, when all is right, we connect the new proxy.
354 ms_proxyDefault
->Close();
356 ms_proxyDefault
= new wxHTTP();
357 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
361 void wxURL::SetProxy(const wxString
& url_proxy
)
365 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
376 wxString hostname
, port
;
381 pos
= tmp_str
.Find(wxT(':'));
382 // This is an invalid proxy name.
386 hostname
= tmp_str(0, pos
);
387 port
= tmp_str(pos
, tmp_str
.Length()-pos
);
389 addr
.Hostname(hostname
);
392 // Finally, create the whole stuff.
393 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
395 m_proxy
= new wxHTTP();
396 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
404 #endif // wxUSE_SOCKETS
406 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
412 for (i
= 0; i
< uri
.Len(); i
++)
414 wxChar c
= uri
.GetChar(i
);
418 // GRG, Apr/2000: changed to "%20" instead of '+'
420 out_str
+= wxT("%20");
424 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
426 // - Alphanumeric characters are never escaped
427 // - Unreserved marks are never escaped
428 // - Delimiters must be escaped if they appear within a component
429 // but not if they are used to separate components. Here we have
430 // no clear way to distinguish between these two cases, so they
431 // are escaped unless they are passed in the 'delims' parameter
432 // (allowed delimiters).
434 static const wxChar marks
[] = wxT("-_.!~*()'");
436 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
438 hexa_code
.Printf(wxT("%%%02X"), c
);
439 out_str
+= hexa_code
;
451 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
456 while (i
< uri
.Len())
459 if (uri
[i
] == wxT('%'))
462 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
463 code
= (uri
[i
] - wxT('A') + 10) * 16;
465 code
= (uri
[i
] - wxT('0')) * 16;
468 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
469 code
+= (uri
[i
] - wxT('A')) + 10;
471 code
+= (uri
[i
] - wxT('0'));
474 new_uri
+= (wxChar
)code
;
483 // ----------------------------------------------------------------------
484 // A module which deletes the default proxy if we created it
485 // ----------------------------------------------------------------------
489 class wxURLModule
: public wxModule
492 virtual bool OnInit();
493 virtual void OnExit();
496 DECLARE_DYNAMIC_CLASS(wxURLModule
)
499 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
501 bool wxURLModule::OnInit()
503 // env var HTTP_PROXY contains the address of the default proxy to use if
504 // set, but don't try to create this proxy right now because it will slow
505 // down the program startup (especially if there is no DNS server
506 // available, in which case it may take up to 1 minute)
508 if ( getenv("HTTP_PROXY") )
510 wxURL::ms_useDefaultProxy
= TRUE
;
516 void wxURLModule::OnExit()
518 delete wxURL::ms_proxyDefault
;
519 wxURL::ms_proxyDefault
= NULL
;
522 #endif // wxUSE_SOCKETS