Commit 5ca20089 authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

urlmon: Reimplement URLDownloadToFileW on top of URL moniker.

parent 9d0ae904
......@@ -11,6 +11,7 @@ C_SRCS = \
bindctx.c \
binding.c \
bindprot.c \
download.c \
file.c \
format.c \
ftp.c \
......
......@@ -1254,173 +1254,6 @@ HRESULT WINAPI URLDownloadToFileA(LPUNKNOWN pCaller,
}
/***********************************************************************
* URLDownloadToFileW (URLMON.@)
*
* Downloads URL szURL to rile szFileName and call lpfnCB callback to
* report progress.
*
* PARAMS
* pCaller [I] controlling IUnknown interface.
* szURL [I] URL of the file to download
* szFileName [I] file name to store the content of the URL
* dwReserved [I] reserved - set to 0
* lpfnCB [I] callback for progress report
*
* RETURNS
* S_OK on success
* E_OUTOFMEMORY when going out of memory
*/
HRESULT WINAPI URLDownloadToFileW(LPUNKNOWN pCaller,
LPCWSTR szURL,
LPCWSTR szFileName,
DWORD dwReserved,
LPBINDSTATUSCALLBACK lpfnCB)
{
HINTERNET hinternet, hcon, hreq;
BOOL r;
CHAR buffer[0x1000];
DWORD sz, total, written;
DWORD total_size = 0xFFFFFFFF, arg_size = sizeof(total_size);
URL_COMPONENTSW url;
WCHAR host[0x80], path[0x100];
HANDLE hfile;
static const WCHAR wszAppName[]={'u','r','l','m','o','n','.','d','l','l',0};
/* Note: all error codes would need to be checked agains real Windows behaviour... */
TRACE("(%p,%s,%s,%08x,%p) stub!\n", pCaller, debugstr_w(szURL), debugstr_w(szFileName), dwReserved, lpfnCB);
if ((szURL == NULL) || (szFileName == NULL)) {
FIXME(" cannot accept NULL strings !\n");
return E_INVALIDARG;
}
/* Would be better to use the application name here rather than 'urlmon' :-/ */
hinternet = InternetOpenW(wszAppName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hinternet == NULL) {
return E_OUTOFMEMORY;
}
memset(&url, 0, sizeof(url));
url.dwStructSize = sizeof(url);
url.lpszHostName = host;
url.dwHostNameLength = sizeof(host);
url.lpszUrlPath = path;
url.dwUrlPathLength = sizeof(path);
if (!InternetCrackUrlW(szURL, 0, 0, &url)) {
InternetCloseHandle(hinternet);
return E_OUTOFMEMORY;
}
if (lpfnCB) {
if (IBindStatusCallback_OnProgress(lpfnCB, 0, 0, BINDSTATUS_CONNECTING, url.lpszHostName) == E_ABORT) {
InternetCloseHandle(hinternet);
return S_OK;
}
}
hcon = InternetConnectW(hinternet, url.lpszHostName, url.nPort,
url.lpszUserName, url.lpszPassword,
INTERNET_SERVICE_HTTP, 0, 0);
if (!hcon) {
InternetCloseHandle(hinternet);
return E_OUTOFMEMORY;
}
hreq = HttpOpenRequestW(hcon, NULL, url.lpszUrlPath, NULL, NULL, NULL, 0, 0);
if (!hreq) {
InternetCloseHandle(hinternet);
InternetCloseHandle(hcon);
return E_OUTOFMEMORY;
}
if (!HttpSendRequestW(hreq, NULL, 0, NULL, 0)) {
InternetCloseHandle(hinternet);
InternetCloseHandle(hcon);
InternetCloseHandle(hreq);
return E_OUTOFMEMORY;
}
if (HttpQueryInfoW(hreq, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
&total_size, &arg_size, NULL)) {
TRACE(" total size : %d\n", total_size);
}
hfile = CreateFileW(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL );
if (hfile == INVALID_HANDLE_VALUE) {
return E_ACCESSDENIED;
}
if (lpfnCB) {
if (IBindStatusCallback_OnProgress(lpfnCB, 0, total_size != 0xFFFFFFFF ? total_size : 0,
BINDSTATUS_BEGINDOWNLOADDATA, szURL) == E_ABORT) {
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return S_OK;
}
}
total = 0;
while (1) {
r = InternetReadFile(hreq, buffer, sizeof(buffer), &sz);
if (!r) {
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return E_OUTOFMEMORY;
}
if (!sz)
break;
total += sz;
if (lpfnCB) {
if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
BINDSTATUS_DOWNLOADINGDATA, szURL) == E_ABORT) {
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return S_OK;
}
}
if (!WriteFile(hfile, buffer, sz, &written, NULL)) {
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return E_OUTOFMEMORY;
}
}
if (lpfnCB) {
if (IBindStatusCallback_OnProgress(lpfnCB, total, total_size != 0xFFFFFFFF ? total_size : 0,
BINDSTATUS_ENDDOWNLOADDATA, szURL) == E_ABORT) {
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return S_OK;
}
}
InternetCloseHandle(hreq);
InternetCloseHandle(hcon);
InternetCloseHandle(hinternet);
CloseHandle(hfile);
return S_OK;
}
/***********************************************************************
* URLDownloadToCacheFileA (URLMON.@)
*/
HRESULT WINAPI URLDownloadToCacheFileA(LPUNKNOWN lpUnkCaller, LPCSTR szURL, LPSTR szFileName,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment