Request return a pointer to body. It will reduce the memory usage in big responses.

This commit is contained in:
Juan Basso 2010-12-05 23:36:28 -02:00
parent c4743a2438
commit 97fe32f87c
2 changed files with 24 additions and 7 deletions

View file

@ -213,15 +213,16 @@ class HttpSocket extends CakeSocket {
* method and provide a more granular interface.
*
* @param mixed $request Either an URI string, or an array defining host/uri
* @return mixed false on error, request body on success
* @return mixed null on error, reference to request body on success
*/
public function request($request = array()) {
public function &request($request = array()) {
$this->reset(false);
if (is_string($request)) {
$request = array('uri' => $request);
} elseif (!is_array($request)) {
return false;
$return = false;
return $return;
}
if (!isset($request['uri'])) {
@ -354,7 +355,7 @@ class HttpSocket extends CakeSocket {
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request, either false on failure or the response to the request.
*/
public function get($uri = null, $query = array(), $request = array()) {
public function &get($uri = null, $query = array(), $request = array()) {
if (!empty($query)) {
$uri = $this->_parseUri($uri);
if (isset($uri['query'])) {
@ -386,7 +387,7 @@ class HttpSocket extends CakeSocket {
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request, either false on failure or the response to the request.
*/
public function post($uri = null, $data = array(), $request = array()) {
public function &post($uri = null, $data = array(), $request = array()) {
$request = Set::merge(array('method' => 'POST', 'uri' => $uri, 'body' => $data), $request);
return $this->request($request);
}
@ -399,7 +400,7 @@ class HttpSocket extends CakeSocket {
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request
*/
public function put($uri = null, $data = array(), $request = array()) {
public function &put($uri = null, $data = array(), $request = array()) {
$request = Set::merge(array('method' => 'PUT', 'uri' => $uri, 'body' => $data), $request);
return $this->request($request);
}
@ -412,7 +413,7 @@ class HttpSocket extends CakeSocket {
* @param array $request An indexed array with indexes such as 'method' or uri
* @return mixed Result of request
*/
public function delete($uri = null, $data = array(), $request = array()) {
public function &delete($uri = null, $data = array(), $request = array()) {
$request = Set::merge(array('method' => 'DELETE', 'uri' => $uri, 'body' => $data), $request);
return $this->request($request);
}

View file

@ -621,6 +621,22 @@ class HttpSocketTest extends CakeTestCase {
$this->assertFalse($this->Socket->connected);
}
/**
* testRequestResultAsPointer method
*
* @return void
*/
public function testRequestResultAsPointer() {
$request = array('uri' => 'htpp://www.cakephp.org/');
$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a cookie test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->connected = true;
$data =& $this->Socket->request($request);
$this->assertEqual($data, $this->Socket->response['body']);
$data = 'new data';
$this->assertEqual($data, $this->Socket->response['body']);
}
/**
* testProxy method
*