QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / proxy / proxy_resolver_winhttp.cc
blobbfdce33252ff972e0037bfe4052ef979abbeb0f0
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"
7 #include <windows.h>
8 #include <winhttp.h>
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "net/base/net_errors.h"
13 #include "net/proxy/proxy_info.h"
14 #include "net/proxy/proxy_resolver.h"
15 #include "url/gurl.h"
17 #pragma comment(lib, "winhttp.lib")
19 using base::TimeDelta;
20 using base::TimeTicks;
22 namespace net {
23 namespace {
25 static void FreeInfo(WINHTTP_PROXY_INFO* info) {
26 if (info->lpszProxy)
27 GlobalFree(info->lpszProxy);
28 if (info->lpszProxyBypass)
29 GlobalFree(info->lpszProxyBypass);
32 static Error WinHttpErrorToNetError(DWORD win_http_error) {
33 switch (win_http_error) {
34 case ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR:
35 case ERROR_WINHTTP_INTERNAL_ERROR:
36 case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
37 return ERR_FAILED;
38 case ERROR_WINHTTP_LOGIN_FAILURE:
39 return ERR_PROXY_AUTH_UNSUPPORTED;
40 case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT:
41 return ERR_PAC_SCRIPT_FAILED;
42 case ERROR_WINHTTP_INVALID_URL:
43 case ERROR_WINHTTP_OPERATION_CANCELLED:
44 case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT:
45 case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
46 return ERR_PAC_STATUS_NOT_OK;
47 case ERROR_NOT_ENOUGH_MEMORY:
48 return ERR_INSUFFICIENT_RESOURCES;
49 default:
50 return ERR_FAILED;
54 class ProxyResolverWinHttp : public ProxyResolver {
55 public:
56 ProxyResolverWinHttp(
57 const scoped_refptr<ProxyResolverScriptData>& script_data);
58 ~ProxyResolverWinHttp() override;
60 // ProxyResolver implementation:
61 int GetProxyForURL(const GURL& url,
62 ProxyInfo* results,
63 const CompletionCallback& /*callback*/,
64 RequestHandle* /*request*/,
65 const BoundNetLog& /*net_log*/) override;
66 void CancelRequest(RequestHandle request) override;
68 LoadState GetLoadState(RequestHandle request) const override;
70 private:
71 bool OpenWinHttpSession();
72 void CloseWinHttpSession();
74 // Proxy configuration is cached on the session handle.
75 HINTERNET session_handle_;
77 const GURL pac_url_;
79 DISALLOW_COPY_AND_ASSIGN(ProxyResolverWinHttp);
82 ProxyResolverWinHttp::ProxyResolverWinHttp(
83 const scoped_refptr<ProxyResolverScriptData>& script_data)
84 : session_handle_(NULL),
85 pac_url_(script_data->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
86 ? GURL("http://wpad/wpad.dat")
87 : script_data->url()) {
90 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
91 CloseWinHttpSession();
94 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url,
95 ProxyInfo* results,
96 const CompletionCallback& /*callback*/,
97 RequestHandle* /*request*/,
98 const BoundNetLog& /*net_log*/) {
99 // If we don't have a WinHTTP session, then create a new one.
100 if (!session_handle_ && !OpenWinHttpSession())
101 return ERR_FAILED;
103 // If we have been given an empty PAC url, then use auto-detection.
105 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this
106 // to avoid WinHTTP's auto-detection code, which while more featureful (it
107 // supports DHCP based auto-detection) also appears to have issues.
109 WINHTTP_AUTOPROXY_OPTIONS options = {0};
110 options.fAutoLogonIfChallenged = FALSE;
111 options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
112 base::string16 pac_url16 = base::ASCIIToUTF16(pac_url_.spec());
113 options.lpszAutoConfigUrl = pac_url16.c_str();
115 WINHTTP_PROXY_INFO info = {0};
116 DCHECK(session_handle_);
118 // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
119 // necessary to first try resolving with fAutoLogonIfChallenged set to false.
120 // Otherwise, we fail over to trying it with a value of true. This way we
121 // get good performance in the case where WinHTTP uses an out-of-process
122 // resolver. This is important for Vista and Win2k3.
123 BOOL ok = WinHttpGetProxyForUrl(session_handle_,
124 base::ASCIIToUTF16(query_url.spec()).c_str(),
125 &options, &info);
126 if (!ok) {
127 if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
128 options.fAutoLogonIfChallenged = TRUE;
129 ok = WinHttpGetProxyForUrl(
130 session_handle_, base::ASCIIToUTF16(query_url.spec()).c_str(),
131 &options, &info);
133 if (!ok) {
134 DWORD error = GetLastError();
135 // If we got here because of RPC timeout during out of process PAC
136 // resolution, no further requests on this session are going to work.
137 if (ERROR_WINHTTP_TIMEOUT == error ||
138 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
139 CloseWinHttpSession();
141 return WinHttpErrorToNetError(error);
145 int rv = OK;
147 switch (info.dwAccessType) {
148 case WINHTTP_ACCESS_TYPE_NO_PROXY:
149 results->UseDirect();
150 break;
151 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
152 // According to MSDN:
154 // The proxy server list contains one or more of the following strings
155 // separated by semicolons or whitespace.
157 // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
159 // Based on this description, ProxyInfo::UseNamedProxy() isn't
160 // going to handle all the variations (in particular <scheme>=).
162 // However in practice, it seems that WinHTTP is simply returning
163 // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
164 // proxy types, and stops the list when PAC encounters a "DIRECT".
165 // So UseNamedProxy() should work OK.
166 results->UseNamedProxy(base::UTF16ToASCII(info.lpszProxy));
167 break;
168 default:
169 NOTREACHED();
170 rv = ERR_FAILED;
173 FreeInfo(&info);
174 return rv;
177 void ProxyResolverWinHttp::CancelRequest(RequestHandle request) {
178 // This is a synchronous ProxyResolver; no possibility for async requests.
179 NOTREACHED();
182 LoadState ProxyResolverWinHttp::GetLoadState(RequestHandle request) const {
183 NOTREACHED();
184 return LOAD_STATE_IDLE;
187 bool ProxyResolverWinHttp::OpenWinHttpSession() {
188 DCHECK(!session_handle_);
189 session_handle_ = WinHttpOpen(NULL,
190 WINHTTP_ACCESS_TYPE_NO_PROXY,
191 WINHTTP_NO_PROXY_NAME,
192 WINHTTP_NO_PROXY_BYPASS,
194 if (!session_handle_)
195 return false;
197 // Since this session handle will never be used for WinHTTP connections,
198 // these timeouts don't really mean much individually. However, WinHTTP's
199 // out of process PAC resolution will use a combined (sum of all timeouts)
200 // value to wait for an RPC reply.
201 BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
202 DCHECK(rv);
204 return true;
207 void ProxyResolverWinHttp::CloseWinHttpSession() {
208 if (session_handle_) {
209 WinHttpCloseHandle(session_handle_);
210 session_handle_ = NULL;
214 } // namespace
216 ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp()
217 : ProxyResolverFactory(false /*expects_pac_bytes*/) {
220 int ProxyResolverFactoryWinHttp::CreateProxyResolver(
221 const scoped_refptr<ProxyResolverScriptData>& pac_script,
222 scoped_ptr<ProxyResolver>* resolver,
223 const CompletionCallback& callback,
224 scoped_ptr<Request>* request) {
225 resolver->reset(new ProxyResolverWinHttp(pac_script));
226 return OK;
229 } // namespace net