|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\HTTP; |
| 15 | + |
| 16 | +use Closure; |
| 17 | + |
| 18 | +/** |
| 19 | + * HTTP response that streams its body to the client as it is generated, |
| 20 | + * instead of buffering the complete body first. |
| 21 | + * |
| 22 | + * @see \CodeIgniter\HTTP\StreamResponseTest |
| 23 | + */ |
| 24 | +class StreamResponse extends Response implements NonBufferedResponseInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var Closure(static): void |
| 28 | + */ |
| 29 | + private readonly Closure $callback; |
| 30 | + |
| 31 | + /** |
| 32 | + * @param (Closure(static): void)|iterable<string> $callbackOrChunks A callback that |
| 33 | + * streams output via write(), or an iterable of |
| 34 | + * string chunks to be written in order |
| 35 | + */ |
| 36 | + public function __construct(Closure|iterable $callbackOrChunks) |
| 37 | + { |
| 38 | + parent::__construct(); |
| 39 | + |
| 40 | + $this->callback = $callbackOrChunks instanceof Closure |
| 41 | + ? $callbackOrChunks |
| 42 | + : static function (self $response) use ($callbackOrChunks): void { |
| 43 | + foreach ($callbackOrChunks as $chunk) { |
| 44 | + if (! $response->write($chunk)) { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Write a chunk of the streamed body. |
| 53 | + * |
| 54 | + * @param bool $flush Whether to flush output to the client immediately. |
| 55 | + * Pass false when writing many small chunks, then call |
| 56 | + * flush() at intervals. |
| 57 | + * |
| 58 | + * @return bool false if the client has disconnected |
| 59 | + */ |
| 60 | + public function write(string $chunk, bool $flush = true): bool |
| 61 | + { |
| 62 | + if (! $this->isClientConnected()) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + echo $chunk; |
| 67 | + |
| 68 | + if ($flush) { |
| 69 | + $this->flush(); |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Flush buffered output to the client. |
| 77 | + */ |
| 78 | + public function flush(): void |
| 79 | + { |
| 80 | + if (! service('environment')->isTesting()) { |
| 81 | + if (ob_get_level() > 0) { |
| 82 | + ob_flush(); |
| 83 | + } |
| 84 | + |
| 85 | + flush(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Whether the client connection is still open. |
| 91 | + * |
| 92 | + * Note: PHP only detects a disconnect when attempting to write, |
| 93 | + * so this may return true until the next write() call fails. |
| 94 | + */ |
| 95 | + public function isClientConnected(): bool |
| 96 | + { |
| 97 | + return connection_status() === CONNECTION_NORMAL && connection_aborted() === 0; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * {@inheritDoc} |
| 102 | + * |
| 103 | + * @return $this |
| 104 | + */ |
| 105 | + public function send() |
| 106 | + { |
| 107 | + // Turn off output buffering completely, even if php.ini output_buffering is not off |
| 108 | + if (! service('environment')->isTesting()) { |
| 109 | + set_time_limit(0); |
| 110 | + ini_set('zlib.output_compression', 'Off'); |
| 111 | + |
| 112 | + while (ob_get_level() > 0) { |
| 113 | + ob_end_clean(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Close session if active to prevent blocking other requests |
| 118 | + if (session_status() === PHP_SESSION_ACTIVE) { |
| 119 | + session_write_close(); |
| 120 | + } |
| 121 | + |
| 122 | + $this->prepareStreamHeaders(); |
| 123 | + |
| 124 | + // Intentionally skip CSP finalize: the body is streamed, not buffered HTML. |
| 125 | + $this->sendHeaders(); |
| 126 | + $this->sendCookies(); |
| 127 | + |
| 128 | + ($this->callback)($this); |
| 129 | + |
| 130 | + return $this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Applies headers needed for unbuffered delivery, without overriding |
| 135 | + * any the developer has already set. |
| 136 | + */ |
| 137 | + protected function prepareStreamHeaders(): void |
| 138 | + { |
| 139 | + if (! $this->hasHeader('X-Accel-Buffering')) { |
| 140 | + $this->setHeader('X-Accel-Buffering', 'no'); |
| 141 | + } |
| 142 | + |
| 143 | + if (! $this->hasHeader('Content-Encoding')) { |
| 144 | + $this->setHeader('Content-Encoding', 'identity'); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * {@inheritDoc} |
| 150 | + * |
| 151 | + * No-op — body is streamed via the callback, not stored. |
| 152 | + * |
| 153 | + * @return $this |
| 154 | + */ |
| 155 | + public function sendBody() |
| 156 | + { |
| 157 | + return $this; |
| 158 | + } |
| 159 | +} |
0 commit comments