2 * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
29 #if USE(NETWORK_SESSION)
31 #include "AuthenticationManager.h"
33 #include "NetworkCORSPreflightChecker.h"
34 #include "SessionTracker.h"
35 #include <WebCore/ContentSecurityPolicy.h>
36 #include <WebCore/CrossOriginAccessControl.h>
37 #include <WebCore/CrossOriginPreflightResultCache.h>
39 #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(m_parameters.sessionID.isAlwaysOnLoggingAllowed(), Network, "%p - PingLoad::" fmt, this, ##__VA_ARGS__)
43 using namespace WebCore;
45 PingLoad::PingLoad(NetworkResourceLoadParameters&& parameters, HTTPHeaderMap&& originalRequestHeaders)
46 : m_parameters(WTFMove(parameters))
47 , m_originalRequestHeaders(WTFMove(originalRequestHeaders))
48 , m_timeoutTimer(*this, &PingLoad::timeoutTimerFired)
49 , m_isSameOriginRequest(securityOrigin().canRequest(m_parameters.request.url()))
51 ASSERT(m_parameters.sourceOrigin);
53 // If the server never responds, this object will hang around forever.
54 // Set a very generous timeout, just in case.
55 m_timeoutTimer.startOneShot(60000_s);
57 if (m_isSameOriginRequest || m_parameters.mode == FetchOptions::Mode::NoCors) {
58 loadRequest(ResourceRequest { m_parameters.request });
62 makeCrossOriginAccessRequest(ResourceRequest { m_parameters.request });
67 if (m_redirectHandler)
68 m_redirectHandler({ });
71 ASSERT(m_task->client() == this);
72 m_task->clearClient();
77 void PingLoad::loadRequest(ResourceRequest&& request)
79 RELEASE_LOG_IF_ALLOWED("startNetworkLoad");
80 if (auto* networkSession = SessionTracker::networkSession(m_parameters.sessionID)) {
81 auto loadParameters = m_parameters;
82 loadParameters.request = WTFMove(request);
83 m_task = NetworkDataTask::create(*networkSession, *this, WTFMove(loadParameters));
89 SecurityOrigin& PingLoad::securityOrigin() const
91 return m_origin ? *m_origin : *m_parameters.sourceOrigin;
94 void PingLoad::willPerformHTTPRedirection(ResourceResponse&& redirectResponse, ResourceRequest&& request, RedirectCompletionHandler&& completionHandler)
96 RELEASE_LOG_IF_ALLOWED("willPerformHTTPRedirection - shouldFollowRedirects? %d", m_parameters.shouldFollowRedirects);
97 if (!m_parameters.shouldFollowRedirects) {
98 completionHandler({ });
102 if (auto* contentSecurityPolicy = this->contentSecurityPolicy()) {
103 if (!contentSecurityPolicy->allowConnectToSource(request.url(), redirectResponse.isNull() ? ContentSecurityPolicy::RedirectResponseReceived::No : ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
104 RELEASE_LOG_IF_ALLOWED("willPerformHTTPRedirection - Redirect was blocked by CSP");
105 completionHandler({ });
110 // FIXME: We should ensure the number of redirects does not exceed 20.
112 if (isAllowedRedirect(request.url())) {
113 completionHandler(WTFMove(request));
116 RELEASE_LOG_IF_ALLOWED("willPerformHTTPRedirection - Redirect requires a CORS preflight");
118 // Force any subsequent request to use these checks.
119 m_isSameOriginRequest = false;
121 // Use a unique origin for subsequent loads if needed.
122 // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch (Step 10).
123 ASSERT(m_parameters.mode == FetchOptions::Mode::Cors);
124 if (!securityOrigin().canRequest(redirectResponse.url()) && !protocolHostAndPortAreEqual(redirectResponse.url(), request.url())) {
125 if (!m_origin || !m_origin->isUnique())
126 m_origin = SecurityOrigin::createUnique();
129 // Except in case where preflight is needed, loading should be able to continue on its own.
130 if (m_isSimpleRequest && isSimpleCrossOriginAccessRequest(request.httpMethod(), m_originalRequestHeaders)) {
131 completionHandler(WTFMove(request));
135 m_parameters.allowStoredCredentials = DoNotAllowStoredCredentials;
136 m_redirectHandler = WTFMove(completionHandler);
138 // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
139 request.setHTTPHeaderFields(m_parameters.request.httpHeaderFields());
141 makeCrossOriginAccessRequest(WTFMove(request));
144 void PingLoad::didReceiveChallenge(const AuthenticationChallenge&, ChallengeCompletionHandler&& completionHandler)
146 RELEASE_LOG_IF_ALLOWED("didReceiveChallenge");
147 completionHandler(AuthenticationChallengeDisposition::Cancel, { });
151 void PingLoad::didReceiveResponseNetworkSession(ResourceResponse&& response, ResponseCompletionHandler&& completionHandler)
153 RELEASE_LOG_IF_ALLOWED("didReceiveResponseNetworkSession - httpStatusCode: %d", response.httpStatusCode());
154 completionHandler(PolicyAction::PolicyIgnore);
158 void PingLoad::didReceiveData(Ref<SharedBuffer>&&)
160 RELEASE_LOG_IF_ALLOWED("didReceiveData");
161 ASSERT_NOT_REACHED();
164 void PingLoad::didCompleteWithError(const ResourceError& error, const NetworkLoadMetrics&)
167 RELEASE_LOG_IF_ALLOWED("didComplete");
169 RELEASE_LOG_IF_ALLOWED("didCompleteWithError, error_code: %d", error.errorCode());
173 void PingLoad::didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend)
177 void PingLoad::wasBlocked()
179 RELEASE_LOG_IF_ALLOWED("wasBlocked");
183 void PingLoad::cannotShowURL()
185 RELEASE_LOG_IF_ALLOWED("cannotShowURL");
189 void PingLoad::timeoutTimerFired()
191 RELEASE_LOG_IF_ALLOWED("timeoutTimerFired");
195 bool PingLoad::isAllowedRedirect(const URL& url) const
197 if (m_parameters.mode == FetchOptions::Mode::NoCors)
200 return m_isSameOriginRequest && securityOrigin().canRequest(url);
203 ContentSecurityPolicy* PingLoad::contentSecurityPolicy() const
205 if (!m_contentSecurityPolicy && m_parameters.cspResponseHeaders) {
206 m_contentSecurityPolicy = std::make_unique<ContentSecurityPolicy>(*m_parameters.sourceOrigin);
207 m_contentSecurityPolicy->didReceiveHeaders(*m_parameters.cspResponseHeaders, ContentSecurityPolicy::ReportParsingErrors::No);
209 return m_contentSecurityPolicy.get();
212 void PingLoad::makeCrossOriginAccessRequest(ResourceRequest&& request)
214 ASSERT(m_parameters.mode == FetchOptions::Mode::Cors);
215 RELEASE_LOG_IF_ALLOWED("makeCrossOriginAccessRequest");
217 if (isSimpleCrossOriginAccessRequest(request.httpMethod(), m_originalRequestHeaders)) {
218 makeSimpleCrossOriginAccessRequest(WTFMove(request));
222 m_isSimpleRequest = false;
223 if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_parameters.allowStoredCredentials, request.httpMethod(), m_originalRequestHeaders)) {
224 RELEASE_LOG_IF_ALLOWED("makeCrossOriginAccessRequest - preflight can be skipped thanks to cached result");
225 preflightSuccess(WTFMove(request));
227 makeCrossOriginAccessRequestWithPreflight(WTFMove(request));
230 void PingLoad::makeSimpleCrossOriginAccessRequest(ResourceRequest&& request)
232 ASSERT(isSimpleCrossOriginAccessRequest(request.httpMethod(), m_originalRequestHeaders));
233 RELEASE_LOG_IF_ALLOWED("makeSimpleCrossOriginAccessRequest");
235 if (!request.url().protocolIsInHTTPFamily()) {
236 RELEASE_LOG_IF_ALLOWED("makeSimpleCrossOriginAccessRequest: Cross origin requests are only supported for HTTP.");
240 updateRequestForAccessControl(request, securityOrigin(), m_parameters.allowStoredCredentials);
241 loadRequest(WTFMove(request));
244 void PingLoad::makeCrossOriginAccessRequestWithPreflight(ResourceRequest&& request)
246 RELEASE_LOG_IF_ALLOWED("makeCrossOriginAccessRequestWithPreflight");
247 ASSERT(!m_corsPreflightChecker);
249 NetworkCORSPreflightChecker::Parameters parameters = {
252 m_parameters.sessionID,
253 m_parameters.allowStoredCredentials
255 m_corsPreflightChecker = std::make_unique<NetworkCORSPreflightChecker>(WTFMove(parameters), [this](NetworkCORSPreflightChecker::Result result) {
256 RELEASE_LOG_IF_ALLOWED("makeCrossOriginAccessRequestWithPreflight preflight complete, success: %d forRedirect? %d", result == NetworkCORSPreflightChecker::Result::Success, !!m_redirectHandler);
257 auto corsPreflightChecker = WTFMove(m_corsPreflightChecker);
258 if (result == NetworkCORSPreflightChecker::Result::Success)
259 preflightSuccess(ResourceRequest { corsPreflightChecker->originalRequest() });
263 m_corsPreflightChecker->startPreflight();
266 void PingLoad::preflightSuccess(ResourceRequest&& request)
268 RELEASE_LOG_IF_ALLOWED("preflightSuccess");
270 ResourceRequest actualRequest = WTFMove(request);
271 updateRequestForAccessControl(actualRequest, securityOrigin(), m_parameters.allowStoredCredentials);
273 if (auto redirectHandler = std::exchange(m_redirectHandler, nullptr))
274 redirectHandler(WTFMove(actualRequest));
276 loadRequest(WTFMove(actualRequest));
279 } // namespace WebKit
281 #endif // USE(NETWORK_SESSION)