Title: [173173] trunk
Revision
173173
Author
[email protected]
Date
2014-09-02 09:00:14 -0700 (Tue, 02 Sep 2014)

Log Message

CachedResourceLoader should check redirections to reuse or not cached resources
https://bugs.webkit.org/show_bug.cgi?id=131757

Patch by Youenn Fablet <[email protected]> on 2014-09-02
Reviewed by Antti Koivisto.

Source/WebCore:

Added cache-control redirection check to properly determine revalidation policy.
Tightened redirection cache-control header check by testing for no-cache and must-revalidate directives.
Added redirection freshness check.

Test: http/tests/cache/cache-redirections.html

* loader/cache/CachedRawResource.cpp:
(WebCore::CachedRawResource::canReuse): Removed redirection check (now handled by CachedResource::redirectChainAllowsReuse).
* loader/cache/CachedResource.cpp:
(WebCore::currentAge): Moved from WebCore::CachedResource::currentAge method to static function. Added response and responseTimestamp as parameters.
(WebCore::CachedResource::CachedResource): Initialized redirection chain status (no redirection and expiracy date set to never).
(WebCore::CachedResource::isExpired): Updated according new currentAge/freshnessLifetime method parameters.
(WebCore::CachedResource::freshnessLifetime): Added response as parameter.
(WebCore::CachedResource::willSendRequest): Computes whether a redirection can be cached, and if cached for how long it will remain fresh.
(WebCore::CachedResource::redirectChainAllowsReuse): Return false if any of the redirection in the redirection chain cannot be cached or expired.
* loader/cache/CachedResource.h: Added cache chain member that stores whether the redirection chain can be cached and if so when it will be expired.
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::determineRevalidationPolicy): Added check of the redirection chain.

LayoutTests:

Added test checks that fresh redirections allow reuse of cached resoure and expired or not cacheable redirections trigger reloading of resources.

* http/tests/cache/cache-redirections-expected.txt: Added.
* http/tests/cache/cache-redirections.html: Added.
* http/tests/cache/resources/cache-control-redirect.php: Added.
* http/tests/cache/resources/cacheable-random-text.php: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (173172 => 173173)


--- trunk/LayoutTests/ChangeLog	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/LayoutTests/ChangeLog	2014-09-02 16:00:14 UTC (rev 173173)
@@ -1,3 +1,17 @@
+2014-09-02  Youenn Fablet  <[email protected]>
+
+        CachedResourceLoader should check redirections to reuse or not cached resources
+        https://bugs.webkit.org/show_bug.cgi?id=131757
+
+        Reviewed by Antti Koivisto.
+
+        Added test checks that fresh redirections allow reuse of cached resoure and expired or not cacheable redirections trigger reloading of resources.
+
+        * http/tests/cache/cache-redirections-expected.txt: Added.
+        * http/tests/cache/cache-redirections.html: Added.
+        * http/tests/cache/resources/cache-control-redirect.php: Added.
+        * http/tests/cache/resources/cacheable-random-text.php: Added.
+
 2014-09-02  Manuel Rego Casasnovas  <[email protected]>
 
         [CSS Grid Layout] Test coverage for first-line pseudo-element

Added: trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt (0 => 173173)


--- trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/cache/cache-redirections-expected.txt	2014-09-02 16:00:14 UTC (rev 173173)
@@ -0,0 +1,4 @@
+ 
+
+PASS Testing cache reloading strategy according redirections with different cache state 
+

Added: trunk/LayoutTests/http/tests/cache/cache-redirections.html (0 => 173173)


