http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89528

Revision: 89528
Author:   reedy
Date:     2011-06-05 19:51:31 +0000 (Sun, 05 Jun 2011)
Log Message:
-----------
* (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

Modified Paths:
--------------
    trunk/phase3/includes/WebResponse.php
    trunk/phase3/includes/api/ApiFormatBase.php
    trunk/phase3/includes/api/ApiMain.php

Modified: trunk/phase3/includes/WebResponse.php
===================================================================
--- trunk/phase3/includes/WebResponse.php       2011-06-05 19:46:08 UTC (rev 
89527)
+++ trunk/phase3/includes/WebResponse.php       2011-06-05 19:51:31 UTC (rev 
89528)
@@ -17,12 +17,14 @@
         * 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 @@
        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 @@
        }
 
        public function getcookie( $name )  {
-               if ( isset($this->cookies[$name]) ) {
+               if ( isset( $this->cookies[$name] ) ) {
                        return $this->cookies[$name];
                }
        }

Modified: trunk/phase3/includes/api/ApiFormatBase.php
===================================================================
--- trunk/phase3/includes/api/ApiFormatBase.php 2011-06-05 19:46:08 UTC (rev 
89527)
+++ trunk/phase3/includes/api/ApiFormatBase.php 2011-06-05 19:51:31 UTC (rev 
89528)
@@ -145,11 +145,10 @@
                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 ) {
 ?>
 <!DOCTYPE HTML>

Modified: trunk/phase3/includes/api/ApiMain.php
===================================================================
--- trunk/phase3/includes/api/ApiMain.php       2011-06-05 19:46:08 UTC (rev 
89527)
+++ trunk/phase3/includes/api/ApiMain.php       2011-06-05 19:51:31 UTC (rev 
89528)
@@ -370,11 +370,13 @@
                        // 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 @@
        }
 
        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 @@
                        // 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 @@
                // 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 @@
                        }
                }
 
-               header( "Cache-Control: $ccHeader" );
+               $response->header( "Cache-Control: $ccHeader" );
        }
 
        /**
@@ -588,8 +593,12 @@
                        $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 {


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to