8000 [HttpClient] fix data loss when streaming as a PHP resource · symfony/symfony@99884e6 · GitHub
[go: up one dir, main page]

Skip to content

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 99884e6

Browse files
[HttpClient] fix data loss when streaming as a PHP resource
1 parent 053ad8d commit 99884e6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

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

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

2828
/** @var HttpClientInterface */
@@ -103,7 +103,7 @@ public function stream_open(string $path, string $mode, int $options): bool
103103

104104
public function stream_read(int $count)
105105
{
106-
if (null !== $this->content) {
106+
if (\is_resource($this->content)) {
107107
// Empty the internal activity list
108108
foreach ($this->client->stream([$this->response], 0) as $chunk) {
109109
try {
@@ -127,13 +127,32 @@ public function stream_read(int $count)
127127
}
128128
}
129129

130+
if (\is_string($this->content)) {
131+
if (\strlen($this->content) <= $count) {
132+
$data = $this->content;
133+
$this->content = null;
134+
} else {
135+
$data = substr($this->content, 0, $count);
136+
$this->content = substr($this->content, $count);
137+
}
138+
$this->offset += \strlen($data);
139+
140+
return $data;
141+
}
142+
130143
foreach ($this->client->stream([$this->response]) as $chunk) {
131144
try {
132145
$this->eof = true;
133146
$this->eof = !$chunk->isTimeout();
134147
$this->eof = $chunk->isLast();
135148

136149
if ('' !== $data = $chunk->getContent()) {
150+
if (\strlen($data) > $count) {
151+
if (null === $this->content) {
152+
$this->content = substr($data, $count);
153+
}
154+
$data = substr($data, 0, $count);
155+
}
137156
$this->offset += \strlen($data);
138157

139158
return $data;
@@ -155,12 +174,12 @@ public function stream_tell(): int
155174

156175
public function stream_eof(): bool
157176
{
158-
return $this->eof;
177+
return $this->eof && !\is_string($this->content);
159178
}
160179

161180
public function stream_seek(int $offset, int $whence = SEEK_SET): bool
162181
{
163-
if (null === $this->content || 0 !== fseek($this->content, 0, SEEK_END)) {
182+
if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, SEEK_END)) {
164183
return false;
165184
}
166185

0 commit comments

Comments
 (0)
0