Navigation Menu

Skip to content

Commit

Permalink
Support to download requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 11, 2010
1 parent 453c536 commit eeafb55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
46 changes: 45 additions & 1 deletion cake/libs/http_socket.php
Expand Up @@ -132,6 +132,13 @@ class HttpSocket extends CakeSocket {
*/
protected $_proxy = array();

/**
* Resource to receive the content of request
*
* @var mixed
*/
protected $_contentResource = null;

/**
* Build an HTTP Socket using the specified configuration.
*
Expand Down Expand Up @@ -208,6 +215,24 @@ public function setProxyConfig($host, $port = 3128, $method = null, $user = null
$this->_proxy = compact('host', 'port', 'method', 'user', 'pass');
}

/**
* Set the resource to receive the request content. This resource must support fwrite.
*
* @param mixed $resource Resource or false to disable the resource use
* @return void
* @throw Exception
*/
public function setContentResource($resource) {
if ($resource === false) {
$this->_contentResource = null;
return;
}
if (!is_resource($resource)) {
throw new Exception(__('Invalid resource.'));
}
$this->_contentResource = $resource;
}

/**
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
* method and provide a more granular interface.
Expand Down Expand Up @@ -320,8 +345,27 @@ public function &request($request = array()) {
$this->write($this->request['raw']);

$response = null;
$inHeader = true;
while ($data = $this->read()) {
$response .= $data;
if ($this->_contentResource) {
if ($inHeader) {
$response .= $data;
$pos = strpos($response, "\r\n\r\n");
if ($pos !== false) {
$pos += 4;
$data = substr($response, $pos);
fwrite($this->_contentResource, $data);

$response = substr($response, 0, $pos);
$inHeader = false;
}
} else {
fwrite($this->_contentResource, $data);
fflush($this->_contentResource);
}
} else {
$response .= $data;
}
}

if ($connectionType === 'close') {
Expand Down
28 changes: 28 additions & 0 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -638,6 +638,34 @@ public function testRequestResultAsReference() {
$this->assertEqual($data, $this->Socket->response['body']);
}

/**
* testRequestWithResource
*
* @return void
*/
public function testRequestWithResource() {
$serverResponse = "HTTP/1.x 200 OK\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 test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->connected = true;

$f = fopen(TMP . 'download.txt', 'w');
$this->skipUnless($f, 'Can not write in TMP directory.');

$this->Socket->setContentResource($f);
$result = $this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($result, '');
$this->assertEqual($this->Socket->response['header']['Server'], 'CakeHttp Server');
fclose($f);
$this->assertEqual(file_get_contents(TMP . 'download.txt'), '<h1>This is a test!</h1>');
unlink(TMP . 'download.txt');

$this->Socket->setContentResource(false);
$result = $this->Socket->request('http://www.cakephp.org/');
$this->assertEqual($result, '<h1>This is a test!</h1>');
}

/**
* testProxy method
*
Expand Down

0 comments on commit eeafb55

Please sign in to comment.