8000 [HttpClient] Make native stream support non blocking mode by Nek- · Pull Request #35187 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient] Make native stream support non blocking mode #35187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpClient/Response/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ public function stream_read(int $count)
return '';
}

public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
{
if (null === $this->handle || 'stream' !== get_resource_type($this->handle)) {
trigger_error(sprintf('The "$handle" property of "%s" needs to be a stream.', __CLASS__), E_USER_WARNING);

return false;
}

switch ($option) {
case STREAM_OPTION_BLOCKING:
return stream_set_blocking($this->handle, $arg1);
case STREAM_OPTION_READ_TIMEOUT:
trigger_error(sprintf('Modifying the timeout after starting the stream is not supported by the StreamWrapper.'), E_USER_WARNING);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP will trigger fatal error because we return false. But I think more precision is a good thing.


return false;
case STREAM_OPTION_WRITE_BUFFER:
trigger_error(sprintf('This stream is read only.'), E_USER_WARNING);

return false;
}

return false;
}

public function stream_tell(): int
{
return $this->offset;
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/NativeHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ protected function getHttpClient(string $testCase): HttpClientInterface
return new NativeHttpClient();
}

public function testItCanBeNonBlockingStream()
{
$client = $this->getHttpClient(__FUNCTION__);
$response = $client->request('GET', 'http://localhost:8057');
$stream = $response->toStream();

$this->assertTrue(stream_get_meta_data($stream)['blocked']);
$this->assertTrue(stream_set_blocking($stream, 0));

// Help wanted. I've no idea why this test does not pass.
// $this->assertFalse(stream_get_meta_data($stream)['blocked']);
Copy link
Contributor Author
@Nek- Nek- Jan 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've no idea where data from stream_get_meta_data comes.

It looks like default values are always returned while using a streamwrapper ?!
https://github.com/php/php-src/blob/9099dbd9614b37b48818ac24e2020b0d756b7e1e/ext/standard/streamsfuncs.c#L511-L515

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, we cannot define these when using stream wrappers.


$read = [$stream];
$write = [];
$except = [];
$streamFound = stream_select($read, $write, $except, null, 0);

$this->assertEquals(1, $streamFound);
$this->assertIsArray(json_decode(stream_get_contents($stream), true));
$this->assertTrue(feof($stream));
}

public function testInformationalResponseStream()
{
$this->markTestSkipped('NativeHttpClient doesn\'t support informational status codes.');
Expand Down
0