#include "config.h"
#include "WebKitDLL.h"
#include "WebDownload.h"
#include "CString.h"
#include "DefaultDownloadDelegate.h"
#include "MarshallingHelpers.h"
#include "WebError.h"
#include "WebKit.h"
#include "WebKitLogging.h"
#include "WebMutableURLRequest.h"
#include "WebURLAuthenticationChallenge.h"
#include "WebURLCredential.h"
#include "WebURLResponse.h"
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#pragma warning(push, 0)
#include <WebCore/BString.h>
#include <WebCore/NotImplemented.h>
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceHandle.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/ResourceResponse.h>
#include <wtf/CurrentTime.h>
#include <wtf/StdLibExtras.h>
#pragma warning(pop)
using namespace WebCore;
const String& WebDownload::bundleExtension()
{
DEFINE_STATIC_LOCAL(const String, bundleExtension, (".download"));
return bundleExtension;
}
UInt32 WebDownload::bundleMagicNumber()
{
return 0xDECAF4EA;
}
WebDownload::WebDownload()
: m_refCount(0)
{
gClassCount++;
gClassNameCount.add("WebDownload");
}
WebDownload::~WebDownload()
{
LOG(Download, "WebDownload - Destroying download (%p)", this);
cancel();
gClassCount--;
gClassNameCount.remove("WebDownload");
}
WebDownload* WebDownload::createInstance()
{
WebDownload* instance = new WebDownload();
instance->AddRef();
return instance;
}
WebDownload* WebDownload::createInstance(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
{
WebDownload* instance = new WebDownload();
instance->AddRef();
instance->init(handle, request, response, delegate);
return instance;
}
WebDownload* WebDownload::createInstance(const KURL& url, IWebDownloadDelegate* delegate)
{
WebDownload* instance = new WebDownload();
instance->AddRef();
instance->init(url, delegate);
return instance;
}
HRESULT STDMETHODCALLTYPE WebDownload::QueryInterface(REFIID riid, void** ppvObject)
{
*ppvObject = 0;
if (IsEqualGUID(riid, IID_IUnknown))
*ppvObject = static_cast<IWebDownload*>(this);
else if (IsEqualGUID(riid, IID_IWebDownload))
*ppvObject = static_cast<IWebDownload*>(this);
else if (IsEqualGUID(riid, IID_IWebURLAuthenticationChallengeSender))
*ppvObject = static_cast<IWebURLAuthenticationChallengeSender*>(this);
else if (IsEqualGUID(riid, CLSID_WebDownload))
*ppvObject = static_cast<WebDownload*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE WebDownload::AddRef(void)
{
return ++m_refCount;
}
ULONG STDMETHODCALLTYPE WebDownload::Release(void)
{
ULONG newRef = --m_refCount;
if (!newRef)
delete(this);
return newRef;
}
HRESULT STDMETHODCALLTYPE WebDownload::canResumeDownloadDecodedWithEncodingMIMEType(
BSTR,
BOOL*)
{
notImplemented();
return E_FAIL;
}
HRESULT STDMETHODCALLTYPE WebDownload::bundlePathForTargetPath(
BSTR targetPath,
BSTR* bundlePath)
{
if (!targetPath)
return E_INVALIDARG;
String bundle(targetPath, SysStringLen(targetPath));
if (bundle.isEmpty())
return E_INVALIDARG;
if (bundle[bundle.length()-1] == '/')
bundle.truncate(1);
bundle += bundleExtension();
*bundlePath = SysAllocStringLen(bundle.characters(), bundle.length());
if (!*bundlePath)
return E_FAIL;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::request(
IWebURLRequest** request)
{
if (request) {
*request = m_request.get();
if (*request)
(*request)->AddRef();
}
return S_OK;
}
CFDataRef WebDownload::extractResumeDataFromBundle(const String& bundlePath)
{
if (bundlePath.isEmpty()) {
LOG_ERROR("Cannot create resume data from empty download bundle path");
return 0;
}
String nullifiedPath = bundlePath;
FILE* bundle = 0;
if (_wfopen_s(&bundle, nullifiedPath.charactersWithNullTermination(), TEXT("r+b")) || !bundle) {
LOG_ERROR("Failed to open file %s to get resume data", bundlePath.ascii().data());
return 0;
}
CFDataRef result = 0;
Vector<UInt8> footerBuffer;
struct _stat64 fileStat;
if (_fstat64(_fileno(bundle), &fileStat))
goto exit;
fpos_t footerMagicNumberPosition = fileStat.st_size - 4;
ASSERT(footerMagicNumberPosition >= 0);
if (footerMagicNumberPosition < 0)
goto exit;
if (fsetpos(bundle, &footerMagicNumberPosition))
goto exit;
UInt32 footerMagicNumber = 0;
if (fread(&footerMagicNumber, 4, 1, bundle) != 1) {
LOG_ERROR("Failed to read footer magic number from the bundle - errno(%i)", errno);
goto exit;
}
if (footerMagicNumber != bundleMagicNumber()) {
LOG_ERROR("Footer's magic number does not match 0x%X - errno(%i)", bundleMagicNumber(), errno);
goto exit;
}
fpos_t footerLengthPosition = fileStat.st_size - 8;
ASSERT(footerLengthPosition >= 0);
if (footerLengthPosition < 0)
goto exit;
if (fsetpos(bundle, &footerLengthPosition))
goto exit;
UInt32 footerLength = 0;
if (fread(&footerLength, 4, 1, bundle) != 1) {
LOG_ERROR("Failed to read ResumeData length from the bundle - errno(%i)", errno);
goto exit;
}
fpos_t footerStartPosition = fileStat.st_size - 8 - footerLength;
ASSERT(footerStartPosition >= 0);
if (footerStartPosition < 0)
goto exit;
if (fsetpos(bundle, &footerStartPosition))
goto exit;
footerBuffer.resize(footerLength);
if (fread(footerBuffer.data(), 1, footerLength, bundle) != footerLength) {
LOG_ERROR("Failed to read ResumeData from the bundle - errno(%i)", errno);
goto exit;
}
if (errno_t resizeError = _chsize_s(_fileno(bundle), footerStartPosition)) {
LOG_ERROR("Failed to truncate the resume footer off the end of the file - errno(%i)", resizeError);
goto exit;
}
result = CFDataCreate(0, footerBuffer.data(), footerLength);
exit:
fclose(bundle);
return result;
}
HRESULT WebDownload::appendResumeDataToBundle(CFDataRef resumeData, const String& bundlePath)
{
if (!resumeData) {
LOG_ERROR("Invalid resume data to write to bundle path");
return E_FAIL;
}
if (bundlePath.isEmpty()) {
LOG_ERROR("Cannot write resume data to empty download bundle path");
return E_FAIL;
}
String nullifiedPath = bundlePath;
FILE* bundle = 0;
if (_wfopen_s(&bundle, nullifiedPath.charactersWithNullTermination(), TEXT("ab")) || !bundle) {
LOG_ERROR("Failed to open file %s to append resume data", bundlePath.ascii().data());
return E_FAIL;
}
HRESULT hr = E_FAIL;
const UInt8* resumeBytes = CFDataGetBytePtr(resumeData);
ASSERT(resumeBytes);
if (!resumeBytes)
goto exit;
UInt32 resumeLength = CFDataGetLength(resumeData);
ASSERT(resumeLength > 0);
if (resumeLength < 1)
goto exit;
if (fwrite(resumeBytes, 1, resumeLength, bundle) != resumeLength) {
LOG_ERROR("Failed to write resume data to the bundle - errno(%i)", errno);
goto exit;
}
if (fwrite(&resumeLength, 4, 1, bundle) != 1) {
LOG_ERROR("Failed to write footer length to the bundle - errno(%i)", errno);
goto exit;
}
const UInt32& magic = bundleMagicNumber();
if (fwrite(&magic, 4, 1, bundle) != 1) {
LOG_ERROR("Failed to write footer magic number to the bundle - errno(%i)", errno);
goto exit;
}
hr = S_OK;
exit:
fclose(bundle);
return hr;
}