]>
git.saurik.com Git - wxWidgets.git/blob - src/common/url.cpp
8ae14ec4e993c216490258ba92103743408f0a04
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"
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
;
66 if ( ms_useDefaultProxy
&& !ms_proxyDefault
)
68 SetDefaultProxy(getenv("HTTP_PROXY"));
70 if ( !ms_proxyDefault
)
73 ms_useDefaultProxy
= FALSE
;
77 m_useProxy
= ms_proxyDefault
!= NULL
;
78 m_proxy
= ms_proxyDefault
;
79 #endif // wxUSE_SOCKETS
84 bool wxURL::ParseURL()
86 wxString last_url
= m_url
;
88 // If the URL was already parsed (m_protocol != NULL), pass this section.
94 // Extract protocol name
95 if (!PrepProto(last_url
))
97 m_error
= wxURL_SNTXERR
;
101 // Find and create the protocol object
102 if (!FetchProtocol())
104 m_error
= wxURL_NOPROTO
;
108 // Do we need a host name ?
109 if (m_protoinfo
->m_needhost
)
112 if (!PrepHost(last_url
))
114 m_error
= wxURL_SNTXERR
;
120 if (!PrepPath(last_url
))
122 m_error
= wxURL_NOPATH
;
126 // URL parse finished.
131 // We destroy the newly created protocol.
134 // Third, we rebuild the URL.
135 m_url
= m_protoname
+ wxT(":");
136 if (m_protoinfo
->m_needhost
)
137 m_url
= m_url
+ wxT("//") + m_hostname
;
141 // We initialize specific variables.
142 m_protocol
= m_proxy
; // FIXME: we should clone the protocol
146 m_error
= wxURL_NOERR
;
150 void wxURL::CleanData()
162 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
167 // --------------------------------------------------------------
168 // --------- wxURL urls decoders --------------------------------
169 // --------------------------------------------------------------
171 bool wxURL::PrepProto(wxString
& url
)
176 pos
= url
.Find(wxT(':'));
180 m_protoname
= url(0, pos
);
182 url
= url(pos
+1, url
.Length());
187 bool wxURL::PrepHost(wxString
& url
)
192 if ((url
.GetChar(0) != wxT('/')) || (url
.GetChar(1) != wxT('/')))
195 url
= url(2, url
.Length());
197 pos
= url
.Find(wxT('/'));
204 temp_url
= url(0, pos
);
205 url
= url(url
.Find(wxT('/')), url
.Length());
207 // Retrieve service number
208 pos2
= temp_url
.Find(wxT(':'), TRUE
);
209 if (pos2
!= -1 && pos2
< pos
)
211 m_servname
= temp_url(pos2
+1, pos
);
212 if (!m_servname
.IsNumber())
214 temp_url
= temp_url(0, pos2
);
217 // Retrieve user and password.
218 pos2
= temp_url
.Find(wxT('@'));
219 // Even if pos2 equals -1, this code is right.
220 m_hostname
= temp_url(pos2
+1, temp_url
.Length());
223 m_password
= wxT("");
228 temp_url
= temp_url(0, pos2
);
229 pos2
= temp_url
.Find(wxT(':'));
234 m_user
= temp_url(0, pos2
);
235 m_password
= temp_url(pos2
+1, url
.Length());
240 bool wxURL::PrepPath(wxString
& url
)
242 if (url
.Length() != 0)
243 m_path
= ConvertToValidURI(url
);
249 bool wxURL::FetchProtocol()
251 wxProtoInfo
*info
= ms_protocols
;
255 if (m_protoname
== info
->m_protoname
)
257 if (m_servname
.IsNull())
258 m_servname
= info
->m_servname
;
261 m_protocol
= (wxProtocol
*)m_protoinfo
->m_cinfo
->CreateObject();
269 // --------------------------------------------------------------
270 // --------- wxURL get ------------------------------------------
271 // --------------------------------------------------------------
273 wxInputStream
*wxURL::GetInputStream()
275 wxInputStream
*the_i_stream
= NULL
;
279 m_error
= wxURL_NOPROTO
;
283 m_error
= wxURL_NOERR
;
284 if (m_user
!= wxT(""))
286 m_protocol
->SetUser(m_user
);
287 m_protocol
->SetPassword(m_password
);
293 // m_protoinfo is NULL when we use a proxy
294 if (!m_useProxy
&& m_protoinfo
->m_needhost
)
296 if (!addr
.Hostname(m_hostname
))
298 m_error
= wxURL_NOHOST
;
302 addr
.Service(m_servname
);
304 if (!m_protocol
->Connect(addr
, TRUE
)) // Watcom needs the 2nd arg for some reason
306 m_error
= wxURL_CONNERR
;
312 // When we use a proxy, we have to pass the whole URL to it.
314 the_i_stream
= m_protocol
->GetInputStream(m_url
);
316 the_i_stream
= m_protocol
->GetInputStream(m_path
);
320 m_error
= wxURL_PROTOERR
;
328 void wxURL::SetDefaultProxy(const wxString
& url_proxy
)
332 if ( ms_proxyDefault
)
334 ms_proxyDefault
->Close();
335 delete ms_proxyDefault
;
336 ms_proxyDefault
= NULL
;
341 wxString tmp_str
= url_proxy
;
342 int pos
= tmp_str
.Find(wxT(':'));
346 wxString hostname
= tmp_str(0, pos
),
347 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
350 if (!addr
.Hostname(hostname
))
352 if (!addr
.Service(port
))
356 // Finally, when all is right, we connect the new proxy.
357 ms_proxyDefault
->Close();
359 ms_proxyDefault
= new wxHTTP();
360 ms_proxyDefault
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
364 void wxURL::SetProxy(const wxString
& url_proxy
)
368 if ( m_proxy
&& m_proxy
!= ms_proxyDefault
)
379 wxString hostname
, port
;
384 pos
= tmp_str
.Find(wxT(':'));
385 // This is an invalid proxy name.
389 hostname
= tmp_str(0, pos
);
390 port
= tmp_str(pos
+1, tmp_str
.Length()-pos
);
392 addr
.Hostname(hostname
);
395 // Finally, create the whole stuff.
396 if (m_proxy
&& m_proxy
!= ms_proxyDefault
)
398 m_proxy
= new wxHTTP();
399 m_proxy
->Connect(addr
, TRUE
); // Watcom needs the 2nd arg for some reason
407 #endif // wxUSE_SOCKETS
409 wxString
wxURL::ConvertToValidURI(const wxString
& uri
, const wxChar
* delims
)
415 for (i
= 0; i
< uri
.Len(); i
++)
417 wxChar c
= uri
.GetChar(i
);
421 // GRG, Apr/2000: changed to "%20" instead of '+'
423 out_str
+= wxT("%20");
427 // GRG, Apr/2000: modified according to the URI definition (RFC 2396)
429 // - Alphanumeric characters are never escaped
430 // - Unreserved marks are never escaped
431 // - Delimiters must be escaped if they appear within a component
432 // but not if they are used to separate components. Here we have
433 // no clear way to distinguish between these two cases, so they
434 // are escaped unless they are passed in the 'delims' parameter
435 // (allowed delimiters).
437 static const wxChar marks
[] = wxT("-_.!~*()'");
439 if ( !wxIsalnum(c
) && !wxStrchr(marks
, c
) && !wxStrchr(delims
, c
) )
441 hexa_code
.Printf(wxT("%%%02X"), c
);
442 out_str
+= hexa_code
;
454 wxString
wxURL::ConvertFromURI(const wxString
& uri
)
459 while (i
< uri
.Len())
462 if (uri
[i
] == wxT('%'))
465 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
466 code
= (uri
[i
] - wxT('A') + 10) * 16;
468 code
= (uri
[i
] - wxT('0')) * 16;
471 if (uri
[i
] >= wxT('A') && uri
[i
] <= wxT('F'))
472 code
+= (uri
[i
] - wxT('A')) + 10;
474 code
+= (uri
[i
] - wxT('0'));
477 new_uri
+= (wxChar
)code
;
486 // ----------------------------------------------------------------------
487 // A module which deletes the default proxy if we created it
488 // ----------------------------------------------------------------------
492 class wxURLModule
: public wxModule
495 virtual bool OnInit();
496 virtual void OnExit();
499 DECLARE_DYNAMIC_CLASS(wxURLModule
)
502 IMPLEMENT_DYNAMIC_CLASS(wxURLModule
, wxModule
)
504 bool wxURLModule::OnInit()
506 // env var HTTP_PROXY contains the address of the default proxy to use if
507 // set, but don't try to create this proxy right now because it will slow
508 // down the program startup (especially if there is no DNS server
509 // available, in which case it may take up to 1 minute)
511 if ( getenv("HTTP_PROXY") )
513 wxURL::ms_useDefaultProxy
= TRUE
;
519 void wxURLModule::OnExit()
521 delete wxURL::ms_proxyDefault
;
522 wxURL::ms_proxyDefault
= NULL
;
525 #endif // wxUSE_SOCKETS