1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
|
/*
* Copyright (C) 2007 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#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/AuthenticationCF.h>
#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 <WebCore/SystemTime.h>
#pragma warning(pop)
using namespace WebCore;
// CFURLDownload Callbacks ----------------------------------------------------------------
static void didStartCallback(CFURLDownloadRef download, const void *clientInfo);
static CFURLRequestRef willSendRequestCallback(CFURLDownloadRef download, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo);
static void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef download, CFURLAuthChallengeRef challenge, const void *clientInfo);
static void didReceiveResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, const void *clientInfo);
static void willResumeWithResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo);
static void didReceiveDataCallback(CFURLDownloadRef download, CFIndex length, const void *clientInfo);
static Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef download, CFStringRef encodingType, const void *clientInfo);
static void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef download, CFStringRef objectName, const void *clientInfo);
static void didCreateDestinationCallback(CFURLDownloadRef download, CFURLRef path, const void *clientInfo);
static void didFinishCallback(CFURLDownloadRef download, const void *clientInfo);
static void didFailCallback(CFURLDownloadRef download, CFErrorRef error, const void *clientInfo);
// Download Bundle file utilities ----------------------------------------------------------------
static const String BundleExtension(".download");
static UInt32 BundleMagicNumber = 0xDECAF4EA;
static CFDataRef extractResumeDataFromBundle(const String& bundlePath);
static HRESULT appendResumeDataToBundle(CFDataRef resumeData, const String& bundlePath);
// WebDownload ----------------------------------------------------------------
WebDownload::WebDownload()
: m_refCount(0)
{
gClassCount++;
}
void WebDownload::init(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
{
m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
CFURLConnectionRef connection = handle->connection();
if (!connection) {
LOG_ERROR("WebDownload::WebDownload(ResourceHandle*,...) called with an inactive ResourceHandle");
return;
}
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(request));
m_download.adoptCF(CFURLDownloadCreateAndStartWithLoadingConnection(0, connection, request.cfURLRequest(), response.cfURLResponse(), &client));
// It is possible for CFURLDownloadCreateAndStartWithLoadingConnection() to fail if the passed in CFURLConnection is not in a "downloadable state"
// However, we should never hit that case
if (!m_download) {
ASSERT_NOT_REACHED();
LOG_ERROR("WebDownload - Failed to create WebDownload from existing connection (%s)", request.url().string().utf8().data());
} else
LOG(Download, "WebDownload - Created WebDownload %p from existing connection (%s)", this, request.url().string().utf8().data());
// The CFURLDownload either starts successfully and retains the CFURLConnection,
// or it fails to creating and we have a now-useless connection with a dangling ref.
// Either way, we need to release the connection to balance out ref counts
handle->releaseConnectionForDownload();
CFRelease(connection);
}
void WebDownload::init(const KURL& url, IWebDownloadDelegate* delegate)
{
m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
LOG_ERROR("Delegate is %p", m_delegate.get());
ResourceRequest request(url);
CFURLRequestRef cfRequest = request.cfURLRequest();
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(request));
m_download.adoptCF(CFURLDownloadCreate(0, cfRequest, &client));
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - Initialized download of url %s in WebDownload %p", url.string().utf8().data(), this);
}
WebDownload::~WebDownload()
{
LOG(Download, "WebDownload - Destroying download (%p)", this);
cancel();
gClassCount--;
}
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;
}
// IUnknown -------------------------------------------------------------------
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;
}
// IWebDownload -------------------------------------------------------------------
HRESULT STDMETHODCALLTYPE WebDownload::initWithRequest(
/* [in] */ IWebURLRequest* request,
/* [in] */ IWebDownloadDelegate* delegate)
{
COMPtr<WebMutableURLRequest> webRequest;
if (!request || FAILED(request->QueryInterface(&webRequest))) {
LOG(Download, "WebDownload - initWithRequest failed - not a WebMutableURLRequest");
return E_FAIL;
}
if (!delegate)
return E_FAIL;
m_delegate = delegate;
LOG(Download, "Delegate is %p", m_delegate.get());
RetainPtr<CFURLRequestRef> cfRequest = webRequest->resourceRequest().cfURLRequest();
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(webRequest.get()));
m_download.adoptCF(CFURLDownloadCreate(0, cfRequest.get(), &client));
// If for some reason the download failed to create,
// we have particular cleanup to do
if (!m_download) {
m_request = 0;
return E_FAIL;
}
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - initWithRequest complete, started download of url %s", webRequest->resourceRequest().url().string().utf8().data());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::initToResumeWithBundle(
/* [in] */ BSTR bundlePath,
/* [in] */ IWebDownloadDelegate* delegate)
{
LOG(Download, "Attempting resume of download bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
RetainPtr<CFDataRef> resumeData(AdoptCF, extractResumeDataFromBundle(String(bundlePath, SysStringLen(bundlePath))));
if (!resumeData)
return E_FAIL;
if (!delegate)
return E_FAIL;
m_delegate = delegate;
LOG(Download, "Delegate is %p", m_delegate.get());
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
RetainPtr<CFURLRef> pathURL(AdoptCF, MarshallingHelpers::PathStringToFileCFURLRef(String(bundlePath, SysStringLen(bundlePath))));
ASSERT(pathURL);
m_download.adoptCF(CFURLDownloadCreateWithResumeData(0, resumeData.get(), pathURL.get(), &client));
if (!m_download) {
LOG(Download, "Failed to create CFURLDownloadRef for resume");
return E_FAIL;
}
m_bundlePath = String(bundlePath, SysStringLen(bundlePath));
// Attempt to remove the ".download" extension from the bundle for the final file destination
// Failing that, we clear m_destination and will ask the delegate later once the download starts
if (m_bundlePath.endsWith(BundleExtension, false)) {
m_destination = m_bundlePath.copy();
m_destination.truncate(m_destination.length() - BundleExtension.length());
} else
m_destination = String();
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - initWithRequest complete, resumed download of bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::canResumeDownloadDecodedWithEncodingMIMEType(
/* [in] */ BSTR,
/* [out, retval] */ BOOL*)
{
notImplemented();
return E_FAIL;
}
HRESULT STDMETHODCALLTYPE WebDownload::start()
{
LOG(Download, "WebDownload - Starting download (%p)", this);
if (!m_download)
return E_FAIL;
CFURLDownloadStart(m_download.get());
// FIXME: 4950477 - CFURLDownload neglects to make the didStart() client call upon starting the download.
// This is a somewhat critical call, so we'll fake it for now!
didStart();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::cancel()
{
LOG(Download, "WebDownload - Cancelling download (%p)", this);
if (!m_download)
return E_FAIL;
CFURLDownloadCancel(m_download.get());
m_download = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::cancelForResume()
{
LOG(Download, "WebDownload - Cancelling download (%p), writing resume information to file if possible", this);
ASSERT(m_download);
if (!m_download)
return E_FAIL;
HRESULT hr = S_OK;
RetainPtr<CFDataRef> resumeData;
if (m_destination.isEmpty()) {
CFURLDownloadCancel(m_download.get());
goto exit;
}
CFURLDownloadSetDeletesUponFailure(m_download.get(), false);
CFURLDownloadCancel(m_download.get());
resumeData = CFURLDownloadCopyResumeData(m_download.get());
if (!resumeData) {
LOG(Download, "WebDownload - Unable to create resume data for download (%p)", this);
goto exit;
}
appendResumeDataToBundle(resumeData.get(), m_bundlePath);
exit:
m_download = 0;
return hr;
}
HRESULT STDMETHODCALLTYPE WebDownload::deletesFileUponFailure(
/* [out, retval] */ BOOL* result)
{
if (!m_download)
return E_FAIL;
*result = CFURLDownloadDeletesUponFailure(m_download.get());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::bundlePathForTargetPath(
/* [in] */ BSTR targetPath,
/* [out, retval] */ 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(
/* [out, retval] */ IWebURLRequest** request)
{
if (request) {
*request = m_request.get();
if (*request)
(*request)->AddRef();
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::setDeletesFileUponFailure(
/* [in] */ BOOL deletesFileUponFailure)
{
if (!m_download)
return E_FAIL;
CFURLDownloadSetDeletesUponFailure(m_download.get(), !!deletesFileUponFailure);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::setDestination(
/* [in] */ BSTR path,
/* [in] */ BOOL allowOverwrite)
{
if (!m_download)
return E_FAIL;
m_destination = String(path, SysStringLen(path));
m_bundlePath = m_destination + BundleExtension;
CFURLRef pathURL = MarshallingHelpers::PathStringToFileCFURLRef(m_bundlePath);
CFURLDownloadSetDestination(m_download.get(), pathURL, !!allowOverwrite);
CFRelease(pathURL);
LOG(Download, "WebDownload - Set destination to %s", m_bundlePath.ascii().data());
return S_OK;
}
// IWebURLAuthenticationChallengeSender -------------------------------------------------------------------
HRESULT STDMETHODCALLTYPE WebDownload::cancelAuthenticationChallenge(
/* [in] */ IWebURLAuthenticationChallenge*)
{
if (m_download) {
CFURLDownloadCancel(m_download.get());
m_download = 0;
}
// FIXME: Do we need a URL or description for this error code?
ResourceError error(String(WebURLErrorDomain), WebURLErrorUserCancelledAuthentication, "", "");
COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(error));
m_delegate->didFailWithError(this, webError.get());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::continueWithoutCredentialForAuthenticationChallenge(
/* [in] */ IWebURLAuthenticationChallenge* challenge)
{
COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
if (!webChallenge)
return E_NOINTERFACE;
if (m_download)
CFURLDownloadUseCredential(m_download.get(), 0, webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::useCredential(
/* [in] */ IWebURLCredential* credential,
/* [in] */ IWebURLAuthenticationChallenge* challenge)
{
COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
if (!webChallenge)
return E_NOINTERFACE;
COMPtr<WebURLCredential> webCredential(Query, credential);
if (!webCredential)
return E_NOINTERFACE;
RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(webCredential->credential()));
if (m_download)
CFURLDownloadUseCredential(m_download.get(), cfCredential.get(), webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
return S_OK;
}
// CFURLDownload Callbacks -------------------------------------------------------------------
void WebDownload::didStart()
{
#ifndef NDEBUG
m_startTime = m_dataTime = currentTime();
m_received = 0;
LOG(Download, "DOWNLOAD - Started %p at %.3f seconds", this, m_startTime);
#endif
if (FAILED(m_delegate->didBegin(this)))
LOG_ERROR("DownloadDelegate->didBegin failed");
}
CFURLRequestRef WebDownload::willSendRequest(CFURLRequestRef request, CFURLResponseRef response)
{
COMPtr<WebMutableURLRequest> webRequest(AdoptCOM, WebMutableURLRequest::createInstance(ResourceRequest(request)));
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
COMPtr<IWebMutableURLRequest> finalRequest;
if (FAILED(m_delegate->willSendRequest(this, webRequest.get(), webResponse.get(), &finalRequest)))
LOG_ERROR("DownloadDelegate->willSendRequest failed");
if (!finalRequest)
return 0;
COMPtr<WebMutableURLRequest> finalWebRequest(AdoptCOM, WebMutableURLRequest::createInstance(finalRequest.get()));
m_request = finalWebRequest.get();
CFURLRequestRef result = finalWebRequest->resourceRequest().cfURLRequest();
CFRetain(result);
return result;
}
void WebDownload::didReceiveAuthenticationChallenge(CFURLAuthChallengeRef challenge)
{
COMPtr<IWebURLAuthenticationChallenge> webChallenge(AdoptCOM,
WebURLAuthenticationChallenge::createInstance(AuthenticationChallenge(challenge, 0), this));
if (SUCCEEDED(m_delegate->didReceiveAuthenticationChallenge(this, webChallenge.get())))
return;
cancelAuthenticationChallenge(webChallenge.get());
}
void WebDownload::didReceiveResponse(CFURLResponseRef response)
{
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
if (FAILED(m_delegate->didReceiveResponse(this, webResponse.get())))
LOG_ERROR("DownloadDelegate->didReceiveResponse failed");
}
void WebDownload::willResumeWithResponse(CFURLResponseRef response, UInt64 fromByte)
{
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
if (FAILED(m_delegate->willResumeWithResponse(this, webResponse.get(), fromByte)))
LOG_ERROR("DownloadDelegate->willResumeWithResponse failed");
}
void WebDownload::didReceiveData(CFIndex length)
{
#ifndef NDEBUG
m_received += length;
double current = currentTime();
if (current - m_dataTime > 2.0)
LOG(Download, "DOWNLOAD - %p hanged for %.3f seconds - Received %i bytes for a total of %i", this, current - m_dataTime, length, m_received);
m_dataTime = current;
#endif
if (FAILED(m_delegate->didReceiveDataOfLength(this, length)))
LOG_ERROR("DownloadDelegate->didReceiveData failed");
}
bool WebDownload::shouldDecodeDataOfMIMEType(CFStringRef mimeType)
{
BOOL result;
if (FAILED(m_delegate->shouldDecodeSourceDataOfMIMEType(this, BString(mimeType), &result))) {
LOG_ERROR("DownloadDelegate->shouldDecodeSourceDataOfMIMEType failed");
return false;
}
return !!result;
}
void WebDownload::decideDestinationWithSuggestedObjectName(CFStringRef name)
{
if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, BString(name))))
LOG_ERROR("DownloadDelegate->decideDestinationWithSuggestedObjectName failed");
}
void WebDownload::didCreateDestination(CFURLRef destination)
{
// The concept of the ".download bundle" is internal to the WebDownload, so therefore
// we try to mask the delegate from its existence as much as possible by telling it the final
// destination was created, when in reality the bundle was created
String createdDestination = MarshallingHelpers::FileCFURLRefToPathString(destination);
// At this point in receiving CFURLDownload callbacks, we should definitely have the bundle path stored locally
// and it should match with the file that CFURLDownload created
ASSERT(createdDestination == m_bundlePath);
// And we should also always have the final-destination stored
ASSERT(!m_destination.isEmpty());
BString path(m_destination);
if (FAILED(m_delegate->didCreateDestination(this, path)))
LOG_ERROR("DownloadDelegate->didCreateDestination failed");
}
void WebDownload::didFinish()
{
#ifndef NDEBUG
LOG(Download, "DOWNLOAD - Finished %p after %i bytes and %.3f seconds", this, m_received, currentTime() - m_startTime);
#endif
ASSERT(!m_bundlePath.isEmpty() && !m_destination.isEmpty());
LOG(Download, "WebDownload - Moving file from bundle %s to destination %s", m_bundlePath.ascii().data(), m_destination.ascii().data());
// We try to rename the bundle to the final file name. If that fails, we give the delegate one more chance to chose
// the final file name, then we just leave it
if (!MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0)) {
LOG_ERROR("Failed to move bundle %s to %s on completion\nError - %i", m_bundlePath.ascii().data(), m_destination.ascii().data(), GetLastError());
bool reportBundlePathAsFinalPath = true;
BString destinationBSTR(m_destination.characters(), m_destination.length());
if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, destinationBSTR)))
LOG_ERROR("delegate->decideDestinationWithSuggestedFilename() failed");
// The call to m_delegate->decideDestinationWithSuggestedFilename() should have changed our destination, so we'll try the move
// one last time.
if (!m_destination.isEmpty())
if (MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0))
reportBundlePathAsFinalPath = false;
// We either need to tell the delegate our final filename is the bundle filename, or is the file name they just told us to use
if (reportBundlePathAsFinalPath) {
BString bundleBSTR(m_bundlePath);
m_delegate->didCreateDestination(this, bundleBSTR);
} else {
BString finalDestinationBSTR = BString(m_destination);
m_delegate->didCreateDestination(this, finalDestinationBSTR);
}
}
// It's extremely likely the call to delegate->didFinish() will deref this, so lets not let that cause our destruction just yet
COMPtr<WebDownload> protect = this;
if (FAILED(m_delegate->didFinish(this)))
LOG_ERROR("DownloadDelegate->didFinish failed");
m_download = 0;
}
void WebDownload::didFail(CFErrorRef error)
{
COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(ResourceError(error)));
if (FAILED(m_delegate->didFailWithError(this, webError.get())))
LOG_ERROR("DownloadDelegate->didFailWithError failed");
}
// CFURLDownload Callbacks ----------------------------------------------------------------
void didStartCallback(CFURLDownloadRef, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didStart(); }
CFURLRequestRef willSendRequestCallback(CFURLDownloadRef, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo)
{ return ((WebDownload*)clientInfo)->willSendRequest(request, redirectionResponse); }
void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef, CFURLAuthChallengeRef challenge, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveAuthenticationChallenge(challenge); }
void didReceiveResponseCallback(CFURLDownloadRef, CFURLResponseRef response, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveResponse(response); }
void willResumeWithResponseCallback(CFURLDownloadRef, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo)
{ ((WebDownload*)clientInfo)->willResumeWithResponse(response, startingByte); }
void didReceiveDataCallback(CFURLDownloadRef, CFIndex length, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveData(length); }
Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef, CFStringRef encodingType, const void *clientInfo)
{ return ((WebDownload*)clientInfo)->shouldDecodeDataOfMIMEType(encodingType); }
void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef, CFStringRef objectName, const void *clientInfo)
{ ((WebDownload*)clientInfo)->decideDestinationWithSuggestedObjectName(objectName); }
void didCreateDestinationCallback(CFURLDownloadRef, CFURLRef path, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didCreateDestination(path); }
void didFinishCallback(CFURLDownloadRef, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didFinish(); }
void didFailCallback(CFURLDownloadRef, CFErrorRef error, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didFail(error); }
// Download Bundle file utilities ----------------------------------------------------------------
static CFDataRef extractResumeDataFromBundle(const String& bundlePath)
{
if (bundlePath.isEmpty()) {
LOG_ERROR("Cannot create resume data from empty download bundle path");
return 0;
}
// Open a handle to the bundle file
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;
// Stat the file to get its size
struct _stat64 fileStat;
if (_fstat64(_fileno(bundle), &fileStat))
goto exit;
// Check for the bundle magic number at the end of the file
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;
}
// Now we're *reasonably* sure this is a .download bundle we actually wrote.
// Get the length of the resume data
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;
}
// Make sure theres enough bytes to read in for the resume data, and perform the read
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;
}
// CFURLDownload will seek to the appropriate place in the file (before our footer) and start overwriting from there
// However, say we were within a few hundred bytes of the end of a download when it was paused -
// The additional footer extended the length of the file beyond its final length, and there will be junk data leftover
// at the end. Therefore, now that we've retrieved the footer data, we need to truncate it.
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;
}
// Finally, make the resume data. Now, it is possible by some twist of fate the bundle magic number
// was naturally at the end of the file and its not actually a valid bundle. That, or someone engineered
// it that way to try to attack us. In that cause, this CFData will successfully create but when we
// actually try to start the CFURLDownload using this bogus data, it will fail and we will handle that gracefully
result = CFDataCreate(0, footerBuffer.data(), footerLength);
exit:
fclose(bundle);
return result;
}
static HRESULT 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;
}
if (fwrite(&BundleMagicNumber, 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;
}
|