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_resolver_winhttp.h"
      6 
      7 #include <windows.h>
      8 #include <winhttp.h>
      9 
     10 #include "base/histogram.h"
     11 #include "base/string_util.h"
     12 #include "googleurl/src/gurl.h"
     13 #include "net/base/net_errors.h"
     14 #include "net/proxy/proxy_info.h"
     15 
     16 #pragma comment(lib, "winhttp.lib")
     17 
     18 using base::TimeDelta;
     19 using base::TimeTicks;
     20 
     21 namespace net {
     22 
     23 // A small wrapper for histogramming purposes ;-)
     24 static BOOL CallWinHttpGetProxyForUrl(HINTERNET session, LPCWSTR url,
     25                                       WINHTTP_AUTOPROXY_OPTIONS* options,
     26                                       WINHTTP_PROXY_INFO* results) {
     27   TimeTicks time_start = TimeTicks::Now();
     28   BOOL rv = WinHttpGetProxyForUrl(session, url, options, results);
     29   TimeDelta time_delta = TimeTicks::Now() - time_start;
     30   // Record separately success and failure times since they will have very
     31   // different characteristics.
     32   if (rv) {
     33     UMA_HISTOGRAM_LONG_TIMES("Net.GetProxyForUrl_OK", time_delta);
     34   } else {
     35     UMA_HISTOGRAM_LONG_TIMES("Net.GetProxyForUrl_FAIL", time_delta);
     36   }
     37   return rv;
     38 }
     39 
     40 static void FreeInfo(WINHTTP_PROXY_INFO* info) {
     41   if (info->lpszProxy)
     42     GlobalFree(info->lpszProxy);
     43   if (info->lpszProxyBypass)
     44     GlobalFree(info->lpszProxyBypass);
     45 }
     46 
     47 ProxyResolverWinHttp::ProxyResolverWinHttp()
     48     : ProxyResolver(false /*expects_pac_bytes*/), session_handle_(NULL) {
     49 }
     50 
     51 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
     52   CloseWinHttpSession();
     53 }
     54 
     55 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url,
     56                                          ProxyInfo* results,
     57                                          CompletionCallback* /*callback*/,
     58                                          RequestHandle* /*request*/,
     59                                          LoadLog* /*load_log*/) {
     60   // If we don't have a WinHTTP session, then create a new one.
     61   if (!session_handle_ && !OpenWinHttpSession())
     62     return ERR_FAILED;
     63 
     64   // If we have been given an empty PAC url, then use auto-detection.
     65   //
     66   // NOTE: We just use DNS-based auto-detection here like Firefox.  We do this
     67   // to avoid WinHTTP's auto-detection code, which while more featureful (it
     68   // supports DHCP based auto-detection) also appears to have issues.
     69   //
     70   WINHTTP_AUTOPROXY_OPTIONS options = {0};
     71   options.fAutoLogonIfChallenged = FALSE;
     72   options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
     73   std::wstring pac_url_wide = ASCIIToWide(pac_url_.spec());
     74   options.lpszAutoConfigUrl = pac_url_wide.c_str();
     75 
     76   WINHTTP_PROXY_INFO info = {0};
     77   DCHECK(session_handle_);
     78 
     79   // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
     80   // necessary to first try resolving with fAutoLogonIfChallenged set to false.
     81   // Otherwise, we fail over to trying it with a value of true.  This way we
     82   // get good performance in the case where WinHTTP uses an out-of-process
     83   // resolver.  This is important for Vista and Win2k3.
     84   BOOL ok = CallWinHttpGetProxyForUrl(
     85       session_handle_, ASCIIToWide(query_url.spec()).c_str(), &options, &info);
     86   if (!ok) {
     87     if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
     88       options.fAutoLogonIfChallenged = TRUE;
     89       ok = CallWinHttpGetProxyForUrl(
     90           session_handle_, ASCIIToWide(query_url.spec()).c_str(),
     91           &options, &info);
     92     }
     93     if (!ok) {
     94       DWORD error = GetLastError();
     95       LOG(ERROR) << "WinHttpGetProxyForUrl failed: " << error;
     96       // If we got here because of RPC timeout during out of process PAC
     97       // resolution, no further requests on this session are going to work.
     98       if (ERROR_WINHTTP_TIMEOUT == error ||
     99           ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
    100         CloseWinHttpSession();
    101       }
    102       return ERR_FAILED;  // TODO(darin): Bug 1189288: translate error code.
    103     }
    104   }
    105 
    106   int rv = OK;
    107 
    108   switch (info.dwAccessType) {
    109     case WINHTTP_ACCESS_TYPE_NO_PROXY:
    110       results->UseDirect();
    111       break;
    112     case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
    113       // According to MSDN:
    114       //
    115       // The proxy server list contains one or more of the following strings
    116       // separated by semicolons or whitespace.
    117       //
    118       // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
    119       //
    120       // Based on this description, ProxyInfo::UseNamedProxy() isn't
    121       // going to handle all the variations (in particular <scheme>=).
    122       //
    123       // However in practice, it seems that WinHTTP is simply returning
    124       // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
    125       // proxy types, and stops the list when PAC encounters a "DIRECT".
    126       // So UseNamedProxy() should work OK.
    127       results->UseNamedProxy(WideToASCII(info.lpszProxy));
    128       break;
    129     default:
    130       NOTREACHED();
    131       rv = ERR_FAILED;
    132   }
    133 
    134   FreeInfo(&info);
    135   return rv;
    136 }
    137 
    138 void ProxyResolverWinHttp::CancelRequest(RequestHandle request) {
    139   // This is a synchronous ProxyResolver; no possibility for async requests.
    140   NOTREACHED();
    141 }
    142 
    143 int ProxyResolverWinHttp::SetPacScript(const GURL& pac_url,
    144                                        const std::string& /*pac_bytes*/,
    145                                        CompletionCallback* /*callback*/) {
    146   pac_url_ = pac_url.is_valid() ? pac_url : GURL("http://wpad/wpad.dat");
    147   return OK;
    148 }
    149 
    150 bool ProxyResolverWinHttp::OpenWinHttpSession() {
    151   DCHECK(!session_handle_);
    152   session_handle_ = WinHttpOpen(NULL,
    153                                 WINHTTP_ACCESS_TYPE_NO_PROXY,
    154                                 WINHTTP_NO_PROXY_NAME,
    155                                 WINHTTP_NO_PROXY_BYPASS,
    156                                 0);
    157   if (!session_handle_)
    158     return false;
    159 
    160   // Since this session handle will never be used for WinHTTP connections,
    161   // these timeouts don't really mean much individually.  However, WinHTTP's
    162   // out of process PAC resolution will use a combined (sum of all timeouts)
    163   // value to wait for an RPC reply.
    164   BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
    165   DCHECK(rv);
    166 
    167   return true;
    168 }
    169 
    170 void ProxyResolverWinHttp::CloseWinHttpSession() {
    171   if (session_handle_) {
    172     WinHttpCloseHandle(session_handle_);
    173     session_handle_ = NULL;
    174   }
    175 }
    176 
    177 }  // namespace net
    178