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