Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c651f63

Browse filesBrowse files
[HttpClient] fix support for non-blocking resource streams
1 parent 3415224 commit c651f63
Copy full SHA for c651f63

File tree

Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed

‎src/Symfony/Component/HttpClient/Response/StreamWrapper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/StreamWrapper.php
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class StreamWrapper
3737
/** @var resource|null */
3838
private $handle;
3939

40+
private $blocking = true;
41+
private $timeout;
4042
private $eof = false;
4143
private $offset = 0;
4244

@@ -147,7 +149,7 @@ public function stream_read(int $count)
147149
return $data;
148150
}
149151

150-
foreach ($this->client->stream([$this->response]) as $chunk) {
152+
foreach ($this->client->stream([$this->response], $this->blocking ? $this->timeout : 0) as $chunk) {
151153
try {
152154
$this->eof = true;
153155
$this->eof = !$chunk->isTimeout();
@@ -178,6 +180,19 @@ public function stream_read(int $count)
178180
return '';
179181
}
180182

183+
public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
184+
{
185+
if (STREAM_OPTION_BLOCKING === $option) {
186+
$this->blocking = (bool) $arg1;
187+
} elseif (STREAM_OPTION_READ_TIMEOUT === $option) {
188+
$this->timeout = $arg1 + $arg2 / 1e6;
189+
} else {
190+
return false;
191+
}
192+
193+
return true;
194+
}
195+
181196
public function stream_tell(): int
182197
{
183198
return $this->offset;

‎src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,20 @@ public function testToStream404()
7575
$response = $client->request('GET', 'http://localhost:8057/404');
7676
$stream = $response->toStream();
7777
}
78+
79+
public function testNonBlockingStream()
80+
{
81+
$client = $this->getHttpClient(__FUNCTION__);
82+
$response = $client->request('GET', 'http://localhost:8057/timeout-body');
83+
$stream = $response->toStream();
84+
85+
$this->assertTrue(stream_set_blocking($stream, false));
86+
$this->assertSame('<1>', fread($stream, 8192));
87+
$this->assertFalse(feof($stream));
88+
89+
$this->assertTrue(stream_set_blocking($stream, true));
90+
$this->assertSame('<2>', fread($stream, 8192));
91+
$this->assertSame('', fread($stream, 8192));
92+
$this->assertTrue(feof($stream));
93+
}
7894
}

‎src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ protected function getHttpClient(string $testCase): HttpClientInterface
171171

172172
return $client;
173173

174+
case 'testNonBlockingStream':
175+
$responses[] = new MockResponse((function () { yield '<1>'; yield ''; yield '<2>'; })(), ['response_headers' => $headers]);
176+
break;
177+
174178
case 'testMaxDuration':
175179
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
176180
$mock->expects($this->any())

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.