Skip to content

[HttpClient] Fix seeking in not-yet-initialized requests #47808

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

Merged
merged 1 commit into from
Oct 11, 2022
Merged
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
21 changes: 16 additions & 5 deletions src/Symfony/Component/HttpClient/Response/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class StreamWrapper
{
/** @var resource|string|null */
/** @var resource|null */
public $context;

/** @var HttpClientInterface */
Expand All @@ -31,7 +31,7 @@ class StreamWrapper
/** @var ResponseInterface */
private $response;

/** @var resource|null */
/** @var resource|string|null */
private $content;

/** @var resource|null */
Expand Down Expand Up @@ -81,6 +81,7 @@ public function bindHandles(&$handle, &$content): void
{
$this->handle = &$handle;
$this->content = &$content;
$this->offset = null;
}

public function stream_open(string $path, string $mode, int $options): bool
Expand Down Expand Up @@ -125,7 +126,7 @@ public function stream_read(int $count)
}
}

if (0 !== fseek($this->content, $this->offset)) {
if (0 !== fseek($this->content, $this->offset ?? 0)) {
return false;
}

Expand Down Expand Up @@ -154,6 +155,11 @@ public function stream_read(int $count)
try {
$this->eof = true;
$this->eof = !$chunk->isTimeout();

if (!$this->eof && !$this->blocking) {
return '';
}

$this->eof = $chunk->isLast();

if ($chunk->isFirst()) {
Expand Down Expand Up @@ -196,7 +202,7 @@ public function stream_set_option(int $option, int $arg1, ?int $arg2): bool

public function stream_tell(): int
{
return $this->offset;
return $this->offset ?? 0;
}

public function stream_eof(): bool
Expand All @@ -206,14 +212,19 @@ public function stream_eof(): bool

public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
{
if (null === $this->content && null === $this->offset) {
$this->response->getStatusCode();
$this->offset = 0;
}

if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, \SEEK_END)) {
return false;
}

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

if (\SEEK_CUR === $whence) {
$offset += $this->offset;
$offset += $this->offset ?? 0;
}

if (\SEEK_END === $whence || $size < $offset) {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public function testNonBlockingStream()
$this->assertTrue(feof($stream));
}

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

$this->assertSame(0, fseek($stream, 0, \SEEK_CUR));
$this->assertSame('<1>', fread($stream, 8192));
$this->assertFalse(feof($stream));
$this->assertSame('<2>', stream_get_contents($stream));
}

public function testTimeoutIsNotAFatalError()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ protected function getHttpClient(string $testCase): HttpClientInterface
return $client;

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

Expand Down