Use HttpStatus::header instead of manually crafting header()
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 1 Jun 2015 14:31:52 +0000 (15:31 +0100)
committerKrinkle <krinklemail@gmail.com>
Thu, 4 Jun 2015 02:27:35 +0000 (02:27 +0000)
Also:
* Update wfHttpError() to use uppercase DOCTYPE, to match other code
  such as Html.php, wfThumbError(), HttpError.php, etc.

Change-Id: I4027e7fe1a138b03f78797b6d1bfe7bd1064d360

img_auth.php
includes/AjaxResponse.php
includes/GlobalFunctions.php
includes/StreamFile.php
includes/exception/HttpError.php
includes/exception/MWException.php
includes/resourceloader/ResourceLoader.php
includes/specials/SpecialRunJobs.php
thumb.php

index b26e6a5..8859141 100644 (file)
@@ -195,7 +195,7 @@ function wfForbidden( $msg1, $msg2 ) {
                        wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
        );
 
-       header( 'HTTP/1.0 403 Forbidden' );
+       HttpStatus::header( 403 );
        header( 'Cache-Control: no-cache' );
        header( 'Content-Type: text/html; charset=utf-8' );
        echo <<<ENDS
index 8e9f490..b3a6573 100644 (file)
@@ -159,7 +159,7 @@ class AjaxResponse {
        function sendHeaders() {
                if ( $this->mResponseCode ) {
                        $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
-                       header( "Status: " . $this->mResponseCode, true, (int)$n );
+                       HttpStatus::header( $n );
                }
 
                header ( "Content-Type: " . $this->mContentType );
index 8b3b959..25876fb 100644 (file)
@@ -2129,15 +2129,14 @@ function wfVarDump( $var ) {
  */
 function wfHttpError( $code, $label, $desc ) {
        global $wgOut;
-       header( "HTTP/1.0 $code $label" );
-       header( "Status: $code $label" );
+       HttpStatus::header( $code );
        if ( $wgOut ) {
                $wgOut->disable();
                $wgOut->sendCacheControl();
        }
 
        header( 'Content-type: text/html; charset=utf-8' );
-       print "<!doctype html>" .
+       print '<!DOCTYPE html>' .
                '<html><head><title>' .
                htmlspecialchars( $label ) .
                '</title></head><body><h1>' .
index a52b25b..a7522ea 100644 (file)
@@ -78,7 +78,7 @@ class StreamFile {
        ) {
                if ( !is_array( $info ) ) {
                        if ( $sendErrors ) {
-                               header( 'HTTP/1.0 404 Not Found' );
+                               HttpStatus::header( 404  );
                                header( 'Cache-Control: no-cache' );
                                header( 'Content-Type: text/html; charset=utf-8' );
                                $encFile = htmlspecialchars( $path );
@@ -126,7 +126,7 @@ class StreamFile {
                        $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
                        if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) {
                                ini_set( 'zlib.output_compression', 0 );
-                               header( "HTTP/1.0 304 Not Modified" );
+                               HttpStatus::header( 304 );
                                return self::NOT_MODIFIED; // ok
                        }
                }
index d3ee9b9..b910ec6 100644 (file)
@@ -74,9 +74,7 @@ class HttpError extends MWException {
        public function report() {
                $this->doLog();
 
-               $httpMessage = HttpStatus::getMessage( $this->httpCode );
-
-               header( "Status: {$this->httpCode} {$httpMessage}", true, $this->httpCode );
+               HttpStatus::header( $this->httpCode );
                header( 'Content-type: text/html; charset=utf-8' );
 
                print $this->getHTML();
index e54e568..c0186f9 100644 (file)
@@ -238,8 +238,7 @@ class MWException extends Exception {
                } elseif ( self::isCommandLine() ) {
                        MWExceptionHandler::printError( $this->getText() );
                } else {
-                       self::header( 'HTTP/1.1 500 Internal Server Error' );
-                       self::header( 'Status: 500 Internal Server Error' );
+                       self::statusHeader( 500 );
                        self::header( "Content-Type: $wgMimeType; charset=utf-8" );
 
                        $this->reportHTML();
@@ -266,4 +265,9 @@ class MWException extends Exception {
                        header( $header );
                }
        }
+       private static function statusHeader( $code ) {
+               if ( !headers_sent() ) {
+                       HttpStatus::header( $code );
+               }
+       }
 }
index 5bc9dc3..35311d1 100644 (file)
@@ -804,8 +804,7 @@ class ResourceLoader {
                        // sending the 304.
                        wfResetOutputBuffers( /* $resetGzipEncoding = */ true );
 
-                       header( 'HTTP/1.0 304 Not Modified' );
-                       header( 'Status: 304 Not Modified' );
+                       HttpStatus::header( 304 );
 
                        $this->sendResponseHeaders( $context, $etag, false );
                        return true;
index 8cf9367..286a745 100644 (file)
@@ -38,14 +38,14 @@ class SpecialRunJobs extends UnlistedSpecialPage {
                $this->getOutput()->disable();
 
                if ( wfReadOnly() ) {
-                       header( "HTTP/1.0 423 Locked" );
+                       // HTTP 423 Locked
+                       HttpStatus::header( 423 );
                        print 'Wiki is in read-only mode';
 
                        return;
                } elseif ( !$this->getRequest()->wasPosted() ) {
-                       header( "HTTP/1.0 400 Bad Request" );
+                       HttpStatus::header( 400 );
                        print 'Request must be POSTed';
-
                        return;
                }
 
@@ -55,9 +55,8 @@ class SpecialRunJobs extends UnlistedSpecialPage {
                $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
                $missing = array_diff_key( $required, $params );
                if ( count( $missing ) ) {
-                       header( "HTTP/1.0 400 Bad Request" );
+                       HttpStatus::header( 400 );
                        print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
-
                        return;
                }
 
@@ -69,9 +68,8 @@ class SpecialRunJobs extends UnlistedSpecialPage {
                $verified = is_string( $providedSignature )
                        && hash_equals( $correctSignature, $providedSignature );
                if ( !$verified || $params['sigexpiry'] < time() ) {
-                       header( "HTTP/1.0 400 Bad Request" );
+                       HttpStatus::header( 400 );
                        print 'Invalid or stale signature provided';
-
                        return;
                }
 
@@ -83,7 +81,8 @@ class SpecialRunJobs extends UnlistedSpecialPage {
                        // but it needs to know when it is safe to disconnect. Until this
                        // reaches ignore_user_abort(), it is not safe as the jobs won't run.
                        ignore_user_abort( true ); // jobs may take a bit of time
-                       header( "HTTP/1.0 202 Accepted" );
+                       // HTTP 202 Accepted
+                       HttpStatus::header( 202 );
                        ob_flush();
                        flush();
                        // Once the client receives this response, it can disconnect
index b163a7f..8bc04b9 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -235,7 +235,7 @@ function wfStreamThumb( array $params ) {
                $imsUnix = strtotime( $imsString );
                wfRestoreWarnings();
                if ( wfTimestamp( TS_UNIX, $img->getTimestamp() ) <= $imsUnix ) {
-                       header( 'HTTP/1.1 304 Not Modified' );
+                       HttpStatus::header( 304  );
                        return;
                }
        }
@@ -554,14 +554,14 @@ function wfThumbError( $status, $msg ) {
        header( 'Cache-Control: no-cache' );
        header( 'Content-Type: text/html; charset=utf-8' );
        if ( $status == 400 ) {
-               header( 'HTTP/1.1 400 Bad request' );
+               HttpStatus::header( 400 );
        } elseif ( $status == 404 ) {
-               header( 'HTTP/1.1 404 Not found' );
+               HttpStatus::header( 404 );
        } elseif ( $status == 403 ) {
-               header( 'HTTP/1.1 403 Forbidden' );
+               HttpStatus::header( 403 );
                header( 'Vary: Cookie' );
        } else {
-               header( 'HTTP/1.1 500 Internal server error' );
+               HttpStatus::header( 500 );
        }
        if ( $wgShowHostnames ) {
                header( 'X-MW-Thumbnail-Renderer: ' . wfHostname() );