mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Support to download requests.
This commit is contained in:
parent
453c5364c2
commit
eeafb55d31
2 changed files with 73 additions and 1 deletions
|
@ -132,6 +132,13 @@ class HttpSocket extends CakeSocket {
|
||||||
*/
|
*/
|
||||||
protected $_proxy = array();
|
protected $_proxy = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resource to receive the content of request
|
||||||
|
*
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
protected $_contentResource = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build an HTTP Socket using the specified configuration.
|
* Build an HTTP Socket using the specified configuration.
|
||||||
*
|
*
|
||||||
|
@ -208,6 +215,24 @@ class HttpSocket extends CakeSocket {
|
||||||
$this->_proxy = compact('host', 'port', 'method', 'user', 'pass');
|
$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
|
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
|
||||||
* method and provide a more granular interface.
|
* method and provide a more granular interface.
|
||||||
|
@ -320,8 +345,27 @@ class HttpSocket extends CakeSocket {
|
||||||
$this->write($this->request['raw']);
|
$this->write($this->request['raw']);
|
||||||
|
|
||||||
$response = null;
|
$response = null;
|
||||||
|
$inHeader = true;
|
||||||
while ($data = $this->read()) {
|
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') {
|
if ($connectionType === 'close') {
|
||||||
|
|
|
@ -638,6 +638,34 @@ class HttpSocketTest extends CakeTestCase {
|
||||||
$this->assertEqual($data, $this->Socket->response['body']);
|
$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
|
* testProxy method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue