1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2013-2016 - 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.
20 #include "UpdateDownloader.h"
21 #include "..\version.h"
23 CUpdateDownloader::CUpdateDownloader(HWND hwnd
, bool force
, UINT msg
, CEvent
*eventStop
)
27 , m_eventStop(eventStop
)
29 OSVERSIONINFOEX inf
= {0};
30 BruteforceGetWindowsVersionNumber(inf
);
32 m_sWindowsPlatform
= (inf
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) ? L
"NT" : L
"";
33 m_sWindowsVersion
.Format(L
"%ld.%ld", inf
.dwMajorVersion
, inf
.dwMinorVersion
);
34 if (inf
.wServicePackMajor
)
35 m_sWindowsServicePack
.Format(L
"SP%ld", inf
.wServicePackMajor
);
38 userAgent
.Format(L
"TortoiseGit %s; %s; Windows%s%s %s%s%s", _T(STRFILEVER
), _T(TGIT_PLATFORM
), m_sWindowsPlatform
.IsEmpty() ? L
"" : L
" ", (LPCTSTR
)m_sWindowsPlatform
, (LPCTSTR
)m_sWindowsVersion
, m_sWindowsServicePack
.IsEmpty() ? L
"" : L
" ", (LPCTSTR
)m_sWindowsServicePack
);
39 hOpenHandle
= InternetOpen(userAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, nullptr, nullptr, 0);
42 CUpdateDownloader::~CUpdateDownloader(void)
45 InternetCloseHandle(hOpenHandle
);
48 void CUpdateDownloader::BruteforceGetWindowsVersionNumber(OSVERSIONINFOEX
& osVersionInfo
)
50 osVersionInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOEX
);
51 osVersionInfo
.dwMajorVersion
= HIBYTE(_WIN32_WINNT_VISTA
);
52 osVersionInfo
.dwMinorVersion
= LOBYTE(_WIN32_WINNT_VISTA
);
53 osVersionInfo
.dwPlatformId
= VER_PLATFORM_WIN32_NT
;
55 ULONGLONG maskConditioMajor
= ::VerSetConditionMask(0, VER_MAJORVERSION
, VER_LESS
);
56 ULONGLONG maskConditioMinor
= ::VerSetConditionMask(0, VER_MINORVERSION
, VER_LESS
);
57 ULONGLONG maskConditioSPMajor
= ::VerSetConditionMask(0, VER_SERVICEPACKMAJOR
, VER_LESS
);
58 while (!::VerifyVersionInfo(&osVersionInfo
, VER_MAJORVERSION
, maskConditioMajor
))
60 ++osVersionInfo
.dwMajorVersion
;
61 osVersionInfo
.dwMinorVersion
= 0;
62 osVersionInfo
.wServicePackMajor
= 0;
63 osVersionInfo
.wServicePackMinor
= 0;
65 while (!::VerifyVersionInfo(&osVersionInfo
, VER_MINORVERSION
, maskConditioMinor
))
67 ++osVersionInfo
.dwMinorVersion
;
68 osVersionInfo
.wServicePackMajor
= 0;
69 osVersionInfo
.wServicePackMinor
= 0;
71 while (!::VerifyVersionInfo(&osVersionInfo
, VER_SERVICEPACKMAJOR
, maskConditioSPMajor
))
72 ++osVersionInfo
.wServicePackMajor
;
73 // detection of VER_SERVICEPACKMINOR doesn't work reliably
76 DWORD
CUpdateDownloader::DownloadFile(const CString
& url
, const CString
& dest
, bool showProgress
) const
80 URL_COMPONENTS urlComponents
= {0};
81 urlComponents
.dwStructSize
= sizeof(urlComponents
);
82 urlComponents
.lpszHostName
= hostname
.GetBufferSetLength(INTERNET_MAX_HOST_NAME_LENGTH
);
83 urlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
84 urlComponents
.lpszUrlPath
= urlpath
.GetBufferSetLength(INTERNET_MAX_PATH_LENGTH
);
85 urlComponents
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
86 if (!InternetCrackUrl(url
, url
.GetLength(), 0, &urlComponents
))
87 return GetLastError();
88 hostname
.ReleaseBuffer();
89 urlpath
.ReleaseBuffer();
92 DeleteUrlCacheEntry(url
);
95 BOOL enableDecoding
= InternetSetOption(hOpenHandle
, INTERNET_OPTION_HTTP_DECODING
, &bTrue
, sizeof(bTrue
));
97 bool isHttps
= urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
;
98 HINTERNET hConnectHandle
= InternetConnect(hOpenHandle
, hostname
, urlComponents
.nPort
, nullptr, nullptr, isHttps
? INTERNET_SCHEME_HTTP
: urlComponents
.nScheme
, 0, 0);
101 DWORD err
= GetLastError();
102 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s failed on InternetConnect: %d\n", (LPCTSTR
)url
, err
);
105 SCOPE_EXIT
{ InternetCloseHandle(hConnectHandle
); };
106 HINTERNET hResourceHandle
= HttpOpenRequest(hConnectHandle
, nullptr, urlpath
, nullptr, nullptr, nullptr, INTERNET_FLAG_KEEP_CONNECTION
| (isHttps
? INTERNET_FLAG_SECURE
: 0) | (m_bForce
? INTERNET_FLAG_HYPERLINK
: 0), 0);
107 if (!hResourceHandle
)
109 DWORD err
= GetLastError();
110 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s failed on HttpOpenRequest: %d\n", (LPCTSTR
)url
, err
);
113 SCOPE_EXIT
{ InternetCloseHandle(hResourceHandle
); };
116 HttpAddRequestHeaders(hResourceHandle
, L
"Accept-Encoding: gzip, deflate\r\n", (DWORD
)-1, HTTP_ADDREQ_FLAG_ADD
);
120 BOOL httpsendrequest
= HttpSendRequest(hResourceHandle
, nullptr, 0, nullptr, 0);
122 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);
124 if (dwError
== ERROR_INTERNET_FORCE_RETRY
)
126 else if (!httpsendrequest
)
128 DWORD err
= GetLastError();
129 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s failed: %d, %d\n", (LPCTSTR
)url
, httpsendrequest
, err
);
134 DWORD contentLength
= 0;
136 DWORD length
= sizeof(contentLength
);
137 HttpQueryInfo(hResourceHandle
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, (LPVOID
)&contentLength
, &length
, nullptr);
140 DWORD statusCode
= 0;
141 DWORD length
= sizeof(statusCode
);
142 if (!HttpQueryInfo(hResourceHandle
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, (LPVOID
)&statusCode
, &length
, nullptr) || statusCode
!= 200)
144 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s returned %d\n", (LPCTSTR
)url
, statusCode
);
145 if (statusCode
== 404)
146 return ERROR_FILE_NOT_FOUND
;
147 else if (statusCode
== 403)
148 return ERROR_ACCESS_DENIED
;
149 return (DWORD
)INET_E_DOWNLOAD_FAILURE
;
153 CFile destinationFile
;
154 if (!destinationFile
.Open(dest
, CFile::modeCreate
| CFile::modeWrite
))
156 return ERROR_ACCESS_DENIED
;
158 DWORD downloadedSum
= 0; // sum of bytes downloaded so far
161 DWORD size
; // size of the data available
162 if (!InternetQueryDataAvailable(hResourceHandle
, &size
, 0, 0))
164 DWORD err
= GetLastError();
165 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s failed on InternetQueryDataAvailable: %d\n", (LPCTSTR
)url
, err
);
169 DWORD downloaded
; // size of the downloaded data
170 auto buff
= std::make_unique
<char[]>(size
+ 1);
171 if (!InternetReadFile(hResourceHandle
, (LPVOID
)buff
.get(), size
, &downloaded
))
173 DWORD err
= GetLastError();
174 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download of %s failed on InternetReadFile: %d\n", (LPCTSTR
)url
, err
);
181 buff
[downloaded
] = '\0';
182 destinationFile
.Write(buff
.get(), downloaded
);
184 downloadedSum
+= downloaded
;
189 ASSERT(m_uiMsg
&& m_eventStop
);
191 if (contentLength
== 0) // got no content-length from webserver
193 DOWNLOADSTATUS downloadStatus
= { 0, 1 + 1 }; // + 1 for download of signature file
194 ::SendMessage(m_hWnd
, m_uiMsg
, 0, reinterpret_cast<LPARAM
>(&downloadStatus
));
198 if (downloadedSum
> contentLength
)
199 downloadedSum
= contentLength
- 1;
200 DOWNLOADSTATUS downloadStatus
= { downloadedSum
, contentLength
+ 1 }; // + 1 for download of signature file
201 ::SendMessage(m_hWnd
, m_uiMsg
, 0, reinterpret_cast<LPARAM
>(&downloadStatus
));
204 if (::WaitForSingleObject(*m_eventStop
, 0) == WAIT_OBJECT_0
)
206 return (DWORD
)E_ABORT
; // canceled by the user
211 if (downloadedSum
== 0)
213 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) L
": Download size of %s was zero.\n", (LPCTSTR
)url
);
214 return (DWORD
)INET_E_DOWNLOAD_FAILURE
;
216 return ERROR_SUCCESS
;