Skip to content

Commit

Permalink
Merge pull request #141 from tbela99/compute_integrity
Browse files Browse the repository at this point in the history
#137 compute integrity even when HTTP caching is off
  • Loading branch information
tbela99 authored Aug 16, 2021
2 parents 39a7e38 + 34a1d4c commit 4a9d206
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public function onAfterRender()
GZipHelper::register(new Gzip\Helpers\EncryptedLinksHelper());
}

if (!empty($options['cachefiles']) || !empty($options['link_rel'])) {
if (!empty($options['cachefiles']) || !empty($options['link_rel']) || (!empty($options['checksum']) && $options['checksum'] != 'none') ){

GZipHelper::register(new Gzip\Helpers\UrlHelper());
}
Expand Down
29 changes: 20 additions & 9 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
use Exception;
use JProfiler as JProfiler;
use JUri;
use Patchwork\JSqueeze as JSqueeze;
use Peast\Formatter\Compact;
use Peast\Formatter\PrettyPrint;
use Peast\Renderer;
//use Patchwork\JSqueeze as JSqueeze;
use \Peast\Peast;
use \Peast\Formatter\Compact;
use \Peast\Formatter\PrettyPrint;
use \Peast\Renderer;
use function base64_encode;
use function curl_close;
use function curl_exec;
Expand Down Expand Up @@ -395,7 +396,7 @@ public static function getName($name)
public static function js($file, $remote_service = true)
{

static $jsShrink;
static $parser;

$content = '';

Expand All @@ -421,12 +422,22 @@ public static function js($file, $remote_service = true)
return false;
}

if (is_null($jsShrink)) {
if (is_null($parser)) {

$jsShrink = new Renderer(static::$options['minifyjs'] ? new Compact() : new PrettyPrint());
$parser = new Renderer(static::$options['minifyjs'] ? new Compact() : new PrettyPrint());
}

return trim($jsShrink->render(Peast\Peast::latest($content)->parse(), false, false), ';');
try {

return trim($parser->render(Peast::latest($content)->parse(), false, false), ';');
}

catch (Exception $e) {

// error_log($e->getTraceAsString());
}

return $content;
}

/**
Expand Down Expand Up @@ -495,7 +506,7 @@ public static function url($file)

if (empty(static::$options['cachefiles'])) {

if ($file[0] == '/' || preg_match('#^(https?:)?//#')) {
if ($file[0] == '/' || preg_match('#^(https?:)?//#', $file)) {

return $file;
}
Expand Down
1 change: 0 additions & 1 deletion helpers/SecureHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function postProcessHTML($html, array $options = [])
}

$links = [];

$tags = [];

$sections = array_filter([
Expand Down
39 changes: 24 additions & 15 deletions helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function postProcessHTML ($html, array $options = []) {

$checksum = !empty($options['checksum']) ? $options['checksum'] : false;

if ($checksum == 'none') {

$checksum = false;
}

$domains = [];

$html = preg_replace_callback('#<([a-zA-Z0-9:-]+)\s([^>]+)>(?!["\'])#s', function ($matches) use($checksum, $hashFile, $accepted, &$domains, &$pushed, $types, $hashmap, $base, $options) {
Expand Down Expand Up @@ -97,19 +102,19 @@ public function postProcessHTML ($html, array $options = []) {
}
}

if (!empty($options['cachefiles']) && isset($attributes[$attr]) && isset($options['parse_url_attr'][$attr])) {
if ((!empty($options['cachefiles']) || $checksum) && isset($attributes[$attr]) && isset($options['parse_url_attr'][$attr])) {

$file = GZipHelper::getName($attributes[$attr]);

if (GZipHelper::isFile($file)) {

$name = preg_replace('~[#?].*$~', '', $file);

$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));

if (isset(GZipHelper::$static_types[$ext]) && empty($attributes['crossorigin'])) {

$attributes['crossorigin'] = 'anonymous';
// crossorigin="anonymous"
$attributes['crossorigin'] = '';
}

$push_data = empty($types) ? false : GZipHelper::canPush($name, $ext);
Expand All @@ -136,30 +141,34 @@ public function postProcessHTML ($html, array $options = []) {
}
}

if ($checksum) {

$checkSumData = GZipHelper::getChecksum($name, $hashFile, $checksum, $tag == 'script' || ($tag == 'link' && $ext == 'css'));

if ($tag == 'script' || ($tag == 'link' && $ext == 'css')) {

$attributes['integrity'] = $checkSumData['integrity'];
// crossorigin="anonymous"
$attributes['crossorigin'] = '';
}
}

if (isset($accepted[$ext])) {

unset($pushed[$base . $file]);

$checkSumData = GZipHelper::getChecksum($name, $hashFile, $checksum, $tag == 'script' || ($tag == 'link' && $ext == 'css'));
if (!empty($options['cachefiles'])) {

$file = GZipHelper::getHost(JURI::root(true).'/'.GZipHelper::$route.GZipHelper::$pwa_network_strategy . $checkSumData['hash'] . '/' . $file);
$file = GZipHelper::getHost(JURI::root(true).'/'.GZipHelper::$route.GZipHelper::$pwa_network_strategy . (isset($checkSumData['hash']) ? $checkSumData['hash'] : $hashFile($file)) . '/' . $file);
}

if (!empty($push_data)) {

$push_data['href'] = $file;
$pushed[$file] = $push_data;
}

$attributes[$attr] = $file ;

if(!empty($checksum) && $checksum != 'none') {

if ($tag == 'script' || ($tag == 'link' && $ext == 'css')) {

$attributes['integrity'] = $checkSumData['integrity'];
$attributes['crossorigin'] = 'anonymous';
}
}
$attributes[$attr] = $file;
}
}

Expand Down

0 comments on commit 4a9d206

Please sign in to comment.