blob: 82ccdb189b82429e3b1786a64c3ee4305c196861 [file] [log] [blame]
[email protected]f2c971f2011-11-08 00:33:171// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
Lily Houghton582d4622018-01-22 22:43:405#include "net/proxy_resolution/proxy_resolver_winhttp.h"
initial.commit586acc5fe2008-07-26 22:42:526
7#include <windows.h>
8#include <winhttp.h>
9
Avi Drissman13fc8932015-12-20 04:40:4610#include "base/macros.h"
[email protected]fc9be5802013-06-11 10:56:5111#include "base/strings/string_util.h"
[email protected]750b2f3c2013-06-07 18:41:0512#include "base/strings/utf_string_conversions.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4014#include "net/proxy_resolution/proxy_info.h"
15#include "net/proxy_resolution/proxy_resolver.h"
[email protected]f89276a72013-07-12 06:41:5416#include "url/gurl.h"
initial.commit586acc5fe2008-07-26 22:42:5217
[email protected]e1acf6f2008-10-27 20:43:3318using base::TimeDelta;
19using base::TimeTicks;
20
initial.commit586acc5fe2008-07-26 22:42:5221namespace net {
sammc514748c2015-05-01 06:15:0422namespace {
initial.commit586acc5fe2008-07-26 22:42:5223
initial.commit586acc5fe2008-07-26 22:42:5224static void FreeInfo(WINHTTP_PROXY_INFO* info) {
25 if (info->lpszProxy)
26 GlobalFree(info->lpszProxy);
27 if (info->lpszProxyBypass)
28 GlobalFree(info->lpszProxyBypass);
29}
30
ellyjonesa4904a72015-08-18 19:22:2531static Error WinHttpErrorToNetError(DWORD win_http_error) {
32 switch (win_http_error) {
33 case ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR:
34 case ERROR_WINHTTP_INTERNAL_ERROR:
35 case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
36 return ERR_FAILED;
37 case ERROR_WINHTTP_LOGIN_FAILURE:
38 return ERR_PROXY_AUTH_UNSUPPORTED;
39 case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT:
40 return ERR_PAC_SCRIPT_FAILED;
41 case ERROR_WINHTTP_INVALID_URL:
42 case ERROR_WINHTTP_OPERATION_CANCELLED:
43 case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT:
44 case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
45 return ERR_PAC_STATUS_NOT_OK;
46 case ERROR_NOT_ENOUGH_MEMORY:
47 return ERR_INSUFFICIENT_RESOURCES;
48 default:
49 return ERR_FAILED;
50 }
51}
52
sammc514748c2015-05-01 06:15:0453class ProxyResolverWinHttp : public ProxyResolver {
54 public:
Lily Houghton99597862018-03-07 16:40:4255 ProxyResolverWinHttp(const scoped_refptr<PacFileData>& script_data);
sammc514748c2015-05-01 06:15:0456 ~ProxyResolverWinHttp() override;
57
58 // ProxyResolver implementation:
59 int GetProxyForURL(const GURL& url,
60 ProxyInfo* results,
Bence Békycc5b88a2018-05-25 20:24:1761 CompletionOnceCallback /*callback*/,
maksim.sisov7e157262016-10-20 11:19:5562 std::unique_ptr<Request>* /*request*/,
tfarina428341112016-09-22 13:38:2063 const NetLogWithSource& /*net_log*/) override;
sammc514748c2015-05-01 06:15:0464
sammc514748c2015-05-01 06:15:0465 private:
66 bool OpenWinHttpSession();
67 void CloseWinHttpSession();
68
69 // Proxy configuration is cached on the session handle.
70 HINTERNET session_handle_;
71
72 const GURL pac_url_;
73
74 DISALLOW_COPY_AND_ASSIGN(ProxyResolverWinHttp);
75};
76
77ProxyResolverWinHttp::ProxyResolverWinHttp(
Lily Houghton99597862018-03-07 16:40:4278 const scoped_refptr<PacFileData>& script_data)
sammce90c9212015-05-27 23:43:3579 : session_handle_(NULL),
Lily Houghton99597862018-03-07 16:40:4280 pac_url_(script_data->type() == PacFileData::TYPE_AUTO_DETECT
sammc514748c2015-05-01 06:15:0481 ? GURL("http://wpad/wpad.dat")
Lily Houghton99597862018-03-07 16:40:4282 : script_data->url()) {}
initial.commit586acc5fe2008-07-26 22:42:5283
[email protected]928fb582008-08-11 15:40:2384ProxyResolverWinHttp::~ProxyResolverWinHttp() {
initial.commit586acc5fe2008-07-26 22:42:5285 CloseWinHttpSession();
86}
87
[email protected]96fbab42008-12-02 06:57:4488int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url,
[email protected]775fd9e2009-07-26 21:12:2089 ProxyInfo* results,
Bence Békycc5b88a2018-05-25 20:24:1790 CompletionOnceCallback /*callback*/,
maksim.sisov7e157262016-10-20 11:19:5591 std::unique_ptr<Request>* /*request*/,
tfarina428341112016-09-22 13:38:2092 const NetLogWithSource& /*net_log*/) {
initial.commit586acc5fe2008-07-26 22:42:5293 // If we don't have a WinHTTP session, then create a new one.
94 if (!session_handle_ && !OpenWinHttpSession())
95 return ERR_FAILED;
96
97 // If we have been given an empty PAC url, then use auto-detection.
98 //
99 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this
100 // to avoid WinHTTP's auto-detection code, which while more featureful (it
101 // supports DHCP based auto-detection) also appears to have issues.
102 //
103 WINHTTP_AUTOPROXY_OPTIONS options = {0};
[email protected]f9ad22f2008-09-09 20:35:40104 options.fAutoLogonIfChallenged = FALSE;
initial.commit586acc5fe2008-07-26 22:42:52105 options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
thestige5c64d92014-11-07 01:19:24106 base::string16 pac_url16 = base::ASCIIToUTF16(pac_url_.spec());
107 options.lpszAutoConfigUrl = pac_url16.c_str();
initial.commit586acc5fe2008-07-26 22:42:52108
109 WINHTTP_PROXY_INFO info = {0};
110 DCHECK(session_handle_);
initial.commit586acc5fe2008-07-26 22:42:52111
[email protected]f9ad22f2008-09-09 20:35:40112 // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
113 // necessary to first try resolving with fAutoLogonIfChallenged set to false.
114 // Otherwise, we fail over to trying it with a value of true. This way we
115 // get good performance in the case where WinHTTP uses an out-of-process
116 // resolver. This is important for Vista and Win2k3.
[email protected]ad65a3e2013-12-25 18:18:01117 BOOL ok = WinHttpGetProxyForUrl(session_handle_,
thestige5c64d92014-11-07 01:19:24118 base::ASCIIToUTF16(query_url.spec()).c_str(),
[email protected]ad65a3e2013-12-25 18:18:01119 &options, &info);
[email protected]f9ad22f2008-09-09 20:35:40120 if (!ok) {
121 if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
122 options.fAutoLogonIfChallenged = TRUE;
[email protected]95c971992010-12-03 22:02:42123 ok = WinHttpGetProxyForUrl(
thestige5c64d92014-11-07 01:19:24124 session_handle_, base::ASCIIToUTF16(query_url.spec()).c_str(),
[email protected]96fbab42008-12-02 06:57:44125 &options, &info);
initial.commit586acc5fe2008-07-26 22:42:52126 }
[email protected]f9ad22f2008-09-09 20:35:40127 if (!ok) {
128 DWORD error = GetLastError();
[email protected]f9ad22f2008-09-09 20:35:40129 // If we got here because of RPC timeout during out of process PAC
130 // resolution, no further requests on this session are going to work.
131 if (ERROR_WINHTTP_TIMEOUT == error ||
132 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
133 CloseWinHttpSession();
134 }
ellyjonesa4904a72015-08-18 19:22:25135 return WinHttpErrorToNetError(error);
[email protected]f9ad22f2008-09-09 20:35:40136 }
initial.commit586acc5fe2008-07-26 22:42:52137 }
138
139 int rv = OK;
140
141 switch (info.dwAccessType) {
142 case WINHTTP_ACCESS_TYPE_NO_PROXY:
143 results->UseDirect();
144 break;
145 case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
[email protected]f6fb2de2009-02-19 08:11:42146 // According to MSDN:
147 //
148 // The proxy server list contains one or more of the following strings
149 // separated by semicolons or whitespace.
150 //
151 // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
152 //
153 // Based on this description, ProxyInfo::UseNamedProxy() isn't
154 // going to handle all the variations (in particular <scheme>=).
155 //
156 // However in practice, it seems that WinHTTP is simply returning
157 // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
158 // proxy types, and stops the list when PAC encounters a "DIRECT".
159 // So UseNamedProxy() should work OK.
[email protected]74f778e2014-03-14 21:11:46160 results->UseNamedProxy(base::UTF16ToASCII(info.lpszProxy));
initial.commit586acc5fe2008-07-26 22:42:52161 break;
162 default:
163 NOTREACHED();
164 rv = ERR_FAILED;
165 }
166
167 FreeInfo(&info);
168 return rv;
169}
170
[email protected]928fb582008-08-11 15:40:23171bool ProxyResolverWinHttp::OpenWinHttpSession() {
initial.commit586acc5fe2008-07-26 22:42:52172 DCHECK(!session_handle_);
173 session_handle_ = WinHttpOpen(NULL,
174 WINHTTP_ACCESS_TYPE_NO_PROXY,
175 WINHTTP_NO_PROXY_NAME,
176 WINHTTP_NO_PROXY_BYPASS,
177 0);
178 if (!session_handle_)
179 return false;
180
[email protected]928fb582008-08-11 15:40:23181 // Since this session handle will never be used for WinHTTP connections,
initial.commit586acc5fe2008-07-26 22:42:52182 // these timeouts don't really mean much individually. However, WinHTTP's
183 // out of process PAC resolution will use a combined (sum of all timeouts)
184 // value to wait for an RPC reply.
185 BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
186 DCHECK(rv);
187
188 return true;
189}
190
[email protected]928fb582008-08-11 15:40:23191void ProxyResolverWinHttp::CloseWinHttpSession() {
initial.commit586acc5fe2008-07-26 22:42:52192 if (session_handle_) {
193 WinHttpCloseHandle(session_handle_);
194 session_handle_ = NULL;
195 }
196}
197
sammc514748c2015-05-01 06:15:04198} // namespace
199
200ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp()
201 : ProxyResolverFactory(false /*expects_pac_bytes*/) {
202}
203
204int ProxyResolverFactoryWinHttp::CreateProxyResolver(
Lily Houghton99597862018-03-07 16:40:42205 const scoped_refptr<PacFileData>& pac_script,
danakj8a98ca22016-04-16 02:47:36206 std::unique_ptr<ProxyResolver>* resolver,
Bence Békycc5b88a2018-05-25 20:24:17207 CompletionOnceCallback callback,
danakj8a98ca22016-04-16 02:47:36208 std::unique_ptr<Request>* request) {
sammc514748c2015-05-01 06:15:04209 resolver->reset(new ProxyResolverWinHttp(pac_script));
210 return OK;
211}
212
initial.commit586acc5fe2008-07-26 22:42:52213} // namespace net