Skip to content

Commit

Permalink
Request return a pointer to body. It will reduce the memory usage in …
Browse files Browse the repository at this point in the history
…big responses.
  • Loading branch information
jrbasso committed Dec 6, 2010
1 parent c4743a2 commit 97fe32f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions cake/libs/http_socket.php
Expand Up @@ -213,15 +213,16 @@ public function setProxyConfig($host, $port = 3128, $method = null, $user = null
* 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'])) {
Expand Down Expand Up @@ -354,7 +355,7 @@ public function request($request = array()) {
* @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'])) {
Expand Down Expand Up @@ -386,7 +387,7 @@ public function get($uri = null, $query = array(), $request = array()) {
* @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);
}
Expand All @@ -399,7 +400,7 @@ public function post($uri = null, $data = array(), $request = array()) {
* @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);
}
Expand All @@ -412,7 +413,7 @@ public function put($uri = null, $data = array(), $request = array()) {
* @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);
}
Expand Down
16 changes: 16 additions & 0 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -621,6 +621,22 @@ public function testRequest3() {
$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
*
Expand Down

0 comments on commit 97fe32f

Please sign in to comment.