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 "Document.h" 36#include "FormData.h" 37#include "Frame.h" 38#include "FrameLoader.h" 39#include "FrameLoaderClient.h" 40#include "InspectorInstrumentation.h" 41#include "Page.h" 42#include "ProgressTracker.h" 43#include "ResourceHandle.h" 44#include "ResourceRequest.h" 45#include "ResourceResponse.h" 46#include "SecurityOrigin.h" 47#include "SecurityPolicy.h" 48#include <wtf/OwnPtr.h> 49#include <wtf/text/CString.h> 50 51namespace WebCore { 52 53void PingLoader::loadImage(Frame* frame, const KURL& url) 54{ 55 if (!frame->document()->securityOrigin()->canDisplay(url)) { 56 FrameLoader::reportLocalLoadFailed(frame, url); 57 return; 58 } 59 60 ResourceRequest request(url); 61#if PLATFORM(BLACKBERRY) 62 request.setTargetType(ResourceRequest::TargetIsImage); 63#endif 64 request.setHTTPHeaderField("Cache-Control", "max-age=0"); 65 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), request.url(), frame->loader()->outgoingReferrer()); 66 if (!referrer.isEmpty()) 67 request.setHTTPReferrer(referrer); 68 frame->loader()->addExtraFieldsToSubresourceRequest(request); 69 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request)); 70 71 // Leak the ping loader, since it will kill itself as soon as it receives a response. 72 PingLoader* leakedPingLoader = pingLoader.leakPtr(); 73 UNUSED_PARAM(leakedPingLoader); 74} 75 76// http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing 77void PingLoader::sendPing(Frame* frame, const KURL& pingURL, const KURL& destinationURL) 78{ 79 ResourceRequest request(pingURL); 80#if PLATFORM(BLACKBERRY) 81 request.setTargetType(ResourceRequest::TargetIsSubresource); 82#endif 83 request.setHTTPMethod("POST"); 84 request.setHTTPContentType("text/ping"); 85 request.setHTTPBody(FormData::create("PING")); 86 request.setHTTPHeaderField("Cache-Control", "max-age=0"); 87 frame->loader()->addExtraFieldsToSubresourceRequest(request); 88 89 SecurityOrigin* sourceOrigin = frame->document()->securityOrigin(); 90 RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL); 91 FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin->toString()); 92 request.setHTTPHeaderField("Ping-To", destinationURL); 93 if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->loader()->outgoingReferrer())) { 94 request.setHTTPHeaderField("Ping-From", frame->document()->url()); 95 if (!sourceOrigin->isSameSchemeHostPort(pingOrigin.get())) { 96 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), pingURL, frame->loader()->outgoingReferrer()); 97 if (!referrer.isEmpty()) 98 request.setHTTPReferrer(referrer); 99 } 100 } 101 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request)); 102 103 // Leak the ping loader, since it will kill itself as soon as it receives a response. 104 PingLoader* leakedPingLoader = pingLoader.leakPtr(); 105 UNUSED_PARAM(leakedPingLoader); 106} 107 108void PingLoader::sendViolationReport(Frame* frame, const KURL& reportURL, PassRefPtr<FormData> report) 109{ 110 ResourceRequest request(reportURL); 111#if PLATFORM(BLACKBERRY) 112 request.setTargetType(ResourceRequest::TargetIsSubresource); 113#endif 114 request.setHTTPMethod("POST"); 115 request.setHTTPContentType("application/json"); 116 request.setHTTPBody(report); 117 frame->loader()->addExtraFieldsToSubresourceRequest(request); 118 119 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), reportURL, frame->loader()->outgoingReferrer()); 120 if (!referrer.isEmpty()) 121 request.setHTTPReferrer(referrer); 122 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request)); 123 124 // Leak the ping loader, since it will kill itself as soon as it receives a response. 125 PingLoader* leakedPingLoader = pingLoader.leakPtr(); 126 UNUSED_PARAM(leakedPingLoader); 127} 128 129PingLoader::PingLoader(Frame* frame, ResourceRequest& request) 130 : m_timeout(this, &PingLoader::timeout) 131{ 132 unsigned long identifier = frame->page()->progress()->createUniqueIdentifier(); 133 // FIXME: Why activeDocumentLoader? I would have expected documentLoader(). 134 // Itseems like the PingLoader should be associated with the current 135 // Document in the Frame, but the activeDocumentLoader will be associated 136 // with the provisional DocumentLoader if there is a provisional 137 // DocumentLoader. 138 m_shouldUseCredentialStorage = frame->loader()->client()->shouldUseCredentialStorage(frame->loader()->activeDocumentLoader(), identifier); 139 m_handle = ResourceHandle::create(frame->loader()->networkingContext(), request, this, false, false); 140 141 InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame->loader()->activeDocumentLoader(), request, ResourceResponse()); 142 143 // If the server never responds, FrameLoader won't be able to cancel this load and 144 // we'll sit here waiting forever. Set a very generous timeout, just in case. 145 m_timeout.startOneShot(60000); 146} 147 148PingLoader::~PingLoader() 149{ 150 if (m_handle) 151 m_handle->cancel(); 152} 153 154} 155