• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/proxy_resolution/win/proxy_resolver_winhttp.h"
6 
7 #include <windows.h>
8 #include <winhttp.h>
9 
10 #include <memory>
11 
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "net/base/net_errors.h"
15 #include "net/proxy_resolution/proxy_info.h"
16 #include "net/proxy_resolution/proxy_resolver.h"
17 #include "url/gurl.h"
18 
19 using base::TimeTicks;
20 
21 namespace net {
22 namespace {
23 
FreeInfo(WINHTTP_PROXY_INFO * info)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 
WinHttpErrorToNetError(DWORD win_http_error)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_HTTP_RESPONSE_CODE_FAILURE;
46     case ERROR_NOT_ENOUGH_MEMORY:
47       return ERR_INSUFFICIENT_RESOURCES;
48     default:
49       return ERR_FAILED;
50   }
51 }
52 
53 class ProxyResolverWinHttp : public ProxyResolver {
54  public:
55   ProxyResolverWinHttp(const scoped_refptr<PacFileData>& script_data);
56 
57   ProxyResolverWinHttp(const ProxyResolverWinHttp&) = delete;
58   ProxyResolverWinHttp& operator=(const ProxyResolverWinHttp&) = delete;
59 
60   ~ProxyResolverWinHttp() override;
61 
62   // ProxyResolver implementation:
63   int GetProxyForURL(const GURL& url,
64                      const NetworkAnonymizationKey& network_anymization_key,
65                      ProxyInfo* results,
66                      CompletionOnceCallback /*callback*/,
67                      std::unique_ptr<Request>* /*request*/,
68                      const NetLogWithSource& /*net_log*/) override;
69 
70  private:
71   bool OpenWinHttpSession();
72   void CloseWinHttpSession();
73 
74   // Proxy configuration is cached on the session handle.
75   HINTERNET session_handle_ = nullptr;
76 
77   const GURL pac_url_;
78 };
79 
ProxyResolverWinHttp(const scoped_refptr<PacFileData> & script_data)80 ProxyResolverWinHttp::ProxyResolverWinHttp(
81     const scoped_refptr<PacFileData>& script_data)
82     : pac_url_(script_data->type() == PacFileData::TYPE_AUTO_DETECT
83                    ? GURL("http://wpad/wpad.dat")
84                    : script_data->url()) {}
85 
~ProxyResolverWinHttp()86 ProxyResolverWinHttp::~ProxyResolverWinHttp() {
87   CloseWinHttpSession();
88 }
89 
GetProxyForURL(const GURL & query_url,const NetworkAnonymizationKey & network_anonymization_key,ProxyInfo * results,CompletionOnceCallback,std::unique_ptr<Request> *,const NetLogWithSource &)90 int ProxyResolverWinHttp::GetProxyForURL(
91     const GURL& query_url,
92     const NetworkAnonymizationKey& network_anonymization_key,
93     ProxyInfo* results,
94     CompletionOnceCallback /*callback*/,
95     std::unique_ptr<Request>* /*request*/,
96     const NetLogWithSource& /*net_log*/) {
97   // If we don't have a WinHTTP session, then create a new one.
98   if (!session_handle_ && !OpenWinHttpSession())
99     return ERR_FAILED;
100 
101   // Windows' system resolver does not support WebSocket URLs in proxy.pac. This
102   // was tested in version 10.0.16299, and is also implied by the description of
103   // the ERROR_WINHTTP_UNRECOGNIZED_SCHEME error code in the Microsoft
104   // documentation at
105   // https://docs.microsoft.com/en-us/windows/desktop/api/winhttp/nf-winhttp-winhttpgetproxyforurl.
106   // See https://crbug.com/862121.
107   GURL mutable_query_url = query_url;
108   if (query_url.SchemeIsWSOrWSS()) {
109     GURL::Replacements replacements;
110     replacements.SetSchemeStr(query_url.SchemeIsCryptographic() ? "https"
111                                                                 : "http");
112     mutable_query_url = query_url.ReplaceComponents(replacements);
113   }
114 
115   // If we have been given an empty PAC url, then use auto-detection.
116   //
117   // NOTE: We just use DNS-based auto-detection here like Firefox.  We do this
118   // to avoid WinHTTP's auto-detection code, which while more featureful (it
119   // supports DHCP based auto-detection) also appears to have issues.
120   //
121   WINHTTP_AUTOPROXY_OPTIONS options = {0};
122   options.fAutoLogonIfChallenged = FALSE;
123   options.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
124   std::u16string pac_url16 = base::ASCIIToUTF16(pac_url_.spec());
125   options.lpszAutoConfigUrl = base::as_wcstr(pac_url16);
126 
127   WINHTTP_PROXY_INFO info = {0};
128   DCHECK(session_handle_);
129 
130   // Per http://msdn.microsoft.com/en-us/library/aa383153(VS.85).aspx, it is
131   // necessary to first try resolving with fAutoLogonIfChallenged set to false.
132   // Otherwise, we fail over to trying it with a value of true.  This way we
133   // get good performance in the case where WinHTTP uses an out-of-process
134   // resolver.  This is important for Vista and Win2k3.
135   BOOL ok = WinHttpGetProxyForUrl(
136       session_handle_,
137       base::as_wcstr(base::ASCIIToUTF16(mutable_query_url.spec())), &options,
138       &info);
139   if (!ok) {
140     if (ERROR_WINHTTP_LOGIN_FAILURE == GetLastError()) {
141       options.fAutoLogonIfChallenged = TRUE;
142       ok = WinHttpGetProxyForUrl(
143           session_handle_,
144           base::as_wcstr(base::ASCIIToUTF16(mutable_query_url.spec())),
145           &options, &info);
146     }
147     if (!ok) {
148       DWORD error = GetLastError();
149       // If we got here because of RPC timeout during out of process PAC
150       // resolution, no further requests on this session are going to work.
151       if (ERROR_WINHTTP_TIMEOUT == error ||
152           ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR == error) {
153         CloseWinHttpSession();
154       }
155       return WinHttpErrorToNetError(error);
156     }
157   }
158 
159   int rv = OK;
160 
161   switch (info.dwAccessType) {
162     case WINHTTP_ACCESS_TYPE_NO_PROXY:
163       results->UseDirect();
164       break;
165     case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
166       // According to MSDN:
167       //
168       // The proxy server list contains one or more of the following strings
169       // separated by semicolons or whitespace.
170       //
171       // ([<scheme>=][<scheme>"://"]<server>[":"<port>])
172       //
173       // Based on this description, ProxyInfo::UseNamedProxy() isn't
174       // going to handle all the variations (in particular <scheme>=).
175       //
176       // However in practice, it seems that WinHTTP is simply returning
177       // things like "foopy1:80;foopy2:80". It strips out the non-HTTP
178       // proxy types, and stops the list when PAC encounters a "DIRECT".
179       // So UseNamedProxy() should work OK.
180       results->UseNamedProxy(base::WideToUTF8(info.lpszProxy));
181       break;
182     default:
183       NOTREACHED();
184       rv = ERR_FAILED;
185   }
186 
187   FreeInfo(&info);
188   return rv;
189 }
190 
OpenWinHttpSession()191 bool ProxyResolverWinHttp::OpenWinHttpSession() {
192   DCHECK(!session_handle_);
193   session_handle_ =
194       WinHttpOpen(nullptr, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME,
195                   WINHTTP_NO_PROXY_BYPASS, 0);
196   if (!session_handle_)
197     return false;
198 
199   // Since this session handle will never be used for WinHTTP connections,
200   // these timeouts don't really mean much individually.  However, WinHTTP's
201   // out of process PAC resolution will use a combined (sum of all timeouts)
202   // value to wait for an RPC reply.
203   BOOL rv = WinHttpSetTimeouts(session_handle_, 10000, 10000, 5000, 5000);
204   DCHECK(rv);
205 
206   return true;
207 }
208 
CloseWinHttpSession()209 void ProxyResolverWinHttp::CloseWinHttpSession() {
210   if (session_handle_) {
211     WinHttpCloseHandle(session_handle_);
212     session_handle_ = nullptr;
213   }
214 }
215 
216 }  // namespace
217 
ProxyResolverFactoryWinHttp()218 ProxyResolverFactoryWinHttp::ProxyResolverFactoryWinHttp()
219     : ProxyResolverFactory(false /*expects_pac_bytes*/) {
220 }
221 
CreateProxyResolver(const scoped_refptr<PacFileData> & pac_script,std::unique_ptr<ProxyResolver> * resolver,CompletionOnceCallback callback,std::unique_ptr<Request> * request)222 int ProxyResolverFactoryWinHttp::CreateProxyResolver(
223     const scoped_refptr<PacFileData>& pac_script,
224     std::unique_ptr<ProxyResolver>* resolver,
225     CompletionOnceCallback callback,
226     std::unique_ptr<Request>* request) {
227   *resolver = std::make_unique<ProxyResolverWinHttp>(pac_script);
228   return OK;
229 }
230 
231 }  // namespace net
232