r64774 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64773‎ | r64774 | r64775 >
Date:05:25, 9 April 2010
Author:mah
Status:ok (Comments)
Tags:
Comment:
* Fix a bug to keep consecutive HTTP requests from sharing results
* Update Login API
Modified paths:
  • /trunk/phase3/includes/HttpFunctions.php (modified) (history)
  • /trunk/phase3/includes/WebRequest.php (modified) (history)
  • /trunk/phase3/maintenance/tests/ApiTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/ApiTest.php
@@ -112,8 +112,24 @@
113113 $sxe = simplexml_load_string( $resp );
114114 $this->assertNotType( "bool", $sxe );
115115 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
116 - $a = $sxe->login[0]->attributes()->result;
117 - $this->assertEquals( ' result="WrongPass"', $a->asXML() );
 116+ $a = $sxe->login[0]->attributes()->result[0];
 117+ $this->assertEquals( ' result="NeedToken"', $a->asXML() );
 118+
 119+ $token = (string)$sxe->login[0]->attributes()->token;
 120+
 121+ $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
 122+ array( "postData" => array(
 123+ "lgtoken" => $token,
 124+ "lgname" => self::$userName,
 125+ "lgpassword" => "bad" ) ) );
 126+
 127+
 128+ $sxe = simplexml_load_string( $resp );
 129+ $this->assertNotType( "bool", $sxe );
 130+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
 131+ $a = $sxe->login[0]->attributes()->result[0];
 132+
 133+ $this->assertEquals( ' result="NeedToken"', $a->asXML() );
118134 }
119135
120136 function testApiLoginGoodPass() {
@@ -125,15 +141,34 @@
126142 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
127143 'be set in LocalSettings.php');
128144 }
129 - $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
130 - array( "postData" => array(
131 - "lgname" => self::$userName,
132 - "lgpassword" => self::$passWord ) ) );
 145+ $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
 146+ array( "method" => "POST",
 147+ "postData" => array(
 148+ "lgname" => self::$userName,
 149+ "lgpassword" => self::$passWord ) ) );
 150+ $req->execute();
 151+
133152 libxml_use_internal_errors( true );
134 - $sxe = simplexml_load_string( $resp );
 153+ $sxe = simplexml_load_string( $req->getContent() );
135154 $this->assertNotType( "bool", $sxe );
136155 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
137 - $a = $sxe->login[0]->attributes()->result;
 156+
 157+ $a = $sxe->login[0]->attributes()->result[0];
 158+ $this->assertEquals( ' result="NeedToken"', $a->asXML() );
 159+ $token = (string)$sxe->login[0]->attributes()->token;
 160+
 161+ $req->setData(array(
 162+ "lgtoken" => $token,
 163+ "lgname" => self::$userName,
 164+ "lgpassword" => self::$passWord ) );
 165+ $req->execute();
 166+
 167+ $sxe = simplexml_load_string( $req->getContent() );
 168+
 169+ $this->assertNotType( "bool", $sxe );
 170+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
 171+ $a = $sxe->login[0]->attributes()->result[0];
 172+
138173 $this->assertEquals( ' result="Success"', $a->asXML() );
139174 }
140175
@@ -146,11 +181,28 @@
147182 $this->markTestIncomplete('This test needs $wgServerName and $wgServer to '.
148183 'be set in LocalSettings.php');
149184 }
150 - $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
151 - array( "method" => "POST",
152 - "postData" => array( "lgname" => self::$userName,
153 - "lgpassword" => self::$passWord ) ) );
 185+ $req = HttpRequest::factory(self::$apiUrl . "?action=login&format=xml",
 186+ array( "method" => "POST",
 187+ "postData" => array(
 188+ "lgname" => self::$userName,
 189+ "lgpassword" => self::$passWord ) ) );
154190 $req->execute();
 191+
 192+ libxml_use_internal_errors( true );
 193+ $sxe = simplexml_load_string( $req->getContent() );
 194+ $this->assertNotType( "bool", $sxe );
 195+ $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
 196+
 197+ $a = $sxe->login[0]->attributes()->result[0];
 198+ $this->assertEquals( ' result="NeedToken"', $a->asXML() );
 199+ $token = (string)$sxe->login[0]->attributes()->token;
 200+
 201+ $req->setData(array(
 202+ "lgtoken" => $token,
 203+ "lgname" => self::$userName,
 204+ "lgpassword" => self::$passWord ) );
 205+ $req->execute();
 206+
155207 $cj = $req->getCookieJar();
156208 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
157209 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
Index: trunk/phase3/includes/WebRequest.php
@@ -774,7 +774,7 @@
775775 }
776776
777777 public function setSessionData( $key, $data ) {
778 - $this->notImplemented( __METHOD__ );
 778+ $this->session[$key] = $data;
779779 }
780780
781781 public function isPathInfoBad() {
Index: trunk/phase3/includes/HttpFunctions.php
@@ -212,6 +212,15 @@
213213 }
214214
215215 /**
 216+ * Set the parameters of the request
 217+ * @param $params array
 218+ * @todo overload the args param
 219+ */
 220+ public function setData($args) {
 221+ $this->postData = $args;
 222+ }
 223+
 224+ /**
216225 * Take care of setting up the proxy
217226 * (override in subclass)
218227 * @return string
@@ -296,6 +305,8 @@
297306 public function execute() {
298307 global $wgTitle;
299308
 309+ $this->content = "";
 310+
300311 if( strtoupper($this->method) == "HEAD" ) {
301312 $this->headersOnly = true;
302313 }

Comments

#Comment by Duplicatebug (talk | contribs)   17:39, 1 April 2011

It is bad, when the content defaults to an empty string, because some caller only check for false.

From Import.php

		$data = Http::request( $method, $url );
		if( $data !== false ) {

When the url is a redirect and that redirect is not automatically resolved (see r67684), than the status is OK and Http::request returns (correctly) the content and that is the default empty string, but there should be no content.

Status & tagging log