* (bug 22179) Internal use of API (FauxRequest) results in HTTP headers being set
authorSam Reed <reedy@users.mediawiki.org>
Sun, 5 Jun 2011 19:51:31 +0000 (19:51 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Sun, 5 Jun 2011 19:51:31 +0000 (19:51 +0000)
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
includes/api/ApiFormatBase.php
includes/api/ApiMain.php

index a0f7776..1dded19 100644 (file)
@@ -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];
                }
        }
index df5b7c2..fe00991 100644 (file)
@@ -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 ) {
 ?>
index 1b9ae2f..0cf1d37 100644 (file)
@@ -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 {