OLD | NEW |
| (Empty) |
1 // Copyright 2007-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Some simple network diags. | |
17 | |
18 #include "omaha/net/net_diags.h" | |
19 #include <windows.h> | |
20 #include <stdarg.h> | |
21 #include <vector> | |
22 #include "omaha/base/app_util.h" | |
23 #include "omaha/base/browser_utils.h" | |
24 #include "omaha/base/debug.h" | |
25 #include "omaha/base/system_info.h" | |
26 #include "omaha/base/time.h" | |
27 #include "omaha/base/utils.h" | |
28 #include "omaha/net/network_config.h" | |
29 #include "omaha/net/network_request.h" | |
30 | |
31 namespace omaha { | |
32 | |
33 NetDiags::NetDiags() { | |
34 Initialize(); | |
35 } | |
36 | |
37 NetDiags::~NetDiags() { | |
38 ::CoUninitialize(); | |
39 } | |
40 | |
41 bool PrintToConsole(const TCHAR* format, ...) { | |
42 ASSERT1(format); | |
43 va_list arg_list; | |
44 va_start(arg_list, format); | |
45 CString msg; | |
46 msg.FormatV(format, arg_list); | |
47 ASSERT1(msg.GetLength() > 0); | |
48 va_end(arg_list); | |
49 | |
50 DWORD count = 0; | |
51 bool result = !!::WriteConsole(::GetStdHandle(STD_OUTPUT_HANDLE), | |
52 msg, | |
53 msg.GetLength(), | |
54 &count, | |
55 NULL); | |
56 ASSERT1(result); | |
57 ASSERT1(msg.GetLength() == static_cast<int>(count)); | |
58 return result; | |
59 } | |
60 | |
61 void NetDiags::Initialize() { | |
62 if (!SystemInfo::IsRunningOnXPOrLater()) { | |
63 ::MessageBox(NULL, | |
64 _T("GoogleUpdate.exe"), | |
65 _T("\"GoogleUpdate.exe /NetDiags\" only runs on Windows XP or later."), | |
66 MB_OK); | |
67 ::ExitProcess(1); | |
68 } | |
69 | |
70 if (!AttachConsoleWrap(ATTACH_PARENT_PROCESS)) { | |
71 ::MessageBox(NULL, | |
72 _T("GoogleUpdate.exe"), | |
73 _T("Please run \"GoogleUpdate.exe /NetDiags\" from a cmd.exe window."), | |
74 MB_OK); | |
75 ::ExitProcess(1); | |
76 } | |
77 | |
78 PrintToConsole(_T("\n")); | |
79 HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); | |
80 if (FAILED(hr)) { | |
81 PrintToConsole(_T("Failed to ::CoInitialize() [0x%x]\n"), hr); | |
82 ::ExitProcess(1); | |
83 } | |
84 | |
85 NetworkConfig* network_config = NULL; | |
86 hr = NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config); | |
87 if (FAILED(hr)) { | |
88 PrintToConsole(_T("Failed to GetUserNetworkConfig() [0x%x]\n"), hr); | |
89 ::ExitProcess(1); | |
90 } | |
91 | |
92 // Initialize the detection chain: GoogleProxy, FireFox if it is the | |
93 // default browser, and IE. | |
94 network_config->Clear(); | |
95 BrowserType browser_type(BROWSER_UNKNOWN); | |
96 GetDefaultBrowserType(&browser_type); | |
97 if (browser_type == BROWSER_FIREFOX) { | |
98 PrintToConsole(_T("Default browser is Firefox\n")); | |
99 network_config->Add(new FirefoxProxyDetector()); | |
100 } | |
101 network_config->Add(new IEProxyDetector()); | |
102 | |
103 std::vector<ProxyConfig> configs = network_config->GetConfigurations(); | |
104 if (configs.empty()) { | |
105 PrintToConsole(_T("No Network Configurations to display\n")); | |
106 } else { | |
107 PrintToConsole(_T("[Detected Network Configurations][\n%s]\n"), | |
108 NetworkConfig::ToString(configs)); | |
109 } | |
110 } | |
111 | |
112 void NetDiags::OnProgress(int bytes, int bytes_total, int, const TCHAR*) { | |
113 PrintToConsole(_T("\n[Downloading %d of %d]\n"), bytes, bytes_total); | |
114 } | |
115 | |
116 void NetDiags::OnRequestBegin() { | |
117 PrintToConsole(_T("\n[Download begins]\n")); | |
118 } | |
119 | |
120 void NetDiags::OnRequestRetryScheduled(time64 next_retry_time) { | |
121 time64 now = GetCurrent100NSTime(); | |
122 ASSERT1(next_retry_time > now); | |
123 | |
124 if (next_retry_time > now) { | |
125 PrintToConsole(_T("\n[Download will retry in %d seconds]\n"), | |
126 CeilingDivide(next_retry_time - now, kSecsTo100ns)); | |
127 } | |
128 } | |
129 | |
130 // http get. | |
131 void NetDiags::DoGet(const CString& url) { | |
132 PrintToConsole(_T("\nGET request for [%s]\n"), url); | |
133 NetworkConfig* network_config = NULL; | |
134 NetworkConfigManager& network_manager = NetworkConfigManager::Instance(); | |
135 HRESULT hr = network_manager.GetUserNetworkConfig(&network_config); | |
136 if (FAILED(hr)) { | |
137 PrintToConsole(_T("Failed to GetUserNetworkConfig() [0x%x]\n"), hr); | |
138 return; | |
139 } | |
140 NetworkRequest network_request(network_config->session()); | |
141 | |
142 network_request.set_callback(this); | |
143 network_request.set_num_retries(2); | |
144 std::vector<uint8> response; | |
145 hr = GetRequest(&network_request, url, &response); | |
146 int status = network_request.http_status_code(); | |
147 if (FAILED(hr)) { | |
148 PrintToConsole(_T("GET request failed. HRESULT=[0x%x], HTTP Status=[%d]\n"), | |
149 hr, status); | |
150 return; | |
151 } | |
152 | |
153 PrintToConsole(_T("HTTP Status=[%d]\n"), status); | |
154 PrintToConsole(_T("HTTP Response=\n[%s]\n"), Utf8BufferToWideChar(response)); | |
155 } | |
156 | |
157 // http download. | |
158 void NetDiags::DoDownload(const CString& url) { | |
159 PrintToConsole(_T("\nDownload request for [%s]\n"), url); | |
160 NetworkConfig* network_config = NULL; | |
161 NetworkConfigManager& network_manager = NetworkConfigManager::Instance(); | |
162 HRESULT hr = network_manager.GetUserNetworkConfig(&network_config); | |
163 if (FAILED(hr)) { | |
164 PrintToConsole(_T("Failed to GetUserNetworkConfig() [0x%x]\n"), hr); | |
165 return; | |
166 } | |
167 NetworkRequest network_request(network_config->session()); | |
168 | |
169 network_request.set_callback(this); | |
170 network_request.set_num_retries(2); | |
171 | |
172 CString temp_dir = app_util::GetTempDir(); | |
173 CString temp_file; | |
174 if (!::GetTempFileName(temp_dir, | |
175 _T("tmp"), | |
176 0, | |
177 CStrBuf(temp_file, MAX_PATH))) { | |
178 PrintToConsole(_T("::GetTempFileName Failed [%d]\n"), ::GetLastError()); | |
179 return; | |
180 } | |
181 | |
182 hr = network_request.DownloadFile(url, temp_file); | |
183 int status = network_request.http_status_code(); | |
184 if (FAILED(hr)) { | |
185 PrintToConsole(_T("Download failed. HRESULT=[0x%x], HTTP Status=[%d]\n"), | |
186 hr, status); | |
187 return; | |
188 } | |
189 | |
190 PrintToConsole(_T("HTTP Status=[%d]\n"), status); | |
191 PrintToConsole(_T("Downloaded File=[%s]\n"), temp_file); | |
192 if (!::DeleteFile(temp_file)) { | |
193 PrintToConsole(_T("::DeleteFile Failed [%s][%d]\n"), | |
194 temp_file, ::GetLastError()); | |
195 return; | |
196 } else { | |
197 PrintToConsole(_T("Deleted file [%s]\n"), temp_file); | |
198 } | |
199 } | |
200 | |
201 // Run the tests. | |
202 int NetDiags::Main() { | |
203 DoGet(_T("http://www.google.com/robots.txt")); | |
204 DoGet(_T("https://www.google.com/robots.txt")); | |
205 DoDownload(_T("http://www.google.com/intl/en_ALL/images/logo.gif")); | |
206 return 0; | |
207 } | |
208 | |
209 } // namespace omaha | |
210 | |
OLD | NEW |