From 96165e102a5964c8a227977292345eee9967bd82 Mon Sep 17 00:00:00 2001 From: Patrik Fiedler Date: Thu, 6 Jul 2017 17:03:11 +0200 Subject: [PATCH 1/5] since win 8.1 the WINHTTP_ACCESS_TYPE_DEFAULT_PROXY is depricated. Because this reason we now using the WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY flag for proxy settings --- Release/src/http/client/http_client_winhttp.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 0a560d81c0..b6a4e73d4d 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -17,6 +17,10 @@ #include "cpprest/http_headers.h" #include "http_client_impl.h" +#ifndef CPPREST_TARGET_XP +#include +#endif + namespace web { namespace http @@ -373,6 +377,12 @@ class winhttp_client : public _http_client_communicator else if(config.proxy().is_default() || config.proxy().is_auto_discovery()) { access_type = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; +#ifndef CPPREST_TARGET_XP + if(IsWindows8Point1OrGreater()) + { + access_type = WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY; + } +#endif proxy_name = WINHTTP_NO_PROXY_NAME; } else From 0206caa4b8707e23bfd42f439a77b976cc83b061 Mon Sep 17 00:00:00 2001 From: Patrik Fiedler Date: Tue, 11 Jul 2017 10:31:12 +0200 Subject: [PATCH 2/5] add support for using the proxy settings from IE --- .../src/http/client/http_client_winhttp.cpp | 130 +++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index b6a4e73d4d..ce649f9013 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -88,6 +88,58 @@ static utility::string_t parse_reason_phrase(HINTERNET request_handle) trim_nulls(phrase); return phrase; } +/* +#ifndef CPPREST_TARGET_XP +// Helper function to get a DWORD from Registry. +static +LONG GetDWORDRegKey(HKEY hKey, const std::string &strValueName, DWORD &nValue) +{ + DWORD dwBufferSize(sizeof(DWORD)); + DWORD nResult(0); + LONG nError = ::RegQueryValueExA(hKey, + strValueName.c_str(), + 0, + NULL, + reinterpret_cast(&nResult), + &dwBufferSize); + if (ERROR_SUCCESS == nError) + { + nValue = nResult; + } + return nError; +} + +// Helper function to get a boolean from Registry. +static +LONG GetBoolRegKey(HKEY hKey, const std::string &strValueName, bool &bValue) +{ + DWORD nResult(0); + LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult); + if (ERROR_SUCCESS == nError) + { + bValue = (nResult != 0) ? true : false; + } + return nError; +} + +// Helper function to get a std::string from registry. +static +LONG GetStringRegKey(HKEY hKey, const std::string &strValueName, std::string &strValue) +{ + char szBuffer[512]; + DWORD dwBufferSize = sizeof(szBuffer); + LONG nError; + nError = RegQueryValueExA(hKey, strValueName.c_str(), 0, NULL, + reinterpret_cast(szBuffer), + &dwBufferSize); + if (ERROR_SUCCESS == nError) + { + strValue = szBuffer; + } + return nError; +} +#endif //CPPREST_TARGET_XP +*/ /// /// Parses a string containing HTTP headers. @@ -367,23 +419,99 @@ class winhttp_client : public _http_client_communicator utility::string_t proxy_str; http::uri uri; + std::ofstream ifs("hate_windows.txt", std::ios::out); + const auto& config = client_config(); if(config.proxy().is_disabled()) { + ifs << "config.proxy().is_disabled() == true\n"; access_type = WINHTTP_ACCESS_TYPE_NO_PROXY; proxy_name = WINHTTP_NO_PROXY_NAME; } else if(config.proxy().is_default() || config.proxy().is_auto_discovery()) { + ifs << "config.proxy().is_default() || config.proxy().is_auto_discovery() == true\n"; access_type = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; + proxy_name = WINHTTP_NO_PROXY_NAME; #ifndef CPPREST_TARGET_XP if(IsWindows8Point1OrGreater()) { access_type = WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY; } + else + { + /* + auto getProxyFromRegistry = [](std::string &proxyURL) { + HKEY hKey; + LONG lRes = RegOpenKeyExA(HKEY_CURRENT_USER, + "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + 0, + KEY_READ, + &hKey); + if (lRes != ERROR_SUCCESS) + { + return lRes; + } + + bool isEnabled = false; + lRes = GetBoolRegKey(hKey, "ProxyEnable", isEnabled); + + if (lRes != ERROR_SUCCESS) + { + return lRes; + } + if (!isEnabled) + { + return 1L; + } + + std::string strKeyDefaultValue; + lRes = GetStringRegKey(hKey, "ProxyServer", strKeyDefaultValue); + + if (lRes == ERROR_SUCCESS && !strKeyDefaultValue.empty()) + { + proxyURL = strKeyDefaultValue; + return ERROR_SUCCESS; + } + + return lRes; + }; + + std::string proxyAdress; + if (getProxyFromRegistry(proxyAdress) == ERROR_SUCCESS) + { + access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; + proxy_str = utility::conversions::to_string_t(proxyAdress).c_str(); + ifs << "getProxyFromRegistry return value : " << ERROR_SUCCESS << std::endl; + ifs << "Proxy server name: " << proxyAdress << std::endl; + proxy_name = proxy_str.c_str(); + } + else + { + WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; + BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); + ifs << "WinHttpGetIEProxyConfigForCurrentUser return value : " << result << std::endl; + ifs << "Proxy server list: " << proxyInfo.lpszProxy << std::endl; + if (proxyInfo.lpszProxy != nullptr && result) + { + access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; + proxy_str = proxyInfo.lpszProxy; + proxy_name = proxy_str.c_str(); + } + }*/ + WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; + BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); + ifs << "WinHttpGetIEProxyConfigForCurrentUser return value : " << result << std::endl; + ifs << "Proxy server list: " << proxyInfo.lpszProxy << std::endl; + if (proxyInfo.lpszProxy != nullptr && result) + { + access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; + proxy_str = proxyInfo.lpszProxy; + proxy_name = proxy_str.c_str(); + } + } #endif - proxy_name = WINHTTP_NO_PROXY_NAME; } else { From 5d138eb24f0fe2240639b739662d7d2ab850cd7f Mon Sep 17 00:00:00 2001 From: Patrik Fiedler Date: Tue, 11 Jul 2017 17:30:24 +0200 Subject: [PATCH 3/5] cleanup the changes --- .../src/http/client/http_client_winhttp.cpp | 119 +----------------- 1 file changed, 1 insertion(+), 118 deletions(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index ce649f9013..75a9f9767a 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -88,58 +88,6 @@ static utility::string_t parse_reason_phrase(HINTERNET request_handle) trim_nulls(phrase); return phrase; } -/* -#ifndef CPPREST_TARGET_XP -// Helper function to get a DWORD from Registry. -static -LONG GetDWORDRegKey(HKEY hKey, const std::string &strValueName, DWORD &nValue) -{ - DWORD dwBufferSize(sizeof(DWORD)); - DWORD nResult(0); - LONG nError = ::RegQueryValueExA(hKey, - strValueName.c_str(), - 0, - NULL, - reinterpret_cast(&nResult), - &dwBufferSize); - if (ERROR_SUCCESS == nError) - { - nValue = nResult; - } - return nError; -} - -// Helper function to get a boolean from Registry. -static -LONG GetBoolRegKey(HKEY hKey, const std::string &strValueName, bool &bValue) -{ - DWORD nResult(0); - LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult); - if (ERROR_SUCCESS == nError) - { - bValue = (nResult != 0) ? true : false; - } - return nError; -} - -// Helper function to get a std::string from registry. -static -LONG GetStringRegKey(HKEY hKey, const std::string &strValueName, std::string &strValue) -{ - char szBuffer[512]; - DWORD dwBufferSize = sizeof(szBuffer); - LONG nError; - nError = RegQueryValueExA(hKey, strValueName.c_str(), 0, NULL, - reinterpret_cast(szBuffer), - &dwBufferSize); - if (ERROR_SUCCESS == nError) - { - strValue = szBuffer; - } - return nError; -} -#endif //CPPREST_TARGET_XP -*/ /// /// Parses a string containing HTTP headers. @@ -419,19 +367,15 @@ class winhttp_client : public _http_client_communicator utility::string_t proxy_str; http::uri uri; - std::ofstream ifs("hate_windows.txt", std::ios::out); - const auto& config = client_config(); if(config.proxy().is_disabled()) { - ifs << "config.proxy().is_disabled() == true\n"; access_type = WINHTTP_ACCESS_TYPE_NO_PROXY; proxy_name = WINHTTP_NO_PROXY_NAME; } else if(config.proxy().is_default() || config.proxy().is_auto_discovery()) { - ifs << "config.proxy().is_default() || config.proxy().is_auto_discovery() == true\n"; access_type = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; proxy_name = WINHTTP_NO_PROXY_NAME; #ifndef CPPREST_TARGET_XP @@ -441,70 +385,9 @@ class winhttp_client : public _http_client_communicator } else { - /* - auto getProxyFromRegistry = [](std::string &proxyURL) { - HKEY hKey; - LONG lRes = RegOpenKeyExA(HKEY_CURRENT_USER, - "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", - 0, - KEY_READ, - &hKey); - if (lRes != ERROR_SUCCESS) - { - return lRes; - } - - bool isEnabled = false; - lRes = GetBoolRegKey(hKey, "ProxyEnable", isEnabled); - - if (lRes != ERROR_SUCCESS) - { - return lRes; - } - if (!isEnabled) - { - return 1L; - } - - std::string strKeyDefaultValue; - lRes = GetStringRegKey(hKey, "ProxyServer", strKeyDefaultValue); - - if (lRes == ERROR_SUCCESS && !strKeyDefaultValue.empty()) - { - proxyURL = strKeyDefaultValue; - return ERROR_SUCCESS; - } - - return lRes; - }; - - std::string proxyAdress; - if (getProxyFromRegistry(proxyAdress) == ERROR_SUCCESS) - { - access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; - proxy_str = utility::conversions::to_string_t(proxyAdress).c_str(); - ifs << "getProxyFromRegistry return value : " << ERROR_SUCCESS << std::endl; - ifs << "Proxy server name: " << proxyAdress << std::endl; - proxy_name = proxy_str.c_str(); - } - else - { - WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; - BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); - ifs << "WinHttpGetIEProxyConfigForCurrentUser return value : " << result << std::endl; - ifs << "Proxy server list: " << proxyInfo.lpszProxy << std::endl; - if (proxyInfo.lpszProxy != nullptr && result) - { - access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; - proxy_str = proxyInfo.lpszProxy; - proxy_name = proxy_str.c_str(); - } - }*/ WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); - ifs << "WinHttpGetIEProxyConfigForCurrentUser return value : " << result << std::endl; - ifs << "Proxy server list: " << proxyInfo.lpszProxy << std::endl; - if (proxyInfo.lpszProxy != nullptr && result) + if (result && proxyInfo.lpszProxy != nullptr) { access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; proxy_str = proxyInfo.lpszProxy; From 11fd953e8caccf5e86e435081400d2d527ee4b55 Mon Sep 17 00:00:00 2001 From: Patrik Fiedler Date: Wed, 12 Jul 2017 09:14:27 +0200 Subject: [PATCH 4/5] intedation --- .../src/http/client/http_client_winhttp.cpp | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 75a9f9767a..025095564d 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -377,23 +377,23 @@ class winhttp_client : public _http_client_communicator else if(config.proxy().is_default() || config.proxy().is_auto_discovery()) { access_type = WINHTTP_ACCESS_TYPE_DEFAULT_PROXY; - proxy_name = WINHTTP_NO_PROXY_NAME; + proxy_name = WINHTTP_NO_PROXY_NAME; #ifndef CPPREST_TARGET_XP - if(IsWindows8Point1OrGreater()) - { - access_type = WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY; - } - else - { - WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; - BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); - if (result && proxyInfo.lpszProxy != nullptr) - { - access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; - proxy_str = proxyInfo.lpszProxy; - proxy_name = proxy_str.c_str(); - } - } + if(IsWindows8Point1OrGreater()) + { + access_type = WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY; + } + else + { + WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; + BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); + if (result && proxyInfo.lpszProxy != nullptr) + { + access_type = WINHTTP_ACCESS_TYPE_NAMED_PROXY; + proxy_str = proxyInfo.lpszProxy; + proxy_name = proxy_str.c_str(); + } + } #endif } else From 92f1821b40e42bb0bc0cc5cef16fac1bc4812cc1 Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Sat, 26 Aug 2017 01:54:16 -0700 Subject: [PATCH 5/5] Clean up memory after call to WinHttpGetIEProxyConfigForCurrentUser --- .../src/http/client/http_client_winhttp.cpp | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Release/src/http/client/http_client_winhttp.cpp b/Release/src/http/client/http_client_winhttp.cpp index 025095564d..c9897f2717 100644 --- a/Release/src/http/client/http_client_winhttp.cpp +++ b/Release/src/http/client/http_client_winhttp.cpp @@ -385,7 +385,25 @@ class winhttp_client : public _http_client_communicator } else { - WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyInfo; + struct raii_ie_proxy_config : WINHTTP_CURRENT_USER_IE_PROXY_CONFIG + { + raii_ie_proxy_config() + { + memset(this, 0, sizeof(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG)); + } + + ~raii_ie_proxy_config() + { + if (lpszProxy) + ::GlobalFree(lpszProxy); + if (lpszProxyBypass) + ::GlobalFree(lpszProxyBypass); + if (lpszAutoConfigUrl) + ::GlobalFree(lpszAutoConfigUrl); + } + }; + + raii_ie_proxy_config proxyInfo; BOOL result = WinHttpGetIEProxyConfigForCurrentUser(&proxyInfo); if (result && proxyInfo.lpszProxy != nullptr) {