[email protected] | f2c971f | 2011-11-08 00:33:17 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/proxy_resolver_winhttp.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6 | |
| 7 | #include <windows.h> |
| 8 | #include <winhttp.h> |
| 9 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 13 | #include "net/base/net_errors.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 14 | #include "net/proxy_resolution/proxy_info.h" |
| 15 | #include "net/proxy_resolution/proxy_resolver.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 16 | #include "url/gurl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 18 | using base::TimeDelta; |
| 19 | using base::TimeTicks; |
| 20 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | namespace net { |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 22 | namespace { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 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 | |
ellyjones | a4904a7 | 2015-08-18 19:22:25 | [diff] [blame] | 31 | static 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 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 53 | class ProxyResolverWinHttp : public ProxyResolver { |
| 54 | public: |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 55 | ProxyResolverWinHttp(const scoped_refptr<PacFileData>& script_data); |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 56 | ~ProxyResolverWinHttp() override; |
| 57 | |
| 58 | // ProxyResolver implementation: |
| 59 | int GetProxyForURL(const GURL& url, |
| 60 | ProxyInfo* results, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 61 | CompletionOnceCallback /*callback*/, |
maksim.sisov | 7e15726 | 2016-10-20 11:19:55 | [diff] [blame] | 62 | std::unique_ptr<Request>* /*request*/, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 63 | const NetLogWithSource& /*net_log*/) override; |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 64 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 65 | 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 | |
| 77 | ProxyResolverWinHttp::ProxyResolverWinHttp( |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 78 | const scoped_refptr<PacFileData>& script_data) |
sammc | e90c921 | 2015-05-27 23:43:35 | [diff] [blame] | 79 | : session_handle_(NULL), |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 80 | pac_url_(script_data->type() == PacFileData::TYPE_AUTO_DETECT |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 81 | ? GURL("http://wpad/wpad.dat") |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 82 | : script_data->url()) {} |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | |
[email protected] | 928fb58 | 2008-08-11 15:40:23 | [diff] [blame] | 84 | ProxyResolverWinHttp::~ProxyResolverWinHttp() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 85 | CloseWinHttpSession(); |
| 86 | } |
| 87 | |
[email protected] | 96fbab4 | 2008-12-02 06:57:44 | [diff] [blame] | 88 | int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url, |
[email protected] | 775fd9e | 2009-07-26 21:12:20 | [diff] [blame] | 89 | ProxyInfo* results, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 90 | CompletionOnceCallback /*callback*/, |
maksim.sisov | 7e15726 | 2016-10-20 11:19:55 | [diff] [blame] | 91 | std::unique_ptr<Request>* /*request*/, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 92 | const NetLogWithSource& /*net_log*/) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 93 | // 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] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 104 | options.fAutoLogonIfChallenged = FALSE; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 105 | options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL; |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 106 | base::string16 pac_url16 = base::ASCIIToUTF16(pac_url_.spec()); |
| 107 | options.lpszAutoConfigUrl = pac_url16.c_str(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 108 | |
| 109 | WINHTTP_PROXY_INFO info = {0}; |
| 110 | DCHECK(session_handle_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 111 | |
[email protected] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 112 | // 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] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 117 | BOOL ok = WinHttpGetProxyForUrl(session_handle_, |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 118 | base::ASCIIToUTF16(query_url.spec()).c_str(), |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 119 | &options, &info); |
[email protected] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 120 | if (!ok) { |
| 121 | if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) { |
| 122 | options.fAutoLogonIfChallenged = TRUE; |
[email protected] | 95c97199 | 2010-12-03 22:02:42 | [diff] [blame] | 123 | ok = WinHttpGetProxyForUrl( |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 124 | session_handle_, base::ASCIIToUTF16(query_url.spec()).c_str(), |
[email protected] | 96fbab4 | 2008-12-02 06:57:44 | [diff] [blame] | 125 | &options, &info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | } |
[email protected] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 127 | if (!ok) { |
| 128 | DWORD error = GetLastError(); |
[email protected] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 129 | // 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 | } |
ellyjones | a4904a7 | 2015-08-18 19:22:25 | [diff] [blame] | 135 | return WinHttpErrorToNetError(error); |
[email protected] | f9ad22f | 2008-09-09 20:35:40 | [diff] [blame] | 136 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 137 | } |
| 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] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 146 | // 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] | 74f778e | 2014-03-14 21:11:46 | [diff] [blame] | 160 | results->UseNamedProxy(base::UTF16ToASCII(info.lpszProxy)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | NOTREACHED(); |
| 164 | rv = ERR_FAILED; |
| 165 | } |
| 166 | |
| 167 | FreeInfo(&info); |
| 168 | return rv; |
| 169 | } |
| 170 | |
[email protected] | 928fb58 | 2008-08-11 15:40:23 | [diff] [blame] | 171 | bool ProxyResolverWinHttp::OpenWinHttpSession() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 172 | 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] | 928fb58 | 2008-08-11 15:40:23 | [diff] [blame] | 181 | // Since this session handle will never be used for WinHTTP connections, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 182 | // 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] | 928fb58 | 2008-08-11 15:40:23 | [diff] [blame] | 191 | void ProxyResolverWinHttp::CloseWinHttpSession() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 192 | if (session_handle_) { |
| 193 | WinHttpCloseHandle(session_handle_); |
| 194 | session_handle_ = NULL; |
| 195 | } |
| 196 | } |
| 197 | |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 198 | } // namespace |
| 199 | |
| 200 | ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp() |
| 201 | : ProxyResolverFactory(false /*expects_pac_bytes*/) { |
| 202 | } |
| 203 | |
| 204 | int ProxyResolverFactoryWinHttp::CreateProxyResolver( |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 205 | const scoped_refptr<PacFileData>& pac_script, |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 206 | std::unique_ptr<ProxyResolver>* resolver, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 207 | CompletionOnceCallback callback, |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 208 | std::unique_ptr<Request>* request) { |
sammc | 514748c | 2015-05-01 06:15:04 | [diff] [blame] | 209 | resolver->reset(new ProxyResolverWinHttp(pac_script)); |
| 210 | return OK; |
| 211 | } |
| 212 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 213 | } // namespace net |