WebResponse: Implement statusHeader() using the new HttpStatus::header()
authorTimo Tijhof <krinklemail@gmail.com>
Sun, 24 May 2015 12:31:11 +0000 (14:31 +0200)
committerKrinkle <krinklemail@gmail.com>
Thu, 4 Jun 2015 02:27:30 +0000 (02:27 +0000)
* 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

includes/MediaWiki.php
includes/OutputPage.php
includes/WebResponse.php
includes/actions/RawAction.php
includes/api/ApiMain.php
includes/page/Article.php
includes/page/ImagePage.php
tests/phpunit/includes/FauxResponseTest.php
thumb.php

index d03b76a..df0870b 100644 (file)
@@ -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' );
index ba9fcba..500488c 100644 (file)
@@ -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
index ab34931..1b6947c 100644 (file)
@@ -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.
index 727bed2..b04ffbe 100644 (file)
@@ -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 ) ) ) {
index 2b7937e..a8db20c 100644 (file)
@@ -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";
 
index 48f3161..11a3638 100644 (file)
@@ -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)
index 8f635cf..cebc4c4 100644 (file)
@@ -614,8 +614,8 @@ EOT
                        $out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $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 );
index 4a974ba..39a0eff 100644 (file)
@@ -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,
index 051c39e..b163a7f 100644 (file)
--- 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: ' .