From 25443d3a7f2554c813c6110748fbc8625529bc47 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 13 Aug 2023 10:29:04 +0000 Subject: [PATCH] Coding Standards: Bring more consistency to `Last-Modified` and `ETag` checks. This updates two fragments for sending a `304 Not Modified` header to better align with each other by using consistent variable names and formatting. Follow-up to [1036], [1037], [1043], [2534], [2584], [2627], [12603], [12936], [56362]. See #58831. git-svn-id: https://develop.svn.wordpress.org/trunk@56395 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp.php | 19 ++++++++++++------- src/wp-includes/ms-files.php | 32 +++++++++++++++++++------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 8f66d98929c64..ce88c102cfade 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -505,8 +505,8 @@ public function send_headers() { } $wp_last_modified .= ' GMT'; + $wp_etag = '"' . md5( $wp_last_modified ) . '"'; - $wp_etag = '"' . md5( $wp_last_modified ) . '"'; $headers['Last-Modified'] = $wp_last_modified; $headers['ETag'] = $wp_etag; @@ -514,19 +514,24 @@ public function send_headers() { if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) { $client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ); } else { - $client_etag = false; + $client_etag = ''; + } + + if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { + $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); + } else { + $client_last_modified = ''; } - $client_last_modified = empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? '' : trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); // If string is empty, return 0. If not, attempt to parse into a timestamp. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0; - // Make a timestamp for our most recent modification.. + // Make a timestamp for our most recent modification. $wp_modified_timestamp = strtotime( $wp_last_modified ); - if ( ( $client_last_modified && $client_etag ) ? - ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) : - ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) ) + if ( ( $client_last_modified && $client_etag ) + ? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) + : ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) ) ) { $status = 304; $exit_required = true; diff --git a/src/wp-includes/ms-files.php b/src/wp-includes/ms-files.php index 6026a71db6f38..c909a32407d64 100644 --- a/src/wp-includes/ms-files.php +++ b/src/wp-includes/ms-files.php @@ -54,30 +54,36 @@ exit; } -$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); -$etag = '"' . md5( $last_modified ) . '"'; -header( "Last-Modified: $last_modified GMT" ); -header( 'ETag: ' . $etag ); +$wp_last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); +$wp_etag = '"' . md5( $wp_last_modified ) . '"'; + +header( "Last-Modified: $wp_last_modified GMT" ); +header( 'ETag: ' . $wp_etag ); header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' ); // Support for conditional GET - use stripslashes() to avoid formatting.php dependency. -$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false; +if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) { + $client_etag = stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ); +} else { + $client_etag = ''; +} -if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { - $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false; +if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { + $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); +} else { + $client_last_modified = ''; } -$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); // If string is empty, return 0. If not, attempt to parse into a timestamp. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0; -// Make a timestamp for our most recent modification... -$modified_timestamp = strtotime( $last_modified ); +// Make a timestamp for our most recent modification. +$wp_modified_timestamp = strtotime( $wp_last_modified ); if ( ( $client_last_modified && $client_etag ) - ? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) ) - : ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) ) - ) { + ? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) + : ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) ) +) { status_header( 304 ); exit; }