AjaxResponse: Fix broken logic for extracting HTTP status codes
authorBryan Davis <bd808@wikimedia.org>
Thu, 11 Jun 2015 15:13:00 +0000 (09:13 -0600)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 11 Jun 2015 15:42:02 +0000 (16:42 +0100)
Follows-up 6584cef207 (r16266; 2006; MediaWiki 1.8.0).
This regex was just trimming whitespace from the start of the string. It
matched whitespace, then a match group for a first sequence of digits. The
replacement is just the digits match. This is essentially `ltrim()`. It didn't
account for the content after the match.

> echo preg_replace( '/^ *(\d+)/', '\1', '200 OK' );
> "200 OK"

This was causing:
> Warning: Unknown HTTP status code 200 OK in libs/HttpStatus.php:100
> Warning: Unknown HTTP status code 304 Not Modified OK in libs/HttpStatus.php:100

It defaults to HTTP 200. So presumbaly the impact was low. Though it may've caused
304 responses to have been broken (content body missing).

Bug: T102028
Change-Id: Iafff9982bbbee893c13f891901dde88f998db7a6

includes/AjaxResponse.php

index b3a6573..2984c33 100644 (file)
@@ -86,7 +86,7 @@ class AjaxResponse {
 
                $this->mDisabled = false;
                $this->mText = '';
-               $this->mResponseCode = '200 OK';
+               $this->mResponseCode = 200;
                $this->mLastModified = false;
                $this->mContentType = 'application/x-wiki';
 
@@ -158,7 +158,11 @@ class AjaxResponse {
         */
        function sendHeaders() {
                if ( $this->mResponseCode ) {
-                       $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
+                       // For back-compat, it is supported that mResponseCode be a string like " 200 OK"
+                       // (with leading space and the status message after). Cast response code to an integer
+                       // to take advantage of PHP's conversion rules which will turn "  200 OK" into 200.
+                       // http://php.net/string#language.types.string.conversion
+                       $n = intval( trim( $this->mResponseCode ) );
                        HttpStatus::header( $n );
                }
 
@@ -246,7 +250,7 @@ class AjaxResponse {
                                $ismodsince >= $wgCacheEpoch
                        ) {
                                ini_set( 'zlib.output_compression', 0 );
-                               $this->setResponseCode( "304 Not Modified" );
+                               $this->setResponseCode( 304 );
                                $this->disable();
                                $this->mLastModified = $lastmod;