2 * Copyright (C) 2011 Google 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 AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "CachedRawResource.h"
29 #include "CachedRawResourceClient.h"
30 #include "CachedResourceClientWalker.h"
31 #include "CachedResourceLoader.h"
32 #include "HTTPHeaderNames.h"
33 #include "SharedBuffer.h"
34 #include "SubresourceLoader.h"
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/text/StringView.h>
40 CachedRawResource::CachedRawResource(ResourceRequest& resourceRequest, Type type, SessionID sessionID)
41 : CachedResource(resourceRequest, type, sessionID)
43 , m_allowEncodedDataReplacement(true)
45 ASSERT(isMainOrRawResource());
48 const char* CachedRawResource::calculateIncrementalDataChunk(SharedBuffer* data, unsigned& incrementalDataLength)
50 incrementalDataLength = 0;
54 unsigned previousDataLength = encodedSize();
55 ASSERT(data->size() >= previousDataLength);
56 incrementalDataLength = data->size() - previousDataLength;
57 return data->data() + previousDataLength;
60 void CachedRawResource::addDataBuffer(SharedBuffer& data)
62 CachedResourceHandle<CachedRawResource> protect(this);
63 ASSERT(dataBufferingPolicy() == BufferData);
66 unsigned incrementalDataLength;
67 const char* incrementalData = calculateIncrementalDataChunk(&data, incrementalDataLength);
68 setEncodedSize(data.size());
69 notifyClientsDataWasReceived(incrementalData, incrementalDataLength);
70 if (dataBufferingPolicy() == DoNotBufferData) {
72 m_loader->setDataBufferingPolicy(DoNotBufferData);
77 CachedResource::addDataBuffer(data);
80 void CachedRawResource::addData(const char* data, unsigned length)
82 ASSERT(dataBufferingPolicy() == DoNotBufferData);
83 notifyClientsDataWasReceived(data, length);
84 CachedResource::addData(data, length);
87 void CachedRawResource::finishLoading(SharedBuffer* data)
89 CachedResourceHandle<CachedRawResource> protect(this);
90 DataBufferingPolicy dataBufferingPolicy = this->dataBufferingPolicy();
91 if (dataBufferingPolicy == BufferData) {
94 unsigned incrementalDataLength;
95 const char* incrementalData = calculateIncrementalDataChunk(data, incrementalDataLength);
97 setEncodedSize(data->size());
98 notifyClientsDataWasReceived(incrementalData, incrementalDataLength);
101 m_allowEncodedDataReplacement = !m_loader->isQuickLookResource();
103 CachedResource::finishLoading(data);
104 if (dataBufferingPolicy == BufferData && this->dataBufferingPolicy() == DoNotBufferData) {
106 m_loader->setDataBufferingPolicy(DoNotBufferData);
111 void CachedRawResource::notifyClientsDataWasReceived(const char* data, unsigned length)
116 CachedResourceHandle<CachedRawResource> protect(this);
117 CachedResourceClientWalker<CachedRawResourceClient> w(m_clients);
118 while (CachedRawResourceClient* c = w.next())
119 c->dataReceived(this, data, length);
122 void CachedRawResource::didAddClient(CachedResourceClient* c)
126 // The calls to the client can result in events running, potentially causing
127 // this resource to be evicted from the cache and all clients to be removed,
128 // so a protector is necessary.
129 CachedResourceHandle<CachedRawResource> protect(this);
130 CachedRawResourceClient* client = static_cast<CachedRawResourceClient*>(c);
131 size_t redirectCount = m_redirectChain.size();
132 for (size_t i = 0; i < redirectCount; i++) {
133 RedirectPair redirect = m_redirectChain[i];
134 ResourceRequest request(redirect.m_request);
135 client->redirectReceived(this, request, redirect.m_redirectResponse);
139 ASSERT(redirectCount == m_redirectChain.size());
141 if (!m_response.isNull()) {
142 ResourceResponse response(m_response);
143 if (validationCompleting())
144 response.setSource(ResourceResponse::Source::MemoryCacheAfterValidation);
146 ASSERT(!validationInProgress());
147 response.setSource(ResourceResponse::Source::MemoryCache);
149 client->responseReceived(this, response);
154 client->dataReceived(this, m_data->data(), m_data->size());
157 CachedResource::didAddClient(client);
160 void CachedRawResource::allClientsRemoved()
163 m_loader->cancelIfNotFinishing();
166 void CachedRawResource::redirectReceived(ResourceRequest& request, const ResourceResponse& response)
168 CachedResourceHandle<CachedRawResource> protect(this);
169 if (!response.isNull()) {
170 CachedResourceClientWalker<CachedRawResourceClient> w(m_clients);
171 while (CachedRawResourceClient* c = w.next())
172 c->redirectReceived(this, request, response);
173 m_redirectChain.append(RedirectPair(request, response));
175 CachedResource::redirectReceived(request, response);
178 void CachedRawResource::responseReceived(const ResourceResponse& response)
180 CachedResourceHandle<CachedRawResource> protect(this);
182 m_identifier = m_loader->identifier();
183 CachedResource::responseReceived(response);
184 CachedResourceClientWalker<CachedRawResourceClient> w(m_clients);
185 while (CachedRawResourceClient* c = w.next())
186 c->responseReceived(this, m_response);
189 void CachedRawResource::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
191 CachedResourceClientWalker<CachedRawResourceClient> w(m_clients);
192 while (CachedRawResourceClient* c = w.next())
193 c->dataSent(this, bytesSent, totalBytesToBeSent);
196 void CachedRawResource::switchClientsToRevalidatedResource()
199 // If we're in the middle of a successful revalidation, responseReceived() hasn't been called, so we haven't set m_identifier.
200 ASSERT(!m_identifier);
201 downcast<CachedRawResource>(*resourceToRevalidate()).m_identifier = m_loader->identifier();
202 CachedResource::switchClientsToRevalidatedResource();
205 void CachedRawResource::setDefersLoading(bool defers)
208 m_loader->setDefersLoading(defers);
211 void CachedRawResource::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
213 m_options.setDataBufferingPolicy(dataBufferingPolicy);
216 static bool shouldIgnoreHeaderForCacheReuse(HTTPHeaderName name)
219 // FIXME: This list of headers that don't affect cache policy almost certainly isn't complete.
220 case HTTPHeaderName::Accept:
221 case HTTPHeaderName::CacheControl:
222 case HTTPHeaderName::Pragma:
223 case HTTPHeaderName::Purpose:
224 case HTTPHeaderName::Referer:
225 case HTTPHeaderName::UserAgent:
233 bool CachedRawResource::canReuse(const ResourceRequest& newRequest) const
235 if (dataBufferingPolicy() == DoNotBufferData)
238 if (m_resourceRequest.httpMethod() != newRequest.httpMethod())
241 if (m_resourceRequest.httpBody() != newRequest.httpBody())
244 if (m_resourceRequest.allowCookies() != newRequest.allowCookies())
247 // Ensure most headers match the existing headers before continuing.
248 // Note that the list of ignored headers includes some headers explicitly related to caching.
249 // A more detailed check of caching policy will be performed later, this is simply a list of
250 // headers that we might permit to be different and still reuse the existing CachedResource.
251 const HTTPHeaderMap& newHeaders = newRequest.httpHeaderFields();
252 const HTTPHeaderMap& oldHeaders = m_resourceRequest.httpHeaderFields();
254 for (const auto& header : newHeaders) {
255 if (header.keyAsHTTPHeaderName) {
256 if (!shouldIgnoreHeaderForCacheReuse(header.keyAsHTTPHeaderName.value())
257 && header.value != oldHeaders.commonHeaders().get(header.keyAsHTTPHeaderName.value()))
259 } else if (header.value != oldHeaders.uncommonHeaders().get(header.key))
263 // For this second loop, we don't actually need to compare values, checking that the
264 // key is contained in newHeaders is sufficient due to the previous loop.
265 for (const auto& header : oldHeaders) {
266 if (header.keyAsHTTPHeaderName) {
267 if (!shouldIgnoreHeaderForCacheReuse(header.keyAsHTTPHeaderName.value())
268 && !newHeaders.commonHeaders().contains(header.keyAsHTTPHeaderName.value()))
270 } else if (!newHeaders.uncommonHeaders().contains(header.key))
277 void CachedRawResource::clear()
282 m_loader->clearResourceData();
285 } // namespace WebCore