Skip to content

Commit

Permalink
ZF-8057: fixed missing check for existing object instance, added test…
Browse files Browse the repository at this point in the history
… for that

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19308 44c647ce-9c0f-0410-b52a-842ac1e357ba
  • Loading branch information
bate committed Nov 30, 2009
1 parent ee7c067 commit 88b2ea2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
86 changes: 44 additions & 42 deletions library/Zend/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Zend_Http_Client
*
* @var Zend_Uri_Http
*/
protected $uri;
protected $uri = null;

/**
* Associative array of request headers
Expand Down Expand Up @@ -548,8 +548,10 @@ public function setAuth($user, $password = '', $type = self::AUTH_BASIC)
$this->auth = null;

// Clear the auth information in the uri instance as well
$this->getUri()->setUsername('');
$this->getUri()->setPassword('');
if ($this->uri instanceof Zend_Uri_Http) {
$this->getUri()->setUsername('');
$this->getUri()->setPassword('');
}
// Else, set up authentication
} else {
// Check we got a proper authentication type
Expand Down Expand Up @@ -739,7 +741,7 @@ public function setEncType($enctype = self::ENC_URLENCODED)
* 1. For advanced user who would like to set their own data, already encoded
* 2. For backwards compatibilty: If someone uses the old post($data) method.
* this method will be used to set the encoded data.
*
*
* $data can also be stream (such as file) from which the data will be read.
*
* @param string|resource $data
Expand All @@ -765,7 +767,7 @@ public function setRawData($data, $enctype = null)
*
* Should be used to reset the request parameters if the client is
* used for several concurrent requests.
*
*
* clearAll parameter controls if we clean just parameters or also
* headers and last_*
*
Expand All @@ -785,13 +787,13 @@ public function resetParameters($clearAll = false)
$this->last_request = null;
$this->last_response = null;
} else {
// Clear outdated headers
if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) {
unset($this->headers[strtolower(self::CONTENT_TYPE)]);
}
if (isset($this->headers[strtolower(self::CONTENT_LENGTH)])) {
unset($this->headers[strtolower(self::CONTENT_LENGTH)]);
}
// Clear outdated headers
if (isset($this->headers[strtolower(self::CONTENT_TYPE)])) {
unset($this->headers[strtolower(self::CONTENT_TYPE)]);
}
if (isset($this->headers[strtolower(self::CONTENT_LENGTH)])) {
unset($this->headers[strtolower(self::CONTENT_LENGTH)]);
}
}

return $this;
Expand Down Expand Up @@ -871,7 +873,7 @@ public function getAdapter()

/**
* Set streaming for received data
*
*
* @param string|boolean $streamfile Stream file, true for temp file, false/null for no streaming
* @return Zend_Http_Client
*/
Expand All @@ -880,7 +882,7 @@ public function setStream($streamfile = true)
$this->setConfig(array("output_stream" => $streamfile));
return $this;
}

/**
* Get status of streaming for received data
* @return boolean|string
Expand All @@ -889,10 +891,10 @@ public function getStream()
{
return $this->config["output_stream"];
}

/**
* Create temporary stream
*
*
* @return resource
*/
protected function _openTempStream()
Expand All @@ -903,13 +905,13 @@ protected function _openTempStream()
$this->_stream_name = tempnam(isset($this->config['stream_tmp_dir'])?$this->config['stream_tmp_dir']:sys_get_temp_dir(),
'Zend_Http_Client');
}

$fp = fopen($this->_stream_name, "w+b");
if(!$fp) {
$this->close();
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception("Could not open temp file $name");

}
return $fp;
}
Expand Down Expand Up @@ -963,7 +965,7 @@ public function request($method = null)
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Adapter does not support streaming');
}

// Open the connection, send the request and read the response
$this->adapter->connect($uri->getHost(), $uri->getPort(),
($uri->getScheme() == 'https' ? true : false));
Expand All @@ -973,36 +975,36 @@ public function request($method = null)
$stream = $this->_openTempStream();
$this->adapter->setOutputStream($stream);
} else {
/** @see Zend_Http_Client_Exception */
/** @see Zend_Http_Client_Exception */
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Adapter does not support streaming');
}
}
}

$this->last_request = $this->adapter->write($this->method,
$uri, $this->config['httpversion'], $headers, $body);

$response = $this->adapter->read();
if (! $response) {
/** @see Zend_Http_Client_Exception */
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
}
if($this->config['output_stream']) {
rewind($stream);
// cleanup the adapter
$this->adapter->setOutputStream(null);
$response = Zend_Http_Response_Stream::fromStream($response, $stream);
$response->setStreamName($this->_stream_name);
if(!is_string($this->config['output_stream'])) {
// we used temp name, will need to clean up
$response->setCleanup(true);
}
} else {
$response = Zend_Http_Response::fromString($response);
if (! $response) {
/** @see Zend_Http_Client_Exception */
require_once 'Zend/Http/Client/Exception.php';
throw new Zend_Http_Client_Exception('Unable to read response, or response is empty');
}

if($this->config['output_stream']) {
rewind($stream);
// cleanup the adapter
$this->adapter->setOutputStream(null);
$response = Zend_Http_Response_Stream::fromStream($response, $stream);
$response->setStreamName($this->_stream_name);
if(!is_string($this->config['output_stream'])) {
// we used temp name, will need to clean up
$response->setCleanup(true);
}
} else {
$response = Zend_Http_Response::fromString($response);
}

if ($this->config['storeresponse']) {
$this->last_response = $response;
}
Expand Down Expand Up @@ -1156,7 +1158,7 @@ protected function _prepareBody()
if ($this->method == self::TRACE) {
return '';
}

if (isset($this->raw_post_data) && is_resource($this->raw_post_data)) {
return $this->raw_post_data;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Zend/Http/Client/StaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ public function testMultibyteRawPostDataZF2098()
$this->assertEquals(filesize($bodyFile), (int) $match[1]);
}

/**
* @group ZF-8057
*/
public function testSetDisabledAuthBeforSettingUriBug()
{
$client = new Zend_Http_Client_StaticTest_Mock();
// if the bug exists this call should creates a fatal error
$client->setAuth(false);
}

/**
* Data providers
*/
Expand Down

0 comments on commit 88b2ea2

Please sign in to comment.