Make WordPress Core


Ignore:
Timestamp:
03/02/2025 10:05:08 PM (3 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Improve performance for HEAD requests.

By default, the REST API responds to HEAD rqeuests by calling the GET handler and omitting the body from the response. While convenient, this ends up performing needless work that slows down the API response time.

This commit adjusts the Core controllers to specifically handle HEAD requests by not preparing the response body.

Fixes #56481.
Props antonvlasenko, janusdev, ironprogrammer, swissspidy, spacedmonkey, mukesh27, mamaduka, timothyblynjacobs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r55562 r59899  
    10821082        $this->assertSame( 'rest_invalid_param', $valid->get_error_code() );
    10831083    }
     1084
     1085    /**
     1086     * Tests that WP_REST_Request::is_method() correctly detects the request method,
     1087     * regardless of case sensitivity.
     1088     *
     1089     * @dataProvider data_is_method_should_detect_method_ignoring_case
     1090     * @ticket 56481
     1091     *
     1092     * @param string $method       The expected HTTP method of the request.
     1093     * @param string $input_method The HTTP method to check against.
     1094     * @param bool   $expected     The expected result of the is_method() check.
     1095     */
     1096    public function test_is_method_should_detect_method_ignoring_case( $method, $input_method, $expected ) {
     1097        $request = new WP_REST_Request();
     1098        $request->set_method( $method );
     1099        $result = $request->is_method( $input_method );
     1100
     1101        $this->assertSame( $expected, $result, 'Failed asserting that the WP_REST_Request::is_method() method correctly detects the request method.' );
     1102    }
     1103
     1104    /**
     1105     * Provides test cases for verifying HTTP method comparison is case-insensitive.
     1106     *
     1107     * @return array
     1108     */
     1109    public function data_is_method_should_detect_method_ignoring_case() {
     1110        return array(
     1111            // GET.
     1112            'GET same case'          => array( 'GET', 'GET', true ),
     1113            'GET different case'     => array( 'GET', 'get', true ),
     1114            'GET different case #2'  => array( 'GET', 'get', true ),
     1115            'GET different case #3'  => array( 'GET', 'gEt', true ),
     1116            'GET wrong method'       => array( 'GET', 'POST', false ),
     1117            // POST.
     1118            'POST same case'         => array( 'POST', 'POST', true ),
     1119            'POST different case'    => array( 'POST', 'post', true ),
     1120            'POST different case #2' => array( 'POST', 'pOsT', true ),
     1121            'POST wrong method'      => array( 'POST', 'GET', false ),
     1122            // HEAD.
     1123            'HEAD same case'         => array( 'HEAD', 'HEAD', true ),
     1124            'HEAD different case'    => array( 'HEAD', 'head', true ),
     1125            'HEAD different case #2' => array( 'HEAD', 'HeAd', true ),
     1126            'HEAD wrong method'      => array( 'HEAD', 'GET', false ),
     1127        );
     1128    }
    10841129}
Note: See TracChangeset for help on using the changeset viewer.