blob: b9253d1a197e6e90725d70849e9c81cca0708253 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 "core/loader/PingLoader.h"
34
35#include "core/dom/Document.h"
36#include "core/inspector/InspectorInstrumentation.h"
37#include "core/loader/FrameLoader.h"
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010038#include "core/loader/FrameLoaderClient.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010039#include "core/loader/UniqueIdentifier.h"
40#include "core/page/Frame.h"
Ben Murdoch83750172013-07-24 10:36:59 +010041#include "core/platform/chromium/support/WrappedResourceRequest.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042#include "core/platform/network/FormData.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043#include "core/platform/network/ResourceRequest.h"
44#include "core/platform/network/ResourceResponse.h"
Ben Murdoch83750172013-07-24 10:36:59 +010045#include "public/platform/Platform.h"
46#include "public/platform/WebURLLoader.h"
Torne (Richard Coles)e5249552013-05-15 11:35:13 +010047#include "weborigin/SecurityOrigin.h"
48#include "weborigin/SecurityPolicy.h"
Ben Murdoch591b9582013-07-10 11:41:44 +010049#include "wtf/OwnPtr.h"
50#include "wtf/UnusedParam.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010051
52namespace WebCore {
53
54void PingLoader::loadImage(Frame* frame, const KURL& url)
55{
56 if (!frame->document()->securityOrigin()->canDisplay(url)) {
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010057 FrameLoader::reportLocalLoadFailed(frame, url.string());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010058 return;
59 }
60
61 ResourceRequest request(url);
62 request.setTargetType(ResourceRequest::TargetIsImage);
63 request.setHTTPHeaderField("Cache-Control", "max-age=0");
64 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), request.url(), frame->loader()->outgoingReferrer());
65 if (!referrer.isEmpty())
66 request.setHTTPReferrer(referrer);
67 frame->loader()->addExtraFieldsToRequest(request);
68 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
69
70 // Leak the ping loader, since it will kill itself as soon as it receives a response.
71 PingLoader* leakedPingLoader = pingLoader.leakPtr();
72 UNUSED_PARAM(leakedPingLoader);
73}
74
75// http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
76void PingLoader::sendPing(Frame* frame, const KURL& pingURL, const KURL& destinationURL)
77{
78 ResourceRequest request(pingURL);
79 request.setTargetType(ResourceRequest::TargetIsSubresource);
80 request.setHTTPMethod("POST");
81 request.setHTTPContentType("text/ping");
82 request.setHTTPBody(FormData::create("PING"));
83 request.setHTTPHeaderField("Cache-Control", "max-age=0");
84 frame->loader()->addExtraFieldsToRequest(request);
85
86 SecurityOrigin* sourceOrigin = frame->document()->securityOrigin();
87 RefPtr<SecurityOrigin> pingOrigin = SecurityOrigin::create(pingURL);
88 FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin->toString());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010089 request.setHTTPHeaderField("Ping-To", destinationURL.string());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010090 if (!SecurityPolicy::shouldHideReferrer(pingURL, frame->loader()->outgoingReferrer())) {
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010091 request.setHTTPHeaderField("Ping-From", frame->document()->url().string());
92 if (!sourceOrigin->isSameSchemeHostPort(pingOrigin.get())) {
93 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), pingURL, frame->loader()->outgoingReferrer());
94 if (!referrer.isEmpty())
95 request.setHTTPReferrer(referrer);
96 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010097 }
98 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request));
99
100 // Leak the ping loader, since it will kill itself as soon as it receives a response.
101 PingLoader* leakedPingLoader = pingLoader.leakPtr();
102 UNUSED_PARAM(leakedPingLoader);
103}
104
Ben Murdoche69819b2013-07-17 14:56:49 +0100105void PingLoader::sendViolationReport(Frame* frame, const KURL& reportURL, PassRefPtr<FormData> report, ViolationReportType type)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100106{
107 ResourceRequest request(reportURL);
108 request.setTargetType(ResourceRequest::TargetIsSubresource);
109 request.setHTTPMethod("POST");
Ben Murdoche69819b2013-07-17 14:56:49 +0100110 request.setHTTPContentType(type == ContentSecurityPolicyViolationReport ? "application/csp-report" : "application/json");
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111 request.setHTTPBody(report);
112 frame->loader()->addExtraFieldsToRequest(request);
113
114 String referrer = SecurityPolicy::generateReferrerHeader(frame->document()->referrerPolicy(), reportURL, frame->loader()->outgoingReferrer());
115 if (!referrer.isEmpty())
116 request.setHTTPReferrer(referrer);
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100117 OwnPtr<PingLoader> pingLoader = adoptPtr(new PingLoader(frame, request, SecurityOrigin::create(reportURL)->isSameSchemeHostPort(frame->document()->securityOrigin()) ? AllowStoredCredentials : DoNotAllowStoredCredentials));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100118
119 // Leak the ping loader, since it will kill itself as soon as it receives a response.
120 PingLoader* leakedPingLoader = pingLoader.leakPtr();
121 UNUSED_PARAM(leakedPingLoader);
122}
123
Torne (Richard Coles)e5249552013-05-15 11:35:13 +0100124PingLoader::PingLoader(Frame* frame, ResourceRequest& request, StoredCredentials credentialsAllowed)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100125 : m_timeout(this, &PingLoader::timeout)
126{
Ben Murdoch1fad5ca2013-08-07 11:05:11 +0100127 frame->loader()->client()->didDispatchPingLoader(request.url());
128
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100129 unsigned long identifier = createUniqueIdentifier();
Ben Murdoch83750172013-07-24 10:36:59 +0100130 m_loader = adoptPtr(WebKit::Platform::current()->createURLLoader());
131 ASSERT(m_loader);
132 WebKit::WrappedResourceRequest wrappedRequest(request);
133 wrappedRequest.setAllowStoredCredentials(credentialsAllowed == AllowStoredCredentials);
134 m_loader->loadAsynchronously(wrappedRequest, this);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100135
136 InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame->loader()->activeDocumentLoader(), request, ResourceResponse());
137
138 // If the server never responds, FrameLoader won't be able to cancel this load and
139 // we'll sit here waiting forever. Set a very generous timeout, just in case.
140 m_timeout.startOneShot(60000);
141}
142
143PingLoader::~PingLoader()
144{
Ben Murdoch83750172013-07-24 10:36:59 +0100145 if (m_loader)
146 m_loader->cancel();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100147}
148
149}