Skip to content

Commit b3e4857

Browse files
stodorovicdonnchawp
authored andcommitted
Optimize apache_response_headers (#496)
1 parent 6100603 commit b3e4857

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

wp-cache-phase2.php

+38-15
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,15 @@ function wp_cache_serve_cache_file() {
196196

197197
// don't try to match modified dates if using dynamic code.
198198
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
199-
if ( function_exists( 'apache_request_headers' ) ) {
200-
$request = apache_request_headers();
201-
$remote_mod_time = ( isset ( $request[ 'If-Modified-Since' ] ) ) ? $request[ 'If-Modified-Since' ] : null;
202-
} else {
203-
if ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) )
204-
$remote_mod_time = $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ];
205-
else
206-
$remote_mod_time = null;
199+
$headers = apache_request_headers();
200+
$remote_mod_time = isset ( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;
201+
202+
if ( is_null( $remote_mod_time ) && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
203+
$remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
207204
}
205+
208206
$local_mod_time = gmdate("D, d M Y H:i:s",filemtime( $file )).' GMT';
209-
if ( !is_null($remote_mod_time) && $remote_mod_time == $local_mod_time ) {
207+
if ( !is_null($remote_mod_time) && $remote_mod_time == $local_mod_time ) {
210208
header( $_SERVER[ 'SERVER_PROTOCOL' ] . " 304 Not Modified" );
211209
exit();
212210
}
@@ -1355,15 +1353,21 @@ function wpcache_logged_in_message() {
13551353
functionwp_cache_user_agent_is_rejected() {
13561354
global$cache_rejected_user_agent;
13571355

1358-
if (!function_exists('apache_request_headers')) returnfalse;
1356+
if ( empty( $cache_rejected_user_agent ) || ! is_array( $cache_rejected_user_agent ) ) {
1357+
returnfalse;
1358+
}
1359+
13591360
$headers = apache_request_headers();
1360-
if (!isset($headers["User-Agent"])) returnfalse;
1361-
if ( false == is_array( $cache_rejected_user_agent ) )
1361+
if ( empty( $headers['User-Agent'] ) ) {
13621362
returnfalse;
1363-
foreach ($cache_rejected_user_agentas$expr) {
1364-
if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
1363+
}
1364+
1365+
foreach ( $cache_rejected_user_agentas$user_agent ) {
1366+
if ( ! empty( $user_agent ) && stristr( $headers['User-Agent'], $user_agent ) ) {
13651367
returntrue;
1368+
}
13661369
}
1370+
13671371
returnfalse;
13681372
}
13691373

@@ -3057,4 +3061,23 @@ function wp_cache_gc_watcher() {
30573061
}
30583062
}
30593063

3060-
?>
3064+
if ( ! function_exists( 'apache_request_headers' ) ) {
3065+
/**
3066+
* A fallback for get request headers.
3067+
* Based on comments from http://php.net/manual/en/function.apache-request-headers.php
3068+
*
3069+
* @return array List of request headers
3070+
*/
3071+
functionapache_request_headers() {
3072+
$headers = array();
3073+
3074+
foreach ( array_keys( $_SERVER ) as$skey ) {
3075+
if ( 0 === strncmp( $skey, 'HTTP_' ) ) {
3076+
$header = implode( '-', array_map( 'ucfirst', array_slice( explode( '_', strtolower( $skey ) ) , 1 ) ) );
3077+
$headers[ $header ] = $_SERVER[ $skey ];
3078+
}
3079+
}
3080+
3081+
return$headers;
3082+
}
3083+
}

wp-cache.php

-15
Original file line numberDiff line numberDiff line change
@@ -1840,21 +1840,6 @@ function wp_cache_sanitize_value($text, & $array) {
18401840
return$text;
18411841
}
18421842

1843-
// from tehjosh at gamingg dot net http://uk2.php.net/manual/en/function.apache-request-headers.php#73964
1844-
// fixed bug in second substr()
1845-
if( !function_exists('apache_request_headers') ) {
1846-
functionapache_request_headers() {
1847-
$headers = array();
1848-
foreach(array_keys($_SERVER) as$skey) {
1849-
if(substr($skey, 0, 5) == "HTTP_") {
1850-
$headername = str_replace("", "-", ucwords(strtolower(str_replace("_", "", substr($skey, 5)))));
1851-
$headers[$headername] = $_SERVER[$skey];
1852-
}
1853-
}
1854-
return$headers;
1855-
}
1856-
}
1857-
18581843
functionwp_cache_update_rejected_ua() {
18591844
global$cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;
18601845

0 commit comments

Comments
 (0)
close