http/tests/workers/service/service-worker-download.https.html times out with async...
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / ServiceWorkerClientFetch.cpp
1 /*
2  * Copyright (C) 2017 Apple 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
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "ServiceWorkerClientFetch.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "DataReference.h"
32 #include "WebSWClientConnection.h"
33 #include "WebServiceWorkerProvider.h"
34 #include <WebCore/CrossOriginAccessControl.h>
35 #include <WebCore/MIMETypeRegistry.h>
36 #include <WebCore/NotImplemented.h>
37 #include <WebCore/ResourceError.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 Ref<ServiceWorkerClientFetch> ServiceWorkerClientFetch::create(WebServiceWorkerProvider& serviceWorkerProvider, Ref<WebCore::ResourceLoader>&& loader, uint64_t identifier, Ref<WebSWClientConnection>&& connection, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Callback&& callback)
44 {
45     auto fetch = adoptRef(*new ServiceWorkerClientFetch { serviceWorkerProvider, WTFMove(loader), identifier, WTFMove(connection), shouldClearReferrerOnHTTPSToHTTPRedirect, WTFMove(callback) });
46     fetch->start();
47     return fetch;
48 }
49
50 ServiceWorkerClientFetch::~ServiceWorkerClientFetch()
51 {
52 }
53
54 ServiceWorkerClientFetch::ServiceWorkerClientFetch(WebServiceWorkerProvider& serviceWorkerProvider, Ref<WebCore::ResourceLoader>&& loader, uint64_t identifier, Ref<WebSWClientConnection>&& connection, bool shouldClearReferrerOnHTTPSToHTTPRedirect, Callback&& callback)
55     : m_serviceWorkerProvider(serviceWorkerProvider)
56     , m_loader(WTFMove(loader))
57     , m_identifier(identifier)
58     , m_connection(WTFMove(connection))
59     , m_callback(WTFMove(callback))
60     , m_shouldClearReferrerOnHTTPSToHTTPRedirect(shouldClearReferrerOnHTTPSToHTTPRedirect)
61 {
62 }
63
64 void ServiceWorkerClientFetch::start()
65 {
66     auto request = m_loader->request();
67     auto& options = m_loader->options();
68
69     auto referrer = request.httpReferrer();
70
71     m_didFail = false;
72     m_didFinish = false;
73
74     // We are intercepting fetch calls after going through the HTTP layer, which may add some specific headers.
75     cleanHTTPRequestHeadersForAccessControl(request, options.httpHeadersToKeep);
76
77     ASSERT(options.serviceWorkersMode != ServiceWorkersMode::None);
78     m_connection->startFetch(m_loader->identifier(), options.serviceWorkerRegistrationIdentifier.value(), request, options, referrer);
79
80     m_redirectionStatus = RedirectionStatus::None;
81 }
82
83 // https://fetch.spec.whatwg.org/#http-fetch step 3.3
84 std::optional<ResourceError> ServiceWorkerClientFetch::validateResponse(const ResourceResponse& response)
85 {
86     // FIXME: make a better error reporting.
87     if (response.type() == ResourceResponse::Type::Error)
88         return ResourceError { ResourceError::Type::General };
89
90     auto& options = m_loader->options();
91     if (options.mode != FetchOptions::Mode::NoCors && response.tainting() == ResourceResponse::Tainting::Opaque)
92         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker is opaque"), ResourceError::Type::AccessControl };
93
94     // Navigate mode induces manual redirect.
95     if (options.redirect != FetchOptions::Redirect::Manual && options.mode != FetchOptions::Mode::Navigate && response.tainting() == ResourceResponse::Tainting::Opaqueredirect)
96         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker is opaque redirect"), ResourceError::Type::AccessControl };
97
98     if ((options.redirect != FetchOptions::Redirect::Follow || options.mode == FetchOptions::Mode::Navigate) && response.isRedirected())
99         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker has redirections"), ResourceError::Type::AccessControl };
100
101     return std::nullopt;
102 }
103
104 void ServiceWorkerClientFetch::didReceiveResponse(ResourceResponse&& response)
105 {
106     m_isCheckingResponse = true;
107     callOnMainThread([this, protectedThis = makeRef(*this), response = WTFMove(response)]() mutable {
108         if (!m_loader) {
109             m_isCheckingResponse = false;
110             return;
111         }
112
113         if (auto error = validateResponse(response)) {
114             m_isCheckingResponse = false;
115             m_loader->didFail(error.value());
116             ASSERT(!m_loader);
117             if (auto callback = WTFMove(m_callback))
118                 callback(Result::Succeeded);
119             return;
120         }
121         response.setSource(ResourceResponse::Source::ServiceWorker);
122
123         if (response.isRedirection() && response.httpHeaderFields().contains(HTTPHeaderName::Location)) {
124             m_isCheckingResponse = false;
125             continueLoadingAfterCheckingResponse();
126             m_redirectionStatus = RedirectionStatus::Receiving;
127             m_loader->willSendRequest(m_loader->request().redirectedRequest(response, m_shouldClearReferrerOnHTTPSToHTTPRedirect), response, [protectedThis = makeRef(*this), this](ResourceRequest&& request) {
128                 if (request.isNull() || !m_callback)
129                     return;
130
131                 ASSERT(request == m_loader->request());
132                 if (m_redirectionStatus == RedirectionStatus::Received) {
133                     start();
134                     return;
135                 }
136                 m_redirectionStatus = RedirectionStatus::Following;
137             });
138             return;
139         }
140
141         // In case of main resource and mime type is the default one, we set it to text/html to pass more service worker WPT tests.
142         // FIXME: We should refine our MIME type sniffing strategy for synthetic responses.
143         if (m_loader->originalRequest().requester() == ResourceRequest::Requester::Main) {
144             if (response.mimeType() == defaultMIMEType()) {
145                 response.setMimeType(ASCIILiteral("text/html"));
146                 response.setTextEncodingName(ASCIILiteral("UTF-8"));
147             }
148         }
149
150         // As per https://fetch.spec.whatwg.org/#main-fetch step 9, copy request's url list in response's url list if empty.
151         if (response.url().isNull())
152             response.setURL(m_loader->request().url());
153
154         m_loader->didReceiveResponse(response, [this, protectedThis = WTFMove(protectedThis)] {
155             m_isCheckingResponse = false;
156             continueLoadingAfterCheckingResponse();
157             if (auto callback = WTFMove(m_callback))
158                 callback(Result::Succeeded);
159         });
160     });
161 }
162
163 void ServiceWorkerClientFetch::didReceiveData(const IPC::DataReference& dataReference, int64_t encodedDataLength)
164 {
165     auto* data = reinterpret_cast<const char*>(dataReference.data());
166     if (!m_buffer) {
167         m_buffer = SharedBuffer::create(data, dataReference.size());
168         m_encodedDataLength = encodedDataLength;
169     } else {
170         m_buffer->append(data, dataReference.size());
171         m_encodedDataLength += encodedDataLength;
172     }
173
174     if (m_isCheckingResponse)
175         return;
176
177     callOnMainThread([this, protectedThis = makeRef(*this)] {
178         if (!m_loader)
179             return;
180
181         m_loader->didReceiveBuffer(m_buffer.releaseNonNull(), m_encodedDataLength, DataPayloadBytes);
182         m_encodedDataLength = 0;
183     });
184 }
185
186 void ServiceWorkerClientFetch::didReceiveFormData(const IPC::FormDataReference&)
187 {
188     // FIXME: Implement form data reading.
189 }
190
191 void ServiceWorkerClientFetch::didFinish()
192 {
193     m_didFinish = true;
194
195     if (m_isCheckingResponse)
196         return;
197
198     callOnMainThread([this, protectedThis = makeRef(*this)] {
199         if (!m_loader)
200             return;
201
202         switch (m_redirectionStatus) {
203         case RedirectionStatus::None:
204             break;
205         case RedirectionStatus::Receiving:
206             m_redirectionStatus = RedirectionStatus::Received;
207             return;
208         case RedirectionStatus::Following:
209             start();
210             return;
211         case RedirectionStatus::Received:
212             ASSERT_NOT_REACHED();
213             m_redirectionStatus = RedirectionStatus::None;
214         }
215
216         ASSERT(!m_callback);
217
218         m_loader->didFinishLoading(NetworkLoadMetrics { });
219         m_serviceWorkerProvider.fetchFinished(m_identifier);
220     });
221 }
222
223 void ServiceWorkerClientFetch::didFail()
224 {
225     m_didFail = true;
226
227     if (m_isCheckingResponse)
228         return;
229
230     callOnMainThread([this, protectedThis = makeRef(*this)] {
231         if (!m_loader)
232             return;
233
234         m_loader->didFail({ ResourceError::Type::General });
235
236         if (auto callback = WTFMove(m_callback))
237             callback(Result::Succeeded);
238
239         m_serviceWorkerProvider.fetchFinished(m_identifier);
240     });
241 }
242
243 void ServiceWorkerClientFetch::didNotHandle()
244 {
245     ASSERT(!m_isCheckingResponse);
246
247     callOnMainThread([this, protectedThis = makeRef(*this)] {
248         if (!m_loader)
249             return;
250
251         if (auto callback = WTFMove(m_callback))
252             callback(Result::Unhandled);
253
254         m_serviceWorkerProvider.fetchFinished(m_identifier);
255     });
256 }
257
258 void ServiceWorkerClientFetch::cancel()
259 {
260     if (auto callback = WTFMove(m_callback))
261         callback(Result::Cancelled);
262     m_loader = nullptr;
263     m_buffer = nullptr;
264 }
265
266 void ServiceWorkerClientFetch::continueLoadingAfterCheckingResponse()
267 {
268     ASSERT(!m_isCheckingResponse);
269     if (!m_loader)
270         return;
271
272     if (m_encodedDataLength) {
273         callOnMainThread([this, protectedThis = makeRef(*this)] {
274             if (!m_loader || !m_encodedDataLength)
275                 return;
276             m_loader->didReceiveBuffer(m_buffer.releaseNonNull(), m_encodedDataLength, DataPayloadBytes);
277             m_encodedDataLength = 0;
278         });
279     }
280
281     if (m_didFail) {
282         didFail();
283         return;
284     }
285
286     if (m_didFinish)
287         didFinish();
288 }
289
290 } // namespace WebKit
291
292 #endif // ENABLE(SERVICE_WORKER)