1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* Copyright (C) 2011 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "CachedRawResource.h"
#include "CachedRawResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "CachedResourceLoader.h"
#include "HTTPHeaderNames.h"
#include "Logging.h"
#include "SharedBuffer.h"
#include "SubresourceLoader.h"
#include <wtf/CompletionHandler.h>
#include <wtf/SetForScope.h>
#include <wtf/text/StringView.h>
#define RELEASE_LOG_ALWAYS(fmt, ...) RELEASE_LOG(Network, "%p - CachedRawResource::" fmt, this, ##__VA_ARGS__)
namespace WebCore {
CachedRawResource::CachedRawResource(CachedResourceRequest&& request, Type type, PAL::SessionID sessionID, const CookieJar* cookieJar)
: CachedResource(WTFMove(request), type, sessionID, cookieJar)
{
ASSERT(isMainOrMediaOrIconOrRawResource());
}
std::optional<SharedBufferDataView> CachedRawResource::calculateIncrementalDataChunk(const FragmentedSharedBuffer& data) const
{
size_t previousDataLength = encodedSize();
if (data.size() <= previousDataLength)
return std::nullopt;
return data.getSomeData(previousDataLength);
}
void CachedRawResource::updateBuffer(const FragmentedSharedBuffer& data)
{
// Skip any updateBuffers triggered from nested runloops. We'll have the complete buffer in finishLoading.
if (m_inIncrementalDataNotify)
return;
// We need to keep a strong reference to both the SharedBuffer and the current CachedRawResource instance
// as notifyClientsDataWasReceived call may delete both.
CachedResourceHandle protectedThis(this);
Ref protectedData { data };
ASSERT(dataBufferingPolicy() == DataBufferingPolicy::BufferData);
// While m_data is immutable, we need to drop the const, this will be removed in bug 236736.
m_data = const_cast<FragmentedSharedBuffer*>(&data);
// Notify clients only of the newly appended content since the last run.
auto previousDataSize = encodedSize();
while (data.size() > previousDataSize) {
auto incrementalData = data.getSomeData(previousDataSize);
previousDataSize += incrementalData.size();
SetForScope notifyScope(m_inIncrementalDataNotify, true);
notifyClientsDataWasReceived(incrementalData.createSharedBuffer());
}
setEncodedSize(data.size());
if (dataBufferingPolicy() == DataBufferingPolicy::DoNotBufferData) {
if (RefPtr loader = m_loader)
loader->setDataBufferingPolicy(DataBufferingPolicy::DoNotBufferData);
clear();
} else
CachedResource::updateBuffer(data);
if (m_delayedFinishLoading) {
auto delayedFinishLoading = std::exchange(m_delayedFinishLoading, std::nullopt);
finishLoading(delayedFinishLoading->buffer.get(), { });
}
}
void CachedRawResource::updateData(const SharedBuffer& buffer)
{
ASSERT(dataBufferingPolicy() == DataBufferingPolicy::DoNotBufferData);
notifyClientsDataWasReceived(buffer);
CachedResource::updateData(buffer);
}
void CachedRawResource::finishLoading(const FragmentedSharedBuffer* data, const NetworkLoadMetrics& metrics)
{
if (m_inIncrementalDataNotify) {
// We may get here synchronously from updateBuffer() if the callback there ends up spinning a runloop.
// In that case delay the call.
m_delayedFinishLoading = std::make_optional(DelayedFinishLoading { data });
return;
};
CachedResourceHandle protectedThis { this };
DataBufferingPolicy dataBufferingPolicy = this->dataBufferingPolicy();
if (dataBufferingPolicy == DataBufferingPolicy::BufferData) {
m_data = const_cast<FragmentedSharedBuffer*>(data);
if (data) {
if (auto incrementalData = calculateIncrementalDataChunk(*data)) {
setEncodedSize(data->size());
notifyClientsDataWasReceived(incrementalData->createSharedBuffer());
}
}
}
#if USE(QUICK_LOOK)
m_allowEncodedDataReplacement = m_loader && !m_loader->isQuickLookResource();
#endif
CachedResource::finishLoading(data, metrics);
if (dataBufferingPolicy == DataBufferingPolicy::BufferData && this->dataBufferingPolicy() == DataBufferingPolicy::DoNotBufferData) {
if (RefPtr loader = m_loader)
loader->setDataBufferingPolicy(DataBufferingPolicy::DoNotBufferData);
clear();
}
}
void CachedRawResource::notifyClientsDataWasReceived(const SharedBuffer& buffer)
{
if (buffer.isEmpty())
return;
CachedResourceHandle protectedThis { this };
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next())
c->dataReceived(*this, buffer);
}
static void iterateRedirects(CachedResourceHandle<CachedRawResource>&& handle, CachedRawResourceClient& client, Vector<std::pair<ResourceRequest, ResourceResponse>>&& redirectsInReverseOrder, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
{
if (!handle->hasClient(client) || redirectsInReverseOrder.isEmpty())
return completionHandler({ });
auto redirectPair = redirectsInReverseOrder.takeLast();
client.redirectReceived(*handle, WTFMove(redirectPair.first), WTFMove(redirectPair.second), [handle = WTFMove(handle), client = WeakPtr { client }, redirectsInReverseOrder = WTFMove(redirectsInReverseOrder), completionHandler = WTFMove(completionHandler)] (ResourceRequest&&) mutable {
// Ignore the new request because we can't do anything with it.
// We're just replying a redirect chain that has already happened.
if (!client)
return completionHandler({ });
iterateRedirects(WTFMove(handle), *client, WTFMove(redirectsInReverseOrder), WTFMove(completionHandler));
});
}
void CachedRawResource::didAddClient(CachedResourceClient& c)
{
auto& client = downcast<CachedRawResourceClient>(c);
size_t redirectCount = m_redirectChain.size();
Vector<std::pair<ResourceRequest, ResourceResponse>> redirectsInReverseOrder(redirectCount, [&](size_t i) {
const auto& pair = m_redirectChain[redirectCount - i - 1];
return std::pair<ResourceRequest, ResourceResponse> { pair.m_request, pair.m_redirectResponse };
});
iterateRedirects(CachedResourceHandle { this }, client, WTFMove(redirectsInReverseOrder), [this, protectedThis = CachedResourceHandle { this }, client = WeakPtr { client }] (ResourceRequest&&) mutable {
if (!client || !hasClient(*client))
return;
auto responseProcessedHandler = [this, protectedThis = WTFMove(protectedThis), client] {
if (!client || !hasClient(*client))
return;
if (RefPtr data = m_data) {
data->forEachSegmentAsSharedBuffer([&](auto&& buffer) {
if (!client || hasClient(*client))
client->dataReceived(*this, buffer);
});
}
if (!client || !hasClient(*client))
return;
CachedResource::didAddClient(*client);
};
if (!response().isNull()) {
ResourceResponse response(CachedResource::response());
if (validationCompleting())
response.setSource(ResourceResponse::Source::MemoryCacheAfterValidation);
else {
ASSERT(!validationInProgress());
response.setSource(ResourceResponse::Source::MemoryCache);
}
client->responseReceived(*this, response, WTFMove(responseProcessedHandler));
} else
responseProcessedHandler();
});
}
void CachedRawResource::allClientsRemoved()
{
if (RefPtr loader = m_loader)
loader->cancelIfNotFinishing();
}
static void iterateClients(CachedResourceClientWalker<CachedRawResourceClient>&& walker, CachedResourceHandle<CachedRawResource>&& handle, ResourceRequest&& request, std::unique_ptr<ResourceResponse>&& response, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
{
auto client = walker.next();
if (!client)
return completionHandler(WTFMove(request));
const ResourceResponse& responseReference = *response;
client->redirectReceived(*handle, WTFMove(request), responseReference, [walker = WTFMove(walker), handle = WTFMove(handle), response = WTFMove(response), completionHandler = WTFMove(completionHandler)] (ResourceRequest&& request) mutable {
iterateClients(WTFMove(walker), WTFMove(handle), WTFMove(request), WTFMove(response), WTFMove(completionHandler));
});
}
void CachedRawResource::redirectReceived(ResourceRequest&& request, const ResourceResponse& response, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
{
RELEASE_LOG_ALWAYS("redirectReceived:");
if (response.isNull())
CachedResource::redirectReceived(WTFMove(request), response, WTFMove(completionHandler));
else {
m_redirectChain.append(RedirectPair(request, response));
iterateClients(CachedResourceClientWalker<CachedRawResourceClient>(*this), CachedResourceHandle { this }, WTFMove(request), makeUnique<ResourceResponse>(response), [this, protectedThis = CachedResourceHandle { this }, completionHandler = WTFMove(completionHandler), response] (ResourceRequest&& request) mutable {
CachedResource::redirectReceived(WTFMove(request), response, WTFMove(completionHandler));
});
}
}
void CachedRawResource::responseReceived(const ResourceResponse& newResponse)
{
CachedResourceHandle protectedThis { this };
if (!m_resourceLoaderIdentifier)
m_resourceLoaderIdentifier = m_loader->identifier();
CachedResource::responseReceived(newResponse);
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next())
c->responseReceived(*this, response(), nullptr);
}
bool CachedRawResource::shouldCacheResponse(const ResourceResponse& response)
{
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next()) {
if (!c->shouldCacheResponse(*this, response))
return false;
}
return true;
}
void CachedRawResource::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
{
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next())
c->dataSent(*this, bytesSent, totalBytesToBeSent);
}
void CachedRawResource::finishedTimingForWorkerLoad(ResourceTiming&& resourceTiming)
{
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next())
c->finishedTimingForWorkerLoad(*this, resourceTiming);
}
void CachedRawResource::switchClientsToRevalidatedResource()
{
ASSERT(m_loader);
// If we're in the middle of a successful revalidation, responseReceived() hasn't been called, so we haven't set m_identifier.
ASSERT(!m_resourceLoaderIdentifier);
downcast<CachedRawResource>(*resourceToRevalidate()).m_resourceLoaderIdentifier = m_loader->identifier();
CachedResource::switchClientsToRevalidatedResource();
}
void CachedRawResource::setDefersLoading(bool defers)
{
if (RefPtr loader = m_loader)
loader->setDefersLoading(defers);
}
void CachedRawResource::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
{
m_options.dataBufferingPolicy = dataBufferingPolicy;
}
static bool shouldIgnoreHeaderForCacheReuse(HTTPHeaderName name)
{
switch (name) {
// FIXME: This list of headers that don't affect cache policy almost certainly isn't complete.
case HTTPHeaderName::Accept:
case HTTPHeaderName::CacheControl:
case HTTPHeaderName::Pragma:
case HTTPHeaderName::Purpose:
case HTTPHeaderName::Referer:
case HTTPHeaderName::UserAgent:
return true;
default:
return false;
}
}
bool CachedRawResource::canReuse(const ResourceRequest& newRequest) const
{
if (dataBufferingPolicy() == DataBufferingPolicy::DoNotBufferData)
return false;
if (m_resourceRequest.httpMethod() != newRequest.httpMethod())
return false;
if (m_resourceRequest.httpBody() != newRequest.httpBody())
return false;
if (m_resourceRequest.allowCookies() != newRequest.allowCookies())
return false;
if (newRequest.isConditional())
return false;
// Ensure most headers match the existing headers before continuing.
// Note that the list of ignored headers includes some headers explicitly related to caching.
// A more detailed check of caching policy will be performed later, this is simply a list of
// headers that we might permit to be different and still reuse the existing CachedResource.
const HTTPHeaderMap& newHeaders = newRequest.httpHeaderFields();
const HTTPHeaderMap& oldHeaders = m_resourceRequest.httpHeaderFields();
for (const auto& header : newHeaders) {
if (header.keyAsHTTPHeaderName) {
if (!shouldIgnoreHeaderForCacheReuse(header.keyAsHTTPHeaderName.value())
&& header.value != oldHeaders.get(header.keyAsHTTPHeaderName.value()))
return false;
} else if (header.value != oldHeaders.get(header.key))
return false;
}
// For this second loop, we don't actually need to compare values, checking that the
// key is contained in newHeaders is sufficient due to the previous loop.
for (const auto& header : oldHeaders) {
if (header.keyAsHTTPHeaderName) {
if (!shouldIgnoreHeaderForCacheReuse(header.keyAsHTTPHeaderName.value())
&& !newHeaders.contains(header.keyAsHTTPHeaderName.value()))
return false;
} else if (!newHeaders.contains(header.key))
return false;
}
return true;
}
void CachedRawResource::clear()
{
m_data = nullptr;
setEncodedSize(0);
if (RefPtr loader = m_loader)
loader->clearResourceData();
}
#if USE(QUICK_LOOK)
void CachedRawResource::previewResponseReceived(const ResourceResponse& newResponse)
{
CachedResourceHandle protectedThis { this };
CachedResource::previewResponseReceived(newResponse);
CachedResourceClientWalker<CachedRawResourceClient> walker(*this);
while (CachedRawResourceClient* c = walker.next())
c->previewResponseReceived(*this, response());
}
#endif
} // namespace WebCore
|