--- trunk/LayoutTests/http/tests/cache/cache-redirections.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/cache/cache-redirections.html	2014-09-02 16:00:14 UTC (rev 173173)
@@ -0,0 +1,73 @@
+<html>
+<head>
+   <title>Testing cache reloading strategy according redirections with different cache state</title>
+    <script src=""
+    <script src=""
+</head>
+<body>
+<iframe id="iframe1"></iframe>
+<iframe id="iframe2"></iframe>
+<div id="log"></div>
+<script>
+
+var test = async_test();
+test.step(function() {
+
+var counter = 0;
+var resource = "No";
+var baseURL = "resources/cache-control-redirect?code=302&url=""
+var testCases = [
+{name : "Should reuse resource (cacheable redirection)", expected : true, url : baseURL + "&cache_control=public, max-age=1000", reloadDelay : 0},
+{name : "Should reload resource (no-store redirection)", expected : false, url: baseURL + "&cache_control=no-store&max_age=1000", reloadDelay : 0},
+{name : "Should reload resource (no-cache redirection)", expected : false, url: baseURL + "&cache_control=no-cache&max_age=1000", reloadDelay : 0},
+{name : "Should reload resource (must-revalidate redirection)", expected : false, url: baseURL + "&cache_control=must-revalidate&max_age=1000", reloadDelay : 0},
+{name : "Should reload resource (expired redirection)", expected : false,  url: baseURL + "&cache_control=public, max-age=1", reloadDelay : 1000},
+];
+
+function loadResource()
+{
+    setFrame1Loader();
+	document.getElementById('iframe1').src = ""
+}
+
+function reloadResource()
+{
+    setFrame2Loader();
+	document.getElementById('iframe2').src  = ""
+}
+
+function setFrame1Loader() 
+{
+    document.getElementById('iframe2')._onload_  = null;
+    document.getElementById('iframe1')._onload_  = test.step_func(function()
+    {
+        resource = document.getElementById('iframe1').contentDocument.body.innerHTML;
+        setTimeout(test.step_func(reloadResource), testCases[counter].reloadDelay);
+    });
+}
+
+function setFrame2Loader() 
+{
+    document.getElementById('iframe1')._onload_  = null;
+    document.getElementById('iframe2')._onload_  = test.step_func(function ()
+    {
+        var testResult = resource == document.getElementById('iframe2').contentDocument.body.innerHTML;
+        assert_true(testResult == testCases[counter].expected, testCases[counter].name);
+
+        document.getElementById('iframe1').contentDocument.body.innerHTML = "frame 1";
+        document.getElementById('iframe2').contentDocument.body.innerHTML = "frame 2";
+        if (++counter < testCases.length)
+            loadResource();
+        else
+            test.done();
+    });
+}
+
+document._onload_ = setTimeout(loadResource, 1);
+
+});
+
+
+</script>
+</body>
+</html>

Added: trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php (0 => 173173)


--- trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php	                        (rev 0)
+++ trunk/LayoutTests/http/tests/cache/resources/cache-control-redirect.php	2014-09-02 16:00:14 UTC (rev 173173)
@@ -0,0 +1,27 @@
+<?php
+    $code = $_GET['code'];
+    if (!isset($code))
+        $code = 302;
+    header('HTTP/1.1 ' . $code);
+
+    $url = ""
+    $random_id = $_GET['random_id'];
+    if (isset($random_id)) {
+        $id = '';
+        $charset = 'ABCDEFGHIKLMNOPQRSTUVWXYZ0123456789';
+        for ($i = 0; $i < 16; $i++)
+            $id .= $charset[rand(0, strlen($charset) - 1)];
+        header('Location: ' . $url . '?id=' . $id);
+    }
+    else
+        header('Location: ' . $url);
+
+    $max_age = $_GET['max_age'];
+    if (!isset($max_age)) {
+        $expires = gmdate(DATE_RFC1123, time() + $max_age);
+        header('Expires: ' . $expires);
+    }
+
+    $cache_control = $_GET['cache_control'];
+    header('Cache-Control: ' . $cache_control);
+?>

Added: trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php (0 => 173173)


--- trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php	                        (rev 0)
+++ trunk/LayoutTests/http/tests/cache/resources/cacheable-random-text.php	2014-09-02 16:00:14 UTC (rev 173173)
@@ -0,0 +1,14 @@
+<?php
+    $max_age = 12 * 31 * 24 * 60 * 60; //one year
+    header('Cache-Control: public, max-age=' . $max_age);
+    header('Content-Type: text/html');
+
+    $id = $GET['id'];
+    if (!isset($GET['random_id'])) {
+        $id = '';
+        $charset = 'ABCDEFGHIKLMNOPQRSTUVWXYZ0123456789';
+        for ($i = 0; $i < 16; $i++)
+            $id .= $charset[rand(0, strlen($charset) - 1)];
+    }
+    echo '<html><body>Some random text' . $id .  '</hml></body>';
+?>

Modified: trunk/Source/WebCore/ChangeLog (173172 => 173173)


