From 4011ba1d238306946c9110865a24fc86417e5171 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 7 Jul 2017 19:38:47 +0200 Subject: [PATCH 1/4] Don't use proxy in BingRequest sample if not defined Using empty proxy URI results in failing to open the connection when using WinHTTP, so the current code is broken out of the box. --- Release/samples/BingRequest/bingrequest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Release/samples/BingRequest/bingrequest.cpp b/Release/samples/BingRequest/bingrequest.cpp index faa0938652..387b2102fb 100644 --- a/Release/samples/BingRequest/bingrequest.cpp +++ b/Release/samples/BingRequest/bingrequest.cpp @@ -29,7 +29,7 @@ web::http::client::http_client_config client_config_for_proxy() wchar_t* pValue; size_t len; auto err = _wdupenv_s(&pValue, &len, L"http_proxy"); - if (!err) { + if (!err && pValue) { std::unique_ptr holder(pValue, [](wchar_t* p) { free(p); }); uri proxy_uri(std::wstring(pValue, len)); #else From 58d91e6e759ce1264590bcffcc293a22197b919b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 8 Jul 2017 13:19:47 +0200 Subject: [PATCH 2/4] Fix length of the proxy URI string in BingRequest sample The length returned by _wdupenv_s() includes the terminating NUL character, which isn't really part of the string. URI parsing seems to discard it anyhow currently (intentionally or not?), but including it breaks comparisons with any literal strings, for example, such as are done in the upcoming commit. --- Release/samples/BingRequest/bingrequest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Release/samples/BingRequest/bingrequest.cpp b/Release/samples/BingRequest/bingrequest.cpp index 387b2102fb..d7aad05417 100644 --- a/Release/samples/BingRequest/bingrequest.cpp +++ b/Release/samples/BingRequest/bingrequest.cpp @@ -29,9 +29,9 @@ web::http::client::http_client_config client_config_for_proxy() wchar_t* pValue; size_t len; auto err = _wdupenv_s(&pValue, &len, L"http_proxy"); - if (!err && pValue) { + if (!err && pValue && len) { std::unique_ptr holder(pValue, [](wchar_t* p) { free(p); }); - uri proxy_uri(std::wstring(pValue, len)); + uri proxy_uri(std::wstring(pValue, len - 1)); #else if(const char* env_http_proxy = std::getenv("http_proxy")) { uri proxy_uri(utility::conversions::to_string_t(env_http_proxy)); From 49775fe47c1fd9135baa352598d4bb9be67342a7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 8 Jul 2017 13:26:16 +0200 Subject: [PATCH 3/4] Support "auto" in "http_proxy" environment variable Allow easily testing proxy auto-discovery using this sample too. --- Release/samples/BingRequest/bingrequest.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Release/samples/BingRequest/bingrequest.cpp b/Release/samples/BingRequest/bingrequest.cpp index d7aad05417..066d210dd2 100644 --- a/Release/samples/BingRequest/bingrequest.cpp +++ b/Release/samples/BingRequest/bingrequest.cpp @@ -31,12 +31,21 @@ web::http::client::http_client_config client_config_for_proxy() auto err = _wdupenv_s(&pValue, &len, L"http_proxy"); if (!err && pValue && len) { std::unique_ptr holder(pValue, [](wchar_t* p) { free(p); }); - uri proxy_uri(std::wstring(pValue, len - 1)); + std::wstring env_http_proxy_string(pValue, len - 1); #else if(const char* env_http_proxy = std::getenv("http_proxy")) { - uri proxy_uri(utility::conversions::to_string_t(env_http_proxy)); + std::string env_http_proxy_string(env_http_proxy); #endif - web::web_proxy proxy(proxy_uri); + web::web_proxy proxy; + if (env_http_proxy_string != U("auto")) + { + uri proxy_uri(env_http_proxy_string); + proxy = std::move(web::web_proxy(proxy_uri)); + } + else + { + proxy = std::move(web::web_proxy(web::web_proxy::use_auto_discovery)); + } client_config.set_proxy(proxy); } From 065d0752b20492ba384b533e95a5ee5ac03f0eea Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Sat, 26 Aug 2017 02:45:08 -0700 Subject: [PATCH 4/4] Simplify PR #494 --- Release/samples/BingRequest/bingrequest.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/Release/samples/BingRequest/bingrequest.cpp b/Release/samples/BingRequest/bingrequest.cpp index 066d210dd2..726d6d194a 100644 --- a/Release/samples/BingRequest/bingrequest.cpp +++ b/Release/samples/BingRequest/bingrequest.cpp @@ -26,27 +26,22 @@ web::http::client::http_client_config client_config_for_proxy() { web::http::client::http_client_config client_config; #ifdef _WIN32 - wchar_t* pValue; - size_t len; + wchar_t* pValue = nullptr; + std::unique_ptr holder(nullptr, [](wchar_t* p) { free(p); }); + size_t len = 0; auto err = _wdupenv_s(&pValue, &len, L"http_proxy"); + if (pValue) + holder.reset(pValue); if (!err && pValue && len) { - std::unique_ptr holder(pValue, [](wchar_t* p) { free(p); }); std::wstring env_http_proxy_string(pValue, len - 1); #else if(const char* env_http_proxy = std::getenv("http_proxy")) { std::string env_http_proxy_string(env_http_proxy); #endif - web::web_proxy proxy; - if (env_http_proxy_string != U("auto")) - { - uri proxy_uri(env_http_proxy_string); - proxy = std::move(web::web_proxy(proxy_uri)); - } + if (env_http_proxy_string == U("auto")) + client_config.set_proxy(web::web_proxy::use_auto_discovery); else - { - proxy = std::move(web::web_proxy(web::web_proxy::use_auto_discovery)); - } - client_config.set_proxy(proxy); + client_config.set_proxy(web::web_proxy(env_http_proxy_string)); } return client_config;