8#if defined(_WIN32) || defined(_WIN64)
21bool get_proxy_configuration_win(
const std::string &target_url, std::string &proxy_str, std::string &err_msg) {
22 HINTERNET hSession = NULL;
24 std::wstring wtarget_url;
25 if (target_url.find(
"http://") == std::string::npos) {
26 wtarget_url =
L"http://";
28 wtarget_url += std::wstring(target_url.begin(), target_url.end());
30 std::stringstream info;
31 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_proxy;
32 WINHTTP_AUTOPROXY_OPTIONS proxy_options;
33 WINHTTP_PROXY_INFO proxy_info;
34 ZeroMemory(&proxy_options,
sizeof(proxy_options));
35 ZeroMemory(&ie_proxy,
sizeof(ie_proxy));
36 ZeroMemory(&proxy_info,
sizeof(proxy_info));
41 hSession = WinHttpOpen(L
"NetworkProxyWin FindingProxy/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
42 WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
45 info <<
"Failed to create the session (Error Code: " << GetLastError() <<
").";
49 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_proxy)) {
51 info <<
"Could not find the proxy settings (Error code :" << GetLastError();
55 if (ie_proxy.lpszProxy) {
58 proxy = ie_proxy.lpszProxy;
62 if (ie_proxy.fAutoDetect) {
64 proxy_options.dwFlags |= WINHTTP_AUTOPROXY_AUTO_DETECT;
65 proxy_options.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DNS_A;
68 if (ie_proxy.lpszAutoConfigUrl) {
70 proxy_options.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
71 proxy_options.lpszAutoConfigUrl = ie_proxy.lpszAutoConfigUrl;
74 if (!WinHttpGetProxyForUrl(hSession, wtarget_url.c_str(), &proxy_options, &proxy_info)) {
75 info <<
"Could not find the proxy for this url (Error code :" << GetLastError() <<
").";
81 if (proxy_info.dwAccessType == WINHTTP_ACCESS_TYPE_NO_PROXY) {
86 if (proxy_info.lpszProxy) {
88 proxy = proxy_info.lpszProxy;
96 if (ie_proxy.lpszAutoConfigUrl)
97 GlobalFree(ie_proxy.lpszAutoConfigUrl);
98 if (ie_proxy.lpszProxy)
99 GlobalFree(ie_proxy.lpszProxy);
100 if (ie_proxy.lpszProxyBypass)
101 GlobalFree(ie_proxy.lpszProxyBypass);
103 if (proxy_info.lpszProxyBypass)
104 GlobalFree(proxy_info.lpszProxyBypass);
105 if (proxy_info.lpszProxy)
106 GlobalFree(proxy_info.lpszProxy);
110 WinHttpCloseHandle(hSession);
113 err_msg = info.str();
116 proxy_str =
std::
string(proxy.begin(), proxy.end());
124NetworkProxy::NetworkProxy() : m_logger("network_proxy_logger_win") {}
134 std::string errmsg, proxy_option;
135 m_logger.
debug() <<
"Attempt to get the windows proxy configuration for this connection" << std::endl;
136 if (get_proxy_configuration_win(targetURLString, proxy_option, errmsg)) {
137 std::string proxyServer;
139 if (!proxy_option.empty()) {
140 size_t pos = proxy_option.rfind(
':');
141 if (pos != std::string::npos) {
142 if (pos == 4 || pos == 5)
144 proxyServer = proxy_option;
147 proxyServer = std::string(proxy_option.begin(), proxy_option.begin() + pos);
148 std::stringstream port_str;
149 port_str << std::string(proxy_option.begin() + pos + 1, proxy_option.end());
150 port_str >> proxyPort;
153 proxyServer = proxy_option;
157 proxyInfo = ProxyInfo(proxyServer, proxyPort,
true);
160 "information. It will attempt without proxy. "
161 << errmsg << std::endl;
void debug(const std::string &msg)
Logs at debug level.
void information(const std::string &msg)
Logs at information level.
ProxyInfo getHttpProxy(const std::string &targetURLString)
Get http proxy information.
Mantid::Kernel::Logger m_logger
Logger object.
Helper class which provides the Collimation Length for SANS instruments.