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.
Built from https://develop.svn.wordpress.org/trunk@56395


git-svn-id: http://core.svn.wordpress.org/trunk@55907 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-08-13 10:31:23 +00:00
parent 22f80e9382
commit 11aa9b4da5
3 changed files with 33 additions and 22 deletions

View File

@ -505,8 +505,8 @@ class WP {
} }
$wp_last_modified .= ' GMT'; $wp_last_modified .= ' GMT';
$wp_etag = '"' . md5( $wp_last_modified ) . '"'; $wp_etag = '"' . md5( $wp_last_modified ) . '"';
$headers['Last-Modified'] = $wp_last_modified; $headers['Last-Modified'] = $wp_last_modified;
$headers['ETag'] = $wp_etag; $headers['ETag'] = $wp_etag;
@ -514,19 +514,24 @@ class WP {
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) { if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
$client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ); $client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
} else { } 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. // 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; $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 ); $wp_modified_timestamp = strtotime( $wp_last_modified );
if ( ( $client_last_modified && $client_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 ) )
( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) ) : ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
) { ) {
$status = 304; $status = 304;
$exit_required = true; $exit_required = true;

View File

@ -54,29 +54,35 @@ if ( WPMU_ACCEL_REDIRECT ) {
exit; exit;
} }
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); $wp_last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"'; $wp_etag = '"' . md5( $wp_last_modified ) . '"';
header( "Last-Modified: $last_modified GMT" );
header( 'ETag: ' . $etag ); header( "Last-Modified: $wp_last_modified GMT" );
header( 'ETag: ' . $wp_etag );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' ); header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
// Support for conditional GET - use stripslashes() to avoid formatting.php dependency. // 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'] );
if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { } else {
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = false; $client_etag = '';
} }
if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
} else {
$client_last_modified = '';
}
// If string is empty, return 0. If not, attempt to parse into a timestamp. // 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; $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.
$modified_timestamp = strtotime( $last_modified ); $wp_modified_timestamp = strtotime( $wp_last_modified );
if ( ( $client_last_modified && $client_etag ) if ( ( $client_last_modified && $client_etag )
? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) ) ? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) )
: ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) ) : ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
) { ) {
status_header( 304 ); status_header( 304 );
exit; exit;

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.4-alpha-56394'; $wp_version = '6.4-alpha-56395';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.