Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/DefaultWebRequestWinInet.cpp

Issue 11588009: Support of gzip, deflate added to WinInet web request implementation (Closed)
Left Patch Set: Created Sept. 3, 2013, 11:44 a.m.
Right Patch Set: Use WinInet instead of WinHTTP Created Oct. 7, 2013, 9:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « libadblockplus.gyp ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "AdblockPlus/DefaultWebRequest.h" 18 #include "AdblockPlus/DefaultWebRequest.h"
19 #include <algorithm> 19 #include <algorithm>
20 #include <sstream> 20 #include <sstream>
21 #include <Windows.h> 21 #include <Windows.h>
22 #include <winhttp.h> 22 #include <WinInet.h>
23 #include <Shlwapi.h> 23 #include <Shlwapi.h>
24 24
25 #include "Utils.h" 25 #include "Utils.h"
26 26
27 class WinHttpHandle 27 class WinHttpHandle
Wladimir Palant 2013/10/08 07:24:28 It would probably make sense to rename this class
28 { 28 {
29 public: 29 public:
30 HINTERNET handle; 30 HINTERNET handle;
31 WinHttpHandle(HINTERNET handle = 0) : handle(handle) {} 31 WinHttpHandle(HINTERNET handle = 0) : handle(handle) {}
32 32
33 ~WinHttpHandle() 33 ~WinHttpHandle()
34 { 34 {
35 if (handle) WinHttpCloseHandle(handle); 35 if (handle) InternetCloseHandle(handle);
36 handle = 0; 36 handle = 0;
37 } 37 }
38 // Overloaded HINTERNET cast 38 // Overloaded HINTERNET cast
39 operator HINTERNET() { return handle; } 39 operator HINTERNET() { return handle; }
40 40
41 }; 41 };
42 42
43 43
44 long WindowsErrorToGeckoError(DWORD err) 44 long WindowsErrorToGeckoError(DWORD err)
45 { 45 {
46 // TODO: Add some more error code translations for easier debugging 46 // TODO: Add some more error code translations for easier debugging
47 switch (err) 47 switch (err)
48 { 48 {
49 case ERROR_INVALID_HANDLE: 49 case ERROR_INVALID_HANDLE:
50 return AdblockPlus::WebRequest::NS_ERROR_NOT_INITIALIZED; 50 return AdblockPlus::WebRequest::NS_ERROR_NOT_INITIALIZED;
51 case ERROR_OUTOFMEMORY: 51 case ERROR_OUTOFMEMORY:
52 return AdblockPlus::WebRequest::NS_ERROR_OUT_OF_MEMORY; 52 return AdblockPlus::WebRequest::NS_ERROR_OUT_OF_MEMORY;
53 case ERROR_WINHTTP_UNRECOGNIZED_SCHEME: 53 case ERROR_INTERNET_UNRECOGNIZED_SCHEME:
54 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_PROTOCOL; 54 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_PROTOCOL;
55 case ERROR_WINHTTP_CONNECTION_ERROR: 55 case ERROR_INTERNET_CANNOT_CONNECT:
56 return AdblockPlus::WebRequest::NS_ERROR_NET_INTERRUPT; 56 return AdblockPlus::WebRequest::NS_ERROR_NET_INTERRUPT;
57 case ERROR_WINHTTP_INVALID_URL: 57 case ERROR_INTERNET_INVALID_URL:
58 return AdblockPlus::WebRequest::NS_ERROR_MALFORMED_URI; 58 return AdblockPlus::WebRequest::NS_ERROR_MALFORMED_URI;
59 case ERROR_WINHTTP_TIMEOUT: 59 case ERROR_INTERNET_TIMEOUT:
60 return AdblockPlus::WebRequest::NS_ERROR_NET_TIMEOUT; 60 return AdblockPlus::WebRequest::NS_ERROR_NET_TIMEOUT;
61 case ERROR_WINHTTP_NAME_NOT_RESOLVED: 61 case ERROR_INTERNET_NAME_NOT_RESOLVED:
62 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_HOST; 62 return AdblockPlus::WebRequest::NS_ERROR_UNKNOWN_HOST;
63 63
64 default: 64 default:
65 return AdblockPlus::WebRequest::NS_CUSTOM_ERROR_BASE + err; 65 return AdblockPlus::WebRequest::NS_CUSTOM_ERROR_BASE + err;
66 } 66 }
67 }
68
69 BOOL GetProxySettings(std::wstring& proxyName, std::wstring& proxyBypass)
70 {
71 BOOL bResult = TRUE;
72
73 // Get Proxy config info.
74 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
75
76 ::ZeroMemory(&proxyConfig, sizeof(proxyConfig));
77
78 if (WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig))
79 {
80 if (proxyConfig.lpszProxy != 0)
81 {
82 proxyName.assign(proxyConfig.lpszProxy);
83 }
84 if (proxyConfig.lpszProxyBypass != 0)
85 {
86 proxyBypass.assign(proxyConfig.lpszProxyBypass);
87 }
88 }
89 else
90 {
91 bResult = FALSE;
92 }
93
94 // The strings need to be freed.
95 if (proxyConfig.lpszProxy != NULL)
96 {
97 ::GlobalFree(proxyConfig.lpszProxy);
98 }
99 if (proxyConfig.lpszAutoConfigUrl != NULL)
100 {
101 ::GlobalFree(proxyConfig.lpszAutoConfigUrl);
102 }
103 if (proxyConfig.lpszProxyBypass!= NULL)
104 {
105 ::GlobalFree(proxyConfig.lpszProxyBypass);
106 }
107
108 return bResult;
109 } 67 }
110 68
111 void ParseResponseHeaders(HINTERNET hRequest, AdblockPlus::ServerResponse* resul t) 69 void ParseResponseHeaders(HINTERNET hRequest, AdblockPlus::ServerResponse* resul t)
112 { 70 {
113 if (!result) 71 if (!result)
114 { 72 {
115 throw std::runtime_error("ParseResponseHeaders - second parameter is 0"); 73 throw std::runtime_error("ParseResponseHeaders - second parameter is 0");
116 } 74 }
117 75
118 if (!hRequest) 76 if (!hRequest)
119 { 77 {
120 throw std::runtime_error("ParseResponseHeaders - request is 0"); 78 throw std::runtime_error("ParseResponseHeaders - request is 0");
121 return; 79 return;
122 } 80 }
123 // Parse the response headers 81 // Parse the response headers
124 BOOL res = 0; 82 BOOL res = 0;
125 DWORD bufLen = 0; 83 DWORD bufLen = 0;
126 res = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS, WINHTTP_HEADER_ NAME_BY_INDEX, WINHTTP_NO_OUTPUT_BUFFER, &bufLen, WINHTTP_NO_HEADER_INDEX); 84 res = InternetQueryOption(hRequest, HTTP_QUERY_RAW_HEADERS, 0, &bufLen);
127 if (bufLen == 0) 85 if (bufLen == 0)
128 { 86 {
129 // There are not headers 87 // There are not headers
130 return; 88 return;
131 } 89 }
132 std::wstring responseHeaders; 90 std::wstring responseHeaders;
133 responseHeaders.resize(bufLen / sizeof(std::wstring::value_type) + 1); 91 responseHeaders.resize(bufLen / sizeof(std::wstring::value_type) + 1);
134 res = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_RAW_HEADERS, WINHTTP_HEADER_ NAME_BY_INDEX, &responseHeaders[0], &bufLen, WINHTTP_NO_HEADER_INDEX); 92 res = InternetQueryOption(hRequest, HTTP_QUERY_RAW_HEADERS, &responseHeaders[0 ], &bufLen);
135 if (res) 93 if (res)
136 { 94 {
137 // Iterate through each header. Separator is '\0' 95 // Iterate through each header. Separator is '\0'
138 int nextHeaderNameStart = 0; 96 int nextHeaderNameStart = 0;
139 int headerNameEnd = 0; 97 int headerNameEnd = 0;
140 int headerValueStart = 0; 98 int headerValueStart = 0;
141 int prevHeaderStart = 0; 99 int prevHeaderStart = 0;
142 while ((nextHeaderNameStart = responseHeaders.find(L'\0', nextHeaderNameStar t)) != std::wstring::npos) 100 while ((nextHeaderNameStart = responseHeaders.find(L'\0', nextHeaderNameStar t)) != std::wstring::npos)
143 { 101 {
144 headerNameEnd = responseHeaders.find(L":", prevHeaderStart); 102 headerNameEnd = responseHeaders.find(L":", prevHeaderStart);
(...skipping 20 matching lines...) Expand all
165 std::pair<std::string, std::string>(headerName, headerValue)); 123 std::pair<std::string, std::string>(headerName, headerValue));
166 124
167 nextHeaderNameStart++; 125 nextHeaderNameStart++;
168 prevHeaderStart = nextHeaderNameStart; 126 prevHeaderStart = nextHeaderNameStart;
169 } 127 }
170 } 128 }
171 129
172 // Get the response status code 130 // Get the response status code
173 std::wstring statusStr; 131 std::wstring statusStr;
174 DWORD statusLen = 0; 132 DWORD statusLen = 0;
175 res = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_ NAME_BY_INDEX, &statusStr[0], &statusLen, WINHTTP_NO_HEADER_INDEX); 133 res = HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &statusStr[0], &statusLe n, 0);
176 if (statusLen == 0) 134 if (statusLen == 0)
177 { 135 {
178 throw std::exception("Can't parse the status code"); 136 throw std::exception("Can't parse the status code");
179 } 137 }
180 statusStr.resize(statusLen / sizeof(std::wstring::value_type) + 1); 138 statusStr.resize(statusLen / sizeof(std::wstring::value_type) + 1);
181 res = WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE, WINHTTP_HEADER_ NAME_BY_INDEX, &statusStr[0], &statusLen, WINHTTP_NO_HEADER_INDEX); 139 res = HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &statusStr[0], &statusLe n, 0);
182 if ((statusLen == 0) || (!res)) 140 if ((statusLen == 0) || (!res))
183 { 141 {
184 throw std::exception("Can't parse the status code"); 142 throw std::exception("Can't parse the status code");
185 } 143 }
186 std::wistringstream(statusStr) >> result->responseStatus; 144 std::wistringstream(statusStr) >> result->responseStatus;
187 result->status = AdblockPlus::DefaultWebRequest::NS_OK; 145 result->status = AdblockPlus::DefaultWebRequest::NS_OK;
188 146
189 } 147 }
190 148
191 AdblockPlus::ServerResponse AdblockPlus::DefaultWebRequest::GET( 149 AdblockPlus::ServerResponse AdblockPlus::DefaultWebRequest::GET(
(...skipping 14 matching lines...) Expand all
206 headersString += requestHeaders[i].first + ": "; 164 headersString += requestHeaders[i].first + ": ";
207 headersString += requestHeaders[i].second + "\r\n"; 165 headersString += requestHeaders[i].second + "\r\n";
208 } 166 }
209 std::wstring headers = Utils::ToUtf16String(headersString); 167 std::wstring headers = Utils::ToUtf16String(headersString);
210 168
211 WinHttpHandle hSession(0), hConnect(0), hRequest(0); 169 WinHttpHandle hSession(0), hConnect(0), hRequest(0);
212 170
213 // Use WinHttpOpen to obtain a session handle. 171 // Use WinHttpOpen to obtain a session handle.
214 std::wstring proxyName, proxyBypass; 172 std::wstring proxyName, proxyBypass;
215 173
216 GetProxySettings(proxyName, proxyBypass); 174 hSession.handle = InternetOpen(L"Adblock Plus", INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
217 if (proxyName.empty()) 175
218 {
219 hSession.handle = WinHttpOpen(L"Adblock Plus", WINHTTP_ACCESS_TYPE_DEFAULT_P ROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
220 }
221 else
222 {
223 hSession.handle = WinHttpOpen(L"Adblock Plus", WINHTTP_ACCESS_TYPE_DEFAULT_P ROXY, proxyName.c_str(), proxyBypass.c_str(), 0);
224
225
226 // Make sure the proxy is set. Just providing it to the above call is not en ough sometimes
227 WINHTTP_PROXY_INFO proxyInfo;
228 proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
229 proxyInfo.lpszProxy = (LPWSTR)proxyName.c_str();
230 proxyInfo.lpszProxyBypass = (LPWSTR)proxyBypass.c_str();
231 WinHttpSetOption(hSession.handle, WINHTTP_OPTION_PROXY , &proxyInfo, sizeof( WINHTTP_PROXY_INFO));
232 }
233 if (!hSession) 176 if (!hSession)
234 { 177 {
235 result.status = WindowsErrorToGeckoError(GetLastError()); 178 result.status = WindowsErrorToGeckoError(GetLastError());
236 return result; 179 return result;
237 } 180 }
238 URL_COMPONENTS urlComponents; 181 URL_COMPONENTS urlComponents;
239 182
240 // Initialize the URL_COMPONENTS structure. 183 // Initialize the URL_COMPONENTS structure.
241 ZeroMemory(&urlComponents, sizeof(urlComponents)); 184 ZeroMemory(&urlComponents, sizeof(urlComponents));
242 urlComponents.dwStructSize = sizeof(urlComponents); 185 urlComponents.dwStructSize = sizeof(urlComponents);
243 186
244 // Set required component lengths to non-zero so that they are cracked. 187 // Set required component lengths to non-zero so that they are cracked.
245 urlComponents.dwSchemeLength = (DWORD)-1; 188 urlComponents.dwSchemeLength = (DWORD)-1;
246 urlComponents.dwHostNameLength = (DWORD)-1; 189 urlComponents.dwHostNameLength = (DWORD)-1;
247 urlComponents.dwUrlPathLength = (DWORD)-1; 190 urlComponents.dwUrlPathLength = (DWORD)-1;
248 urlComponents.dwExtraInfoLength = (DWORD)-1; 191 urlComponents.dwExtraInfoLength = (DWORD)-1;
249 res = WinHttpCrackUrl(canonizedUrl.c_str(), canonizedUrl.length(), 0, &urlComp onents); 192 res = InternetCrackUrl(canonizedUrl.c_str(), canonizedUrl.length(), 0, &urlCom ponents);
250 if (!res) 193 if (!res)
251 { 194 {
252 result.status = WindowsErrorToGeckoError(GetLastError()); 195 result.status = WindowsErrorToGeckoError(GetLastError());
253 return result; 196 return result;
254 } 197 }
255 std::wstring hostName(urlComponents.lpszHostName, urlComponents.dwHostNameLeng th); 198 std::wstring hostName(urlComponents.lpszHostName, urlComponents.dwHostNameLeng th);
256 bool isSecure = urlComponents.nScheme == INTERNET_SCHEME_HTTPS; 199 bool isSecure = urlComponents.nScheme == INTERNET_SCHEME_HTTPS;
200 // Create an HTTP request handle.
257 if (isSecure) 201 if (isSecure)
258 { 202 {
259 hConnect.handle = WinHttpConnect(hSession, hostName.c_str(), urlComponents.n Port, 0); 203 hConnect.handle = InternetConnect(hSession, hostName.c_str(), urlComponents. nPort, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
260 } 204 }
261 else 205 else
262 { 206 {
263 hConnect.handle = WinHttpConnect(hSession, hostName.c_str(), urlComponents.n Port, 0); 207 hConnect.handle = InternetConnect(hSession, hostName.c_str(), urlComponents. nPort, 0, 0, INTERNET_SERVICE_HTTP, 0, 0);
264 } 208 }
Wladimir Palant 2013/10/08 07:24:28 Am I missing something or is this if block complet
265
266
267 // Create an HTTP request handle.
268 if (!hConnect) 209 if (!hConnect)
269 { 210 {
270 result.status = WindowsErrorToGeckoError(GetLastError()); 211 result.status = WindowsErrorToGeckoError(GetLastError());
271 return result; 212 return result;
272 } 213 }
273 DWORD flags = 0; 214 DWORD flags = 0;
274 if (isSecure) 215 if (isSecure)
275 { 216 {
276 flags = WINHTTP_FLAG_SECURE; 217 flags = INTERNET_FLAG_SECURE;
277 } 218 }
278 hRequest.handle = WinHttpOpenRequest(hConnect, L"GET", urlComponents.lpszUrlPa th, 0, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, flags); 219 hRequest.handle = HttpOpenRequest(hConnect, L"GET", urlComponents.lpszUrlPath, L"HTTP/1.1", 0, 0, flags, 0);
279 220
280 // Send a request. 221 // Send a request.
281 if (!hRequest) 222 if (!hRequest)
282 { 223 {
283 result.status = WindowsErrorToGeckoError(GetLastError()); 224 result.status = WindowsErrorToGeckoError(GetLastError());
284 return result; 225 return result;
285 } 226 }
286 HTTP_VERSION_INFO ver; 227 BOOL b = TRUE;
287 DWORD l = sizeof(ver); 228 res = InternetSetOption(hRequest, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b) );
Oleksandr 2013/10/07 21:36:18 This is a key change. Since WinHTTP does not suppo
288 WinHttpQueryOption(NULL, WINHTTP_OPTION_HTTP_VERSION, (void*)&ver, &l); 229 if (res == TRUE)
Wladimir Palant 2013/09/04 07:21:25 Nit: explicit conversion to void* is unnecessary I
289 if (ver.dwMinorVersion == 1)
Oleksandr 2013/09/03 11:49:41 No compression for IE6 (HTTP 1.0)
Wladimir Palant 2013/09/04 07:21:25 I don't like hardcoding the expectation here that
290 { 230 {
291 headers += L"Accept-Encoding: gzip, deflate\r\n"; 231 headers += L"Accept-Encoding: gzip, deflate\r\n";
292 } 232 }
293 if (headers.length() > 0) 233 if (headers.length() > 0)
294 { 234 {
295 res = ::WinHttpSendRequest(hRequest, headers.c_str(), headers.length(), WINH TTP_NO_REQUEST_DATA, 0, 0, 0); 235 res = ::HttpSendRequest(hRequest, headers.c_str(), headers.length(), 0, 0);
296 } 236 }
297 else 237 else
298 { 238 {
299 res = ::WinHttpSendRequest(hRequest, 0, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0) ; 239 res = ::HttpSendRequest(hRequest, 0, 0, 0, 0);
300 } 240 }
301 241
302 // End the request. 242 // End the request.
303 if (!res)
304 {
305 result.status = WindowsErrorToGeckoError(GetLastError());
306 return result;
307 }
308
309 res = WinHttpReceiveResponse(hRequest, 0);
310 if (!res) 243 if (!res)
311 { 244 {
312 result.status = WindowsErrorToGeckoError(GetLastError()); 245 result.status = WindowsErrorToGeckoError(GetLastError());
313 return result; 246 return result;
314 } 247 }
315 248
316 ParseResponseHeaders(hRequest, &result); 249 ParseResponseHeaders(hRequest, &result);
317 250
318 std::auto_ptr<char> outBuffer; 251 std::auto_ptr<char> outBuffer;
319 DWORD downloadSize, downloaded; 252 DWORD downloadSize, downloaded;
320 253
321 // Download the actual response 254 // Download the actual response
322 // Keep checking for data until there is nothing left. 255 // Keep checking for data until there is nothing left.
323 do 256 do
324 { 257 {
325 // Check for available data. 258 // Check for available data.
326 downloadSize = 0; 259 downloadSize = 0;
327 if (!WinHttpQueryDataAvailable(hRequest, &downloadSize)) 260 if (!InternetQueryDataAvailable(hRequest, &downloadSize, 0, 0))
328 { 261 {
329 result.responseStatus = WindowsErrorToGeckoError(GetLastError()); 262 result.responseStatus = WindowsErrorToGeckoError(GetLastError());
330 break; 263 break;
331 } 264 }
332 // Allocate space for the buffer. 265 // Allocate space for the buffer.
333 outBuffer.reset(new char[downloadSize+1]); 266 outBuffer.reset(new char[downloadSize+1]);
334 if (!outBuffer.get()) 267 if (!outBuffer.get())
335 { 268 {
336 //Out of memory? 269 //Out of memory?
337 result.status = NS_ERROR_OUT_OF_MEMORY; 270 result.status = NS_ERROR_OUT_OF_MEMORY;
338 break; 271 break;
339 } 272 }
340 else 273 else
341 { 274 {
342 // Read the data. 275 // Read the data.
343 ZeroMemory(outBuffer.get(), downloadSize+1); 276 ZeroMemory(outBuffer.get(), downloadSize+1);
344 277
345 if (WinHttpReadData(hRequest, outBuffer.get(), downloadSize, &downloaded)) 278 if (InternetReadFile(hRequest, outBuffer.get(), downloadSize, &downloaded) )
346 { 279 {
347 result.responseText.append(outBuffer.get(), downloaded); 280 result.responseText.append(outBuffer.get(), downloaded);
348 } 281 }
349 } 282 }
350 } while (downloadSize > 0); 283 } while (downloadSize > 0);
351 return result; 284 return result;
352 } 285 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld