Skip to content

Commit 25443d3

Browse files
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
1 parent 6929fd8 commit 25443d3

2 files changed

Lines changed: 31 additions & 20 deletions

File tree

src/wp-includes/class-wp.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,28 +505,33 @@ public function send_headers() {
505505
}
506506

507507
$wp_last_modified .= ' GMT';
508+
$wp_etag = '"' . md5( $wp_last_modified ) . '"';
508509

509-
$wp_etag = '"' . md5( $wp_last_modified ) . '"';
510510
$headers['Last-Modified'] = $wp_last_modified;
511511
$headers['ETag'] = $wp_etag;
512512

513513
// Support for conditional GET.
514514
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
515515
$client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] );
516516
} else {
517-
$client_etag = false;
517+
$client_etag = '';
518+
}
519+
520+
if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
521+
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
522+
} else {
523+
$client_last_modified = '';
518524
}
519525

520-
$client_last_modified = empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ? '' : trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
521526
// If string is empty, return 0. If not, attempt to parse into a timestamp.
522527
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
523528

524-
// Make a timestamp for our most recent modification..
529+
// Make a timestamp for our most recent modification.
525530
$wp_modified_timestamp = strtotime( $wp_last_modified );
526531

527-
if ( ( $client_last_modified && $client_etag ) ?
528-
( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) :
529-
( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
532+
if ( ( $client_last_modified && $client_etag )
533+
? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) )
534+
: ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
530535
) {
531536
$status = 304;
532537
$exit_required = true;

src/wp-includes/ms-files.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,36 @@
5454
exit;
5555
}
5656

57-
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
58-
$etag = '"' . md5( $last_modified ) . '"';
59-
header( "Last-Modified: $last_modified GMT" );
60-
header( 'ETag: ' . $etag );
57+
$wp_last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
58+
$wp_etag = '"' . md5( $wp_last_modified ) . '"';
59+
60+
header( "Last-Modified: $wp_last_modified GMT" );
61+
header( 'ETag: ' . $wp_etag );
6162
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
6263

6364
// Support for conditional GET - use stripslashes() to avoid formatting.php dependency.
64-
$client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
65+
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) {
66+
$client_etag = stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] );
67+
} else {
68+
$client_etag = '';
69+
}
6570

66-
if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
67-
$_SERVER['HTTP_IF_MODIFIED_SINCE'] = false;
71+
if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
72+
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
73+
} else {
74+
$client_last_modified = '';
6875
}
6976

70-
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
7177
// If string is empty, return 0. If not, attempt to parse into a timestamp.
7278
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
7379

74-
// Make a timestamp for our most recent modification...
75-
$modified_timestamp = strtotime( $last_modified );
80+
// Make a timestamp for our most recent modification.
81+
$wp_modified_timestamp = strtotime( $wp_last_modified );
7682

7783
if ( ( $client_last_modified && $client_etag )
78-
? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag == $etag ) )
79-
: ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag == $etag ) )
80-
) {
84+
? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) )
85+
: ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) )
86+
) {
8187
status_header( 304 );
8288
exit;
8389
}

0 commit comments

Comments
 (0)