[HttpClient] fix support for non-blocking resource streams
This commit is contained in:
parent
3415224479
commit
c651f63813
@ -37,6 +37,8 @@ class StreamWrapper
|
|||||||
/** @var resource|null */
|
/** @var resource|null */
|
||||||
private $handle;
|
private $handle;
|
||||||
|
|
||||||
|
private $blocking = true;
|
||||||
|
private $timeout;
|
||||||
private $eof = false;
|
private $eof = false;
|
||||||
private $offset = 0;
|
private $offset = 0;
|
||||||
|
|
||||||
@ -147,7 +149,7 @@ class StreamWrapper
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->client->stream([$this->response]) as $chunk) {
|
foreach ($this->client->stream([$this->response], $this->blocking ? $this->timeout : 0) as $chunk) {
|
||||||
try {
|
try {
|
||||||
$this->eof = true;
|
$this->eof = true;
|
||||||
$this->eof = !$chunk->isTimeout();
|
$this->eof = !$chunk->isTimeout();
|
||||||
@ -178,6 +180,19 @@ class StreamWrapper
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
|
||||||
|
{
|
||||||
|
if (STREAM_OPTION_BLOCKING === $option) {
|
||||||
|
$this->blocking = (bool) $arg1;
|
||||||
|
} elseif (STREAM_OPTION_READ_TIMEOUT === $option) {
|
||||||
|
$this->timeout = $arg1 + $arg2 / 1e6;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public function stream_tell(): int
|
public function stream_tell(): int
|
||||||
{
|
{
|
||||||
return $this->offset;
|
return $this->offset;
|
||||||
|
@ -75,4 +75,20 @@ abstract class HttpClientTestCase extends BaseHttpClientTestCase
|
|||||||
$response = $client->request('GET', 'http://localhost:8057/404');
|
$response = $client->request('GET', 'http://localhost:8057/404');
|
||||||
$stream = $response->toStream();
|
$stream = $response->toStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNonBlockingStream()
|
||||||
|
{
|
||||||
|
$client = $this->getHttpClient(__FUNCTION__);
|
||||||
|
$response = $client->request('GET', 'http://localhost:8057/timeout-body');
|
||||||
|
$stream = $response->toStream();
|
||||||
|
|
||||||
|
$this->assertTrue(stream_set_blocking($stream, false));
|
||||||
|
$this->assertSame('<1>', fread($stream, 8192));
|
||||||
|
$this->assertFalse(feof($stream));
|
||||||
|
|
||||||
|
$this->assertTrue(stream_set_blocking($stream, true));
|
||||||
|
$this->assertSame('<2>', fread($stream, 8192));
|
||||||
|
$this->assertSame('', fread($stream, 8192));
|
||||||
|
$this->assertTrue(feof($stream));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,10 @@ class MockHttpClientTest extends HttpClientTestCase
|
|||||||
|
|
||||||
return $client;
|
return $client;
|
||||||
|
|
||||||
|
case 'testNonBlockingStream':
|
||||||
|
$responses[] = new MockResponse((function () { yield '<1>'; yield ''; yield '<2>'; })(), ['response_headers' => $headers]);
|
||||||
|
break;
|
||||||
|
|
||||||
case 'testMaxDuration':
|
case 'testMaxDuration':
|
||||||
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
|
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
|
||||||
$mock->expects($this->any())
|
$mock->expects($this->any())
|
||||||
|
Reference in New Issue
Block a user