From b3b4770cfe667826770af1c9241ec7a05a924a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 3 Dec 2019 14:39:05 -0500 Subject: [PATCH 1/5] prevent race condition by writing to temp file --- inc/classes/Buffer/class-cache.php | 39 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/inc/classes/Buffer/class-cache.php b/inc/classes/Buffer/class-cache.php index 94f6de10bd..919e854478 100644 --- a/inc/classes/Buffer/class-cache.php +++ b/inc/classes/Buffer/class-cache.php @@ -305,13 +305,7 @@ public function maybe_process_buffer( $buffer ) { } } - // Save the cache file. - rocket_put_content( $cache_filepath, $buffer . $footprint ); - - if ( function_exists( 'gzencode' ) ) { - rocket_put_content( $cache_filepath . '_gzip', gzencode( $buffer . $footprint, apply_filters( 'rocket_gzencode_level_compression', 3 ) ) ); - } - + $this->write_cache_file( $cache_filepath, $buffer . $footprint ); $this->maybe_create_nginx_mobile_file( $cache_dir_path ); // Send headers with the last modified time of the cache file. @@ -334,6 +328,37 @@ public function maybe_process_buffer( $buffer ) { return $buffer . $footprint; } + /** + * Writes the cache file(s) + * + * @since 3.5 + * @author Remy Perona + * + * @param string $cache_filepath Absolute path to the cache file. + * @param string $content Content to write in the cache file. + * @return void + */ + private function write_cache_file( $cache_filepath, $content ) { + $gzip_filepath = $cache_filepath . '_gzip'; + $temp_filepath = $cache_filepath . '_temp'; + $temp_gzip_filepath = $gzip_filepath . '_temp'; + + if ( rocket_direct_filesystem()->exists( $temp_filepath ) ) { + return; + } + + // Save the cache file. + rocket_put_content( $temp_filepath, $content ); + rocket_direct_filesystem()->move( $temp_filepath, $cache_filepath ); + rocket_direct_filesystem()->delete( $temp_filepath ); + + if ( function_exists( 'gzencode' ) ) { + rocket_put_content( $temp_gzip_filepath, gzencode( $content, apply_filters( 'rocket_gzencode_level_compression', 3 ) ) ); + rocket_direct_filesystem()->move( $temp_gzip_filepath, $gzip_filepath ); + rocket_direct_filesystem()->delete( $temp_gzip_filepath ); + } + } + /** * Get the path to the cache file. * From 90d5fae4b9ba3279e907808b006f7975fd614eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 3 Dec 2019 14:39:37 -0500 Subject: [PATCH 2/5] fix issue with firefox detection for webp --- inc/classes/subscriber/Media/class-webp-subscriber.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/inc/classes/subscriber/Media/class-webp-subscriber.php b/inc/classes/subscriber/Media/class-webp-subscriber.php index 8e655cf4a0..43171a7543 100644 --- a/inc/classes/subscriber/Media/class-webp-subscriber.php +++ b/inc/classes/subscriber/Media/class-webp-subscriber.php @@ -154,7 +154,15 @@ public function convert_to_webp( $html ) { } if ( ! $http_accept || false === strpos( $http_accept, 'webp' ) ) { - return $html; + $user_agent = isset( $this->server['HTTP_USER_AGENT'] ) ? $this->server['HTTP_USER_AGENT'] : ''; + + if ( $user_agent && preg_match( '#Firefox/(?[0-9]{2})#i', $this->server['HTTP_USER_AGENT'], $matches ) ) { + if ( 66 >= (int) $matches['version'] ) { + return $html; + } + } else { + return $html; + } } $extensions = $this->get_extensions(); From 3ffde902699993daa97e7aaa19d86dc27cdb7c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 3 Dec 2019 14:43:38 -0500 Subject: [PATCH 3/5] remove unnecessary delete calls --- inc/classes/Buffer/class-cache.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/inc/classes/Buffer/class-cache.php b/inc/classes/Buffer/class-cache.php index 919e854478..028ca2a809 100644 --- a/inc/classes/Buffer/class-cache.php +++ b/inc/classes/Buffer/class-cache.php @@ -350,12 +350,10 @@ private function write_cache_file( $cache_filepath, $content ) { // Save the cache file. rocket_put_content( $temp_filepath, $content ); rocket_direct_filesystem()->move( $temp_filepath, $cache_filepath ); - rocket_direct_filesystem()->delete( $temp_filepath ); if ( function_exists( 'gzencode' ) ) { rocket_put_content( $temp_gzip_filepath, gzencode( $content, apply_filters( 'rocket_gzencode_level_compression', 3 ) ) ); rocket_direct_filesystem()->move( $temp_gzip_filepath, $gzip_filepath ); - rocket_direct_filesystem()->delete( $temp_gzip_filepath ); } } From 32a262311691ba5cc5dd456d58d39ec7e52a3842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 4 Dec 2019 09:07:48 -0500 Subject: [PATCH 4/5] Add DocBlock for compression level filter --- inc/classes/Buffer/class-cache.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/inc/classes/Buffer/class-cache.php b/inc/classes/Buffer/class-cache.php index 028ca2a809..2af195449b 100644 --- a/inc/classes/Buffer/class-cache.php +++ b/inc/classes/Buffer/class-cache.php @@ -352,7 +352,14 @@ private function write_cache_file( $cache_filepath, $content ) { rocket_direct_filesystem()->move( $temp_filepath, $cache_filepath ); if ( function_exists( 'gzencode' ) ) { - rocket_put_content( $temp_gzip_filepath, gzencode( $content, apply_filters( 'rocket_gzencode_level_compression', 3 ) ) ); + /** + * Filters the Gzip compression level to use for the cache file + * + * @param int $compression_level Compression level between 0 and 9. + */ + $compression_level = apply_filters( 'rocket_gzencode_level_compression', 3 ); + + rocket_put_content( $temp_gzip_filepath, gzencode( $content, $compression_level ) ); rocket_direct_filesystem()->move( $temp_gzip_filepath, $gzip_filepath ); } } From 5b15e023dc321f378cb8d6448e4ac7e82113bb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 4 Dec 2019 09:08:31 -0500 Subject: [PATCH 5/5] Update RegEx to match for future versions of Firefox MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Grégory Viguier --- inc/classes/subscriber/Media/class-webp-subscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/classes/subscriber/Media/class-webp-subscriber.php b/inc/classes/subscriber/Media/class-webp-subscriber.php index 43171a7543..16090b4887 100644 --- a/inc/classes/subscriber/Media/class-webp-subscriber.php +++ b/inc/classes/subscriber/Media/class-webp-subscriber.php @@ -156,7 +156,7 @@ public function convert_to_webp( $html ) { if ( ! $http_accept || false === strpos( $http_accept, 'webp' ) ) { $user_agent = isset( $this->server['HTTP_USER_AGENT'] ) ? $this->server['HTTP_USER_AGENT'] : ''; - if ( $user_agent && preg_match( '#Firefox/(?[0-9]{2})#i', $this->server['HTTP_USER_AGENT'], $matches ) ) { + if ( $user_agent && preg_match( '#Firefox/(?[0-9]{2,})#i', $this->server['HTTP_USER_AGENT'], $matches ) ) { if ( 66 >= (int) $matches['version'] ) { return $html; }