Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 78571fa

Browse files
committed
Merge branch 'hotfix/122' into develop
Forward port #122
2 parents 6c5e602 + 2757b26 commit 78571fa

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ All notable changes to this project will be documented in this file, in reverse
4040

4141
### Fixed
4242

43+
- [#122](https://github.com/zendframework/zend-http/pull/122) fixes an issue with the stream response whereby if the `outputstream`
44+
option is set, the output file was opened twice; it is now opened exactly once.
45+
4346
- [#147](https://github.com/zendframework/zend-http/pull/147) fixes an issue with header retrieval when the header line is malformed.
4447
Previously, an exception would be raised if a specific `HeaderInterface` implementation determined
4548
the header line was invalid. Now, `Header::has()` will return false for such headers, allowing

src/Client.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Client implements Stdlib\DispatchableInterface
6868
*/
6969
protected $streamName;
7070

71+
/**
72+
* @var resource|null
73+
*/
74+
protected $streamHandle = null;
75+
7176
/**
7277
* @var array of Header\SetCookie
7378
*/
@@ -925,11 +930,17 @@ public function send(Request $request = null)
925930
throw new Client\Exception\RuntimeException('Adapter does not support streaming');
926931
}
927932

933+
$this->streamHandle = null;
928934
// calling protected method to allow extending classes
929935
// to wrap the interaction with the adapter
930936
$response = $this->doRequest($uri, $method, $secure, $headers, $body);
937+
$stream = $this->streamHandle;
938+
$this->streamHandle = null;
931939

932940
if (! $response) {
941+
if ($stream !== null) {
942+
fclose($stream);
943+
}
933944
throw new Exception\RuntimeException('Unable to read response, or response is empty');
934945
}
935946

@@ -940,9 +951,11 @@ public function send(Request $request = null)
940951
}
941952

942953
if ($this->config['outputstream']) {
943-
$stream = $this->getStream();
944-
if (! is_resource($stream) && is_string($stream)) {
945-
$stream = fopen($stream, 'r');
954+
if ($stream === null) {
955+
$stream = $this->getStream();
956+
if (! is_resource($stream) && is_string($stream)) {
957+
$stream = fopen($stream, 'r');
958+
}
946959
}
947960
$streamMetaData = stream_get_meta_data($stream);
948961
if ($streamMetaData['seekable']) {
@@ -1424,8 +1437,8 @@ protected function doRequest(Http $uri, $method, $secure = false, $headers = [],
14241437

14251438
if ($this->config['outputstream']) {
14261439
if ($this->adapter instanceof Client\Adapter\StreamInterface) {
1427-
$stream = $this->openTempStream();
1428-
$this->adapter->setOutputStream($stream);
1440+
$this->streamHandle = $this->openTempStream();
1441+
$this->adapter->setOutputStream($this->streamHandle);
14291442
} else {
14301443
throw new Exception\RuntimeException('Adapter does not support streaming');
14311444
}

test/Client/StaticTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,36 @@ public function testOpenTempStreamWithValidFileDoesntThrowsException()
501501
// @todo verify link is still active
502502
}
503503

504+
/**
505+
* Test if a downloaded file can be deleted
506+
*
507+
* @group ZF-9685
508+
*/
509+
public function testDownloadedFileCanBeDeleted()
510+
{
511+
if (! getenv('TESTS_ZEND_HTTP_CLIENT_ONLINE')) {
512+
$this->markTestSkipped('Zend\Http\Client online tests are not enabled');
513+
}
514+
$url = 'http://www.example.com/';
515+
$outputFile = @tempnam(@sys_get_temp_dir(), 'zht');
516+
if (! is_file($outputFile)) {
517+
$this->markTestSkipped('Failed to create a temporary file');
518+
}
519+
$config = [
520+
'outputstream' => $outputFile,
521+
];
522+
$client = new HTTPClient($url, $config);
523+
524+
$result = $client->send();
525+
526+
$this->assertInstanceOf('Zend\Http\Response\Stream', $result);
527+
if (DIRECTORY_SEPARATOR === '\\') {
528+
$this->assertFalse(@unlink($outputFile), 'Deleting an open file should fail on Windows');
529+
}
530+
fclose($result->getStream());
531+
$this->assertTrue(@unlink($outputFile), 'Failed to delete downloaded file');
532+
}
533+
504534
/**
505535
* Testing if the connection can be closed
506536
*

0 commit comments

Comments
 (0)