API: Only take HTTP code from ApiUsageException
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index d901f54..8389b24 100644 (file)
@@ -593,9 +593,13 @@ class ApiMain extends ApiBase {
                // Printer may not be initialized if the extractRequestParams() fails for the main module
                $this->createErrorPrinter();
 
+               // Get desired HTTP code from an ApiUsageException. Don't use codes from other
+               // exception types, as they are unlikely to be intended as an HTTP code.
+               $httpCode = $e instanceof ApiUsageException ? $e->getCode() : 0;
+
                $failed = false;
                try {
-                       $this->printResult( $e->getCode() );
+                       $this->printResult( $httpCode );
                } catch ( ApiUsageException $ex ) {
                        // The error printer itself is failing. Try suppressing its request
                        // parameters and redo.
@@ -617,10 +621,10 @@ class ApiMain extends ApiBase {
                        $this->mPrinter = null;
                        $this->createErrorPrinter();
                        $this->mPrinter->forceDefaultParams();
-                       if ( $e->getCode() ) {
+                       if ( $httpCode ) {
                                $response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
                        }
-                       $this->printResult( $e->getCode() );
+                       $this->printResult( $httpCode );
                }
        }
 
@@ -916,32 +920,20 @@ class ApiMain extends ApiBase {
                        return;
                }
 
-               $useKeyHeader = $config->get( 'UseKeyHeader' );
                if ( $this->mCacheMode == 'anon-public-user-private' ) {
                        $out->addVaryHeader( 'Cookie' );
                        $response->header( $out->getVaryHeader() );
-                       if ( $useKeyHeader ) {
-                               $response->header( $out->getKeyHeader() );
-                               if ( $out->haveCacheVaryCookies() ) {
-                                       // Logged in, mark this request private
-                                       $response->header( "Cache-Control: $privateCache" );
-                                       return;
-                               }
-                               // Logged out, send normal public headers below
-                       } elseif ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
+                       if ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
                                // Logged in or otherwise has session (e.g. anonymous users who have edited)
                                // Mark request private
                                $response->header( "Cache-Control: $privateCache" );
 
                                return;
-                       } // else no Key and anonymous, send public headers below
+                       } // else anonymous, send public headers below
                }
 
                // Send public headers
                $response->header( $out->getVaryHeader() );
-               if ( $useKeyHeader ) {
-                       $response->header( $out->getKeyHeader() );
-               }
 
                // If nobody called setCacheMaxAge(), use the (s)maxage parameters
                if ( !isset( $this->mCacheControl['s-maxage'] ) ) {