Set Accept-Encoding only if INTERNET_OPTION_HTTP_DECODING could be set on the connection
[TortoiseGit.git] / src / TortoiseProc / UpdateDownloader.cpp
blob92767ab491ef58b34cf9a771b7b64091562d3c72
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "UpdateDownloader.h"
21 #include "..\version.h"
22 #include "SysInfo.h"
24 CUpdateDownloader::CUpdateDownloader(HWND hwnd, bool force, UINT msg, CEvent *eventStop)
25 : m_hWnd(hwnd)
26 , m_bForce(force)
27 , m_uiMsg(msg)
28 , m_eventStop(eventStop)
30 OSVERSIONINFOEX inf = {0};
31 BruteforceGetWindowsVersionNumber(inf);
33 CString userAgent;
34 userAgent.Format(L"TortoiseGit %s; %s; Windows%s %ld.%ld", _T(STRFILEVER), _T(TGIT_PLATFORM), (inf.dwPlatformId == VER_PLATFORM_WIN32_NT) ? _T(" NT") : _T(""), inf.dwMajorVersion, inf.dwMinorVersion);
35 hOpenHandle = InternetOpen(userAgent, INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, 0);
38 CUpdateDownloader::~CUpdateDownloader(void)
40 if (hOpenHandle)
41 InternetCloseHandle(hOpenHandle);
44 void CUpdateDownloader::BruteforceGetWindowsVersionNumber(OSVERSIONINFOEX& osVersionInfo)
46 osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
47 GetVersionEx((OSVERSIONINFO *)&osVersionInfo);
49 ULONGLONG maskConditioMajor = ::VerSetConditionMask(0, VER_MAJORVERSION, VER_LESS);
50 ULONGLONG maskConditioMinor = ::VerSetConditionMask(0, VER_MINORVERSION, VER_LESS);
51 while (!::VerifyVersionInfo(&osVersionInfo, VER_MAJORVERSION, maskConditioMajor))
53 ++osVersionInfo.dwMajorVersion;
54 osVersionInfo.dwMinorVersion = 0;
56 while (!::VerifyVersionInfo(&osVersionInfo, VER_MINORVERSION, maskConditioMinor))
57 ++osVersionInfo.dwMinorVersion;
60 BOOL CUpdateDownloader::DownloadFile(const CString& url, const CString& dest, bool showProgress) const
62 CString hostname;
63 CString urlpath;
64 URL_COMPONENTS urlComponents = {0};
65 urlComponents.dwStructSize = sizeof(urlComponents);
66 urlComponents.lpszHostName = hostname.GetBufferSetLength(INTERNET_MAX_HOST_NAME_LENGTH);
67 urlComponents.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
68 urlComponents.lpszUrlPath = urlpath.GetBufferSetLength(INTERNET_MAX_PATH_LENGTH);
69 urlComponents.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
70 if (!InternetCrackUrl(url, url.GetLength(), 0, &urlComponents))
71 return GetLastError();
72 hostname.ReleaseBuffer();
73 urlpath.ReleaseBuffer();
75 if (m_bForce)
76 DeleteUrlCacheEntry(url);
78 BOOL bTrue = TRUE;
79 BOOL enableDecoding = InternetSetOption(hOpenHandle, INTERNET_OPTION_HTTP_DECODING, &bTrue, sizeof(bTrue));
81 bool isHttps = urlComponents.nScheme == INTERNET_SCHEME_HTTPS;
82 HINTERNET hConnectHandle = InternetConnect(hOpenHandle, hostname, urlComponents.nPort, nullptr, nullptr, isHttps ? INTERNET_SCHEME_HTTP : urlComponents.nScheme, 0, 0);
83 if (!hConnectHandle)
85 DWORD err = GetLastError();
86 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s failed on InternetConnect: %d\n"), url, err);
87 return err;
89 HINTERNET hResourceHandle = HttpOpenRequest(hConnectHandle, nullptr, urlpath, nullptr, nullptr, nullptr, INTERNET_FLAG_KEEP_CONNECTION | (isHttps ? INTERNET_FLAG_SECURE : 0), 0);
90 if (!hResourceHandle)
92 DWORD err = GetLastError();
93 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s failed on HttpOpenRequest: %d\n"), url, err);
94 InternetCloseHandle(hConnectHandle);
95 return err;
98 if (enableDecoding && SysInfo::Instance().IsVistaOrLater())
99 HttpAddRequestHeaders(hResourceHandle, L"Accept-Encoding: gzip, deflate\r\n", (DWORD)-1, HTTP_ADDREQ_FLAG_ADD);
102 resend:
103 BOOL httpsendrequest = HttpSendRequest(hResourceHandle, nullptr, 0, nullptr, 0);
105 DWORD dwError = InternetErrorDlg(m_hWnd, hResourceHandle, ERROR_SUCCESS, FLAGS_ERROR_UI_FILTER_FOR_ERRORS | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS | FLAGS_ERROR_UI_FLAGS_GENERATE_DATA, nullptr);
107 if (dwError == ERROR_INTERNET_FORCE_RETRY)
108 goto resend;
109 else if (!httpsendrequest)
111 DWORD err = GetLastError();
112 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s failed: %d, %d\n"), url, httpsendrequest, err);
113 InternetCloseHandle(hResourceHandle);
114 InternetCloseHandle(hConnectHandle);
115 return err;
119 DWORD contentLength = 0;
121 DWORD length = sizeof(contentLength);
122 HttpQueryInfo(hResourceHandle, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, (LPVOID)&contentLength, &length, NULL);
125 DWORD statusCode = 0;
126 DWORD length = sizeof(statusCode);
127 if (!HttpQueryInfo(hResourceHandle, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, (LPVOID)&statusCode, &length, NULL) || statusCode != 200)
129 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s returned %d\n"), url, statusCode);
130 InternetCloseHandle(hResourceHandle);
131 InternetCloseHandle(hConnectHandle);
132 if (statusCode == 404)
133 return ERROR_FILE_NOT_FOUND;
134 else if (statusCode == 403)
135 return ERROR_ACCESS_DENIED;
136 return INET_E_DOWNLOAD_FAILURE;
140 CFile destinationFile;
141 if (!destinationFile.Open(dest, CFile::modeCreate | CFile::modeWrite))
143 InternetCloseHandle(hResourceHandle);
144 InternetCloseHandle(hConnectHandle);
145 return ERROR_ACCESS_DENIED;
147 DWORD downloadedSum = 0; // sum of bytes downloaded so far
150 DWORD size; // size of the data available
151 if (!InternetQueryDataAvailable(hResourceHandle, &size, 0, 0))
153 DWORD err = GetLastError();
154 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s failed on InternetQueryDataAvailable: %d\n"), url, err);
155 InternetCloseHandle(hResourceHandle);
156 InternetCloseHandle(hConnectHandle);
157 return err;
160 DWORD downloaded; // size of the downloaded data
161 LPTSTR lpszData = new TCHAR[size + 1];
162 if (!InternetReadFile(hResourceHandle, (LPVOID)lpszData, size, &downloaded))
164 delete[] lpszData;
165 DWORD err = GetLastError();
166 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download of %s failed on InternetReadFile: %d\n"), url, err);
167 InternetCloseHandle(hResourceHandle);
168 InternetCloseHandle(hConnectHandle);
169 return err;
172 if (downloaded == 0)
174 delete[] lpszData;
175 break;
178 lpszData[downloaded] = '\0';
179 destinationFile.Write(lpszData, downloaded);
180 delete[] lpszData;
182 downloadedSum += downloaded;
184 if (!showProgress)
185 continue;
187 ASSERT(m_uiMsg && m_eventStop);
189 if (contentLength == 0) // got no content-length from webserver
191 DOWNLOADSTATUS downloadStatus = { 0, 1 + 1 }; // + 1 for download of signature file
192 ::SendMessage(m_hWnd, m_uiMsg, 0, reinterpret_cast<LPARAM>(&downloadStatus));
194 else
196 if (downloadedSum > contentLength)
197 downloadedSum = contentLength - 1;
198 DOWNLOADSTATUS downloadStatus = { downloadedSum, contentLength + 1 }; // + 1 for download of signature file
199 ::SendMessage(m_hWnd, m_uiMsg, 0, reinterpret_cast<LPARAM>(&downloadStatus));
202 if (::WaitForSingleObject(*m_eventStop, 0) == WAIT_OBJECT_0)
204 InternetCloseHandle(hResourceHandle);
205 InternetCloseHandle(hConnectHandle);
206 return E_ABORT; // canceled by the user
209 while (true);
210 destinationFile.Close();
211 InternetCloseHandle(hResourceHandle);
212 InternetCloseHandle(hConnectHandle);
213 if (downloadedSum == 0)
215 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": Download size of %s was zero.\n"), url);
216 return INET_E_DOWNLOAD_FAILURE;
218 return 0;