Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (C) 2010 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  *
     30  */
     31 
     32 #include "config.h"
     33 #include "PingLoader.h"
     34 
     35 #include "FormData.h"
     36 #include "Frame.h"
     37 #include "FrameLoaderClient.h"
     38 #include "Page.h"
     39 #include "ProgressTracker.h"
     40 #include "ResourceHandle.h"
     41 #include "SecurityOrigin.h"
     42 #include <wtf/OwnPtr.h>
     43 #include <wtf/UnusedParam.h>
     44 #include <wtf/text/CString.h>
     45 
     46 namespace WebCore {
     47 
     48 void PingLoader::loadImage(Frame* frame, const KURL& url)
     49 {
     50     if (!frame->document()->securityOrigin()->canDisplay(url)) {
     51         FrameLoader::reportLocalLoadFailed(frame, url);
     52         return;
     53     }
     54 
     55     ResourceRequest request(url);
     56     request.setTargetType(ResourceRequest::TargetIsImage);
     57     request.setHTTPHeaderField("Cache-Control", "max-age=0");
     58     if (!SecurityOrigin::shouldHideReferrer(request.url(), frame->loader()->outgoingReferrer()))
     59         request.setHTTPReferrer(frame->loader()->outgoingReferrer());
     60     frame->loader()->addExtraFieldsToSubresourceRequest(request);
     61     OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
     62 
     63     // Leak the ping loader, since it will kill itself as soon as it receives a response.
     64     PingLoader* leakedPingLoader = pingLoader.leakPtr();
     65     UNUSED_PARAM(leakedPingLoader);
     66 }
     67 
     68 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
     69 void PingLoader::sendPing(Frame* frame, const KURL& pingURL, const KURL& destinationURL)
     70 {
     71     ResourceRequest request(pingURL);
     72     request.setTargetType(ResourceRequest::TargetIsSubresource);
     73     request.setHTTPMethod("POST");
     74     request.setHTTPContentType("text/ping");
     75     request.setHTTPBody(FormData::create("PING"));
     76     request.setHTTPHeaderField("Cache-Control", "max-age=0");
     77     frame->loader()->addExtraFieldsToSubresourceRequest(request);
     78 
     79     SecurityOrigin* sourceOrigin = frame->document()->securityOrigin();
     80     RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
     81     FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin->toString());
     82     request.setHTTPHeaderField("Ping-To", destinationURL);
     83     if (sourceOrigin->isSameSchemeHostPort(pingOrigin.get()))
     84         request.setHTTPHeaderField("Ping-From", frame->document()->url());
     85     else if (!SecurityOrigin::shouldHideReferrer(pingURL, frame->loader()->outgoingReferrer()))
     86         request.setHTTPReferrer(frame->loader()->outgoingReferrer());
     87     OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
     88 
     89     // Leak the ping loader, since it will kill itself as soon as it receives a response.
     90     PingLoader* leakedPingLoader = pingLoader.leakPtr();
     91     UNUSED_PARAM(leakedPingLoader);
     92 }
     93 
     94 PingLoader::PingLoader(Frame* frame, const ResourceRequest& request)
     95     : m_timeout(this, &PingLoader::timeout)
     96 {
     97     unsigned long identifier = frame->page()->progress()->createUniqueIdentifier();
     98     m_shouldUseCredentialStorage = frame->loader()->client()->shouldUseCredentialStorage(frame->loader()->activeDocumentLoader(), identifier);
     99     m_handle = ResourceHandle::create(frame->loader()->networkingContext(), request, this, false, false);
    100 
    101     // If the server never responds, FrameLoader won't be able to cancel this load and
    102     // we'll sit here waiting forever. Set a very generous timeout, just in case.
    103     m_timeout.startOneShot(60000);
    104 }
    105 
    106 PingLoader::~PingLoader()
    107 {
    108     m_handle->cancel();
    109 }
    110 
    111 }
    112