Home | History | Annotate | Download | only in proxy
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "net/proxy/proxy_config_service_win.h"
      6 
      7 #include <windows.h>
      8 #include <winhttp.h>
      9 
     10 #include "base/logging.h"
     11 #include "base/string_tokenizer.h"
     12 #include "base/string_util.h"
     13 #include "net/base/net_errors.h"
     14 #include "net/proxy/proxy_config.h"
     15 
     16 #pragma comment(lib, "winhttp.lib")
     17 
     18 namespace net {
     19 
     20 static void FreeIEConfig(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG* ie_config) {
     21   if (ie_config->lpszAutoConfigUrl)
     22     GlobalFree(ie_config->lpszAutoConfigUrl);
     23   if (ie_config->lpszProxy)
     24     GlobalFree(ie_config->lpszProxy);
     25   if (ie_config->lpszProxyBypass)
     26     GlobalFree(ie_config->lpszProxyBypass);
     27 }
     28 
     29 int ProxyConfigServiceWin::GetProxyConfig(ProxyConfig* config) {
     30   WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0};
     31   if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) {
     32     LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " <<
     33         GetLastError();
     34     return ERR_FAILED;  // TODO(darin): Bug 1189288: translate error code.
     35   }
     36   SetFromIEConfig(config, ie_config);
     37   FreeIEConfig(&ie_config);
     38   return OK;
     39 }
     40 
     41 // static
     42 void ProxyConfigServiceWin::SetFromIEConfig(
     43     ProxyConfig* config,
     44     const WINHTTP_CURRENT_USER_IE_PROXY_CONFIG& ie_config) {
     45   if (ie_config.fAutoDetect)
     46     config->auto_detect = true;
     47   if (ie_config.lpszProxy) {
     48     // lpszProxy may be a single proxy, or a proxy per scheme. The format
     49     // is compatible with ProxyConfig::ProxyRules's string format.
     50     config->proxy_rules.ParseFromString(WideToASCII(ie_config.lpszProxy));
     51   }
     52   if (ie_config.lpszProxyBypass) {
     53     std::string proxy_bypass = WideToASCII(ie_config.lpszProxyBypass);
     54 
     55     StringTokenizer proxy_server_bypass_list(proxy_bypass, "; \t\n\r");
     56     while (proxy_server_bypass_list.GetNext()) {
     57       std::string bypass_url_domain = proxy_server_bypass_list.token();
     58       if (bypass_url_domain == "<local>")
     59         config->proxy_bypass_local_names = true;
     60       else
     61         config->proxy_bypass.push_back(bypass_url_domain);
     62     }
     63   }
     64   if (ie_config.lpszAutoConfigUrl)
     65     config->pac_url = GURL(ie_config.lpszAutoConfigUrl);
     66 }
     67 
     68 }  // namespace net
     69