REST: Properly handle HEAD requests
authorTim Starling <tstarling@wikimedia.org>
Tue, 10 Sep 2019 06:26:53 +0000 (16:26 +1000)
committerTim Starling <tstarling@wikimedia.org>
Tue, 17 Sep 2019 06:03:14 +0000 (16:03 +1000)
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

includes/Rest/Router.php
tests/phpunit/unit/includes/Rest/RouterTest.php

index 1c5f66b..6821d89 100644 (file)
@@ -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 ) )
                                );
index 9f51371..58039ea 100644 (file)
@@ -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 );