| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/config.h> |
| 21 |
|
| 22 |
#include <cstddef> |
| 23 |
#include <string_view> |
| 24 |
|
| 25 |
#include <cppuhelper/supportsservice.hxx> |
| 26 |
#include <com/sun/star/uno/XComponentContext.hpp> |
| 27 |
#include <o3tl/string_view.hxx> |
| 28 |
#include <rtl/ustrbuf.hxx> |
| 29 |
#include <sal/log.hxx> |
| 30 |
|
| 31 |
#include "wininetbackend.hxx" |
| 32 |
|
| 33 |
#if !defined WIN32_LEAN_AND_MEAN |
| 34 |
# define WIN32_LEAN_AND_MEAN |
| 35 |
#endif |
| 36 |
#include <windows.h> |
| 37 |
#include <wininet.h> |
| 38 |
#include <sal/alloca.h> |
| 39 |
|
| 40 |
#define WININET_DLL_NAME L"wininet.dll" |
| 41 |
#define EQUAL_SIGN '=' |
| 42 |
#define COLON ':' |
| 43 |
#define SPACE ' ' |
| 44 |
#define SEMI_COLON ';' |
| 45 |
|
| 46 |
namespace { |
| 47 |
|
| 48 |
struct Library { |
| 49 |
HMODULE module; |
| 50 |
|
| 51 |
explicit Library(HMODULE theModule): module(theModule) {} |
| 52 |
|
| 53 |
~Library() { if (module) FreeLibrary(module); } |
| 54 |
}; |
| 55 |
|
| 56 |
struct ProxyEntry |
| 57 |
{ |
| 58 |
OUString Server; |
| 59 |
OUString Port; |
| 60 |
}; |
| 61 |
|
| 62 |
ProxyEntry ReadProxyEntry(std::u16string_view aProxy, std::size_t& i) |
| 63 |
{ |
| 64 |
ProxyEntry aProxyEntry; |
| 65 |
|
| 66 |
aProxyEntry.Server = o3tl::getToken( aProxy, COLON, i ); |
| 67 |
if ( i != std::u16string_view::npos ) |
| 68 |
aProxyEntry.Port = o3tl::getToken( aProxy, COLON, i ); |
| 69 |
|
| 70 |
return aProxyEntry; |
| 71 |
} |
| 72 |
|
| 73 |
ProxyEntry FindProxyEntry(std::u16string_view aProxyList, std::u16string_view aType) |
| 74 |
{ |
| 75 |
std::size_t nIndex = 0; |
| 76 |
|
| 77 |
do |
| 78 |
{ |
| 79 |
// get the next token, e.g. ftp=server:port |
| 80 |
std::u16string_view nextToken = o3tl::getToken( aProxyList, SPACE, nIndex ); |
| 81 |
|
| 82 |
// split the next token again into the parts separated |
| 83 |
// through '=', e.g. ftp=server:port -> ftp and server:port |
| 84 |
std::size_t i = 0; |
| 85 |
if( nextToken.find( EQUAL_SIGN ) != std::u16string_view::npos ) |
| 86 |
{ |
| 87 |
if( aType == o3tl::getToken( nextToken, EQUAL_SIGN, i ) ) |
| 88 |
return ReadProxyEntry(nextToken, i); |
| 89 |
} |
| 90 |
else if( aType.empty()) |
| 91 |
return ReadProxyEntry(nextToken, i); |
| 92 |
|
| 93 |
} while ( nIndex != std::u16string_view::npos ); |
| 94 |
|
| 95 |
return ProxyEntry(); |
| 96 |
} |
| 97 |
|
| 98 |
} // unnamed namespace |
| 99 |
|
| 100 |
WinInetBackend::WinInetBackend() |
| 101 |
{ |
| 102 |
Library hWinInetDll( LoadLibraryW( WININET_DLL_NAME ) ); |
| 103 |
if( hWinInetDll.module ) |
| 104 |
{ |
| 105 |
typedef BOOL ( WINAPI *InternetQueryOption_Proc_T )( HINTERNET, DWORD, LPVOID, LPDWORD ); |
| 106 |
|
| 107 |
InternetQueryOption_Proc_T lpfnInternetQueryOption = |
| 108 |
reinterpret_cast< InternetQueryOption_Proc_T >( |
| 109 |
GetProcAddress( hWinInetDll.module, "InternetQueryOptionW" ) ); |
| 110 |
if (lpfnInternetQueryOption) |
| 111 |
{ |
| 112 |
// Some Windows versions would fail the InternetQueryOption call |
| 113 |
// with ERROR_OUTOFMEMORY when the initial dwLength were zero (and |
| 114 |
// are apparently fine with the initial sizeof (INTERNET_PROXY_INFO) |
| 115 |
// and need no reallocation), while other versions fail with |
| 116 |
// ERROR_INSUFFICIENT_BUFFER upon that initial dwLength and need a |
| 117 |
// reallocation: |
| 118 |
INTERNET_PROXY_INFO pi; |
| 119 |
LPINTERNET_PROXY_INFO lpi = π |
| 120 |
DWORD dwLength = sizeof (pi); |
| 121 |
bool ok = lpfnInternetQueryOption( |
| 122 |
nullptr, |
| 123 |
INTERNET_OPTION_PROXY, |
| 124 |
lpi, |
| 125 |
&dwLength ); |
| 126 |
if (!ok) |
| 127 |
{ |
| 128 |
DWORD err = GetLastError(); |
| 129 |
if (err == ERROR_INSUFFICIENT_BUFFER) |
| 130 |
{ |
| 131 |
// allocate sufficient space on the stack |
| 132 |
// insufficient space on the stack results |
| 133 |
// in a stack overflow exception, we assume |
| 134 |
// this never happens, because of the relatively |
| 135 |
// small amount of memory we need |
| 136 |
// alloca is nice because it is fast and we don't |
| 137 |
// have to free the allocated memory, it will be |
| 138 |
// automatically done |
| 139 |
lpi = static_cast< LPINTERNET_PROXY_INFO >( |
| 140 |
alloca( dwLength ) ); |
| 141 |
ok = lpfnInternetQueryOption( |
| 142 |
nullptr, |
| 143 |
INTERNET_OPTION_PROXY, |
| 144 |
lpi, |
| 145 |
&dwLength ); |
| 146 |
if (!ok) |
| 147 |
{ |
| 148 |
err = GetLastError(); |
| 149 |
} |
| 150 |
} |
| 151 |
if (!ok) |
| 152 |
{ |
| 153 |
SAL_WARN( |
| 154 |
"shell", |
| 155 |
"InternetQueryOption INTERNET_OPTION_PROXY" |
| 156 |
" GetLastError=" << err); |
| 157 |
return; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// if a proxy is disabled, InternetQueryOption returns |
| 162 |
// an empty proxy list, so we don't have to check if |
| 163 |
// proxy is enabled or not |
| 164 |
|
| 165 |
// We use InternetQueryOptionW (see https://msdn.microsoft.com/en-us/library/aa385101); |
| 166 |
// it fills INTERNET_PROXY_INFO struct which is defined in WinInet.h to have LPCTSTR |
| 167 |
// (i.e., the UNICODE-dependent generic string type expanding to const wchar_t* when |
| 168 |
// UNICODE is defined, and InternetQueryOption macro expands to InternetQueryOptionW). |
| 169 |
// Thus, it's natural to expect that W version would return wide strings. But it's not |
| 170 |
// true. The W version still returns const char* in INTERNET_PROXY_INFO. |
| 171 |
OUString aProxyList = OUString::createFromAscii( lpi->lpszProxy ); |
| 172 |
OUString aProxyBypassList = OUString::createFromAscii( lpi->lpszProxyBypass ); |
| 173 |
|
| 174 |
// override default for ProxyType, which is "0" meaning "No proxies". |
| 175 |
valueProxyType_.IsPresent = true; |
| 176 |
valueProxyType_.Value <<= sal_Int32(1); |
| 177 |
|
| 178 |
// fill proxy bypass list |
| 179 |
if( aProxyBypassList.getLength() > 0 ) |
| 180 |
{ |
| 181 |
OUStringBuffer aReverseList; |
| 182 |
sal_Int32 nIndex = 0; |
| 183 |
do |
| 184 |
{ |
| 185 |
OUString aToken = aProxyBypassList.getToken( 0, SPACE, nIndex ); |
| 186 |
if ( aProxyList.indexOf( aToken ) == -1 ) |
| 187 |
{ |
| 188 |
if ( aReverseList.getLength() ) |
| 189 |
{ |
| 190 |
aReverseList.insert( 0, sal_Unicode( SEMI_COLON ) ); |
| 191 |
aReverseList.insert( 0, aToken ); |
| 192 |
} |
| 193 |
else |
| 194 |
aReverseList = aToken; |
| 195 |
} |
| 196 |
} |
| 197 |
while ( nIndex >= 0 ); |
| 198 |
|
| 199 |
aProxyBypassList = aReverseList.makeStringAndClear(); |
| 200 |
|
| 201 |
valueNoProxy_.IsPresent = true; |
| 202 |
valueNoProxy_.Value <<= aProxyBypassList.replace( SPACE, SEMI_COLON ); |
| 203 |
} |
| 204 |
|
| 205 |
if( aProxyList.getLength() > 0 ) |
| 206 |
{ |
| 207 |
|
| 208 |
// this implementation follows the algorithm |
| 209 |
// of the internet explorer |
| 210 |
// if there are type-dependent proxy settings |
| 211 |
// and type independent proxy settings in the |
| 212 |
// registry the internet explorer chooses the |
| 213 |
// type independent proxy for all settings |
| 214 |
// e.g. imagine the following registry entry |
| 215 |
// ftp=server:port;http=server:port;server:port |
| 216 |
// the last token server:port is type independent |
| 217 |
// so the ie chooses this proxy server |
| 218 |
|
| 219 |
// if there is no port specified for a type independent |
| 220 |
// server the ie uses the port of an http server if |
| 221 |
// there is one and it has a port |
| 222 |
|
| 223 |
|
| 224 |
ProxyEntry aTypeIndepProxy = FindProxyEntry( aProxyList, u""); |
| 225 |
ProxyEntry aHttpProxy = FindProxyEntry( aProxyList, u"http" ); |
| 226 |
ProxyEntry aHttpsProxy = FindProxyEntry( aProxyList, u"https" ); |
| 227 |
|
| 228 |
if( aTypeIndepProxy.Server.getLength() ) |
| 229 |
{ |
| 230 |
aHttpProxy.Server = aTypeIndepProxy.Server; |
| 231 |
aHttpsProxy.Server = aTypeIndepProxy.Server; |
| 232 |
|
| 233 |
if( aTypeIndepProxy.Port.getLength() ) |
| 234 |
{ |
| 235 |
aHttpProxy.Port = aTypeIndepProxy.Port; |
| 236 |
aHttpsProxy.Port = aTypeIndepProxy.Port; |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
aHttpsProxy.Port = aHttpProxy.Port; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// http proxy name |
| 245 |
if( aHttpProxy.Server.getLength() > 0 ) |
| 246 |
{ |
| 247 |
valueHttpProxyName_.IsPresent = true; |
| 248 |
valueHttpProxyName_.Value <<= aHttpProxy.Server; |
| 249 |
} |
| 250 |
|
| 251 |
// http proxy port |
| 252 |
if( aHttpProxy.Port.getLength() > 0 ) |
| 253 |
{ |
| 254 |
valueHttpProxyPort_.IsPresent = true; |
| 255 |
valueHttpProxyPort_.Value <<= aHttpProxy.Port.toInt32(); |
| 256 |
} |
| 257 |
|
| 258 |
// https proxy name |
| 259 |
if( aHttpsProxy.Server.getLength() > 0 ) |
| 260 |
{ |
| 261 |
valueHttpsProxyName_.IsPresent = true; |
| 262 |
valueHttpsProxyName_.Value <<= aHttpsProxy.Server; |
| 263 |
} |
| 264 |
|
| 265 |
// https proxy port |
| 266 |
if( aHttpsProxy.Port.getLength() > 0 ) |
| 267 |
{ |
| 268 |
valueHttpsProxyPort_.IsPresent = true; |
| 269 |
valueHttpsProxyPort_.Value <<= aHttpsProxy.Port.toInt32(); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
WinInetBackend::~WinInetBackend() |
| 277 |
{ |
| 278 |
} |
| 279 |
|
| 280 |
void WinInetBackend::setPropertyValue( |
| 281 |
OUString const &, css::uno::Any const &) |
| 282 |
{ |
| 283 |
throw css::lang::IllegalArgumentException( |
| 284 |
"setPropertyValue not supported", |
| 285 |
getXWeak(), -1); |
| 286 |
} |
| 287 |
|
| 288 |
css::uno::Any WinInetBackend::getPropertyValue( |
| 289 |
OUString const & PropertyName) |
| 290 |
{ |
| 291 |
if (PropertyName == "ooInetHTTPProxyName") |
| 292 |
{ |
| 293 |
return css::uno::Any(valueHttpProxyName_); |
| 294 |
} else if ( PropertyName == "ooInetHTTPProxyPort" ) |
| 295 |
{ |
| 296 |
return css::uno::Any(valueHttpProxyPort_); |
| 297 |
} else if ( PropertyName == "ooInetHTTPSProxyName" ) |
| 298 |
{ |
| 299 |
return css::uno::Any(valueHttpsProxyName_); |
| 300 |
} else if ( PropertyName == "ooInetHTTPSProxyPort" ) |
| 301 |
{ |
| 302 |
return css::uno::Any(valueHttpsProxyPort_); |
| 303 |
} else if ( PropertyName == "ooInetNoProxy" ) |
| 304 |
{ |
| 305 |
return css::uno::Any(valueNoProxy_); |
| 306 |
} else if ( PropertyName == "ooInetProxyType" ) |
| 307 |
{ |
| 308 |
return css::uno::Any(valueProxyType_); |
| 309 |
} else { |
| 310 |
throw css::beans::UnknownPropertyException( |
| 311 |
PropertyName, getXWeak()); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
OUString SAL_CALL WinInetBackend::getImplementationName() |
| 316 |
{ |
| 317 |
return "com.sun.star.comp.configuration.backend.WinInetBackend" ; |
| 318 |
} |
| 319 |
|
| 320 |
sal_Bool SAL_CALL WinInetBackend::supportsService(const OUString& aServiceName) |
| 321 |
{ |
| 322 |
return cppu::supportsService(this, aServiceName); |
| 323 |
} |
| 324 |
|
| 325 |
uno::Sequence<OUString> SAL_CALL WinInetBackend::getSupportedServiceNames() |
| 326 |
{ |
| 327 |
return { "com.sun.star.configuration.backend.WinInetBackend" }; |
| 328 |
} |
| 329 |
|
| 330 |
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
| 331 |
shell_WinInetBackend_get_implementation( |
| 332 |
css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&) |
| 333 |
{ |
| 334 |
return cppu::acquire(new WinInetBackend); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |