From 29d159aebad4867c6f6941164952864c41acdd2f Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Sun, 5 Jun 2011 19:51:31 +0000 Subject: [PATCH] * (bug 22179) Internal use of API (FauxRequest) results in HTTP headers being set Per Chad, switch API to use WebResponse::header() wrapper Add $http_response_code to WebResponse::header() Fix some code spacing/whitespace issues --- includes/WebResponse.php | 18 ++++++++++-------- includes/api/ApiFormatBase.php | 7 +++---- includes/api/ApiMain.php | 33 +++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/includes/WebResponse.php b/includes/WebResponse.php index a0f7776618..1dded190eb 100644 --- a/includes/WebResponse.php +++ b/includes/WebResponse.php @@ -17,12 +17,14 @@ class WebResponse { * header() * @param $string String: header to output * @param $replace Bool: replace current similar header + * @param $http_response_code null|int Forces the HTTP response code to the specified value. */ - public function header($string, $replace=true) { - header($string,$replace); + public function header( $string, $replace = true, $http_response_code = null ) { + header( $string, $replace, $http_response_code ); } - /** Set the browser cookie + /** + * Set the browser cookie * @param $name String: name of cookie * @param $value String: value to give cookie * @param $expire Int: number of seconds til cookie expires @@ -59,15 +61,15 @@ class FauxResponse extends WebResponse { private $headers; private $cookies; - public function header($string, $replace=true) { - list($key, $val) = explode(":", $string, 2); + public function header( $string, $replace = true, $http_response_code = null ) { + list( $key, $val ) = explode( ":", $string, 2 ); - if($replace || !isset($this->headers[$key])) { + if( $replace || !isset( $this->headers[$key] ) ) { $this->headers[$key] = $val; } } - public function getheader($key) { + public function getheader( $key ) { return $this->headers[$key]; } @@ -76,7 +78,7 @@ class FauxResponse extends WebResponse { } public function getcookie( $name ) { - if ( isset($this->cookies[$name]) ) { + if ( isset( $this->cookies[$name] ) ) { return $this->cookies[$name]; } } diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index df5b7c29d7..fe0099145a 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -145,10 +145,9 @@ abstract class ApiFormatBase extends ApiBase { if ( is_null( $mime ) ) { return; // skip any initialization } - - if( !$this->getMain()->isInternalMode() ) { - header( "Content-Type: $mime; charset=utf-8" ); - } + + global $wgRequest; + $wgRequest->response()->header( "Content-Type: $mime; charset=utf-8" ); if ( $isHtml ) { ?> diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 1b9ae2f95b..0cf1d374cf 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -370,11 +370,13 @@ class ApiMain extends ApiBase { // Error results should not be cached $this->setCacheMode( 'private' ); + global $wgRequest; + $response = $wgRequest->response(); $headerStr = 'MediaWiki-API-Error: ' . $errCode; if ( $e->getCode() === 0 ) { - header( $headerStr ); + $response->header( $headerStr ); } else { - header( $headerStr, true, $e->getCode() ); + $response->header( $headerStr, true, $e->getCode() ); } // Reset and print just the error message @@ -397,26 +399,29 @@ class ApiMain extends ApiBase { } protected function sendCacheHeaders() { + global $wgRequest; + $response = $wgRequest->response(); + if ( $this->mCacheMode == 'private' ) { - header( 'Cache-Control: private' ); + $response->header( 'Cache-Control: private' ); return; } if ( $this->mCacheMode == 'anon-public-user-private' ) { global $wgUseXVO, $wgOut; - header( 'Vary: Accept-Encoding, Cookie' ); + $response->header( 'Vary: Accept-Encoding, Cookie' ); if ( $wgUseXVO ) { - header( $wgOut->getXVO() ); + $response->header( $wgOut->getXVO() ); if ( $wgOut->haveCacheVaryCookies() ) { // Logged in, mark this request private - header( 'Cache-Control: private' ); + $response->header( 'Cache-Control: private' ); return; } // Logged out, send normal public headers below } elseif ( session_id() != '' ) { // Logged in or otherwise has session (e.g. anonymous users who have edited) // Mark request private - header( 'Cache-Control: private' ); + $response->header( 'Cache-Control: private' ); return; } // else no XVO and anonymous, send public headers below } @@ -433,7 +438,7 @@ class ApiMain extends ApiBase { // Public cache not requested // Sending a Vary header in this case is harmless, and protects us // against conditional calls of setCacheMaxAge(). - header( 'Cache-Control: private' ); + $response->header( 'Cache-Control: private' ); return; } @@ -442,7 +447,7 @@ class ApiMain extends ApiBase { // Send an Expires header $maxAge = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] ); $expiryUnixTime = ( $maxAge == 0 ? 1 : time() + $maxAge ); - header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) ); + $response->header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) ); // Construct the Cache-Control header $ccHeader = ''; @@ -459,7 +464,7 @@ class ApiMain extends ApiBase { } } - header( "Cache-Control: $ccHeader" ); + $response->header( "Cache-Control: $ccHeader" ); } /** @@ -588,8 +593,12 @@ class ApiMain extends ApiBase { $maxLag = $params['maxlag']; list( $host, $lag ) = wfGetLB()->getMaxLag(); if ( $lag > $maxLag ) { - header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); - header( 'X-Database-Lag: ' . intval( $lag ) ); + global $wgRequest; + $response = $wgRequest->response(); + + $response->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); + $response->header( 'X-Database-Lag: ' . intval( $lag ) ); + if ( $wgShowHostnames ) { $this->dieUsage( "Waiting for $host: $lag seconds lagged", 'maxlag' ); } else { -- 2.20.1