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
This commit is contained in:
parent
8b1a6cc58a
commit
9911a36050
2 changed files with 20 additions and 3 deletions
|
|
@ -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 ) )
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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 );
|
||||
|
|
|
|||
Loading…
Reference in a new issue