From b20317ca844258ea3ab954675a44130659066125 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 11 Nov 2021 22:47:12 -0500 Subject: [PATCH] Some minor cleanup of the proxy detect --- common/kicad_curl/kicad_curl_easy.cpp | 4 +- libs/kiplatform/msw/environment.cpp | 62 +++++++++++++++++++-------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/common/kicad_curl/kicad_curl_easy.cpp b/common/kicad_curl/kicad_curl_easy.cpp index ac7f16ae46..aad85e53ae 100644 --- a/common/kicad_curl/kicad_curl_easy.cpp +++ b/common/kicad_curl/kicad_curl_easy.cpp @@ -210,13 +210,13 @@ bool KICAD_CURL_EASY::SetURL( const std::string& aURL ) if( KIPLATFORM::ENV::GetSystemProxyConfig( aURL, cfg ) ) { curl_easy_setopt( m_CURL, CURLOPT_PROXY, static_cast( cfg.host.c_str() ) ); - if( cfg.username != "" ) + if( !cfg.username.empty() ) { curl_easy_setopt( m_CURL, CURLOPT_PROXYUSERNAME, static_cast( cfg.username.c_str() ) ); } - if( cfg.password != "" ) + if( !cfg.password.empty() ) { curl_easy_setopt( m_CURL, CURLOPT_PROXYPASSWORD, static_cast( cfg.password.c_str() ) ); diff --git a/libs/kiplatform/msw/environment.cpp b/libs/kiplatform/msw/environment.cpp index bc90b1f889..3859f61adc 100644 --- a/libs/kiplatform/msw/environment.cpp +++ b/libs/kiplatform/msw/environment.cpp @@ -122,11 +122,14 @@ wxString KIPLATFORM::ENV::GetUserCachePath() bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg ) { - bool autoProxy = false; + // Original source from Microsoft sample (public domain) + // https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/WinhttpProxy/cpp/GetProxy.cpp#L844 + bool autoProxyDetect = false; WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyConfig = { 0 }; WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions = { 0 }; WINHTTP_PROXY_INFO autoProxyInfo = { 0 }; - HINTERNET session = NULL; + HINTERNET session = NULL; + bool success = false; if( WinHttpGetIEProxyConfigForCurrentUser( &ieProxyConfig ) ) { @@ -134,21 +137,21 @@ bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& // we use the ie config simply to handle it off to the other win32 api if( ieProxyConfig.fAutoDetect ) { - autoProxy = true; + autoProxyDetect = true; } if( ieProxyConfig.lpszAutoConfigUrl != NULL ) { - autoProxy = true; + autoProxyDetect = true; autoProxyOptions.lpszAutoConfigUrl = ieProxyConfig.lpszAutoConfigUrl; } } else { - autoProxy = true; + autoProxyDetect = true; } - if( autoProxy ) + if( autoProxyDetect ) { // either we use the ie url or we set the auto detect mode if( autoProxyOptions.lpszAutoConfigUrl != NULL ) @@ -164,7 +167,7 @@ bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& autoProxyOptions.fAutoLogonIfChallenged = TRUE; - autoProxy = + autoProxyDetect = WinHttpGetProxyForUrl( session, aURL.c_str(), &autoProxyOptions, &autoProxyInfo ); if( session ) @@ -173,7 +176,7 @@ bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& } } - if( autoProxy ) + if( autoProxyDetect ) { if( autoProxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY ) { @@ -187,10 +190,8 @@ bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg.host = tokenizer.GetNextToken(); } - return true; + success = true; } - - return false; } else { @@ -202,14 +203,41 @@ bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& if(aCfg.host != ":" && aCfg.host != "::") { - return true; + success = true; } } - else - { - return false; - } } - return false; + // We have to clean up the strings the win32 api returned + if( autoProxyInfo.lpszProxy ) + { + GlobalFree( autoProxyInfo.lpszProxy ); + autoProxyInfo.lpszProxy = NULL; + } + + if( autoProxyInfo.lpszProxyBypass ) + { + GlobalFree( autoProxyInfo.lpszProxyBypass ); + autoProxyInfo.lpszProxyBypass = NULL; + } + + if( ieProxyConfig.lpszAutoConfigUrl != NULL ) + { + GlobalFree( ieProxyConfig.lpszAutoConfigUrl ); + ieProxyConfig.lpszAutoConfigUrl = NULL; + } + + if( ieProxyConfig.lpszProxy != NULL ) + { + GlobalFree( ieProxyConfig.lpszProxy ); + ieProxyConfig.lpszProxy = NULL; + } + + if( ieProxyConfig.lpszProxyBypass != NULL ) + { + GlobalFree( ieProxyConfig.lpszProxyBypass ); + ieProxyConfig.lpszProxyBypass = NULL; + } + + return success; }