--- trunk/Source/WebCore/ChangeLog	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/ChangeLog	2014-09-02 16:00:14 UTC (rev 173173)
@@ -1,3 +1,29 @@
+2014-09-02  Youenn Fablet  <[email protected]>
+
+        CachedResourceLoader should check redirections to reuse or not cached resources
+        https://bugs.webkit.org/show_bug.cgi?id=131757
+
+        Reviewed by Antti Koivisto.
+
+        Added cache-control redirection check to properly determine revalidation policy.
+        Tightened redirection cache-control header check by testing for no-cache and must-revalidate directives.
+        Added redirection freshness check.
+
+        Test: http/tests/cache/cache-redirections.html
+
+        * loader/cache/CachedRawResource.cpp:
+        (WebCore::CachedRawResource::canReuse): Removed redirection check (now handled by CachedResource::redirectChainAllowsReuse).
+        * loader/cache/CachedResource.cpp:
+        (WebCore::currentAge): Moved from WebCore::CachedResource::currentAge method to static function. Added response and responseTimestamp as parameters.
+        (WebCore::CachedResource::CachedResource): Initialized redirection chain status (no redirection and expiracy date set to never).
+        (WebCore::CachedResource::isExpired): Updated according new currentAge/freshnessLifetime method parameters.
+        (WebCore::CachedResource::freshnessLifetime): Added response as parameter.
+        (WebCore::CachedResource::willSendRequest): Computes whether a redirection can be cached, and if cached for how long it will remain fresh.
+        (WebCore::CachedResource::redirectChainAllowsReuse): Return false if any of the redirection in the redirection chain cannot be cached or expired. 
+        * loader/cache/CachedResource.h: Added cache chain member that stores whether the redirection chain can be cached and if so when it will be expired.
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::determineRevalidationPolicy): Added check of the redirection chain.
+
 2014-09-02  Daewoong Jang  <[email protected]>
 
         Prevent decoded images from being destroyed when they're in use.

Modified: trunk/Source/WebCore/loader/cache/CachedRawResource.cpp (173172 => 173173)


--- trunk/Source/WebCore/loader/cache/CachedRawResource.cpp	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedRawResource.cpp	2014-09-02 16:00:14 UTC (rev 173173)
@@ -255,11 +255,6 @@
             return false;
     }
 
-    for (size_t i = 0; i < m_redirectChain.size(); i++) {
-        if (m_redirectChain[i].m_redirectResponse.cacheControlContainsNoStore())
-            return false;
-    }
-
     return true;
 }
 

Modified: trunk/Source/WebCore/loader/cache/CachedResource.cpp (173172 => 173173)


--- trunk/Source/WebCore/loader/cache/CachedResource.cpp	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResource.cpp	2014-09-02 16:00:14 UTC (rev 173173)
@@ -146,6 +146,18 @@
     return memoryCache()->deadDecodedDataDeletionInterval();
 }
 
+static double currentAge(const ResourceResponse& response, double responseTimestamp)
+{
+    // RFC2616 13.2.3
+    // No compensation for latency as that is not terribly important in practice
+    double dateValue = response.date();
+    double apparentAge = std::isfinite(dateValue) ? std::max(0., responseTimestamp - dateValue) : 0;
+    double ageValue = response.age();
+    double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
+    double residentTime = currentTime() - responseTimestamp;
+    return correctedReceivedAge + residentTime;
+}
+
 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
 
 CachedResource::CachedResource(const ResourceRequest& request, Type type, SessionID sessionID)
@@ -180,6 +192,8 @@
     , m_owningCachedResourceLoader(0)
     , m_resourceToRevalidate(0)
     , m_proxyResource(0)
+    , m_redirectChainCacheStatus(NoRedirection)
+    , m_redirectChainEndOfValidity(std::numeric_limits<double>::max())
 {
     ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
     ASSERT(sessionID.isValid());
@@ -386,49 +400,57 @@
     if (m_response.isNull())
         return false;
 
-    return currentAge() > freshnessLifetime();
+    return currentAge(m_response, m_responseTimestamp) > freshnessLifetime(m_response);
 }
 
