Skip to content

Commit ec183a5

Browse files
Merge branch '5.4' into 6.0
* 5.4: Update bootstrap_5_layout.html.twig [HttpClient] Fix seeking in not-yet-initialized requests [Serializer] Allow getting discriminated type by class name
2 parents 2067d3c + 8a3929c commit ec183a5

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

Response/StreamWrapper.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323
class StreamWrapper
2424
{
25-
/** @var resource|string|null */
25+
/** @var resource|null */
2626
public $context;
2727

2828
private $client;
2929

3030
private $response;
3131

32-
/** @var resource|null */
32+
/** @var resource|string|null */
3333
private $content;
3434

3535
/** @var resource|null */
@@ -38,7 +38,7 @@ class StreamWrapper
3838
private bool $blocking = true;
3939
private ?float $timeout = null;
4040
private bool $eof = false;
41-
private int $offset = 0;
41+
private ?int $offset = 0;
4242

4343
/**
4444
* Creates a PHP stream resource from a ResponseInterface.
@@ -87,6 +87,7 @@ public function bindHandles(&$handle, &$content): void
8787
{
8888
$this->handle = &$handle;
8989
$this->content = &$content;
90+
$this->offset = null;
9091
}
9192

9293
public function stream_open(string $path, string $mode, int $options): bool
@@ -131,7 +132,7 @@ public function stream_read(int $count)
131132
}
132133
}
133134

134-
if (0 !== fseek($this->content, $this->offset)) {
135+
if (0 !== fseek($this->content, $this->offset ?? 0)) {
135136
return false;
136137
}
137138

@@ -160,6 +161,11 @@ public function stream_read(int $count)
160161
try {
161162
$this->eof = true;
162163
$this->eof = !$chunk->isTimeout();
164+
165+
if (!$this->eof && !$this->blocking) {
166+
return '';
167+
}
168+
163169
$this->eof = $chunk->isLast();
164170

165171
if ($chunk->isFirst()) {
@@ -202,7 +208,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
202208

203209
public function stream_tell(): int
204210
{
205-
return $this->offset;
211+
return $this->offset ?? 0;
206212
}
207213

208214
public function stream_eof(): bool
@@ -212,14 +218,19 @@ public function stream_eof(): bool
212218

213219
public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
214220
{
221+
if (null === $this->content && null === $this->offset) {
222+
$this->response->getStatusCode();
223+
$this->offset = 0;
224+
}
225+
215226
if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, \SEEK_END)) {
216227
return false;
217228
}
218229

219230
$size = ftell($this->content);
220231

221232
if (\SEEK_CUR === $whence) {
222-
$offset += $this->offset;
233+
$offset += $this->offset ?? 0;
223234
}
224235

225236
if (\SEEK_END === $whence || $size < $offset) {

Tests/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ public function testNonBlockingStream()
131131
$this->assertTrue(feof($stream));
132132
}
133133

134+
public function testSeekAsyncStream()
135+
{
136+
$client = $this->getHttpClient(__FUNCTION__);
137+
$response = $client->request('GET', 'http://localhost:8057/timeout-body');
138+
$stream = $response->toStream(false);
139+
140+
$this->assertSame(0, fseek($stream, 0, \SEEK_CUR));
141+
$this->assertSame('<1>', fread($stream, 8192));
142+
$this->assertFalse(feof($stream));
143+
$this->assertSame('<2>', stream_get_contents($stream));
144+
}
145+
134146
public function testResponseStreamRewind()
135147
{
136148
$client = $this->getHttpClient(__FUNCTION__);

Tests/MockHttpClientTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
443443
return $client;
444444

445445
case 'testNonBlockingStream':
446+
case 'testSeekAsyncStream':
446447
$responses[] = new MockResponse((function () { yield '<1>'; yield ''; yield '<2>'; })(), ['response_headers' => $headers]);
447448
break;
448449

0 commit comments

Comments
 (0)