1*ac9096f4SAndrew Rist /**************************************************************
2*ac9096f4SAndrew Rist  *
3*ac9096f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ac9096f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ac9096f4SAndrew Rist  * distributed with this work for additional information
6*ac9096f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ac9096f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ac9096f4SAndrew Rist  * "License"); you may not use this file except in compliance
9*ac9096f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ac9096f4SAndrew Rist  *
11*ac9096f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ac9096f4SAndrew Rist  *
13*ac9096f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ac9096f4SAndrew Rist  * software distributed under the License is distributed on an
15*ac9096f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ac9096f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ac9096f4SAndrew Rist  * specific language governing permissions and limitations
18*ac9096f4SAndrew Rist  * under the License.
19*ac9096f4SAndrew Rist  *
20*ac9096f4SAndrew Rist  *************************************************************/
21*ac9096f4SAndrew Rist 
22*ac9096f4SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucbhelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir                                 TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  *************************************************************************/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <utility>
34cdf0e10cSrcweir #include <vector>
35cdf0e10cSrcweir #include <list>
36cdf0e10cSrcweir #include <osl/mutex.hxx>
37cdf0e10cSrcweir #include <rtl/ref.hxx>
38cdf0e10cSrcweir #include <osl/socket.hxx>
39cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/util/XChangesListener.hpp>
43cdf0e10cSrcweir #include <com/sun/star/util/XChangesNotifier.hpp>
44cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
45cdf0e10cSrcweir #include "ucbhelper/proxydecider.hxx"
46cdf0e10cSrcweir 
47cdf0e10cSrcweir using namespace com::sun::star;
48cdf0e10cSrcweir using namespace ucbhelper;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #define CONFIG_ROOT_KEY      "org.openoffice.Inet/Settings"
51cdf0e10cSrcweir #define PROXY_TYPE_KEY       "ooInetProxyType"
52cdf0e10cSrcweir #define NO_PROXY_LIST_KEY    "ooInetNoProxy"
53cdf0e10cSrcweir #define HTTP_PROXY_NAME_KEY  "ooInetHTTPProxyName"
54cdf0e10cSrcweir #define HTTP_PROXY_PORT_KEY  "ooInetHTTPProxyPort"
55cdf0e10cSrcweir #define HTTPS_PROXY_NAME_KEY "ooInetHTTPSProxyName"
56cdf0e10cSrcweir #define HTTPS_PROXY_PORT_KEY "ooInetHTTPSProxyPort"
57cdf0e10cSrcweir #define FTP_PROXY_NAME_KEY   "ooInetFTPProxyName"
58cdf0e10cSrcweir #define FTP_PROXY_PORT_KEY   "ooInetFTPProxyPort"
59cdf0e10cSrcweir 
60cdf0e10cSrcweir //=========================================================================
61cdf0e10cSrcweir namespace ucbhelper
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir //=========================================================================
65cdf0e10cSrcweir namespace proxydecider_impl
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 
68cdf0e10cSrcweir // A simple case ignoring wildcard matcher.
69cdf0e10cSrcweir class WildCard
70cdf0e10cSrcweir {
71cdf0e10cSrcweir private:
72cdf0e10cSrcweir     rtl::OString m_aWildString;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir public:
WildCard(const rtl::OUString & rWildCard)75cdf0e10cSrcweir     WildCard( const rtl::OUString& rWildCard )
76cdf0e10cSrcweir     : m_aWildString(
77cdf0e10cSrcweir         rtl::OUStringToOString(
78cdf0e10cSrcweir             rWildCard, RTL_TEXTENCODING_UTF8 ).toAsciiLowerCase() ) {}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     bool Matches( const rtl::OUString & rStr ) const;
81cdf0e10cSrcweir };
82cdf0e10cSrcweir 
83cdf0e10cSrcweir //=========================================================================
84cdf0e10cSrcweir typedef std::pair< WildCard, WildCard > NoProxyListEntry;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir //=========================================================================
87cdf0e10cSrcweir 
88cdf0e10cSrcweir class HostnameCache
89cdf0e10cSrcweir {
90cdf0e10cSrcweir     typedef std::pair< rtl::OUString, rtl::OUString > HostListEntry;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     std::list< HostListEntry >     m_aHostList;
93cdf0e10cSrcweir     sal_uInt32                     m_nCapacity;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir public:
HostnameCache(sal_uInt32 nCapacity)96cdf0e10cSrcweir     explicit HostnameCache( sal_uInt32 nCapacity )
97cdf0e10cSrcweir         : m_nCapacity( nCapacity ) {}
98cdf0e10cSrcweir 
get(const rtl::OUString & rKey,rtl::OUString & rValue) const99cdf0e10cSrcweir     bool get( const rtl::OUString & rKey, rtl::OUString & rValue ) const
100cdf0e10cSrcweir     {
101cdf0e10cSrcweir         std::list< HostListEntry >::const_iterator it
102cdf0e10cSrcweir             = m_aHostList.begin();
103cdf0e10cSrcweir         const std::list< HostListEntry >::const_iterator end
104cdf0e10cSrcweir             = m_aHostList.end();
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         while ( it != end )
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             if ( (*it).first == rKey )
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 rValue = (*it).second;
111cdf0e10cSrcweir                 return true;
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir             it++;
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir         return false;
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
put(const rtl::OUString & rKey,const rtl::OUString & rValue)118cdf0e10cSrcweir     void put( const rtl::OUString & rKey, const rtl::OUString & rValue )
119cdf0e10cSrcweir     {
120cdf0e10cSrcweir         if ( m_aHostList.size() == m_nCapacity )
121cdf0e10cSrcweir             m_aHostList.resize( m_nCapacity / 2 );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         m_aHostList.push_front( HostListEntry( rKey, rValue ) );
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir };
126cdf0e10cSrcweir 
127cdf0e10cSrcweir //=========================================================================
128cdf0e10cSrcweir class InternetProxyDecider_Impl :
129cdf0e10cSrcweir     public cppu::WeakImplHelper1< util::XChangesListener >
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     mutable osl::Mutex                       m_aMutex;
132cdf0e10cSrcweir     InternetProxyServer                      m_aHttpProxy;
133cdf0e10cSrcweir     InternetProxyServer                      m_aHttpsProxy;
134cdf0e10cSrcweir     InternetProxyServer                      m_aFtpProxy;
135cdf0e10cSrcweir     const InternetProxyServer                m_aEmptyProxy;
136cdf0e10cSrcweir     sal_Int32                                m_nProxyType;
137cdf0e10cSrcweir     uno::Reference< util::XChangesNotifier > m_xNotifier;
138cdf0e10cSrcweir     std::vector< NoProxyListEntry >          m_aNoProxyList;
139cdf0e10cSrcweir     mutable HostnameCache                    m_aHostnames;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir private:
142cdf0e10cSrcweir     bool shouldUseProxy( const rtl::OUString & rHost,
143cdf0e10cSrcweir                          sal_Int32 nPort,
144cdf0e10cSrcweir                          bool bUseFullyQualified ) const;
145cdf0e10cSrcweir public:
146cdf0e10cSrcweir     InternetProxyDecider_Impl(
147cdf0e10cSrcweir         const uno::Reference< lang::XMultiServiceFactory >& rxSMgr );
148cdf0e10cSrcweir     virtual ~InternetProxyDecider_Impl();
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     static rtl::Reference< InternetProxyDecider_Impl > createInstance(
151cdf0e10cSrcweir         const uno::Reference< lang::XMultiServiceFactory >& rxSMgr );
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     void dispose();
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     const InternetProxyServer & getProxy( const rtl::OUString & rProtocol,
156cdf0e10cSrcweir                                           const rtl::OUString & rHost,
157cdf0e10cSrcweir                                           sal_Int32 nPort ) const;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     // XChangesListener
160cdf0e10cSrcweir     virtual void SAL_CALL changesOccurred( const util::ChangesEvent& Event )
161cdf0e10cSrcweir         throw( uno::RuntimeException );
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     // XEventListener ( base of XChangesLisetenr )
164cdf0e10cSrcweir     virtual void SAL_CALL disposing( const lang::EventObject& Source )
165cdf0e10cSrcweir         throw( uno::RuntimeException );
166cdf0e10cSrcweir 
167cdf0e10cSrcweir private:
168cdf0e10cSrcweir     void setNoProxyList( const rtl::OUString & rNoProxyList );
169cdf0e10cSrcweir };
170cdf0e10cSrcweir 
171cdf0e10cSrcweir //=========================================================================
172cdf0e10cSrcweir //=========================================================================
173cdf0e10cSrcweir //
174cdf0e10cSrcweir // WildCard Implementation.
175cdf0e10cSrcweir //
176cdf0e10cSrcweir //=========================================================================
177cdf0e10cSrcweir //=========================================================================
178cdf0e10cSrcweir 
Matches(const rtl::OUString & rString) const179cdf0e10cSrcweir bool WildCard::Matches( const rtl::OUString& rString ) const
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     rtl::OString aString
182cdf0e10cSrcweir         = rtl::OUStringToOString(
183cdf0e10cSrcweir                     rString, RTL_TEXTENCODING_UTF8 ).toAsciiLowerCase();
184cdf0e10cSrcweir     const char * pStr  = aString.getStr();
185cdf0e10cSrcweir     const char * pWild = m_aWildString.getStr();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     int pos  = 0;
188cdf0e10cSrcweir     int flag = 0;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     while ( *pWild || flag )
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir         switch ( *pWild )
193cdf0e10cSrcweir         {
194cdf0e10cSrcweir             case '?':
195cdf0e10cSrcweir                 if ( *pStr == '\0' )
196cdf0e10cSrcweir                     return 0;
197cdf0e10cSrcweir                 break;
198cdf0e10cSrcweir 
199cdf0e10cSrcweir             default:
200cdf0e10cSrcweir                 if ( ( *pWild == '\\' ) && ( ( *( pWild + 1 ) == '?' )
201cdf0e10cSrcweir                                              || ( *( pWild + 1 ) == '*') ) )
202cdf0e10cSrcweir                     pWild++;
203cdf0e10cSrcweir                 if ( *pWild != *pStr )
204cdf0e10cSrcweir                     if ( !pos )
205cdf0e10cSrcweir                         return 0;
206cdf0e10cSrcweir                     else
207cdf0e10cSrcweir                         pWild += pos;
208cdf0e10cSrcweir                 else
209cdf0e10cSrcweir                     break;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir                 // Note: fall-thru's are intended!
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             case '*':
214cdf0e10cSrcweir                 while ( *pWild == '*' )
215cdf0e10cSrcweir                     pWild++;
216cdf0e10cSrcweir                 if ( *pWild == '\0' )
217cdf0e10cSrcweir                     return 1;
218cdf0e10cSrcweir                 flag = 1;
219cdf0e10cSrcweir                 pos  = 0;
220cdf0e10cSrcweir                 if ( *pStr == '\0' )
221cdf0e10cSrcweir                     return ( *pWild == '\0' );
222cdf0e10cSrcweir                 while ( *pStr && *pStr != *pWild )
223cdf0e10cSrcweir                 {
224cdf0e10cSrcweir                     if ( *pWild == '?' ) {
225cdf0e10cSrcweir                         pWild++;
226cdf0e10cSrcweir                         while ( *pWild == '*' )
227cdf0e10cSrcweir                             pWild++;
228cdf0e10cSrcweir                     }
229cdf0e10cSrcweir                     pStr++;
230cdf0e10cSrcweir                     if ( *pStr == '\0' )
231cdf0e10cSrcweir                         return ( *pWild == '\0' );
232cdf0e10cSrcweir                 }
233cdf0e10cSrcweir                 break;
234cdf0e10cSrcweir         }
235cdf0e10cSrcweir         if ( *pWild != '\0' )
236cdf0e10cSrcweir             pWild++;
237cdf0e10cSrcweir         if ( *pStr != '\0' )
238cdf0e10cSrcweir             pStr++;
239cdf0e10cSrcweir         else
240cdf0e10cSrcweir             flag = 0;
241cdf0e10cSrcweir         if ( flag )
242cdf0e10cSrcweir             pos--;
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir     return ( *pStr == '\0' ) && ( *pWild == '\0' );
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir //=========================================================================
getConfigStringValue(const uno::Reference<container::XNameAccess> & xNameAccess,const char * key,rtl::OUString & value)248cdf0e10cSrcweir bool getConfigStringValue(
249cdf0e10cSrcweir     const uno::Reference< container::XNameAccess > & xNameAccess,
250cdf0e10cSrcweir     const char * key,
251cdf0e10cSrcweir     rtl::OUString & value )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     try
254cdf0e10cSrcweir     {
255cdf0e10cSrcweir         if ( !( xNameAccess->getByName( rtl::OUString::createFromAscii( key ) )
256cdf0e10cSrcweir                 >>= value ) )
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             OSL_ENSURE( sal_False,
259cdf0e10cSrcweir                         "InternetProxyDecider - "
260cdf0e10cSrcweir                         "Error getting config item value!" );
261cdf0e10cSrcweir             return false;
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir     }
264cdf0e10cSrcweir     catch ( lang::WrappedTargetException const & )
265cdf0e10cSrcweir     {
266cdf0e10cSrcweir         return false;
267cdf0e10cSrcweir     }
268cdf0e10cSrcweir     catch ( container::NoSuchElementException const & )
269cdf0e10cSrcweir     {
270cdf0e10cSrcweir         return false;
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir     return true;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
275cdf0e10cSrcweir //=========================================================================
getConfigInt32Value(const uno::Reference<container::XNameAccess> & xNameAccess,const char * key,sal_Int32 & value)276cdf0e10cSrcweir bool getConfigInt32Value(
277cdf0e10cSrcweir     const uno::Reference< container::XNameAccess > & xNameAccess,
278cdf0e10cSrcweir     const char * key,
279cdf0e10cSrcweir     sal_Int32 & value )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir     try
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         uno::Any aValue = xNameAccess->getByName(
284cdf0e10cSrcweir             rtl::OUString::createFromAscii( key ) );
285cdf0e10cSrcweir         if ( aValue.hasValue() && !( aValue >>= value ) )
286cdf0e10cSrcweir         {
287cdf0e10cSrcweir             OSL_ENSURE( sal_False,
288cdf0e10cSrcweir                         "InternetProxyDecider - "
289cdf0e10cSrcweir                         "Error getting config item value!" );
290cdf0e10cSrcweir             return false;
291cdf0e10cSrcweir         }
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir     catch ( lang::WrappedTargetException const & )
294cdf0e10cSrcweir     {
295cdf0e10cSrcweir         return false;
296cdf0e10cSrcweir     }
297cdf0e10cSrcweir     catch ( container::NoSuchElementException const & )
298cdf0e10cSrcweir     {
299cdf0e10cSrcweir         return false;
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir     return true;
302cdf0e10cSrcweir }
303cdf0e10cSrcweir 
304cdf0e10cSrcweir //=========================================================================
305cdf0e10cSrcweir //=========================================================================
306cdf0e10cSrcweir //
307cdf0e10cSrcweir // InternetProxyDecider_Impl Implementation.
308cdf0e10cSrcweir //
309cdf0e10cSrcweir //=========================================================================
310cdf0e10cSrcweir //=========================================================================
311cdf0e10cSrcweir 
InternetProxyDecider_Impl(const uno::Reference<lang::XMultiServiceFactory> & rxSMgr)312cdf0e10cSrcweir InternetProxyDecider_Impl::InternetProxyDecider_Impl(
313cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory >& rxSMgr )
314cdf0e10cSrcweir     : m_nProxyType( 0 ),
315cdf0e10cSrcweir       m_aHostnames( 256 ) // cache size
316cdf0e10cSrcweir {
317cdf0e10cSrcweir     try
318cdf0e10cSrcweir     {
319cdf0e10cSrcweir         //////////////////////////////////////////////////////////////
320cdf0e10cSrcweir         // Read proxy configuration from config db.
321cdf0e10cSrcweir         //////////////////////////////////////////////////////////////
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         uno::Reference< lang::XMultiServiceFactory > xConfigProv(
324cdf0e10cSrcweir                 rxSMgr->createInstance(
325cdf0e10cSrcweir                     rtl::OUString::createFromAscii(
326cdf0e10cSrcweir                         "com.sun.star.configuration.ConfigurationProvider" ) ),
327cdf0e10cSrcweir                 uno::UNO_QUERY );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir         uno::Sequence< uno::Any > aArguments( 1 );
330cdf0e10cSrcweir         aArguments[ 0 ] <<= rtl::OUString::createFromAscii( CONFIG_ROOT_KEY );
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         uno::Reference< uno::XInterface > xInterface(
333cdf0e10cSrcweir                     xConfigProv->createInstanceWithArguments(
334cdf0e10cSrcweir                         rtl::OUString::createFromAscii(
335cdf0e10cSrcweir                             "com.sun.star.configuration.ConfigurationAccess" ),
336cdf0e10cSrcweir                     aArguments ) );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         OSL_ENSURE( xInterface.is(),
339cdf0e10cSrcweir                     "InternetProxyDecider - No config access!" );
340cdf0e10cSrcweir 
341cdf0e10cSrcweir         if ( xInterface.is() )
342cdf0e10cSrcweir         {
343cdf0e10cSrcweir             uno::Reference< container::XNameAccess > xNameAccess(
344cdf0e10cSrcweir                                             xInterface, uno::UNO_QUERY );
345cdf0e10cSrcweir             OSL_ENSURE( xNameAccess.is(),
346cdf0e10cSrcweir                         "InternetProxyDecider - No name access!" );
347cdf0e10cSrcweir 
348cdf0e10cSrcweir             if ( xNameAccess.is() )
349cdf0e10cSrcweir             {
350cdf0e10cSrcweir                 // *** Proxy type ***
351cdf0e10cSrcweir                 getConfigInt32Value(
352cdf0e10cSrcweir                     xNameAccess, PROXY_TYPE_KEY, m_nProxyType );
353cdf0e10cSrcweir 
354cdf0e10cSrcweir                 // *** No proxy list ***
355cdf0e10cSrcweir                 rtl::OUString aNoProxyList;
356cdf0e10cSrcweir                 getConfigStringValue(
357cdf0e10cSrcweir                     xNameAccess, NO_PROXY_LIST_KEY, aNoProxyList );
358cdf0e10cSrcweir                 setNoProxyList( aNoProxyList );
359cdf0e10cSrcweir 
360cdf0e10cSrcweir                 // *** HTTP ***
361cdf0e10cSrcweir                 getConfigStringValue(
362cdf0e10cSrcweir                     xNameAccess, HTTP_PROXY_NAME_KEY, m_aHttpProxy.aName );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir                 m_aHttpProxy.nPort = -1;
365cdf0e10cSrcweir                 getConfigInt32Value(
366cdf0e10cSrcweir                     xNameAccess, HTTP_PROXY_PORT_KEY, m_aHttpProxy.nPort );
367cdf0e10cSrcweir                 if ( m_aHttpProxy.nPort == -1 )
368cdf0e10cSrcweir                     m_aHttpProxy.nPort = 80; // standard HTTP port.
369cdf0e10cSrcweir 
370cdf0e10cSrcweir                 // *** HTTPS ***
371cdf0e10cSrcweir                 getConfigStringValue(
372cdf0e10cSrcweir                     xNameAccess, HTTPS_PROXY_NAME_KEY, m_aHttpsProxy.aName );
373cdf0e10cSrcweir 
374cdf0e10cSrcweir                 m_aHttpsProxy.nPort = -1;
375cdf0e10cSrcweir                 getConfigInt32Value(
376cdf0e10cSrcweir                     xNameAccess, HTTPS_PROXY_PORT_KEY, m_aHttpsProxy.nPort );
377cdf0e10cSrcweir                 if ( m_aHttpsProxy.nPort == -1 )
378cdf0e10cSrcweir                     m_aHttpsProxy.nPort = 443; // standard HTTPS port.
379cdf0e10cSrcweir 
380cdf0e10cSrcweir                 // *** FTP ***
381cdf0e10cSrcweir                 getConfigStringValue(
382cdf0e10cSrcweir                     xNameAccess, FTP_PROXY_NAME_KEY, m_aFtpProxy.aName );
383cdf0e10cSrcweir 
384cdf0e10cSrcweir                 m_aFtpProxy.nPort = -1;
385cdf0e10cSrcweir                 getConfigInt32Value(
386cdf0e10cSrcweir                     xNameAccess, FTP_PROXY_PORT_KEY, m_aFtpProxy.nPort );
387cdf0e10cSrcweir             }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir             // Register as listener for config changes.
390cdf0e10cSrcweir 
391cdf0e10cSrcweir             m_xNotifier = uno::Reference< util::XChangesNotifier >(
392cdf0e10cSrcweir                                                 xInterface, uno::UNO_QUERY );
393cdf0e10cSrcweir 
394cdf0e10cSrcweir             OSL_ENSURE( m_xNotifier.is(),
395cdf0e10cSrcweir                         "InternetProxyDecider - No notifier!" );
396cdf0e10cSrcweir 
397cdf0e10cSrcweir             if ( m_xNotifier.is() )
398cdf0e10cSrcweir                 m_xNotifier->addChangesListener( this );
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir     }
401cdf0e10cSrcweir     catch ( uno::Exception const & )
402cdf0e10cSrcweir     {
403cdf0e10cSrcweir         // createInstance, createInstanceWithArguments
404cdf0e10cSrcweir         OSL_ENSURE( sal_False, "InternetProxyDecider - Exception!" );
405cdf0e10cSrcweir     }
406cdf0e10cSrcweir }
407cdf0e10cSrcweir 
408cdf0e10cSrcweir //=========================================================================
409cdf0e10cSrcweir // virtual
~InternetProxyDecider_Impl()410cdf0e10cSrcweir InternetProxyDecider_Impl::~InternetProxyDecider_Impl()
411cdf0e10cSrcweir {
412cdf0e10cSrcweir }
413cdf0e10cSrcweir 
414cdf0e10cSrcweir //=========================================================================
dispose()415cdf0e10cSrcweir void InternetProxyDecider_Impl::dispose()
416cdf0e10cSrcweir {
417cdf0e10cSrcweir     uno::Reference< util::XChangesNotifier > xNotifier;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir     if ( m_xNotifier.is() )
420cdf0e10cSrcweir     {
421cdf0e10cSrcweir         osl::Guard< osl::Mutex > aGuard( m_aMutex );
422cdf0e10cSrcweir 
423cdf0e10cSrcweir         if ( m_xNotifier.is() )
424cdf0e10cSrcweir         {
425cdf0e10cSrcweir             xNotifier = m_xNotifier;
426cdf0e10cSrcweir             m_xNotifier.clear();
427cdf0e10cSrcweir         }
428cdf0e10cSrcweir     }
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     // Do this unguarded!
431cdf0e10cSrcweir     if ( xNotifier.is() )
432cdf0e10cSrcweir         xNotifier->removeChangesListener( this );
433cdf0e10cSrcweir }
434cdf0e10cSrcweir 
435cdf0e10cSrcweir //=========================================================================
shouldUseProxy(const rtl::OUString & rHost,sal_Int32 nPort,bool bUseFullyQualified) const436cdf0e10cSrcweir bool InternetProxyDecider_Impl::shouldUseProxy( const rtl::OUString & rHost,
437cdf0e10cSrcweir                                                 sal_Int32 nPort,
438cdf0e10cSrcweir                                                 bool bUseFullyQualified ) const
439cdf0e10cSrcweir {
440cdf0e10cSrcweir     rtl::OUStringBuffer aBuffer;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     if ( ( rHost.indexOf( ':' ) != -1 ) &&
443cdf0e10cSrcweir          ( rHost[ 0 ] != sal_Unicode( '[' ) ) )
444cdf0e10cSrcweir     {
445cdf0e10cSrcweir         // host is given as numeric IPv6 address
446cdf0e10cSrcweir         aBuffer.appendAscii( "[" );
447cdf0e10cSrcweir         aBuffer.append( rHost );
448cdf0e10cSrcweir         aBuffer.appendAscii( "]" );
449cdf0e10cSrcweir     }
450cdf0e10cSrcweir     else
451cdf0e10cSrcweir     {
452cdf0e10cSrcweir         // host is given either as numeric IPv4 address or non-numeric hostname
453cdf0e10cSrcweir         aBuffer.append( rHost );
454cdf0e10cSrcweir     }
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     aBuffer.append( sal_Unicode( ':' ) );
457cdf0e10cSrcweir     aBuffer.append( rtl::OUString::valueOf( nPort ) );
458cdf0e10cSrcweir     const rtl::OUString aHostAndPort( aBuffer.makeStringAndClear() );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     std::vector< NoProxyListEntry >::const_iterator it
461cdf0e10cSrcweir         = m_aNoProxyList.begin();
462cdf0e10cSrcweir     const std::vector< NoProxyListEntry >::const_iterator end
463cdf0e10cSrcweir         = m_aNoProxyList.end();
464cdf0e10cSrcweir 
465cdf0e10cSrcweir     while ( it != end )
466cdf0e10cSrcweir     {
467cdf0e10cSrcweir         if ( bUseFullyQualified )
468cdf0e10cSrcweir         {
469cdf0e10cSrcweir             if ( (*it).second.Matches( aHostAndPort ) )
470cdf0e10cSrcweir                 return false;
471cdf0e10cSrcweir         }
472cdf0e10cSrcweir         else
473cdf0e10cSrcweir         {
474cdf0e10cSrcweir             if ( (*it).first.Matches( aHostAndPort ) )
475cdf0e10cSrcweir                 return false;
476cdf0e10cSrcweir         }
477cdf0e10cSrcweir         it++;
478cdf0e10cSrcweir     }
479cdf0e10cSrcweir 
480cdf0e10cSrcweir     return true;
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483cdf0e10cSrcweir //=========================================================================
getProxy(const rtl::OUString & rProtocol,const rtl::OUString & rHost,sal_Int32 nPort) const484cdf0e10cSrcweir const InternetProxyServer & InternetProxyDecider_Impl::getProxy(
485cdf0e10cSrcweir                                             const rtl::OUString & rProtocol,
486cdf0e10cSrcweir                                             const rtl::OUString & rHost,
487cdf0e10cSrcweir                                             sal_Int32 nPort ) const
488cdf0e10cSrcweir {
489cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
490cdf0e10cSrcweir 
491cdf0e10cSrcweir     if ( m_nProxyType == 0 )
492cdf0e10cSrcweir     {
493cdf0e10cSrcweir         // Never use proxy.
494cdf0e10cSrcweir         return m_aEmptyProxy;
495cdf0e10cSrcweir     }
496cdf0e10cSrcweir 
497cdf0e10cSrcweir     if ( rHost.getLength() && m_aNoProxyList.size() )
498cdf0e10cSrcweir     {
499cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
500cdf0e10cSrcweir         // First, try direct hostname match - #110515#
501cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
502cdf0e10cSrcweir 
503cdf0e10cSrcweir         if ( !shouldUseProxy( rHost, nPort, false ) )
504cdf0e10cSrcweir             return m_aEmptyProxy;
505cdf0e10cSrcweir 
506cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
507cdf0e10cSrcweir         // Second, try match against full qualified hostname - #104401#
508cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
509cdf0e10cSrcweir 
510cdf0e10cSrcweir         rtl::OUString aHost;
511cdf0e10cSrcweir 
512cdf0e10cSrcweir         if ( ( rHost[ 0 ] == sal_Unicode( '[' ) ) &&
513cdf0e10cSrcweir              ( rHost.getLength() > 1 ) )
514cdf0e10cSrcweir         {
515cdf0e10cSrcweir             // host is given as numeric IPv6 address. name resolution
516cdf0e10cSrcweir             // functions need hostname without square brackets.
517cdf0e10cSrcweir             aHost = rHost.copy( 1, rHost.getLength() - 2 );
518cdf0e10cSrcweir         }
519cdf0e10cSrcweir         else
520cdf0e10cSrcweir         {
521cdf0e10cSrcweir             aHost = rHost;
522cdf0e10cSrcweir         }
523cdf0e10cSrcweir 
524cdf0e10cSrcweir         rtl::OUString aFullyQualifiedHost;
525cdf0e10cSrcweir         if ( !m_aHostnames.get( aHost, aFullyQualifiedHost ) )
526cdf0e10cSrcweir         {
527cdf0e10cSrcweir             // This might be quite expensive (DNS lookup).
528cdf0e10cSrcweir             const osl::SocketAddr aAddr( aHost, nPort );
529cdf0e10cSrcweir             aFullyQualifiedHost = aAddr.getHostname().toAsciiLowerCase();
530cdf0e10cSrcweir             m_aHostnames.put( aHost, aFullyQualifiedHost );
531cdf0e10cSrcweir         }
532cdf0e10cSrcweir 
533cdf0e10cSrcweir         // Error resolving name? -> fallback.
534cdf0e10cSrcweir         if ( !aFullyQualifiedHost.getLength() )
535cdf0e10cSrcweir             aFullyQualifiedHost = aHost;
536cdf0e10cSrcweir 
537cdf0e10cSrcweir         if ( aFullyQualifiedHost != aHost )
538cdf0e10cSrcweir         {
539cdf0e10cSrcweir             if ( !shouldUseProxy( aFullyQualifiedHost, nPort, false ) )
540cdf0e10cSrcweir                 return m_aEmptyProxy;
541cdf0e10cSrcweir         }
542cdf0e10cSrcweir 
543cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
544cdf0e10cSrcweir         // Third, try match of fully qualified entries in no-proxy list
545cdf0e10cSrcweir         // against full qualified hostname
546cdf0e10cSrcweir         //
547cdf0e10cSrcweir         // Example:
548cdf0e10cSrcweir         // list: staroffice-doc -> full: xyz.germany.sun.com
549cdf0e10cSrcweir         // in:   staroffice-doc.germany.sun.com -> full: xyz.germany.sun.com
550cdf0e10cSrcweir         //
551cdf0e10cSrcweir         //////////////////////////////////////////////////////////////////
552cdf0e10cSrcweir 
553cdf0e10cSrcweir         if ( !shouldUseProxy( aFullyQualifiedHost, nPort, true ) )
554cdf0e10cSrcweir             return m_aEmptyProxy;
555cdf0e10cSrcweir     }
556cdf0e10cSrcweir 
557cdf0e10cSrcweir     if ( rProtocol.toAsciiLowerCase()
558cdf0e10cSrcweir             .equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ftp" ) ) )
559cdf0e10cSrcweir     {
560cdf0e10cSrcweir         if ( m_aFtpProxy.aName.getLength() > 0 && m_aFtpProxy.nPort >= 0 )
561cdf0e10cSrcweir             return m_aFtpProxy;
562cdf0e10cSrcweir     }
563cdf0e10cSrcweir     else if ( rProtocol.toAsciiLowerCase()
564cdf0e10cSrcweir                   .equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "https" ) ) )
565cdf0e10cSrcweir     {
566cdf0e10cSrcweir         if ( m_aHttpsProxy.aName.getLength() )
567cdf0e10cSrcweir             return m_aHttpsProxy;
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir     else if ( m_aHttpProxy.aName.getLength() )
570cdf0e10cSrcweir     {
571cdf0e10cSrcweir         // All other protocols use the HTTP proxy.
572cdf0e10cSrcweir         return m_aHttpProxy;
573cdf0e10cSrcweir     }
574cdf0e10cSrcweir     return m_aEmptyProxy;
575cdf0e10cSrcweir }
576cdf0e10cSrcweir 
577cdf0e10cSrcweir //=========================================================================
578cdf0e10cSrcweir // virtual
changesOccurred(const util::ChangesEvent & Event)579cdf0e10cSrcweir void SAL_CALL InternetProxyDecider_Impl::changesOccurred(
580cdf0e10cSrcweir                                         const util::ChangesEvent& Event )
581cdf0e10cSrcweir     throw( uno::RuntimeException )
582cdf0e10cSrcweir {
583cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
584cdf0e10cSrcweir 
585cdf0e10cSrcweir     sal_Int32 nCount = Event.Changes.getLength();
586cdf0e10cSrcweir     if ( nCount )
587cdf0e10cSrcweir     {
588cdf0e10cSrcweir         const util::ElementChange* pElementChanges
589cdf0e10cSrcweir             = Event.Changes.getConstArray();
590cdf0e10cSrcweir         for ( sal_Int32 n = 0; n < nCount; ++n )
591cdf0e10cSrcweir         {
592cdf0e10cSrcweir             const util::ElementChange& rElem = pElementChanges[ n ];
593cdf0e10cSrcweir             rtl::OUString aKey;
594cdf0e10cSrcweir             if ( ( rElem.Accessor >>= aKey ) && aKey.getLength() )
595cdf0e10cSrcweir             {
596cdf0e10cSrcweir                 if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
597cdf0e10cSrcweir                                                     PROXY_TYPE_KEY ) ) )
598cdf0e10cSrcweir                 {
599cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_nProxyType ) )
600cdf0e10cSrcweir                     {
601cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
602cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
603cdf0e10cSrcweir                                     "Error getting config item value!" );
604cdf0e10cSrcweir                     }
605cdf0e10cSrcweir                 }
606cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
607cdf0e10cSrcweir                                                     NO_PROXY_LIST_KEY ) ) )
608cdf0e10cSrcweir                 {
609cdf0e10cSrcweir                     rtl::OUString aNoProxyList;
610cdf0e10cSrcweir                     if ( !( rElem.Element >>= aNoProxyList ) )
611cdf0e10cSrcweir                     {
612cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
613cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
614cdf0e10cSrcweir                                     "Error getting config item value!" );
615cdf0e10cSrcweir                     }
616cdf0e10cSrcweir 
617cdf0e10cSrcweir                     setNoProxyList( aNoProxyList );
618cdf0e10cSrcweir                 }
619cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
620cdf0e10cSrcweir                                                     HTTP_PROXY_NAME_KEY ) ) )
621cdf0e10cSrcweir                 {
622cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aHttpProxy.aName ) )
623cdf0e10cSrcweir                     {
624cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
625cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
626cdf0e10cSrcweir                                     "Error getting config item value!" );
627cdf0e10cSrcweir                     }
628cdf0e10cSrcweir                 }
629cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
630cdf0e10cSrcweir                                                     HTTP_PROXY_PORT_KEY ) ) )
631cdf0e10cSrcweir                 {
632cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aHttpProxy.nPort ) )
633cdf0e10cSrcweir                     {
634cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
635cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
636cdf0e10cSrcweir                                     "Error getting config item value!" );
637cdf0e10cSrcweir                     }
638cdf0e10cSrcweir 
639cdf0e10cSrcweir                     if ( m_aHttpProxy.nPort == -1 )
640cdf0e10cSrcweir                         m_aHttpProxy.nPort = 80; // standard HTTP port.
641cdf0e10cSrcweir                 }
642cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
643cdf0e10cSrcweir                                                     HTTPS_PROXY_NAME_KEY ) ) )
644cdf0e10cSrcweir                 {
645cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aHttpsProxy.aName ) )
646cdf0e10cSrcweir                     {
647cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
648cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
649cdf0e10cSrcweir                                     "Error getting config item value!" );
650cdf0e10cSrcweir                     }
651cdf0e10cSrcweir                 }
652cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
653cdf0e10cSrcweir                                                     HTTPS_PROXY_PORT_KEY ) ) )
654cdf0e10cSrcweir                 {
655cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aHttpsProxy.nPort ) )
656cdf0e10cSrcweir                     {
657cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
658cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
659cdf0e10cSrcweir                                     "Error getting config item value!" );
660cdf0e10cSrcweir                     }
661cdf0e10cSrcweir 
662cdf0e10cSrcweir                     if ( m_aHttpsProxy.nPort == -1 )
663cdf0e10cSrcweir                         m_aHttpsProxy.nPort = 443; // standard HTTPS port.
664cdf0e10cSrcweir                 }
665cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
666cdf0e10cSrcweir                                                     FTP_PROXY_NAME_KEY ) ) )
667cdf0e10cSrcweir                 {
668cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aFtpProxy.aName ) )
669cdf0e10cSrcweir                     {
670cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
671cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
672cdf0e10cSrcweir                                     "Error getting config item value!" );
673cdf0e10cSrcweir                     }
674cdf0e10cSrcweir                 }
675cdf0e10cSrcweir                 else if ( aKey.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
676cdf0e10cSrcweir                                                     FTP_PROXY_PORT_KEY ) ) )
677cdf0e10cSrcweir                 {
678cdf0e10cSrcweir                     if ( !( rElem.Element >>= m_aFtpProxy.nPort ) )
679cdf0e10cSrcweir                     {
680cdf0e10cSrcweir                         OSL_ENSURE( sal_False,
681cdf0e10cSrcweir                                     "InternetProxyDecider - changesOccurred - "
682cdf0e10cSrcweir                                     "Error getting config item value!" );
683cdf0e10cSrcweir                     }
684cdf0e10cSrcweir                 }
685cdf0e10cSrcweir             }
686cdf0e10cSrcweir         }
687cdf0e10cSrcweir     }
688cdf0e10cSrcweir }
689cdf0e10cSrcweir 
690cdf0e10cSrcweir //=========================================================================
691cdf0e10cSrcweir // virtual
disposing(const lang::EventObject &)692cdf0e10cSrcweir void SAL_CALL InternetProxyDecider_Impl::disposing(const lang::EventObject&)
693cdf0e10cSrcweir     throw( uno::RuntimeException )
694cdf0e10cSrcweir {
695cdf0e10cSrcweir     if ( m_xNotifier.is() )
696cdf0e10cSrcweir     {
697cdf0e10cSrcweir         osl::Guard< osl::Mutex > aGuard( m_aMutex );
698cdf0e10cSrcweir 
699cdf0e10cSrcweir         if ( m_xNotifier.is() )
700cdf0e10cSrcweir             m_xNotifier.clear();
701cdf0e10cSrcweir     }
702cdf0e10cSrcweir }
703cdf0e10cSrcweir 
704cdf0e10cSrcweir //=========================================================================
setNoProxyList(const rtl::OUString & rNoProxyList)705cdf0e10cSrcweir void InternetProxyDecider_Impl::setNoProxyList(
706cdf0e10cSrcweir                                         const rtl::OUString & rNoProxyList )
707cdf0e10cSrcweir {
708cdf0e10cSrcweir     osl::Guard< osl::Mutex > aGuard( m_aMutex );
709cdf0e10cSrcweir 
710cdf0e10cSrcweir     m_aNoProxyList.clear();
711cdf0e10cSrcweir 
712cdf0e10cSrcweir     if ( rNoProxyList.getLength() )
713cdf0e10cSrcweir     {
714cdf0e10cSrcweir         // List of connection endpoints hostname[:port],
715cdf0e10cSrcweir         // separated by semicolon. Wilcards allowed.
716cdf0e10cSrcweir 
717cdf0e10cSrcweir         sal_Int32 nPos = 0;
718cdf0e10cSrcweir         sal_Int32 nEnd = rNoProxyList.indexOf( ';' );
719cdf0e10cSrcweir         sal_Int32 nLen = rNoProxyList.getLength();
720cdf0e10cSrcweir 
721cdf0e10cSrcweir         do
722cdf0e10cSrcweir         {
723cdf0e10cSrcweir             if ( nEnd == -1 )
724cdf0e10cSrcweir                 nEnd = nLen;
725cdf0e10cSrcweir 
726cdf0e10cSrcweir             rtl::OUString aToken = rNoProxyList.copy( nPos, nEnd - nPos );
727cdf0e10cSrcweir 
728cdf0e10cSrcweir             if ( aToken.getLength() )
729cdf0e10cSrcweir             {
730cdf0e10cSrcweir                 rtl::OUString aServer;
731cdf0e10cSrcweir                 rtl::OUString aPort;
732cdf0e10cSrcweir 
733cdf0e10cSrcweir                 // numerical IPv6 address?
734cdf0e10cSrcweir                 bool bIPv6Address = false;
735cdf0e10cSrcweir                 sal_Int32 nClosedBracketPos = aToken.indexOf( ']' );
736cdf0e10cSrcweir                 if ( nClosedBracketPos == -1 )
737cdf0e10cSrcweir                     nClosedBracketPos = 0;
738cdf0e10cSrcweir                 else
739cdf0e10cSrcweir                     bIPv6Address = true;
740cdf0e10cSrcweir 
741cdf0e10cSrcweir                 sal_Int32 nColonPos = aToken.indexOf( ':', nClosedBracketPos );
742cdf0e10cSrcweir                 if ( nColonPos == -1 )
743cdf0e10cSrcweir                 {
744cdf0e10cSrcweir                     // No port given, server pattern equals current token
745cdf0e10cSrcweir                     aPort = rtl::OUString::createFromAscii( "*" );
746cdf0e10cSrcweir                     if ( aToken.indexOf( '*' ) == -1 )
747cdf0e10cSrcweir                     {
748cdf0e10cSrcweir                         // pattern describes exactly one server
749cdf0e10cSrcweir                         aServer = aToken;
750cdf0e10cSrcweir                     }
751cdf0e10cSrcweir 
752cdf0e10cSrcweir                     aToken += rtl::OUString::createFromAscii( ":*" );
753cdf0e10cSrcweir                 }
754cdf0e10cSrcweir                 else
755cdf0e10cSrcweir                 {
756cdf0e10cSrcweir                     // Port given, extract server pattern
757cdf0e10cSrcweir                     sal_Int32 nAsterixPos = aToken.indexOf( '*' );
758cdf0e10cSrcweir                     aPort = aToken.copy( nColonPos + 1 );
759cdf0e10cSrcweir                     if ( nAsterixPos < nColonPos )
760cdf0e10cSrcweir                     {
761cdf0e10cSrcweir                         // pattern describes exactly one server
762cdf0e10cSrcweir                         aServer = aToken.copy( 0, nColonPos );
763cdf0e10cSrcweir                     }
764cdf0e10cSrcweir                 }
765cdf0e10cSrcweir 
766cdf0e10cSrcweir                 rtl::OUStringBuffer aFullyQualifiedHost;
767cdf0e10cSrcweir                 if ( aServer.getLength() )
768cdf0e10cSrcweir                 {
769cdf0e10cSrcweir                     // Remember fully qualified server name if current list
770cdf0e10cSrcweir                     // entry specifies exactly one non-fully qualified server
771cdf0e10cSrcweir                     // name.
772cdf0e10cSrcweir 
773cdf0e10cSrcweir                     // remove square brackets from host name in case it's
774cdf0e10cSrcweir                     // a numerical IPv6 address.
775cdf0e10cSrcweir                     if ( bIPv6Address )
776cdf0e10cSrcweir                         aServer = aServer.copy( 1, aServer.getLength() - 2 );
777cdf0e10cSrcweir 
778cdf0e10cSrcweir                     // This might be quite expensive (DNS lookup).
779cdf0e10cSrcweir                     const osl::SocketAddr aAddr( aServer, 0 );
780cdf0e10cSrcweir                     rtl::OUString aTmp = aAddr.getHostname().toAsciiLowerCase();
781cdf0e10cSrcweir                     if ( aTmp != aServer.toAsciiLowerCase() )
782cdf0e10cSrcweir                     {
783cdf0e10cSrcweir                         if ( bIPv6Address )
784cdf0e10cSrcweir                         {
785cdf0e10cSrcweir                             aFullyQualifiedHost.appendAscii( "[" );
786cdf0e10cSrcweir                             aFullyQualifiedHost.append( aTmp );
787cdf0e10cSrcweir                             aFullyQualifiedHost.appendAscii( "]" );
788cdf0e10cSrcweir                         }
789cdf0e10cSrcweir                         else
790cdf0e10cSrcweir                         {
791cdf0e10cSrcweir                             aFullyQualifiedHost.append( aTmp );
792cdf0e10cSrcweir                         }
793cdf0e10cSrcweir                         aFullyQualifiedHost.appendAscii( ":" );
794cdf0e10cSrcweir                         aFullyQualifiedHost.append( aPort );
795cdf0e10cSrcweir                     }
796cdf0e10cSrcweir                 }
797cdf0e10cSrcweir 
798cdf0e10cSrcweir                 m_aNoProxyList.push_back(
799cdf0e10cSrcweir                     NoProxyListEntry( WildCard( aToken ),
800cdf0e10cSrcweir                                       WildCard(
801cdf0e10cSrcweir                                         aFullyQualifiedHost
802cdf0e10cSrcweir                                             .makeStringAndClear() ) ) );
803cdf0e10cSrcweir             }
804cdf0e10cSrcweir 
805cdf0e10cSrcweir             if ( nEnd != nLen )
806cdf0e10cSrcweir             {
807cdf0e10cSrcweir                 nPos = nEnd + 1;
808cdf0e10cSrcweir                 nEnd = rNoProxyList.indexOf( ';', nPos );
809cdf0e10cSrcweir             }
810cdf0e10cSrcweir         }
811cdf0e10cSrcweir         while ( nEnd != nLen );
812cdf0e10cSrcweir     }
813cdf0e10cSrcweir }
814cdf0e10cSrcweir 
815cdf0e10cSrcweir } // namespace proxydecider_impl
816cdf0e10cSrcweir 
817cdf0e10cSrcweir //=========================================================================
818cdf0e10cSrcweir //=========================================================================
819cdf0e10cSrcweir //
820cdf0e10cSrcweir // InternetProxyDecider Implementation.
821cdf0e10cSrcweir //
822cdf0e10cSrcweir //=========================================================================
823cdf0e10cSrcweir //=========================================================================
824cdf0e10cSrcweir 
InternetProxyDecider(const uno::Reference<lang::XMultiServiceFactory> & rxSMgr)825cdf0e10cSrcweir InternetProxyDecider::InternetProxyDecider(
826cdf0e10cSrcweir     const uno::Reference< lang::XMultiServiceFactory >& rxSMgr )
827cdf0e10cSrcweir : m_pImpl( new proxydecider_impl::InternetProxyDecider_Impl( rxSMgr ) )
828cdf0e10cSrcweir {
829cdf0e10cSrcweir     m_pImpl->acquire();
830cdf0e10cSrcweir }
831cdf0e10cSrcweir 
832cdf0e10cSrcweir //=========================================================================
~InternetProxyDecider()833cdf0e10cSrcweir InternetProxyDecider::~InternetProxyDecider()
834cdf0e10cSrcweir {
835cdf0e10cSrcweir     // Break circular reference between config listener and notifier.
836cdf0e10cSrcweir     m_pImpl->dispose();
837cdf0e10cSrcweir 
838cdf0e10cSrcweir     // Let him go...
839cdf0e10cSrcweir     m_pImpl->release();
840cdf0e10cSrcweir }
841cdf0e10cSrcweir 
842cdf0e10cSrcweir //=========================================================================
shouldUseProxy(const rtl::OUString & rProtocol,const rtl::OUString & rHost,sal_Int32 nPort) const843cdf0e10cSrcweir bool InternetProxyDecider::shouldUseProxy( const rtl::OUString & rProtocol,
844cdf0e10cSrcweir                                            const rtl::OUString & rHost,
845cdf0e10cSrcweir                                            sal_Int32 nPort ) const
846cdf0e10cSrcweir {
847cdf0e10cSrcweir     const InternetProxyServer & rData = m_pImpl->getProxy( rProtocol,
848cdf0e10cSrcweir                                                            rHost,
849cdf0e10cSrcweir                                                            nPort );
850cdf0e10cSrcweir     return ( rData.aName.getLength() > 0 );
851cdf0e10cSrcweir }
852cdf0e10cSrcweir 
853cdf0e10cSrcweir //=========================================================================
getProxy(const rtl::OUString & rProtocol,const rtl::OUString & rHost,sal_Int32 nPort) const854cdf0e10cSrcweir const InternetProxyServer & InternetProxyDecider::getProxy(
855cdf0e10cSrcweir                                             const rtl::OUString & rProtocol,
856cdf0e10cSrcweir                                             const rtl::OUString & rHost,
857cdf0e10cSrcweir                                             sal_Int32 nPort ) const
858cdf0e10cSrcweir {
859cdf0e10cSrcweir     return m_pImpl->getProxy( rProtocol, rHost, nPort );
860cdf0e10cSrcweir }
861cdf0e10cSrcweir 
862cdf0e10cSrcweir } // namespace ucbhelper
863