Fix broken path in extensions/common/PRESUBMIT.py
[chromium-blink-merge.git] / net / proxy / proxy_resolver_winhttp.cc
blobf7193d16a5f2ec4865caa8d934820a42742007e6
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/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 "net/proxy/proxy_resolver.h"
16 #include "url/gurl.h"
18 #pragma comment(lib, "winhttp.lib")
20 using base::TimeDelta;
21 using base::TimeTicks;
23 namespace net {
24 namespace {
26 static void FreeInfo(WINHTTP_PROXY_INFO* info) {
27 if (info->lpszProxy)
28 GlobalFree(info->lpszProxy);
29 if (info->lpszProxyBypass)
30 GlobalFree(info->lpszProxyBypass);
33 class ProxyResolverWinHttp : public ProxyResolver {
34 public:
35 ProxyResolverWinHttp(
36 const scoped_refptr<ProxyResolverScriptData>& script_data);
37 ~ProxyResolverWinHttp() override;
39 // ProxyResolver implementation:
40 int GetProxyForURL(const GURL& url,
41 ProxyInfo* results,
42 const CompletionCallback& /*callback*/,
43 RequestHandle* /*request*/,
44 const BoundNetLog& /*net_log*/) override;
45 void CancelRequest(RequestHandle request) override;
47 LoadState GetLoadState(RequestHandle request) const override;
49 void CancelSetPacScript() override;
51 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data,
52 const CompletionCallback& /*callback*/) override;
54 private:
55 bool OpenWinHttpSession();
56 void CloseWinHttpSession();
58 // Proxy configuration is cached on the session handle.
59 HINTERNET session_handle_;
61 const GURL pac_url_;
63 DISALLOW_COPY_AND_ASSIGN(ProxyResolverWinHttp);
66 ProxyResolverWinHttp::ProxyResolverWinHttp(
67 const scoped_refptr<ProxyResolverScriptData>& script_data)
68 : ProxyResolver(false /*expects_pac_bytes*/),
69 session_handle_(NULL),
70 pac_url_(script_data->type() == ProxyResolverScriptData::TYPE_AUTO_DETECT
71 ? GURL("http://wpad/wpad.dat")
72 : script_data->url()) {
75 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
76 CloseWinHttpSession();
79 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url,
80 ProxyInfo* results,
81 const CompletionCallback& /*callback*/,
82 RequestHandle* /*request*/,
83 const BoundNetLog& /*net_log*/) {
84 // If we don't have a WinHTTP session, then create a new one.
85 if (!session_handle_ && !OpenWinHttpSession())
86 return ERR_FAILED;
88 // If we have been given an empty PAC url, then use auto-detection.
90 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this
91 // to avoid WinHTTP's auto-detection code, which while more featureful (it
92 // supports DHCP based auto-detection) also appears to have issues.
94 WINHTTP_AUTOPROXY_OPTIONS options = {0};
95 options.fAutoLogonIfChallenged = FALSE;
96 options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
97 base::string16 pac_url16 = base::ASCIIToUTF16(pac_url_.spec());
98 options.lpszAutoConfigUrl = pac_url16.c_str();
100 WINHTTP_PROXY_INFO info = {0};
101 DCHECK(session_handle_);
103 // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
104 // necessary to first try resolving with fAutoLogonIfChallenged set to false.
105 // Otherwise, we fail over to trying it with a value of true. This way we
106 // get good performance in the case where WinHTTP uses an out-of-process
107 // resolver. This is important for Vista and Win2k3.
108 BOOL ok = WinHttpGetProxyForUrl(session_handle_,
109 base::ASCIIToUTF16(query_url.spec()).c_str(),
110 &options, &info);
111 if (!ok) {
112 if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
113 options.fAutoLogonIfChallenged = TRUE;
114 ok = WinHttpGetProxyForUrl(
115 session_handle_, base::ASCIIToUTF16(query_url.spec()).c_str(),
116 &options, &info);
118 if (!ok) {
119 DWORD error = GetLastError();
120 // If we got here because of RPC timeout during out of process PAC
121 // resolution, no further requests on this session are going to work.
122 if (ERROR_WINHTTP_TIMEOUT == error ||
123 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
124 CloseWinHttpSession();
126 return ERR_FAILED; // TODO(darin): Bug 1189288: translate error code.
130 int rv = OK;
132 switch (info.dwAccessType) {
133 case WINHTTP_ACCESS_TYPE_NO_PROXY:
134 results->UseDirect();
135 break;
136 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
137 // According to MSDN:
139 // The proxy server list contains one or more of the following strings
140 // separated by semicolons or whitespace.
142 // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
144 // Based on this description, ProxyInfo::UseNamedProxy() isn't
145 // going to handle all the variations (in particular <scheme>=).
147 // However in practice, it seems that WinHTTP is simply returning
148 // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
149 // proxy types, and stops the list when PAC encounters a "DIRECT".
150 // So UseNamedProxy() should work OK.
151 results->UseNamedProxy(base::UTF16ToASCII(info.lpszProxy));
152 break;
153 default:
154 NOTREACHED();
155 rv = ERR_FAILED;
158 FreeInfo(&info);
159 return rv;
162 void ProxyResolverWinHttp::CancelRequest(RequestHandle request) {
163 // This is a synchronous ProxyResolver; no possibility for async requests.
164 NOTREACHED();
167 LoadState ProxyResolverWinHttp::GetLoadState(RequestHandle request) const {
168 NOTREACHED();
169 return LOAD_STATE_IDLE;
172 void ProxyResolverWinHttp::CancelSetPacScript() {
173 NOTREACHED();
176 int ProxyResolverWinHttp::SetPacScript(
177 const scoped_refptr<ProxyResolverScriptData>& script_data,
178 const CompletionCallback& /*callback*/) {
179 NOTREACHED();
180 return ERR_NOT_IMPLEMENTED;
183 bool ProxyResolverWinHttp::OpenWinHttpSession() {
184 DCHECK(!session_handle_);
185 session_handle_ = WinHttpOpen(NULL,
186 WINHTTP_ACCESS_TYPE_NO_PROXY,
187 WINHTTP_NO_PROXY_NAME,
188 WINHTTP_NO_PROXY_BYPASS,
190 if (!session_handle_)
191 return false;
193 // Since this session handle will never be used for WinHTTP connections,
194 // these timeouts don't really mean much individually. However, WinHTTP's
195 // out of process PAC resolution will use a combined (sum of all timeouts)
196 // value to wait for an RPC reply.
197 BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
198 DCHECK(rv);
200 return true;
203 void ProxyResolverWinHttp::CloseWinHttpSession() {
204 if (session_handle_) {
205 WinHttpCloseHandle(session_handle_);
206 session_handle_ = NULL;
210 } // namespace
212 ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp()
213 : ProxyResolverFactory(false /*expects_pac_bytes*/) {
216 int ProxyResolverFactoryWinHttp::CreateProxyResolver(
217 const scoped_refptr<ProxyResolverScriptData>& pac_script,
218 scoped_ptr<ProxyResolver>* resolver,
219 const CompletionCallback& callback,
220 scoped_ptr<Request>* request) {
221 resolver->reset(new ProxyResolverWinHttp(pac_script));
222 return OK;
225 } // namespace net