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.
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
);
33 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
);
34 hOpenHandle
= InternetOpen(userAgent
, INTERNET_OPEN_TYPE_PRECONFIG
, nullptr, nullptr, 0);
37 CUpdateDownloader::~CUpdateDownloader(void)
40 InternetCloseHandle(hOpenHandle
);
43 void CUpdateDownloader::BruteforceGetWindowsVersionNumber(OSVERSIONINFOEX
& osVersionInfo
)
45 osVersionInfo
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOEX
);
46 GetVersionEx((OSVERSIONINFO
*)&osVersionInfo
);
48 ULONGLONG maskConditioMajor
= ::VerSetConditionMask(0, VER_MAJORVERSION
, VER_LESS
);
49 ULONGLONG maskConditioMinor
= ::VerSetConditionMask(0, VER_MINORVERSION
, VER_LESS
);
50 while (!::VerifyVersionInfo(&osVersionInfo
, VER_MAJORVERSION
, maskConditioMajor
))
52 ++osVersionInfo
.dwMajorVersion
;
53 osVersionInfo
.dwMinorVersion
= 0;
55 while (!::VerifyVersionInfo(&osVersionInfo
, VER_MINORVERSION
, maskConditioMinor
))
56 ++osVersionInfo
.dwMinorVersion
;
59 BOOL
CUpdateDownloader::DownloadFile(const CString
& url
, const CString
& dest
, bool showProgress
) const
63 URL_COMPONENTS urlComponents
= {0};
64 urlComponents
.dwStructSize
= sizeof(urlComponents
);
65 urlComponents
.lpszHostName
= hostname
.GetBufferSetLength(INTERNET_MAX_HOST_NAME_LENGTH
);
66 urlComponents
.dwHostNameLength
= INTERNET_MAX_HOST_NAME_LENGTH
;
67 urlComponents
.lpszUrlPath
= urlpath
.GetBufferSetLength(INTERNET_MAX_PATH_LENGTH
);
68 urlComponents
.dwUrlPathLength
= INTERNET_MAX_PATH_LENGTH
;
69 if (!InternetCrackUrl(url
, url
.GetLength(), 0, &urlComponents
))
70 return GetLastError();
71 hostname
.ReleaseBuffer();
72 urlpath
.ReleaseBuffer();
75 DeleteUrlCacheEntry(url
);
77 HINTERNET hConnectHandle
= InternetConnect(hOpenHandle
, hostname
, urlComponents
.nPort
, nullptr, nullptr, urlComponents
.nScheme
, 0, 0);
80 DWORD err
= GetLastError();
81 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s failed on InternetConnect: %d\n"), url
, err
);
84 HINTERNET hResourceHandle
= HttpOpenRequest(hConnectHandle
, nullptr, urlpath
, nullptr, nullptr, nullptr, INTERNET_FLAG_KEEP_CONNECTION
, 0);
87 DWORD err
= GetLastError();
88 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s failed on HttpOpenRequest: %d\n"), url
, err
);
89 InternetCloseHandle(hConnectHandle
);
95 BOOL httpsendrequest
= HttpSendRequest(hResourceHandle
, nullptr, 0, nullptr, 0);
97 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);
99 if (dwError
== ERROR_INTERNET_FORCE_RETRY
)
101 else if (!httpsendrequest
)
103 DWORD err
= GetLastError();
104 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s failed: %d, %d\n"), url
, httpsendrequest
, err
);
105 InternetCloseHandle(hResourceHandle
);
106 InternetCloseHandle(hConnectHandle
);
111 DWORD contentLength
= 0;
113 DWORD length
= sizeof(contentLength
);
114 HttpQueryInfo(hResourceHandle
, HTTP_QUERY_CONTENT_LENGTH
| HTTP_QUERY_FLAG_NUMBER
, (LPVOID
)&contentLength
, &length
, NULL
);
117 DWORD statusCode
= 0;
118 DWORD length
= sizeof(statusCode
);
119 if (!HttpQueryInfo(hResourceHandle
, HTTP_QUERY_STATUS_CODE
| HTTP_QUERY_FLAG_NUMBER
, (LPVOID
)&statusCode
, &length
, NULL
) || statusCode
!= 200)
121 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s returned %d\n"), url
, statusCode
);
122 InternetCloseHandle(hResourceHandle
);
123 InternetCloseHandle(hConnectHandle
);
124 if (statusCode
== 404)
125 return ERROR_FILE_NOT_FOUND
;
126 else if (statusCode
== 403)
127 return ERROR_ACCESS_DENIED
;
128 return INET_E_DOWNLOAD_FAILURE
;
132 CFile
destinationFile(dest
, CFile::modeCreate
| CFile::modeWrite
);
133 DWORD downloadedSum
= 0; // sum of bytes downloaded so far
136 DWORD size
; // size of the data available
137 if (!InternetQueryDataAvailable(hResourceHandle
, &size
, 0, 0))
139 DWORD err
= GetLastError();
140 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s failed on InternetQueryDataAvailable: %d\n"), url
, err
);
141 InternetCloseHandle(hResourceHandle
);
142 InternetCloseHandle(hConnectHandle
);
146 DWORD downloaded
; // size of the downloaded data
147 LPTSTR lpszData
= new TCHAR
[size
+ 1];
148 if (!InternetReadFile(hResourceHandle
, (LPVOID
)lpszData
, size
, &downloaded
))
151 DWORD err
= GetLastError();
152 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download of %s failed on InternetReadFile: %d\n"), url
, err
);
153 InternetCloseHandle(hResourceHandle
);
154 InternetCloseHandle(hConnectHandle
);
164 lpszData
[downloaded
] = '\0';
165 destinationFile
.Write(lpszData
, downloaded
);
168 downloadedSum
+= downloaded
;
173 ASSERT(m_uiMsg
&& m_eventStop
);
175 if (contentLength
== 0) // got no content-length from webserver
177 DOWNLOADSTATUS downloadStatus
= { 0, 1 + 1 }; // + 1 for download of signature file
178 ::SendMessage(m_hWnd
, m_uiMsg
, 0, reinterpret_cast<LPARAM
>(&downloadStatus
));
182 if (downloadedSum
> contentLength
)
183 downloadedSum
= contentLength
- 1;
184 DOWNLOADSTATUS downloadStatus
= { downloadedSum
, contentLength
+ 1 }; // + 1 for download of signature file
185 ::SendMessage(m_hWnd
, m_uiMsg
, 0, reinterpret_cast<LPARAM
>(&downloadStatus
));
188 if (::WaitForSingleObject(*m_eventStop
, 0) == WAIT_OBJECT_0
)
190 InternetCloseHandle(hResourceHandle
);
191 InternetCloseHandle(hConnectHandle
);
192 return E_ABORT
; // canceled by the user
196 destinationFile
.Close();
197 InternetCloseHandle(hResourceHandle
);
198 InternetCloseHandle(hConnectHandle
);
199 if (downloadedSum
== 0)
201 CTraceToOutputDebugString::Instance()(_T(__FUNCTION__
) _T(": Download size of %s was zero.\n"), url
);
202 return INET_E_DOWNLOAD_FAILURE
;