From bff36836abcb350956db37920d06b83c03b9e5fa Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Fri, 29 Jan 2010 07:25:09 +0000 Subject: [PATCH] =?utf8?q?follow=20up=20r61357=20*=20fix=20up=20fopen=20wa?= =?utf8?q?rnings=20=E2=80=94=20I=20was=20bedazzled=20by=20phpUnit's=20=20?= =?utf8?q?=20set=5Ferror=5Fhandler=20that=20turned=20warnings=20into=20exc?= =?utf8?q?eptions=20and=20Tim=20set=20=20=20me=20right.=20*=20Use=20Tim's?= =?utf8?q?=20read=20loop=20*=20No=20need=20for=20private=20$fh=20member=20?= =?utf8?q?*=20Fix=20some=20minor=20problems=20with=20PHP=20versions=20(no?= =?utf8?q?=20"timeout"=20param=20in=20=20=20pre-5.2.1,=20POST=20issue=20in?= =?utf8?q?=20Hardy's=20PHP=20version=20where=20timeout=20is=20null.)=20*?= =?utf8?q?=20Fix=20how=20tests=20are=20run=20on=20old=20versions=20of=20ph?= =?utf8?q?pUnit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- includes/HttpFunctions.php | 52 +++++++++++++++++-------------- languages/messages/MessagesEn.php | 2 +- tests/HttpTest.php | 16 ++++++---- tests/bootstrap.php | 24 +++++++++++--- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/includes/HttpFunctions.php b/includes/HttpFunctions.php index 8070b01cfd..8a8dc504d9 100644 --- a/includes/HttpFunctions.php +++ b/includes/HttpFunctions.php @@ -184,7 +184,7 @@ class HttpRequest { case 'php': if ( !wfIniGetBool( 'allow_url_fopen' ) ) { throw new MWException( __METHOD__.': allow_url_fopen needs to be enabled for pure PHP http requests to work. '. - 'If possible, curl should be used instead. See http://php.net/curl.' ); + 'If possible, curl should be used instead. See http://php.net/curl.' ); } return new PhpHttpRequest( $url, $options ); default: @@ -365,8 +365,6 @@ class CurlHttpRequest extends HttpRequest { } class PhpHttpRequest extends HttpRequest { - private $fh; - protected function urlToTcp( $url ) { $parsedUrl = parse_url( $url ); @@ -376,7 +374,7 @@ class PhpHttpRequest extends HttpRequest { public function execute() { if ( $this->parsedUrl['scheme'] != 'http' ) { $this->status->fatal( 'http-invalid-scheme', $this->parsedURL['scheme'] ); - } + } parent::execute(); if ( !$this->status->isOK() ) { @@ -399,26 +397,33 @@ class PhpHttpRequest extends HttpRequest { $options['method'] = $this->method; $options['timeout'] = $this->timeout; $options['header'] = implode("\r\n", $this->getHeaderList()); - // FOR NOW: Force everyone to HTTP 1.0 - /* if ( version_compare( "5.3.0", phpversion(), ">" ) ) { */ - $options['protocol_version'] = "1.0"; - /* } else { */ - /* $options['protocol_version'] = "1.1"; */ - /* } */ + // Note that at some future point we may want to support + // HTTP/1.1, but we'd have to write support for chunking + // in version of PHP < 5.3.1 + $options['protocol_version'] = "1.0"; if ( $this->postData ) { $options['content'] = $this->postData; } + $oldTimeout = false; + if ( version_compare( '5.2.1', phpversion(), '>' ) ) { + $oldTimeout = ini_set('default_socket_timeout', $this->timeout); + } + $context = stream_context_create( array( 'http' => $options ) ); - try { - $this->fh = fopen( $this->url, "r", false, $context ); - } catch ( Exception $e ) { - $this->status->fatal( $e->getMessage() ); /* need some l10n help */ + wfSuppressWarnings(); + $fh = fopen( $this->url, "r", false, $context ); + wfRestoreWarnings(); + if ( $oldTimeout !== false ) { + ini_set('default_socket_timeout', $oldTimeout); + } + if ( $fh === false ) { + $this->status->fatal( 'http-request-error' ); return $this->status; } - $result = stream_get_meta_data( $this->fh ); + $result = stream_get_meta_data( $fh ); if ( $result['timed_out'] ) { $this->status->fatal( 'http-timed-out', $this->url ); return $this->status; @@ -426,16 +431,17 @@ class PhpHttpRequest extends HttpRequest { $this->headers = $result['wrapper_data']; - $end = false; - while ( !$end ) { - $contents = fread( $this->fh, 8192 ); - $size = 0; - if ( $contents ) { - $size = call_user_func_array( $this->callback, array( $this->fh, $contents ) ); + while ( !feof( $fh ) ) { + $buf = fread( $fh, 8192 ); + if ( $buf === false ) { + $this->status->fatal( 'http-read-error' ); + break; + } + if ( strlen( $buf ) ) { + call_user_func( $this->callback, $fh, $buf ); } - $end = ( $size == 0 ) || feof( $this->fh ); } - fclose( $this->fh ); + fclose( $fh ); return $this->status; } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 013302e209..86b04b1919 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2154,7 +2154,7 @@ For optimal security, img_auth.php is disabled.', # HTTP errors 'http-invalid-url' => 'Invalid URL: $1', 'http-invalid-scheme' => 'URLs with the "$1" scheme are not supported', -'http-request-error' => 'Error sending request:', +'http-request-error' => 'Unknown error sending request.', # Some likely curl errors. More could be added from 'upload-curl-error6' => 'Could not reach URL', diff --git a/tests/HttpTest.php b/tests/HttpTest.php index 4d0731f3fd..2a1037f4be 100644 --- a/tests/HttpTest.php +++ b/tests/HttpTest.php @@ -1,5 +1,9 @@ markTestIncomplete("This test requires the curl binary at /usr/bin/curl. If you have curl, please file a bug on this test, or, better yet, provide a patch."); + $this->markTestIncomplete("This test requires the curl binary at /usr/bin/curl. If you have curl, please file a bug on this test, or, better yet, provide a patch."); } $content = tempnam( sys_get_temp_dir(), "" ); @@ -65,7 +69,7 @@ class HttpTest extends PhpUnit_Framework_TestCase { function testInstantiation() { Http::$httpEngine = false; - $r = HttpRequest::factory("http://www.example.com/"); + $r = HttpRequest::factory("http://www.example.com/"); if ( self::$has_curl ) { $this->assertThat($r, $this->isInstanceOf( 'CurlHttpRequest' )); } else { @@ -120,7 +124,7 @@ class HttpTest extends PhpUnit_Framework_TestCase { } /* ./phase3/includes/Import.php:1108: $data = Http::request( $method, $url ); */ - /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */ + /* ./includes/Import.php:1124: $link = Title::newFromText( "$interwiki:Special:Export/$page" ); */ /* ./includes/Import.php:1134: return ImportStreamSource::newFromURL( $url, "POST" ); */ function runHTTPRequests($proxy=null) { $opt = array(); @@ -160,8 +164,8 @@ class HttpTest extends PhpUnit_Framework_TestCase { /* ./extensions/BookInformation/drivers/IsbnDb.php:24: if( ( $xml = Http::get( $uri ) ) !== false ) { */ /* ./extensions/BookInformation/drivers/Amazon.php:23: if( ( $xml = Http::get( $uri ) ) !== false ) { */ /* ./extensions/TitleBlacklist/TitleBlacklist.list.php:217: $result = Http::get( $url ); */ - /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */ - /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */ + /* ./extensions/TSPoll/TSPoll.php:68: $get_server = Http::get( 'http://toolserver.org/~jan/poll/dev/main.php?page=wiki_output&id='.$id ); */ + /* ./extensions/TSPoll/TSPoll.php:70: $get_server = Http::get( 'http://toolserver.org/~jan/poll/main.php?page=wiki_output&id='.$id ); */ /* ./extensions/DoubleWiki/DoubleWiki.php:56: $translation = Http::get( $url.$sep.'action=render' ); */ /* ./extensions/ExternalPages/ExternalPages_body.php:177: $serializedText = Http::get( $this->mPageURL ); */ /* ./extensions/Translate/utils/TranslationHelpers.php:143: $suggestions = Http::get( $url, $timeout ); */ @@ -198,7 +202,7 @@ class HttpTest extends PhpUnit_Framework_TestCase { /* ./extensions/LocalisationUpdate/LocalisationUpdate.class.php:172: $basefilecontents = Http::get( $basefile ); */ /* ./extensions/APC/SpecialAPC.php:245: $rss = Http::get( 'http://pecl.php.net/feeds/pkg_apc.rss' ); */ /* ./extensions/Interlanguage/Interlanguage.php:56: $a = Http::get( $url ); */ - /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */ + /* ./extensions/MWSearch/MWSearch_body.php:492: $data = Http::get( $searchUrl, $wgLuceneSearchTimeout, $httpOpts); */ function runHTTPGets($proxy=null) { $opt = array(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1023f06278..3f0e6bed92 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -13,12 +13,26 @@ $IP = dirname( dirname( __FILE__ ) ); define( 'MEDIAWIKI', true ); define( 'MW_PHPUNIT_TEST', true ); -ini_set( 'include_path', "$IP:" .ini_get( 'include_path' ) ); - -require "$IP/includes/Defines.php"; -require "$IP/includes/AutoLoader.php"; -require "$IP/LocalSettings.php"; +require_once "$IP/includes/Defines.php"; +require_once "$IP/includes/AutoLoader.php"; +require_once "$IP/LocalSettings.php"; require_once "$IP/includes/ProfilerStub.php"; require_once "$IP/includes/GlobalFunctions.php"; require_once "$IP/includes/Hooks.php"; + +// for php versions before 5.2.1 +if ( !function_exists('sys_get_temp_dir')) { + function sys_get_temp_dir() { + if( $temp=getenv('TMP') ) return $temp; + if( $temp=getenv('TEMP') ) return $temp; + if( $temp=getenv('TMPDIR') ) return $temp; + $temp=tempnam(__FILE__,''); + if (file_exists($temp)) { + unlink($temp); + return dirname($temp); + } + return null; + } + } + -- 2.20.1