From 70429dc8a2996e00aad05b7831bb69e5bf84a4d6 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 1 Jun 2015 15:31:52 +0100 Subject: [PATCH] Use HttpStatus::header instead of manually crafting header() 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 | 2 +- includes/AjaxResponse.php | 2 +- includes/GlobalFunctions.php | 5 ++--- includes/StreamFile.php | 4 ++-- includes/exception/HttpError.php | 4 +--- includes/exception/MWException.php | 8 ++++++-- includes/resourceloader/ResourceLoader.php | 3 +-- includes/specials/SpecialRunJobs.php | 15 +++++++-------- thumb.php | 10 +++++----- 9 files changed, 26 insertions(+), 27 deletions(-) diff --git a/img_auth.php b/img_auth.php index b26e6a5ec9..885914171c 100644 --- a/img_auth.php +++ b/img_auth.php @@ -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 <<mResponseCode ) { $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode ); - header( "Status: " . $this->mResponseCode, true, (int)$n ); + HttpStatus::header( $n ); } header ( "Content-Type: " . $this->mContentType ); diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 8b3b959a36..25876fb99a 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 "" . + print '' . '' . htmlspecialchars( $label ) . '

' . diff --git a/includes/StreamFile.php b/includes/StreamFile.php index a52b25b02f..a7522ea2b5 100644 --- a/includes/StreamFile.php +++ b/includes/StreamFile.php @@ -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 } } diff --git a/includes/exception/HttpError.php b/includes/exception/HttpError.php index d3ee9b95ba..b910ec629d 100644 --- a/includes/exception/HttpError.php +++ b/includes/exception/HttpError.php @@ -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(); diff --git a/includes/exception/MWException.php b/includes/exception/MWException.php index e54e56852e..c0186f9f76 100644 --- a/includes/exception/MWException.php +++ b/includes/exception/MWException.php @@ -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 ); + } + } } diff --git a/includes/resourceloader/ResourceLoader.php b/includes/resourceloader/ResourceLoader.php index 5bc9dc3cba..35311d1b7a 100644 --- a/includes/resourceloader/ResourceLoader.php +++ b/includes/resourceloader/ResourceLoader.php @@ -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; diff --git a/includes/specials/SpecialRunJobs.php b/includes/specials/SpecialRunJobs.php index 8cf93670ba..286a745653 100644 --- a/includes/specials/SpecialRunJobs.php +++ b/includes/specials/SpecialRunJobs.php @@ -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 diff --git a/thumb.php b/thumb.php index b163a7fbce..8bc04b9e85 100644 --- 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() ); -- 2.20.1