OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_resolver_winhttp.h" | 5 #include "net/proxy/proxy_resolver_winhttp.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <winhttp.h> | 8 #include <winhttp.h> |
9 | 9 |
10 #include "base/histogram.h" | 10 #include "base/histogram.h" |
11 #include "base/string_tokenizer.h" | |
12 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
13 | 12 |
14 #pragma comment(lib, "winhttp.lib") | 13 #pragma comment(lib, "winhttp.lib") |
15 | 14 |
16 using base::TimeDelta; | 15 using base::TimeDelta; |
17 using base::TimeTicks; | 16 using base::TimeTicks; |
18 | 17 |
19 namespace net { | 18 namespace net { |
20 | 19 |
21 // A small wrapper for histogramming purposes ;-) | 20 // A small wrapper for histogramming purposes ;-) |
22 static BOOL CallWinHttpGetProxyForUrl(HINTERNET session, LPCWSTR url, | 21 static BOOL CallWinHttpGetProxyForUrl(HINTERNET session, LPCWSTR url, |
23 WINHTTP_AUTOPROXY_OPTIONS* options, | 22 WINHTTP_AUTOPROXY_OPTIONS* options, |
24 WINHTTP_PROXY_INFO* results) { | 23 WINHTTP_PROXY_INFO* results) { |
25 TimeTicks time_start = TimeTicks::Now(); | 24 TimeTicks time_start = TimeTicks::Now(); |
26 BOOL rv = WinHttpGetProxyForUrl(session, url, options, results); | 25 BOOL rv = WinHttpGetProxyForUrl(session, url, options, results); |
27 TimeDelta time_delta = TimeTicks::Now() - time_start; | 26 TimeDelta time_delta = TimeTicks::Now() - time_start; |
28 // Record separately success and failure times since they will have very | 27 // Record separately success and failure times since they will have very |
29 // different characteristics. | 28 // different characteristics. |
30 if (rv) { | 29 if (rv) { |
31 UMA_HISTOGRAM_LONG_TIMES(L"Net.GetProxyForUrl_OK", time_delta); | 30 UMA_HISTOGRAM_LONG_TIMES(L"Net.GetProxyForUrl_OK", time_delta); |
32 } else { | 31 } else { |
33 UMA_HISTOGRAM_LONG_TIMES(L"Net.GetProxyForUrl_FAIL", time_delta); | 32 UMA_HISTOGRAM_LONG_TIMES(L"Net.GetProxyForUrl_FAIL", time_delta); |
34 } | 33 } |
35 return rv; | 34 return rv; |
36 } | 35 } |
37 | 36 |
38 static void FreeConfig(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG* config) { | |
39 if (config->lpszAutoConfigUrl) | |
40 GlobalFree(config->lpszAutoConfigUrl); | |
41 if (config->lpszProxy) | |
42 GlobalFree(config->lpszProxy); | |
43 if (config->lpszProxyBypass) | |
44 GlobalFree(config->lpszProxyBypass); | |
45 } | |
46 | |
47 static void FreeInfo(WINHTTP_PROXY_INFO* info) { | 37 static void FreeInfo(WINHTTP_PROXY_INFO* info) { |
48 if (info->lpszProxy) | 38 if (info->lpszProxy) |
49 GlobalFree(info->lpszProxy); | 39 GlobalFree(info->lpszProxy); |
50 if (info->lpszProxyBypass) | 40 if (info->lpszProxyBypass) |
51 GlobalFree(info->lpszProxyBypass); | 41 GlobalFree(info->lpszProxyBypass); |
52 } | 42 } |
53 | 43 |
54 ProxyResolverWinHttp::ProxyResolverWinHttp() | 44 ProxyResolverWinHttp::ProxyResolverWinHttp() |
55 : session_handle_(NULL) { | 45 : session_handle_(NULL) { |
56 } | 46 } |
57 | 47 |
58 ProxyResolverWinHttp::~ProxyResolverWinHttp() { | 48 ProxyResolverWinHttp::~ProxyResolverWinHttp() { |
59 CloseWinHttpSession(); | 49 CloseWinHttpSession(); |
60 } | 50 } |
61 | 51 |
62 int ProxyResolverWinHttp::GetProxyConfig(ProxyConfig* config) { | |
63 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {0}; | |
64 if (!WinHttpGetIEProxyConfigForCurrentUser(&ie_config)) { | |
65 LOG(ERROR) << "WinHttpGetIEProxyConfigForCurrentUser failed: " << | |
66 GetLastError(); | |
67 return ERR_FAILED; // TODO(darin): Bug 1189288: translate error code. | |
68 } | |
69 | |
70 if (ie_config.fAutoDetect) | |
71 config->auto_detect = true; | |
72 if (ie_config.lpszProxy) | |
73 config->proxy_server = WideToASCII(ie_config.lpszProxy); | |
74 if (ie_config.lpszProxyBypass) { | |
75 std::string proxy_bypass = WideToASCII(ie_config.lpszProxyBypass); | |
76 | |
77 StringTokenizer proxy_server_bypass_list(proxy_bypass, "; \t\n\r"); | |
78 while (proxy_server_bypass_list.GetNext()) { | |
79 std::string bypass_url_domain = proxy_server_bypass_list.token(); | |
80 if (bypass_url_domain == "<local>") | |
81 config->proxy_bypass_local_names = true; | |
82 else | |
83 config->proxy_bypass.push_back(bypass_url_domain); | |
84 } | |
85 } | |
86 if (ie_config.lpszAutoConfigUrl) | |
87 config->pac_url = GURL(ie_config.lpszAutoConfigUrl); | |
88 | |
89 FreeConfig(&ie_config); | |
90 return OK; | |
91 } | |
92 | |
93 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url, | 52 int ProxyResolverWinHttp::GetProxyForURL(const GURL& query_url, |
94 const GURL& pac_url, | 53 const GURL& pac_url, |
95 ProxyInfo* results) { | 54 ProxyInfo* results) { |
96 // If we don't have a WinHTTP session, then create a new one. | 55 // If we don't have a WinHTTP session, then create a new one. |
97 if (!session_handle_ && !OpenWinHttpSession()) | 56 if (!session_handle_ && !OpenWinHttpSession()) |
98 return ERR_FAILED; | 57 return ERR_FAILED; |
99 | 58 |
100 // If we have been given an empty PAC url, then use auto-detection. | 59 // If we have been given an empty PAC url, then use auto-detection. |
101 // | 60 // |
102 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this | 61 // NOTE: We just use DNS-based auto-detection here like Firefox. We do this |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 139 |
181 void ProxyResolverWinHttp::CloseWinHttpSession() { | 140 void ProxyResolverWinHttp::CloseWinHttpSession() { |
182 if (session_handle_) { | 141 if (session_handle_) { |
183 WinHttpCloseHandle(session_handle_); | 142 WinHttpCloseHandle(session_handle_); |
184 session_handle_ = NULL; | 143 session_handle_ = NULL; |
185 } | 144 } |
186 } | 145 } |
187 | 146 |
188 } // namespace net | 147 } // namespace net |
189 | 148 |
OLD | NEW |