From: Timo Tijhof Date: Sun, 24 May 2015 12:31:11 +0000 (+0200) Subject: WebResponse: Implement statusHeader() using the new HttpStatus::header() X-Git-Tag: 1.31.0-rc.0~11172^2 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/modifier.php?a=commitdiff_plain;h=f57b6af45b1cea7f93857b63716a685d2f55c2bf;p=lhc%2Fweb%2Fwiklou.git WebResponse: Implement statusHeader() using the new HttpStatus::header() * Convert existing use of WebResponse::header() for HTTP status headers to use this new statusHeader() method. * Extend unit test forFauxResponse. I'm not calling HttpStatus::header directly in code. We keep the abstraction layer of WebResponse so that responses can continue to be mocked/fauxed without affecting the outer HTTP response. Change-Id: I8a536e16659fa88b54cffa1457efb889efa5fcd6 --- diff --git a/includes/MediaWiki.php b/includes/MediaWiki.php index d03b76af2e..df0870b899 100644 --- a/includes/MediaWiki.php +++ b/includes/MediaWiki.php @@ -509,7 +509,7 @@ class MediaWiki { list( $host, $lag ) = wfGetLB()->getMaxLag(); if ( $lag > $maxLag ) { $resp = $this->context->getRequest()->response(); - $resp->header( 'HTTP/1.1 503 Service Unavailable' ); + $resp->statusHeader( 503 ); $resp->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); $resp->header( 'X-Database-Lag: ' . intval( $lag ) ); $resp->header( 'Content-Type: text/plain' ); diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ba9fcba1e5..500488ccdb 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -836,10 +836,10 @@ class OutputPage extends ContextSource { } # Not modified - # Give a 304 response code and disable body output + # Give a 304 Not Modified response code and disable body output wfDebug( __METHOD__ . ": NOT MODIFIED, $info\n", 'log' ); ini_set( 'zlib.output_compression', 0 ); - $this->getRequest()->response()->header( "HTTP/1.1 304 Not Modified" ); + $this->getRequest()->response()->statusHeader( 304 ); $this->sendCacheControl(); $this->disable(); @@ -2218,8 +2218,7 @@ class OutputPage extends ContextSource { if ( Hooks::run( "BeforePageRedirect", array( $this, &$redirect, &$code ) ) ) { if ( $code == '301' || $code == '303' ) { if ( !$config->get( 'DebugRedirects' ) ) { - $message = HttpStatus::getMessage( $code ); - $response->header( "HTTP/1.1 $code $message" ); + $response->statusHeader( $code ); } $this->mLastModified = wfTimestamp( TS_RFC2822 ); } @@ -2241,10 +2240,7 @@ class OutputPage extends ContextSource { return; } elseif ( $this->mStatusCode ) { - $message = HttpStatus::getMessage( $this->mStatusCode ); - if ( $message ) { - $response->header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $message ); - } + $response->statusHeader( $this->mStatusCode ); } # Buffer output; final headers may depend on later processing diff --git a/includes/WebResponse.php b/includes/WebResponse.php index ab34931c2c..1b6947cd96 100644 --- a/includes/WebResponse.php +++ b/includes/WebResponse.php @@ -28,7 +28,7 @@ class WebResponse { /** - * Output a HTTP header, wrapper for PHP's header() + * Output an HTTP header, wrapper for PHP's header() * @param string $string Header to output * @param bool $replace Replace current similar header * @param null|int $http_response_code Forces the HTTP response code to the specified value. @@ -53,6 +53,15 @@ class WebResponse { return null; } + /** + * Output an HTTP status code header + * @since 1.26 + * @param int $code Status code + */ + public function statusHeader( $code ) { + HttpStatus::header( $code ); + } + /** * Set the browser cookie * @param string $name The name of the cookie. @@ -162,6 +171,14 @@ class FauxResponse extends WebResponse { } } + /** + * @since 1.26 + * @param int $code Status code + */ + public function statusHeader( $code ) { + $this->code = intval( $code ); + } + /** * @param string $key The name of the header to get (case insensitive). * @return string|null The header value (if set); null otherwise. diff --git a/includes/actions/RawAction.php b/includes/actions/RawAction.php index 727bed2015..b04ffbe2d9 100644 --- a/includes/actions/RawAction.php +++ b/includes/actions/RawAction.php @@ -109,12 +109,12 @@ class RawAction extends FormlessAction { $text = $this->getRawText(); + // Don't return a 404 response for CSS or JavaScript; + // 404s aren't generally cached and it would create + // extra hits when user CSS/JS are on and the user doesn't + // have the pages. if ( $text === false && $contentType == 'text/x-wiki' ) { - # Don't return a 404 response for CSS or JavaScript; - # 404s aren't generally cached and it would create - # extra hits when user CSS/JS are on and the user doesn't - # have the pages. - $response->header( 'HTTP/1.x 404 Not Found' ); + $response->statusHeader( 404 ); } if ( !Hooks::run( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) { diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 2b7937ee77..a8db20cedf 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -577,8 +577,7 @@ class ApiMain extends ApiBase { if ( !in_array( $originParam, $origins ) ) { // origin parameter set but incorrect // Send a 403 response - $message = HttpStatus::getMessage( 403 ); - $response->header( "HTTP/1.1 403 $message", true, 403 ); + $response->statusHeader( 403 ); $response->header( 'Cache-Control: no-cache' ); echo "'origin' parameter does not match Origin header\n"; diff --git a/includes/page/Article.php b/includes/page/Article.php index 48f3161739..11a3638fb9 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -1238,7 +1238,7 @@ class Article implements Page { if ( !$this->mPage->hasViewableContent() && $wgSend404Code && !$validUserPage ) { // If there's no backing content, send a 404 Not Found // for better machine handling of broken links. - $this->getContext()->getRequest()->response()->header( "HTTP/1.1 404 Not Found" ); + $this->getContext()->getRequest()->response()->statusHeader( 404 ); } // Also apply the robot policy for nonexisting pages (even if a 404 was used for sanity) diff --git a/includes/page/ImagePage.php b/includes/page/ImagePage.php index 8f635cfa11..cebc4c40a9 100644 --- a/includes/page/ImagePage.php +++ b/includes/page/ImagePage.php @@ -614,8 +614,8 @@ EOT $out->wrapWikiMsg( "", $nofile ); if ( !$this->getID() && $wgSend404Code ) { // If there is no image, no shared image, and no description page, - // output a 404, to be consistent with articles. - $request->response()->header( 'HTTP/1.1 404 Not Found' ); + // output a 404, to be consistent with Article::showMissingArticle. + $request->response()->statusHeader( 404 ); } } $out->setFileVersion( $this->displayImg ); diff --git a/tests/phpunit/includes/FauxResponseTest.php b/tests/phpunit/includes/FauxResponseTest.php index 4a974ba2db..39a0effa97 100644 --- a/tests/phpunit/includes/FauxResponseTest.php +++ b/tests/phpunit/includes/FauxResponseTest.php @@ -108,6 +108,13 @@ class FauxResponseTest extends MediaWikiTestCase { 'Third parameter overrides the HTTP/... header' ); + $this->response->statusHeader( 210 ); + $this->assertEquals( + 210, + $this->response->getStatusCode(), + 'Handle statusHeader method' + ); + $this->response->header( 'Location: http://localhost/', false, 206 ); $this->assertEquals( 206, diff --git a/thumb.php b/thumb.php index 051c39ea9d..b163a7fbce 100644 --- a/thumb.php +++ b/thumb.php @@ -204,7 +204,7 @@ function wfStreamThumb( array $params ) { if ( $redirectedLocation ) { // File has been moved. Give redirect. $response = RequestContext::getMain()->getRequest()->response(); - $response->header( "HTTP/1.1 302 " . HttpStatus::getMessage( 302 ) ); + $response->statusHeader( 302 ); $response->header( 'Location: ' . $redirectedLocation ); $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' ); @@ -270,7 +270,7 @@ function wfStreamThumb( array $params ) { } elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) { // Request for the "long" thumbnail name; redirect to canonical name $response = RequestContext::getMain()->getRequest()->response(); - $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) ); + $response->statusHeader( 301 ); $response->header( 'Location: ' . wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) ); $response->header( 'Expires: ' .