-double CachedResource::currentAge() const
+double CachedResource::freshnessLifetime(const ResourceResponse& response) const
 {
-    // RFC2616 13.2.3
-    // No compensation for latency as that is not terribly important in practice
-    double dateValue = m_response.date();
-    double apparentAge = std::isfinite(dateValue) ? std::max(0., m_responseTimestamp - dateValue) : 0;
-    double ageValue = m_response.age();
-    double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
-    double residentTime = currentTime() - m_responseTimestamp;
-    return correctedReceivedAge + residentTime;
-}
-
-double CachedResource::freshnessLifetime() const
-{
-    if (!m_response.url().protocolIsInHTTPFamily()) {
+    if (!response.url().protocolIsInHTTPFamily()) {
         // Don't cache non-HTTP main resources since we can't check for freshness.
         // FIXME: We should not cache subresources either, but when we tried this
         // it caused performance and flakiness issues in our test infrastructure.
-        if (m_type == MainResource && !SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(m_response.url().protocol()))
+        if (m_type == MainResource && !SchemeRegistry::shouldCacheResponsesFromURLSchemeIndefinitely(response.url().protocol()))
             return 0;
 
         return std::numeric_limits<double>::max();
     }
 
     // RFC2616 13.2.4
-    double maxAgeValue = m_response.cacheControlMaxAge();
+    double maxAgeValue = response.cacheControlMaxAge();
     if (std::isfinite(maxAgeValue))
         return maxAgeValue;
-    double expiresValue = m_response.expires();
-    double dateValue = m_response.date();
+    double expiresValue = response.expires();
+    double dateValue = response.date();
     double creationTime = std::isfinite(dateValue) ? dateValue : m_responseTimestamp;
     if (std::isfinite(expiresValue))
         return expiresValue - creationTime;
-    double lastModifiedValue = m_response.lastModified();
+    double lastModifiedValue = response.lastModified();
     if (std::isfinite(lastModifiedValue))
         return (creationTime - lastModifiedValue) * 0.1;
     // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
     return 0;
 }
 
+void CachedResource::willSendRequest(ResourceRequest&, const ResourceResponse& response)
+{
+    m_requestedFromNetworkingLayer = true;
+    if (response.isNull())
+        return;
+    if (m_redirectChainCacheStatus == NotCachedRedirection)
+        return;
+    if (response.cacheControlContainsNoStore()
+        || response.cacheControlContainsNoCache()
+        || response.cacheControlContainsMustRevalidate())
+        m_redirectChainCacheStatus = NotCachedRedirection;
+    else {
+        m_redirectChainCacheStatus = CachedRedirection;
+        double responseTimestamp = currentTime();
+        // Store the nearest end of cache validity date
+        m_redirectChainEndOfValidity = std::min(m_redirectChainEndOfValidity, 
+            responseTimestamp + freshnessLifetime(response) - currentAge(response, responseTimestamp));
+    }
+}
+
 void CachedResource::responseReceived(const ResourceResponse& response)
 {
     setResponse(response);
@@ -783,6 +805,19 @@
     return false;
 }
 
+bool CachedResource::redirectChainAllowsReuse() const
+{
+    switch (m_redirectChainCacheStatus) {
+    case NoRedirection:
+        return true;
+    case NotCachedRedirection:
+        return false;
+    case CachedRedirection:
+        return currentTime() <= m_redirectChainEndOfValidity;
+    }
+    return true;
+}
+
 unsigned CachedResource::overheadSize() const
 {
     static const int kAverageClientsHashMapSize = 384;

Modified: trunk/Source/WebCore/loader/cache/CachedResource.h (173172 => 173173)


--- trunk/Source/WebCore/loader/cache/CachedResource.h	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResource.h	2014-09-02 16:00:14 UTC (rev 173173)
@@ -88,6 +88,12 @@
         DecodeError
     };
 
+    enum RedirectChainCacheStatus {
+        NoRedirection,
+        NotCachedRedirection,
+        CachedRedirection
+    };
+
     CachedResource(const ResourceRequest&, Type, SessionID);
     virtual ~CachedResource();
 
@@ -188,7 +194,7 @@
 
     ResourceBuffer* resourceBuffer() const { return m_data.get(); }
 
-    virtual void willSendRequest(ResourceRequest&, const ResourceResponse&) { m_requestedFromNetworkingLayer = true; }
+    virtual void willSendRequest(ResourceRequest&, const ResourceResponse&);
     virtual void responseReceived(const ResourceResponse&);
     void setResponse(const ResourceResponse& response) { m_response = response; }
     const ResourceResponse& response() const { return m_response; }
@@ -225,6 +231,7 @@
     bool canUseCacheValidator() const;
 
     virtual bool mustRevalidateDueToCacheHeaders(CachePolicy) const;
+    bool redirectChainAllowsReuse() const;
 
     bool isCacheValidator() const { return m_resourceToRevalidate; }
     CachedResource* resourceToRevalidate() const { return m_resourceToRevalidate; }
@@ -299,8 +306,7 @@
 
     virtual bool mayTryReplaceEncodedData() const { return false; }
 
-    double currentAge() const;
-    double freshnessLifetime() const;
+    double freshnessLifetime(const ResourceResponse&) const;
 
     void addAdditionalRequestHeaders(CachedResourceLoader*);
     void failBeforeStarting();
@@ -355,6 +361,9 @@
 
     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
     HashSet<CachedResourceHandleBase*> m_handlesToRevalidate;
+
+    RedirectChainCacheStatus m_redirectChainCacheStatus;
+    double m_redirectChainEndOfValidity;
 };
 
 #define CACHED_RESOURCE_TYPE_CASTS(ToClassName, FromClassName, CachedResourceType) \

Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp (173172 => 173173)


--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp	2014-09-02 15:54:26 UTC (rev 173172)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp	2014-09-02 16:00:14 UTC (rev 173173)
@@ -594,6 +594,12 @@
         return Reload;
     }
 
+    // Validate the redirect chain
+    if (!existingResource->redirectChainAllowsReuse()) {
+        LOG(ResourceLoading, "CachedResourceLoader::determineRevalidationPolicy reloading due to not cached or expired redirections.");
+        return Reload;
+    }
+
     // If credentials were sent with the previous request and won't be
     // with this one, or vice versa, re-fetch the resource.
     //
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to