Skip to content
Permalink
Browse files

Use all parameters for hash-based cache marker

  • Loading branch information
apeschar committed Mar 20, 2020
1 parent e217d3c commit 8f25273456b4e128c98eb070df6073e69246f74b
@@ -9,4 +9,8 @@ public static function encode($data) {
public static function decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}

public static function shortHash($data) {
return self::encode(substr(sha1($data, true), 0, 8));
}
}
@@ -2,6 +2,7 @@

namespace Kibo\Phast\Filters\HTML\CSSInlining;

use Kibo\Phast\Common\Base64url;
use Kibo\Phast\Filters\HTML\BaseHTMLStreamFilter;
use Kibo\Phast\Logging\LoggingTrait;
use Kibo\Phast\Parsing\HTML\HTMLStreamElements\Tag;
@@ -250,11 +251,10 @@ private function inlineURL(URL $url, $media, $ieCompatible = true, $currentLevel

$content = $this->cssFilter->apply(Resource::makeWithContent($url, $content), [])->getContent();

$this->cacheMarkers[$url->toString()] =
substr(str_replace(['+', '/'], '', base64_encode(md5(implode("\0", [
$this->retriever->getCacheSalt($url),
$content,
]), true))), 0, 16);
$this->cacheMarkers[$url->toString()] = Base64url::shortHash(implode("\0", [
$this->retriever->getCacheSalt($url),
$content,
]));

$optimized = $this->optimizer->optimizeCSS($content);
if ($optimized === null) {
@@ -3,6 +3,7 @@

use Kibo\Phast\Cache\Cache;
use Kibo\Phast\Common\Base64url;
use Kibo\Phast\Common\JSON;

class TokenRefMaker {
private $cache;
@@ -12,17 +13,18 @@ public function __construct(Cache $cache) {
}

public function getRef($token, array $params) {
$cachedParams = $this->cache->get($token);
$ref = Base64url::shortHash(JSON::encode($params));
$cachedParams = $this->cache->get($ref);
if (!$cachedParams) {
$this->cache->set($token, $params);
$cachedParams = $this->cache->get($token);
$this->cache->set($ref, $params);
$cachedParams = $this->cache->get($ref);
}
if ($cachedParams === $params) {
return Base64url::encode(hex2bin($token));
return $ref;
}
}

public function getParams($ref) {
return $this->cache->get(bin2hex(Base64url::decode($ref)));
return $this->cache->get($ref);
}
}
@@ -563,7 +563,7 @@ public function testUseHashCacheMarker() {
$html = '<html><head><link rel=stylesheet href=/external.css?test></head><body></body></html>';
$this->files['/external.css'] = 'body{background:url(/image.jpg);}';
$this->applyFilter($html);
$this->assertRegExp('/^[a-z0-9]{16}$/i', (string) $this->getCacheMarker());
$this->assertRegExp('/^[a-z0-9]+$/i', (string) $this->getCacheMarker());
}

private function getCacheMarker() {

0 comments on commit 8f25273

Please sign in to comment.
You can’t perform that action at this time.