Make WordPress Core


Ignore:
Timestamp:
05/19/2017 08:26:48 PM (9 years ago)
Author:
jnylen0
Message:

REST API: Avoid sending blank Last-Modified headers with authenticated requests.

This commit adds a new WP_REST_Server#remove_header method and uses it to clear the Last-Modified header when the "no caching" headers are sent (by default for all authenticated REST API requests). This matches the behavior of the nocache_headers function used in other parts of WordPress.

Previously, the REST API would send an empty Last-Modified header in this situation. Under some server and browser configurations, this causes browsers to cache authenticated REST API requests, which is undesirable.

Props iv3rson76, zinigor, rmccue, jnylen0.
Fixes #40444.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r40238 r40805  
    253253                if ( $send_no_cache_headers ) {
    254254                        foreach ( wp_get_nocache_headers() as $header => $header_value ) {
    255                                 $this->send_header( $header, $header_value );
     255                                if ( empty( $header_value ) ) {
     256                                        $this->remove_header( $header );
     257                                } else {
     258                                        $this->send_header( $header, $header_value );
     259                                }
    256260                        }
    257261                }
    … …  
    12641268
    12651269        /**
     1270         * Removes an HTTP header from the current response.
     1271         *
     1272         * @since 4.8.0
     1273         * @access public
     1274         *
     1275         * @param string $key Header key.
     1276         */
     1277        public function remove_header( $key ) {
     1278                if ( function_exists( 'header_remove' ) ) {
     1279                        // In PHP 5.3+ there is a way to remove an already set header.
     1280                        header_remove( $key );
     1281                } else {
     1282                        // In PHP 5.2, send an empty header, but only as a last resort to
     1283                        // override a header already sent.
     1284                        foreach ( headers_list() as $header ) {
     1285                                if ( 0 === stripos( $header, "$key:" ) ) {
     1286                                        $this->send_header( $key, '' );
     1287                                        break;
     1288                                }
     1289                        }
     1290                }
     1291        }
     1292
     1293        /**
    12661294         * Retrieves the raw request entity (body).
    12671295         *
Note: See TracChangeset for help on using the changeset viewer.