Skip to content
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

Don't use buffered output in SapiEmitter when body size is less than buffer #64

Merged
merged 1 commit into from
Mar 7, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.2.1 under development

- Enh #58: Support stream output headers (@xepozz)
- Enh #64: Don't use buffered output in `SapiEmitter` when body size is less than buffer (@Gerych1984, @vjik)

## 2.2.0 December 25, 2023

Expand Down
33 changes: 23 additions & 10 deletions src/SapiEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,34 @@ private function emitBody(ResponseInterface $response): void
}

$level = ob_get_level();

$size = $body->getSize();
if ($size !== null && $size <= $this->bufferSize) {
$this->emitContent($body->getContents(), $level);
return;
}

while (!$body->eof()) {
$output = $body->read($this->bufferSize);
if ($output === '') {
while (ob_get_level() > $level) {
ob_end_flush();
}
continue;
}
echo $output;
// flush the output buffer and send echoed messages to the browser
$this->emitContent($body->read($this->bufferSize), $level);
}
}

private function emitContent(string $content, int $level): void
{
if ($content === '') {
while (ob_get_level() > $level) {
ob_end_flush();
}
flush();
return;
}

echo $content;

// flush the output buffer and send echoed messages to the browser
while (ob_get_level() > $level) {
ob_end_flush();
}
flush();
}

private function shouldOutputBody(ResponseInterface $response): bool
Expand Down
Loading