bug #33058 [HttpClient] fix data loss when streaming as a PHP resource (nicolas-grekas)
This PR was merged into the 4.4 branch.
Discussion
----------
[HttpClient] fix data loss when streaming as a PHP resource
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
I've just experienced failures like:
> StreamWrapper::stream_read - read 822 bytes more data than requested (9014 read, 8192 max) - excess data will be lost
This fixes it.
Commits
-------
99884e63b5
[HttpClient] fix data loss when streaming as a PHP resource
This commit is contained in:
commit
f91fa10c6c
@ -22,7 +22,7 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
*/
|
||||
class StreamWrapper
|
||||
{
|
||||
/** @var resource */
|
||||
/** @var resource|string|null */
|
||||
public $context;
|
||||
|
||||
/** @var HttpClientInterface */
|
||||
@ -103,7 +103,7 @@ class StreamWrapper
|
||||
|
||||
public function stream_read(int $count)
|
||||
{
|
||||
if (null !== $this->content) {
|
||||
if (\is_resource($this->content)) {
|
||||
// Empty the internal activity list
|
||||
foreach ($this->client->stream([$this->response], 0) as $chunk) {
|
||||
try {
|
||||
@ -127,6 +127,19 @@ class StreamWrapper
|
||||
}
|
||||
}
|
||||
|
||||
if (\is_string($this->content)) {
|
||||
if (\strlen($this->content) <= $count) {
|
||||
$data = $this->content;
|
||||
$this->content = null;
|
||||
} else {
|
||||
$data = substr($this->content, 0, $count);
|
||||
$this->content = substr($this->content, $count);
|
||||
}
|
||||
$this->offset += \strlen($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ($this->client->stream([$this->response]) as $chunk) {
|
||||
try {
|
||||
$this->eof = true;
|
||||
@ -134,6 +147,12 @@ class StreamWrapper
|
||||
$this->eof = $chunk->isLast();
|
||||
|
||||
if ('' !== $data = $chunk->getContent()) {
|
||||
if (\strlen($data) > $count) {
|
||||
if (null === $this->content) {
|
||||
$this->content = substr($data, $count);
|
||||
}
|
||||
$data = substr($data, 0, $count);
|
||||
}
|
||||
$this->offset += \strlen($data);
|
||||
|
||||
return $data;
|
||||
@ -155,12 +174,12 @@ class StreamWrapper
|
||||
|
||||
public function stream_eof(): bool
|
||||
{
|
||||
return $this->eof;
|
||||
return $this->eof && !\is_string($this->content);
|
||||
}
|
||||
|
||||
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
|
||||
{
|
||||
if (null === $this->content || 0 !== fseek($this->content, 0, SEEK_END)) {
|
||||
if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, SEEK_END)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user