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.
5 #include "net/proxy/proxy_resolver_winhttp.h"
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"
17 #pragma comment(lib, "winhttp.lib")
19 using base::TimeDelta
;
20 using base::TimeTicks
;
24 static void FreeInfo(WINHTTP_PROXY_INFO
* info
) {
26 GlobalFree(info
->lpszProxy
);
27 if (info
->lpszProxyBypass
)
28 GlobalFree(info
->lpszProxyBypass
);
31 ProxyResolverWinHttp::ProxyResolverWinHttp()
32 : ProxyResolver(false /*expects_pac_bytes*/), session_handle_(NULL
) {
35 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
36 CloseWinHttpSession();
39 int ProxyResolverWinHttp::GetProxyForURL(const GURL
& query_url
,
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())
48 // If we have been given an empty PAC url, then use auto-detection.
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.
54 WINHTTP_AUTOPROXY_OPTIONS options
= {0};
55 options
.fAutoLogonIfChallenged
= FALSE
;
56 options
.dwFlags
= WINHTTP_AUTOPROXY_CONFIG_URL
;
57 base::string16 pac_url16
= base::ASCIIToUTF16(pac_url_
.spec());
58 options
.lpszAutoConfigUrl
= pac_url16
.c_str();
60 WINHTTP_PROXY_INFO info
= {0};
61 DCHECK(session_handle_
);
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(session_handle_
,
69 base::ASCIIToUTF16(query_url
.spec()).c_str(),
72 if (ERROR_WINHTTP_LOGIN_FAILURE
== GetLastError()) {
73 options
.fAutoLogonIfChallenged
= TRUE
;
74 ok
= WinHttpGetProxyForUrl(
75 session_handle_
, base::ASCIIToUTF16(query_url
.spec()).c_str(),
79 DWORD error
= GetLastError();
80 // If we got here because of RPC timeout during out of process PAC
81 // resolution, no further requests on this session are going to work.
82 if (ERROR_WINHTTP_TIMEOUT
== error
||
83 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR
== error
) {
84 CloseWinHttpSession();
86 return ERR_FAILED
; // TODO(darin): Bug 1189288: translate error code.
92 switch (info
.dwAccessType
) {
93 case WINHTTP_ACCESS_TYPE_NO_PROXY
:
96 case WINHTTP_ACCESS_TYPE_NAMED_PROXY
:
99 // The proxy server list contains one or more of the following strings
100 // separated by semicolons or whitespace.
102 // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
104 // Based on this description, ProxyInfo::UseNamedProxy() isn't
105 // going to handle all the variations (in particular <scheme>=).
107 // However in practice, it seems that WinHTTP is simply returning
108 // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
109 // proxy types, and stops the list when PAC encounters a "DIRECT".
110 // So UseNamedProxy() should work OK.
111 results
->UseNamedProxy(base::UTF16ToASCII(info
.lpszProxy
));
122 void ProxyResolverWinHttp::CancelRequest(RequestHandle request
) {
123 // This is a synchronous ProxyResolver; no possibility for async requests.
127 LoadState
ProxyResolverWinHttp::GetLoadState(RequestHandle request
) const {
129 return LOAD_STATE_IDLE
;
132 void ProxyResolverWinHttp::CancelSetPacScript() {
136 int ProxyResolverWinHttp::SetPacScript(
137 const scoped_refptr
<ProxyResolverScriptData
>& script_data
,
138 const CompletionCallback
& /*callback*/) {
139 if (script_data
->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
) {
140 pac_url_
= GURL("http://wpad/wpad.dat");
142 pac_url_
= script_data
->url();
147 bool ProxyResolverWinHttp::OpenWinHttpSession() {
148 DCHECK(!session_handle_
);
149 session_handle_
= WinHttpOpen(NULL
,
150 WINHTTP_ACCESS_TYPE_NO_PROXY
,
151 WINHTTP_NO_PROXY_NAME
,
152 WINHTTP_NO_PROXY_BYPASS
,
154 if (!session_handle_
)
157 // Since this session handle will never be used for WinHTTP connections,
158 // these timeouts don't really mean much individually. However, WinHTTP's
159 // out of process PAC resolution will use a combined (sum of all timeouts)
160 // value to wait for an RPC reply.
161 BOOL rv
= WinHttpSetTimeouts(session_handle_
, 10000, 10000, 5000, 5000);
167 void ProxyResolverWinHttp::CloseWinHttpSession() {
168 if (session_handle_
) {
169 WinHttpCloseHandle(session_handle_
);
170 session_handle_
= NULL
;