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