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