2808f87827ac1dc5334537e037704e8b045d9215
[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     // We are intercepting fetch calls after going through the HTTP layer, which may add some specific headers.
72     cleanHTTPRequestHeadersForAccessControl(request, options.httpHeadersToKeep);
73
74     ASSERT(options.serviceWorkersMode != ServiceWorkersMode::None);
75     m_connection->startFetch(m_loader->identifier(), options.serviceWorkerRegistrationIdentifier.value(), request, options, referrer);
76
77     m_redirectionStatus = RedirectionStatus::None;
78 }
79
80 // https://fetch.spec.whatwg.org/#http-fetch step 3.3
81 std::optional<ResourceError> ServiceWorkerClientFetch::validateResponse(const ResourceResponse& response)
82 {
83     // FIXME: make a better error reporting.
84     if (response.type() == ResourceResponse::Type::Error)
85         return ResourceError { ResourceError::Type::General };
86
87     auto& options = m_loader->options();
88     if (options.mode != FetchOptions::Mode::NoCors && response.tainting() == ResourceResponse::Tainting::Opaque)
89         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker is opaque"), ResourceError::Type::AccessControl };
90
91     // Navigate mode induces manual redirect.
92     if (options.redirect != FetchOptions::Redirect::Manual && options.mode != FetchOptions::Mode::Navigate && response.tainting() == ResourceResponse::Tainting::Opaqueredirect)
93         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker is opaque redirect"), ResourceError::Type::AccessControl };
94
95     if ((options.redirect != FetchOptions::Redirect::Follow || options.mode == FetchOptions::Mode::Navigate) && response.isRedirected())
96         return ResourceError { errorDomainWebKitInternal, 0, response.url(), ASCIILiteral("Response served by service worker has redirections"), ResourceError::Type::AccessControl };
97
98     return std::nullopt;
99 }
100
101 void ServiceWorkerClientFetch::didReceiveResponse(ResourceResponse&& response)
102 {
103     callOnMainThread([this, protectedThis = makeRef(*this), response = WTFMove(response)]() mutable {
104         if (!m_loader)
105             return;
106
107         if (auto error = validateResponse(response)) {
108             m_loader->didFail(error.value());
109             ASSERT(!m_loader);
110             if (auto callback = WTFMove(m_callback))
111                 callback(Result::Succeeded);
112             return;
113         }
114         response.setSource(ResourceResponse::Source::ServiceWorker);
115
116         if (response.isRedirection() && response.httpHeaderFields().contains(HTTPHeaderName::Location)) {
117             m_redirectionStatus = RedirectionStatus::Receiving;
118             m_loader->willSendRequest(m_loader->request().redirectedRequest(response, m_shouldClearReferrerOnHTTPSToHTTPRedirect), response, [protectedThis = makeRef(*this), this](ResourceRequest&& request) {
119                 if (request.isNull() || !m_callback)
120                     return;
121
122                 ASSERT(request == m_loader->request());
123                 if (m_redirectionStatus == RedirectionStatus::Received) {
124                     start();
125                     return;
126                 }
127                 m_redirectionStatus = RedirectionStatus::Following;
128             });
129             return;
130         }
131
132         // 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.
133         // FIXME: We should refine our MIME type sniffing strategy for synthetic responses.
134         if (m_loader->originalRequest().requester() == ResourceRequest::Requester::Main) {
135             if (response.mimeType() == defaultMIMEType()) {
136                 response.setMimeType(ASCIILiteral("text/html"));
137                 response.setTextEncodingName(ASCIILiteral("UTF-8"));
138             }
139         }
140
141         // As per https://fetch.spec.whatwg.org/#main-fetch step 9, copy request's url list in response's url list if empty.
142         if (response.url().isNull())
143             response.setURL(m_loader->request().url());
144
145         m_isCheckingResponse = true;
146         m_loader->didReceiveResponse(response, [this, protectedThis = WTFMove(protectedThis)] {
147             m_isCheckingResponse = false;
148             continueLoadingAfterCheckingResponse();
149             if (auto callback = WTFMove(m_callback))
150                 callback(Result::Succeeded);
151         });
152     });
153 }
154
155 void ServiceWorkerClientFetch::didReceiveData(const IPC::DataReference& dataReference, int64_t encodedDataLength)
156 {
157     auto* data = reinterpret_cast<const char*>(dataReference.data());
158     if (!m_buffer) {
159         m_buffer = SharedBuffer::create(data, dataReference.size());
160         m_encodedDataLength = encodedDataLength;
161     } else {
162         m_buffer->append(data, dataReference.size());
163         m_encodedDataLength += encodedDataLength;
164     }
165
166     if (m_isCheckingResponse)
167         return;
168
169     callOnMainThread([this, protectedThis = makeRef(*this)] {
170         if (!m_loader)
171             return;
172
173         m_loader->didReceiveBuffer(m_buffer.releaseNonNull(), m_encodedDataLength, DataPayloadBytes);
174         m_encodedDataLength = 0;
175     });
176 }
177
178 void ServiceWorkerClientFetch::didReceiveFormData(const IPC::FormDataReference&)
179 {
180     // FIXME: Implement form data reading.
181 }
182
183 void ServiceWorkerClientFetch::didFinish()
184 {
185     m_didFinish = true;
186
187     if (m_isCheckingResponse)
188         return;
189
190     callOnMainThread([this, protectedThis = makeRef(*this)] {
191         if (!m_loader)
192             return;
193
194         switch (m_redirectionStatus) {
195         case RedirectionStatus::None:
196             break;
197         case RedirectionStatus::Receiving:
198             m_redirectionStatus = RedirectionStatus::Received;
199             return;
200         case RedirectionStatus::Following:
201             start();
202             return;
203         case RedirectionStatus::Received:
204             ASSERT_NOT_REACHED();
205             m_redirectionStatus = RedirectionStatus::None;
206         }
207
208         ASSERT(!m_callback);
209
210         m_loader->didFinishLoading(NetworkLoadMetrics { });
211         m_serviceWorkerProvider.fetchFinished(m_identifier);
212     });
213 }
214
215 void ServiceWorkerClientFetch::didFail()
216 {
217     m_didFail = true;
218
219     if (m_isCheckingResponse)
220         return;
221
222     callOnMainThread([this, protectedThis = makeRef(*this)] {
223         if (!m_loader)
224             return;
225
226         m_loader->didFail({ ResourceError::Type::General });
227
228         if (auto callback = WTFMove(m_callback))
229             callback(Result::Succeeded);
230
231         m_serviceWorkerProvider.fetchFinished(m_identifier);
232     });
233 }
234
235 void ServiceWorkerClientFetch::didNotHandle()
236 {
237     ASSERT(!m_isCheckingResponse);
238
239     callOnMainThread([this, protectedThis = makeRef(*this)] {
240         if (!m_loader)
241             return;
242
243         if (auto callback = WTFMove(m_callback))
244             callback(Result::Unhandled);
245
246         m_serviceWorkerProvider.fetchFinished(m_identifier);
247     });
248 }
249
250 void ServiceWorkerClientFetch::cancel()
251 {
252     if (auto callback = WTFMove(m_callback))
253         callback(Result::Cancelled);
254     m_loader = nullptr;
255     m_buffer = nullptr;
256 }
257
258 void ServiceWorkerClientFetch::continueLoadingAfterCheckingResponse()
259 {
260     ASSERT(!m_isCheckingResponse);
261     if (!m_loader)
262         return;
263
264     if (m_encodedDataLength) {
265         callOnMainThread([this, protectedThis = makeRef(*this)] {
266             if (!m_loader || !m_encodedDataLength)
267                 return;
268             m_loader->didReceiveBuffer(m_buffer.releaseNonNull(), m_encodedDataLength, DataPayloadBytes);
269             m_encodedDataLength = 0;
270         });
271     }
272
273     if (m_didFail) {
274         didFail();
275         return;
276     }
277
278     if (m_didFinish)
279         didFinish();
280 }
281
282 } // namespace WebKit
283
284 #endif // ENABLE(SERVICE_WORKER)