-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: add StreamResponse and SSE response factories #10395
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
Open
michalsn
wants to merge
11
commits into
codeigniter4:4.8
Choose a base branch
from
michalsn:feat/stream-response
base: 4.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ba3fe7
feat: add StreamResponse and SSE response factories
michalsn 6169a5b
support callable streaming responses
michalsn 00ab266
add streaming methods to ResponseInterface
michalsn dbea7a3
add stream proxying example
michalsn 25c2ea1
fix rector
michalsn c7715c8
update structarmed
michalsn 7004211
fix psalm error
michalsn 4721eee
refine streaming response headers and docs
michalsn 6d5c62f
preserve protocol version for stream factories
michalsn 20ab029
update user guide example
michalsn d47c78a
cs fix
michalsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * This file is part of CodeIgniter 4 framework. | ||
| * | ||
| * (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
| * | ||
| * For the full copyright and license information, please view | ||
| * the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace CodeIgniter\HTTP; | ||
|
|
||
| use Closure; | ||
|
|
||
| /** | ||
| * HTTP response that streams its body to the client as it is generated, | ||
| * instead of buffering the complete body first. | ||
| * | ||
| * @see \CodeIgniter\HTTP\StreamResponseTest | ||
| */ | ||
| class StreamResponse extends Response implements NonBufferedResponseInterface | ||
| { | ||
| /** | ||
| * @var Closure(static): void | ||
| */ | ||
| private readonly Closure $callback; | ||
|
|
||
| /** | ||
| * @param (callable(static): void)|iterable<string> $callbackOrChunks A callback that | ||
| * streams output via write(), or an iterable of | ||
| * string chunks to be written in order. A value | ||
| * that is both callable and iterable is treated | ||
| * as a callback. | ||
| */ | ||
| public function __construct(callable|iterable $callbackOrChunks) | ||
| { | ||
| parent::__construct(); | ||
|
|
||
| $this->callback = is_callable($callbackOrChunks) | ||
| ? $callbackOrChunks(...) | ||
| : static function (self $response) use ($callbackOrChunks): void { | ||
| foreach ($callbackOrChunks as $chunk) { | ||
| if (! $response->write($chunk)) { | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Write a chunk of the streamed body. | ||
| * | ||
| * @param bool $flush Whether to flush output to the client immediately. | ||
| * Pass false when writing many small chunks, then call | ||
| * flush() at intervals. | ||
| * | ||
| * @return bool false if the client has disconnected | ||
| */ | ||
| public function write(string $chunk, bool $flush = true): bool | ||
| { | ||
| if (! $this->isClientConnected()) { | ||
| return false; | ||
| } | ||
|
|
||
| echo $chunk; | ||
|
|
||
| if ($flush) { | ||
| $this->flush(); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Flush buffered output to the client. | ||
| */ | ||
| public function flush(): void | ||
| { | ||
| if (! service('environment')->isTesting()) { | ||
| if (ob_get_level() > 0) { | ||
| ob_flush(); | ||
| } | ||
|
|
||
| flush(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether the client connection is still open. | ||
| * | ||
| * Note: PHP only detects a disconnect when attempting to write, | ||
| * so this may return true until the next write() call fails. | ||
| */ | ||
| public function isClientConnected(): bool | ||
| { | ||
| return connection_status() === CONNECTION_NORMAL && connection_aborted() === 0; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function send() | ||
| { | ||
| // Turn off output buffering completely, even if php.ini output_buffering is not off | ||
| if (! service('environment')->isTesting()) { | ||
| set_time_limit(0); | ||
| ini_set('zlib.output_compression', 'Off'); | ||
|
|
||
| while (ob_get_level() > 0) { | ||
| ob_end_clean(); | ||
| } | ||
| } | ||
|
|
||
| // Close session if active to prevent blocking other requests | ||
| if (session_status() === PHP_SESSION_ACTIVE) { | ||
| session_write_close(); | ||
| } | ||
|
|
||
| $this->prepareStreamHeaders(); | ||
|
|
||
| // Give CSP a chance to build its headers. Nonce placeholders cannot be | ||
| // replaced in a streamed body; call getCSP()->getScriptNonce() or | ||
| // getStyleNonce() before returning the response instead. | ||
| if ($this->shouldFinalizeCsp()) { | ||
| $this->getCSP()->finalize($this); | ||
| } | ||
|
|
||
| $this->sendHeaders(); | ||
| $this->sendCookies(); | ||
|
|
||
| ($this->callback)($this); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Applies headers needed for unbuffered delivery, without overriding | ||
| * any the developer has already set. | ||
| */ | ||
| protected function prepareStreamHeaders(): void | ||
| { | ||
| if (! $this->hasHeader('X-Accel-Buffering')) { | ||
| $this->setHeader('X-Accel-Buffering', 'no'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * No-op — body is streamed via the callback, not stored. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function sendBody() | ||
| { | ||
| return $this; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two silently discards state the previous response might have stored. For example,
$this->response()->setHeader(...)->stream()will return a StreamResponse without the header sent. Same for->setCookie(). If this is desired, we need to document somewhere that the previously set headers/cookies etc needs to be re-added on the returned stream or SSE response.