From: Tim Starling Date: Tue, 10 Sep 2019 06:26:53 +0000 (+1000) Subject: REST: Properly handle HEAD requests X-Git-Tag: 1.34.0-rc.0~179^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=9911a3605019806e972a26f5d19cf9ac8c16c401;p=lhc%2Fweb%2Fwiklou.git REST: Properly handle HEAD requests Just handle a HEAD request as a GET request, if no specific HEAD handler exists. This is simple and similar to what the rest of MediaWiki does in response to a HEAD request. Bug: T226043 Change-Id: I7b2bd657c20b56844459874131a3d85fabe7db3d --- diff --git a/includes/Rest/Router.php b/includes/Rest/Router.php index 1c5f66b2be..6821d89c27 100644 --- a/includes/Rest/Router.php +++ b/includes/Rest/Router.php @@ -233,15 +233,22 @@ class Router { ); } + $requestMethod = $request->getMethod(); $matchers = $this->getMatchers(); - $matcher = $matchers[$request->getMethod()] ?? null; + $matcher = $matchers[$requestMethod] ?? null; $match = $matcher ? $matcher->match( $relPath ) : null; + // For a HEAD request, execute the GET handler instead if one exists. + // The webserver will discard the body. + if ( !$match && $requestMethod === 'HEAD' && isset( $matchers['GET'] ) ) { + $match = $matchers['GET']->match( $relPath ); + } + if ( !$match ) { // Check for 405 wrong method $allowed = []; foreach ( $matchers as $allowedMethod => $allowedMatcher ) { - if ( $allowedMethod === $request->getMethod() ) { + if ( $allowedMethod === $requestMethod ) { continue; } if ( $allowedMatcher->match( $relPath ) ) { @@ -251,7 +258,7 @@ class Router { if ( $allowed ) { $response = $this->responseFactory->createLocalizedHttpError( 405, ( new MessageValue( 'rest-wrong-method' ) ) - ->textParams( $request->getMethod() ) + ->textParams( $requestMethod ) ->commaListParams( $allowed ) ->numParams( count( $allowed ) ) ); diff --git a/tests/phpunit/unit/includes/Rest/RouterTest.php b/tests/phpunit/unit/includes/Rest/RouterTest.php index 9f51371a55..58039ea846 100644 --- a/tests/phpunit/unit/includes/Rest/RouterTest.php +++ b/tests/phpunit/unit/includes/Rest/RouterTest.php @@ -55,6 +55,16 @@ class RouterTest extends \MediaWikiUnitTestCase { $this->assertSame( 'GET', $response->getHeaderLine( 'Allow' ) ); } + public function testHeadToGet() { + $request = new RequestData( [ + 'uri' => new Uri( '/rest/user/joe/hello' ), + 'method' => 'HEAD' + ] ); + $router = $this->createRouter( $request ); + $response = $router->execute( $request ); + $this->assertSame( 200, $response->getStatusCode() ); + } + public function testNoMatch() { $request = new RequestData( [ 'uri' => new Uri( '/rest/bogus' ) ] ); $router = $this->createRouter( $request );