2008-02-27 02:50:43 +01:00
|
|
|
/*
|
|
|
|
* Queue Manager (BITS) File
|
|
|
|
*
|
2008-03-13 23:57:19 +01:00
|
|
|
* Copyright 2007, 2008 Google (Roy Shea, Dan Hipschman)
|
2008-02-27 02:50:43 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2008-03-13 23:57:19 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
#include "winreg.h"
|
2015-06-12 15:04:44 +02:00
|
|
|
#include "winhttp.h"
|
2015-06-05 10:33:53 +02:00
|
|
|
#define COBJMACROS
|
2008-02-27 02:50:43 +01:00
|
|
|
#include "qmgr.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
|
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
static inline BackgroundCopyFileImpl *impl_from_IBackgroundCopyFile2(
|
|
|
|
IBackgroundCopyFile2 *iface)
|
2013-01-18 05:38:49 +01:00
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
return CONTAINING_RECORD(iface, BackgroundCopyFileImpl, IBackgroundCopyFile2_iface);
|
2013-01-18 05:38:49 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 10:22:09 +01:00
|
|
|
static HRESULT WINAPI BackgroundCopyFile_QueryInterface(
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2 *iface,
|
2008-02-27 02:50:43 +01:00
|
|
|
REFIID riid,
|
2013-01-18 05:38:49 +01:00
|
|
|
void **obj)
|
2008-02-27 02:50:43 +01:00
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
2013-01-18 05:38:49 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
TRACE("(%p)->(%s %p)\n", file, debugstr_guid(riid), obj);
|
2008-02-27 02:50:43 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
if (IsEqualGUID(riid, &IID_IUnknown) ||
|
|
|
|
IsEqualGUID(riid, &IID_IBackgroundCopyFile) ||
|
|
|
|
IsEqualGUID(riid, &IID_IBackgroundCopyFile2))
|
2008-02-27 02:50:43 +01:00
|
|
|
{
|
2013-01-18 05:38:49 +01:00
|
|
|
*obj = iface;
|
2015-06-05 10:33:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*obj = NULL;
|
|
|
|
return E_NOINTERFACE;
|
2008-02-27 02:50:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2_AddRef(iface);
|
|
|
|
return S_OK;
|
2008-02-27 02:50:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
static ULONG WINAPI BackgroundCopyFile_AddRef(
|
|
|
|
IBackgroundCopyFile2 *iface)
|
2013-01-18 05:38:49 +01:00
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
|
|
|
ULONG ref = InterlockedIncrement(&file->ref);
|
|
|
|
TRACE("(%p)->(%d)\n", file, ref);
|
2013-01-18 05:38:49 +01:00
|
|
|
return ref;
|
|
|
|
}
|
2008-02-27 02:50:43 +01:00
|
|
|
|
2013-11-26 10:22:09 +01:00
|
|
|
static ULONG WINAPI BackgroundCopyFile_Release(
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2 *iface)
|
2008-02-27 02:50:43 +01:00
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
|
|
|
ULONG ref = InterlockedDecrement(&file->ref);
|
2008-02-27 02:50:43 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
TRACE("(%p)->(%d)\n", file, ref);
|
2013-01-18 05:38:49 +01:00
|
|
|
|
2008-02-27 02:50:43 +01:00
|
|
|
if (ref == 0)
|
2013-01-18 05:39:15 +01:00
|
|
|
{
|
2015-06-05 10:34:21 +02:00
|
|
|
IBackgroundCopyJob3_Release(&file->owner->IBackgroundCopyJob3_iface);
|
2015-06-05 10:33:53 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, file->info.LocalName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, file->info.RemoteName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, file);
|
2013-01-18 05:39:15 +01:00
|
|
|
}
|
2008-02-27 02:50:43 +01:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2008-02-29 03:59:26 +01:00
|
|
|
/* Get the remote name of a background copy file */
|
2013-11-26 10:22:09 +01:00
|
|
|
static HRESULT WINAPI BackgroundCopyFile_GetRemoteName(
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2 *iface,
|
2008-02-27 02:50:43 +01:00
|
|
|
LPWSTR *pVal)
|
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
2008-02-29 03:59:26 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
TRACE("(%p)->(%p)\n", file, pVal);
|
2008-02-29 03:59:26 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
return return_strval(file->info.RemoteName, pVal);
|
2008-02-27 02:50:43 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 10:22:09 +01:00
|
|
|
static HRESULT WINAPI BackgroundCopyFile_GetLocalName(
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2 *iface,
|
2008-02-27 02:50:43 +01:00
|
|
|
LPWSTR *pVal)
|
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
2008-02-29 03:59:26 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
TRACE("(%p)->(%p)\n", file, pVal);
|
2008-02-29 03:59:26 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
return return_strval(file->info.LocalName, pVal);
|
2008-02-27 02:50:43 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 10:22:09 +01:00
|
|
|
static HRESULT WINAPI BackgroundCopyFile_GetProgress(
|
2015-06-05 10:33:53 +02:00
|
|
|
IBackgroundCopyFile2 *iface,
|
2008-02-27 02:50:43 +01:00
|
|
|
BG_FILE_PROGRESS *pVal)
|
|
|
|
{
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
2008-02-29 04:00:12 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
TRACE("(%p)->(%p)\n", file, pVal);
|
2013-11-26 10:22:09 +01:00
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
EnterCriticalSection(&file->owner->cs);
|
2015-06-23 18:59:40 +02:00
|
|
|
*pVal = file->fileProgress;
|
2015-06-05 10:33:53 +02:00
|
|
|
LeaveCriticalSection(&file->owner->cs);
|
2008-02-29 04:00:12 +01:00
|
|
|
|
|
|
|
return S_OK;
|
2008-02-27 02:50:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
static HRESULT WINAPI BackgroundCopyFile_GetFileRanges(
|
|
|
|
IBackgroundCopyFile2 *iface,
|
|
|
|
DWORD *RangeCount,
|
|
|
|
BG_FILE_RANGE **Ranges)
|
|
|
|
{
|
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
|
|
|
FIXME("(%p)->(%p %p)\n", file, RangeCount, Ranges);
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT WINAPI BackgroundCopyFile_SetRemoteName(
|
|
|
|
IBackgroundCopyFile2 *iface,
|
|
|
|
LPCWSTR Val)
|
|
|
|
{
|
|
|
|
BackgroundCopyFileImpl *file = impl_from_IBackgroundCopyFile2(iface);
|
|
|
|
FIXME("(%p)->(%s)\n", file, debugstr_w(Val));
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const IBackgroundCopyFile2Vtbl BackgroundCopyFile2Vtbl =
|
2008-02-27 02:50:43 +01:00
|
|
|
{
|
2013-11-26 10:22:09 +01:00
|
|
|
BackgroundCopyFile_QueryInterface,
|
|
|
|
BackgroundCopyFile_AddRef,
|
|
|
|
BackgroundCopyFile_Release,
|
|
|
|
BackgroundCopyFile_GetRemoteName,
|
|
|
|
BackgroundCopyFile_GetLocalName,
|
2015-06-05 10:33:53 +02:00
|
|
|
BackgroundCopyFile_GetProgress,
|
|
|
|
BackgroundCopyFile_GetFileRanges,
|
|
|
|
BackgroundCopyFile_SetRemoteName
|
2008-02-27 02:50:43 +01:00
|
|
|
};
|
|
|
|
|
2008-03-12 19:56:13 +01:00
|
|
|
HRESULT BackgroundCopyFileConstructor(BackgroundCopyJobImpl *owner,
|
|
|
|
LPCWSTR remoteName, LPCWSTR localName,
|
2013-01-18 05:38:49 +01:00
|
|
|
BackgroundCopyFileImpl **file)
|
2008-02-27 02:50:43 +01:00
|
|
|
{
|
|
|
|
BackgroundCopyFileImpl *This;
|
|
|
|
|
2013-01-18 05:38:49 +01:00
|
|
|
TRACE("(%s, %s, %p)\n", debugstr_w(remoteName), debugstr_w(localName), file);
|
2008-02-27 02:50:43 +01:00
|
|
|
|
|
|
|
This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
|
|
|
|
if (!This)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2015-06-23 14:14:53 +02:00
|
|
|
This->info.RemoteName = strdupW(remoteName);
|
2008-02-27 02:50:43 +01:00
|
|
|
if (!This->info.RemoteName)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2015-06-23 14:14:53 +02:00
|
|
|
This->info.LocalName = strdupW(localName);
|
2008-02-27 02:50:43 +01:00
|
|
|
if (!This->info.LocalName)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->info.RemoteName);
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2015-06-05 10:33:53 +02:00
|
|
|
This->IBackgroundCopyFile2_iface.lpVtbl = &BackgroundCopyFile2Vtbl;
|
2008-02-27 02:50:43 +01:00
|
|
|
This->ref = 1;
|
|
|
|
|
2008-02-29 04:00:12 +01:00
|
|
|
This->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
|
|
|
|
This->fileProgress.BytesTransferred = 0;
|
|
|
|
This->fileProgress.Completed = FALSE;
|
2008-03-12 19:56:13 +01:00
|
|
|
This->owner = owner;
|
2015-06-12 15:04:44 +02:00
|
|
|
This->read_size = 0;
|
2015-06-12 15:06:59 +02:00
|
|
|
This->tempFileName[0] = 0;
|
2015-06-05 10:34:21 +02:00
|
|
|
IBackgroundCopyJob3_AddRef(&owner->IBackgroundCopyJob3_iface);
|
2008-02-29 04:00:12 +01:00
|
|
|
|
2013-01-18 05:38:49 +01:00
|
|
|
*file = This;
|
2008-02-27 02:50:43 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
2008-03-13 23:54:35 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static HRESULT error_from_http_response(DWORD code)
|
2010-12-04 22:16:22 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case 200: return S_OK;
|
|
|
|
case 400: return BG_E_HTTP_ERROR_400;
|
|
|
|
case 401: return BG_E_HTTP_ERROR_401;
|
|
|
|
case 404: return BG_E_HTTP_ERROR_404;
|
|
|
|
case 407: return BG_E_HTTP_ERROR_407;
|
|
|
|
case 414: return BG_E_HTTP_ERROR_414;
|
|
|
|
case 501: return BG_E_HTTP_ERROR_501;
|
|
|
|
case 503: return BG_E_HTTP_ERROR_503;
|
|
|
|
case 504: return BG_E_HTTP_ERROR_504;
|
|
|
|
case 505: return BG_E_HTTP_ERROR_505;
|
|
|
|
default:
|
|
|
|
FIXME("unhandled response code %u\n", code);
|
|
|
|
return S_OK;
|
|
|
|
}
|
2010-12-04 22:16:22 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static void CALLBACK progress_callback_http(HINTERNET handle, DWORD_PTR context, DWORD status,
|
|
|
|
LPVOID buf, DWORD buflen)
|
2013-12-04 06:58:56 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
BackgroundCopyFileImpl *file = (BackgroundCopyFileImpl *)context;
|
|
|
|
BackgroundCopyJobImpl *job = file->owner;
|
2013-12-04 06:58:56 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
TRACE("%p, %p, %x, %p, %u\n", handle, file, status, buf, buflen);
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE:
|
2013-12-04 06:58:56 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
DWORD code, len, size;
|
|
|
|
|
|
|
|
size = sizeof(code);
|
|
|
|
if (WinHttpQueryHeaders(handle, WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER,
|
|
|
|
NULL, &code, &size, NULL))
|
|
|
|
{
|
|
|
|
if ((job->error.code = error_from_http_response(code)))
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
|
|
|
|
job->error.context = BG_ERROR_CONTEXT_REMOTE_FILE;
|
|
|
|
if (job->error.file) IBackgroundCopyFile2_Release(job->error.file);
|
|
|
|
job->error.file = &file->IBackgroundCopyFile2_iface;
|
|
|
|
IBackgroundCopyFile2_AddRef(job->error.file);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
|
|
|
|
job->error.context = 0;
|
|
|
|
if (job->error.file)
|
|
|
|
{
|
|
|
|
IBackgroundCopyFile2_Release(job->error.file);
|
|
|
|
job->error.file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = sizeof(len);
|
|
|
|
if (WinHttpQueryHeaders(handle, WINHTTP_QUERY_CONTENT_LENGTH|WINHTTP_QUERY_FLAG_NUMBER,
|
|
|
|
NULL, &len, &size, NULL))
|
|
|
|
{
|
|
|
|
file->fileProgress.BytesTotal = len;
|
|
|
|
}
|
|
|
|
break;
|
2015-06-05 10:34:44 +02:00
|
|
|
}
|
2015-06-12 15:04:44 +02:00
|
|
|
case WINHTTP_CALLBACK_STATUS_READ_COMPLETE:
|
2015-06-05 10:34:44 +02:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
file->read_size = buflen;
|
|
|
|
break;
|
2015-06-05 10:34:44 +02:00
|
|
|
}
|
2015-06-12 15:04:44 +02:00
|
|
|
case WINHTTP_CALLBACK_STATUS_REQUEST_ERROR:
|
2015-06-05 10:34:44 +02:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
WINHTTP_ASYNC_RESULT *result = (WINHTTP_ASYNC_RESULT *)buf;
|
|
|
|
job->error.code = result->dwError;
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
2013-12-04 06:58:56 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
SetEvent(job->wait);
|
2008-03-13 23:57:54 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static DWORD wait_for_completion(BackgroundCopyJobImpl *job)
|
2008-03-13 23:57:54 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
HANDLE handles[2] = {job->wait, job->cancel};
|
|
|
|
DWORD error = ERROR_SUCCESS;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
switch (WaitForMultipleObjects(2, handles, FALSE, INFINITE))
|
2008-03-13 23:57:54 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
break;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
error = ERROR_CANCELLED;
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_CANCELLED);
|
|
|
|
break;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
default:
|
|
|
|
error = GetLastError();
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
|
|
|
|
break;
|
|
|
|
}
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
return error;
|
2008-03-13 23:57:54 +01:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:06:26 +02:00
|
|
|
static UINT target_from_index(UINT index)
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0: return WINHTTP_AUTH_TARGET_SERVER;
|
|
|
|
case 1: return WINHTTP_AUTH_TARGET_PROXY;
|
|
|
|
default:
|
|
|
|
ERR("unhandled index %u\n", index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT scheme_from_index(UINT index)
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0: return WINHTTP_AUTH_SCHEME_BASIC;
|
|
|
|
case 1: return WINHTTP_AUTH_SCHEME_NTLM;
|
|
|
|
case 2: return WINHTTP_AUTH_SCHEME_PASSPORT;
|
|
|
|
case 3: return WINHTTP_AUTH_SCHEME_DIGEST;
|
|
|
|
case 4: return WINHTTP_AUTH_SCHEME_NEGOTIATE;
|
|
|
|
default:
|
|
|
|
ERR("unhandled index %u\n", index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL set_request_credentials(HINTERNET req, BackgroundCopyJobImpl *job)
|
|
|
|
{
|
|
|
|
UINT i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < BG_AUTH_TARGET_PROXY; i++)
|
|
|
|
{
|
|
|
|
UINT target = target_from_index(i);
|
|
|
|
for (j = 0; j < BG_AUTH_SCHEME_PASSPORT; j++)
|
|
|
|
{
|
|
|
|
UINT scheme = scheme_from_index(j);
|
|
|
|
const WCHAR *username = job->http_options.creds[i][j].Credentials.Basic.UserName;
|
|
|
|
const WCHAR *password = job->http_options.creds[i][j].Credentials.Basic.Password;
|
|
|
|
|
|
|
|
if (!username) continue;
|
|
|
|
if (!WinHttpSetCredentials(req, target, scheme, username, password, NULL)) return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static BOOL transfer_file_http(BackgroundCopyFileImpl *file, URL_COMPONENTSW *uc,
|
|
|
|
const WCHAR *tmpfile)
|
2008-03-13 23:57:54 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
BackgroundCopyJobImpl *job = file->owner;
|
|
|
|
HANDLE handle;
|
|
|
|
HINTERNET ses, con = NULL, req = NULL;
|
|
|
|
DWORD flags = (uc->nScheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0;
|
|
|
|
char buf[4096];
|
|
|
|
BOOL ret = FALSE;
|
2015-08-17 13:56:50 +02:00
|
|
|
DWORD written;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_CONNECTING);
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
if (!(ses = WinHttpOpen(NULL, 0, NULL, NULL, WINHTTP_FLAG_ASYNC))) return FALSE;
|
|
|
|
WinHttpSetStatusCallback(ses, progress_callback_http, WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS, 0);
|
2015-06-27 09:50:01 +02:00
|
|
|
if (!WinHttpSetOption(ses, WINHTTP_OPTION_CONTEXT_VALUE, &file, sizeof(file))) goto done;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
if (!(con = WinHttpConnect(ses, uc->lpszHostName, uc->nPort, 0))) goto done;
|
|
|
|
if (!(req = WinHttpOpenRequest(con, NULL, uc->lpszUrlPath, NULL, NULL, NULL, flags))) goto done;
|
2015-06-12 15:06:26 +02:00
|
|
|
if (!set_request_credentials(req, job)) goto done;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:05:59 +02:00
|
|
|
if (!(WinHttpSendRequest(req, job->http_options.headers, ~0u, NULL, 0, 0, (DWORD_PTR)file))) goto done;
|
2015-06-12 15:04:44 +02:00
|
|
|
if (wait_for_completion(job) || job->error.code) goto done;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
if (!(WinHttpReceiveResponse(req, NULL))) goto done;
|
|
|
|
if (wait_for_completion(job) || job->error.code) goto done;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
transitionJobState(job, BG_JOB_STATE_CONNECTING, BG_JOB_STATE_TRANSFERRING);
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
handle = CreateFileW(tmpfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) goto done;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
file->read_size = 0;
|
|
|
|
if (!(ret = WinHttpReadData(req, buf, sizeof(buf), NULL))) break;
|
|
|
|
if (wait_for_completion(job) || job->error.code)
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!file->read_size) break;
|
2015-08-17 13:56:50 +02:00
|
|
|
if (!(ret = WriteFile(handle, buf, file->read_size, &written, NULL))) break;
|
2008-03-13 23:57:54 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
file->fileProgress.BytesTransferred += file->read_size;
|
|
|
|
job->jobProgress.BytesTransferred += file->read_size;
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
}
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
CloseHandle(handle);
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
done:
|
|
|
|
WinHttpCloseHandle(req);
|
|
|
|
WinHttpCloseHandle(con);
|
|
|
|
WinHttpCloseHandle(ses);
|
2016-05-17 10:53:07 +02:00
|
|
|
if (!ret && !transitionJobState(job, BG_JOB_STATE_CONNECTING, BG_JOB_STATE_ERROR))
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
SetEvent(job->done);
|
|
|
|
return ret;
|
2015-06-05 10:34:44 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static DWORD CALLBACK progress_callback_local(LARGE_INTEGER totalSize, LARGE_INTEGER totalTransferred,
|
|
|
|
LARGE_INTEGER streamSize, LARGE_INTEGER streamTransferred,
|
|
|
|
DWORD streamNum, DWORD reason, HANDLE srcFile,
|
|
|
|
HANDLE dstFile, LPVOID obj)
|
2015-06-05 10:34:44 +02:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
BackgroundCopyFileImpl *file = obj;
|
|
|
|
BackgroundCopyJobImpl *job = file->owner;
|
|
|
|
ULONG64 diff;
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
diff = (file->fileProgress.BytesTotal == BG_SIZE_UNKNOWN
|
|
|
|
? totalTransferred.QuadPart
|
|
|
|
: totalTransferred.QuadPart - file->fileProgress.BytesTransferred);
|
|
|
|
file->fileProgress.BytesTotal = totalSize.QuadPart;
|
|
|
|
file->fileProgress.BytesTransferred = totalTransferred.QuadPart;
|
|
|
|
job->jobProgress.BytesTransferred += diff;
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
|
|
|
|
return (job->state == BG_JOB_STATE_TRANSFERRING
|
|
|
|
? PROGRESS_CONTINUE
|
|
|
|
: PROGRESS_CANCEL);
|
2015-06-05 10:35:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
static BOOL transfer_file_local(BackgroundCopyFileImpl *file, const WCHAR *tmpname)
|
2015-06-05 10:34:44 +02:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
static const WCHAR fileW[] = {'f','i','l','e',':','/','/',0};
|
|
|
|
BackgroundCopyJobImpl *job = file->owner;
|
|
|
|
const WCHAR *ptr;
|
|
|
|
BOOL ret;
|
2015-06-05 10:35:09 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSFERRING);
|
2015-06-05 10:35:09 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
if (strlenW(file->info.RemoteName) > 7 && !memicmpW(file->info.RemoteName, fileW, 7))
|
|
|
|
ptr = file->info.RemoteName + 7;
|
2015-06-05 10:35:09 +02:00
|
|
|
else
|
2015-06-12 15:04:44 +02:00
|
|
|
ptr = file->info.RemoteName;
|
|
|
|
|
|
|
|
if (!(ret = CopyFileExW(ptr, tmpname, progress_callback_local, file, NULL, 0)))
|
2015-06-05 10:35:09 +02:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
WARN("Local file copy failed: error %u\n", GetLastError());
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_ERROR);
|
2015-06-05 10:35:09 +02:00
|
|
|
}
|
2015-06-05 10:34:44 +02:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
SetEvent(job->done);
|
|
|
|
return ret;
|
2008-03-13 23:57:54 +01:00
|
|
|
}
|
|
|
|
|
2008-03-13 23:54:35 +01:00
|
|
|
BOOL processFile(BackgroundCopyFileImpl *file, BackgroundCopyJobImpl *job)
|
|
|
|
{
|
2008-06-19 23:51:55 +02:00
|
|
|
static const WCHAR prefix[] = {'B','I','T', 0};
|
2015-06-12 15:04:44 +02:00
|
|
|
WCHAR tmpDir[MAX_PATH], tmpName[MAX_PATH];
|
2015-12-23 11:04:57 +01:00
|
|
|
WCHAR host[MAX_PATH];
|
2015-06-12 15:04:44 +02:00
|
|
|
URL_COMPONENTSW uc;
|
|
|
|
BOOL ret;
|
2008-03-13 23:54:35 +01:00
|
|
|
|
|
|
|
if (!GetTempPathW(MAX_PATH, tmpDir))
|
|
|
|
{
|
|
|
|
ERR("Couldn't create temp file name: %d\n", GetLastError());
|
|
|
|
/* Guessing on what state this should give us */
|
|
|
|
transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GetTempFileNameW(tmpDir, prefix, 0, tmpName))
|
|
|
|
{
|
|
|
|
ERR("Couldn't create temp file: %d\n", GetLastError());
|
|
|
|
/* Guessing on what state this should give us */
|
|
|
|
transitionJobState(job, BG_JOB_STATE_QUEUED, BG_JOB_STATE_TRANSIENT_ERROR);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
file->fileProgress.BytesTotal = BG_SIZE_UNKNOWN;
|
|
|
|
file->fileProgress.BytesTransferred = 0;
|
|
|
|
file->fileProgress.Completed = FALSE;
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
|
|
|
|
TRACE("Transferring: %s -> %s -> %s\n",
|
|
|
|
debugstr_w(file->info.RemoteName),
|
|
|
|
debugstr_w(tmpName),
|
|
|
|
debugstr_w(file->info.LocalName));
|
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
uc.dwStructSize = sizeof(uc);
|
|
|
|
uc.nScheme = 0;
|
|
|
|
uc.lpszScheme = NULL;
|
|
|
|
uc.dwSchemeLength = 0;
|
|
|
|
uc.lpszUserName = NULL;
|
|
|
|
uc.dwUserNameLength = 0;
|
|
|
|
uc.lpszPassword = NULL;
|
|
|
|
uc.dwPasswordLength = 0;
|
|
|
|
uc.lpszHostName = host;
|
|
|
|
uc.dwHostNameLength = sizeof(host)/sizeof(host[0]);
|
|
|
|
uc.nPort = 0;
|
2015-12-23 11:04:57 +01:00
|
|
|
uc.lpszUrlPath = NULL;
|
|
|
|
uc.dwUrlPathLength = ~0u;
|
2016-05-22 21:13:55 +02:00
|
|
|
uc.lpszExtraInfo = NULL;
|
|
|
|
uc.dwExtraInfoLength = 0;
|
2015-06-12 15:04:44 +02:00
|
|
|
ret = WinHttpCrackUrl(file->info.RemoteName, 0, 0, &uc);
|
|
|
|
if (!ret)
|
2008-03-13 23:54:35 +01:00
|
|
|
{
|
2015-06-12 15:04:44 +02:00
|
|
|
TRACE("WinHttpCrackUrl failed, trying local file copy\n");
|
2016-05-17 10:53:07 +02:00
|
|
|
if (!transfer_file_local(file, tmpName)) WARN("local transfer failed\n");
|
2015-06-05 10:35:09 +02:00
|
|
|
}
|
2016-05-17 10:53:07 +02:00
|
|
|
else if (!transfer_file_http(file, &uc, tmpName)) WARN("HTTP transfer failed\n");
|
2008-03-13 23:54:35 +01:00
|
|
|
|
2015-06-12 15:04:44 +02:00
|
|
|
if (transitionJobState(job, BG_JOB_STATE_CONNECTING, BG_JOB_STATE_QUEUED) ||
|
|
|
|
transitionJobState(job, BG_JOB_STATE_TRANSFERRING, BG_JOB_STATE_QUEUED))
|
2008-03-13 23:54:35 +01:00
|
|
|
{
|
|
|
|
lstrcpyW(file->tempFileName, tmpName);
|
|
|
|
|
|
|
|
EnterCriticalSection(&job->cs);
|
|
|
|
file->fileProgress.Completed = TRUE;
|
|
|
|
job->jobProgress.FilesTransferred++;
|
|
|
|
LeaveCriticalSection(&job->cs);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DeleteFileW(tmpName);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|