Skip to content

Commit 00ab266

Browse files
committed
add streaming methods to ResponseInterface
1 parent 6169a5b commit 00ab266

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎system/HTTP/ResponseInterface.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,23 @@ public function redirect(string $uri, string $method = 'auto', ?int $code = null
397397
*/
398398
public function download(string $filename = '', $data = '', bool $setMime = false);
399399

400+
/**
401+
* Creates a response that streams its body to the client as it is
402+
* generated, instead of buffering the complete body first.
403+
*
404+
* @param (callable(StreamResponse): void)|iterable<string> $callbackOrChunks A callback that
405+
* streams output via write(), or an iterable of
406+
* string chunks to be written in order
407+
*/
408+
public function stream(callable|iterable $callbackOrChunks): StreamResponse;
409+
410+
/**
411+
* Creates a response for streaming Server-Sent Events (SSE).
412+
*
413+
* @param callable(SSEResponse): void $callback
414+
*/
415+
public function eventStream(callable $callback): SSEResponse;
416+
400417
// --------------------------------------------------------------------
401418
// CSP Methods
402419
// --------------------------------------------------------------------

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ update your implementations to include the new methods or method changes to ensu
5252

5353
- **Cache:** ``CodeIgniter\Cache\CacheInterface::remember()`` now accepts a TTL callable. Custom implementations of ``CacheInterface`` must update the ``$ttl`` parameter type from ``int`` to ``callable|int``.
5454
- **Database:** ``CodeIgniter\Database\ConnectionInterface`` now requires the ``afterCommit()``, ``afterRollback()``, ``inTransaction()``, and ``transaction()`` methods.
55+
- **HTTP:** ``CodeIgniter\HTTP\ResponseInterface`` now requires the ``stream()`` and ``eventStream()`` methods, which create streaming and SSE responses. See :ref:`streaming-responses`.
5556
- **Logging:** ``CodeIgniter\Log\Handlers\HandlerInterface::handle()`` now requires a third parameter ``array $context = []``. Any custom log handler that overrides ``handle()`` - whether implementing ``HandlerInterface`` directly or extending a built-in handler class - must add the parameter to its ``handle()`` method signature.
5657
- **Security:** The ``SecurityInterface``'s ``verify()`` method now has a native return type of ``static``.
5758
- **Validation:** ``CodeIgniter\Validation\ValidationInterface`` now requires the ``getValidatedInput()`` method, which returns a ``CodeIgniter\Input\ValidatedInput`` instance.

‎user_guide_src/source/outgoing/response.rst‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ filters still run and may set headers, but they must not rely on the response
274274
body. View rendering and decorators are not applied - stream your output in
275275
the callback.
276276

277-
Since the body length is unknown in advance, no ``Content-Length`` header is
278-
sent. The client cannot display download progress, and interrupted transfers
279-
cannot be resumed.
277+
``StreamResponse`` does not set a ``Content-Length`` header by default, since
278+
the body length is typically unknown in advance. Without it, clients cannot
279+
display download progress or resume interrupted transfers. If you do know the
280+
total size (for example, when proxying a stream of known length), you may set
281+
``Content-Length`` yourself before returning the response.
280282

281283
.. note:: To serve an existing file, prefer :ref:`DownloadResponse <force-file-download>`,
282284
which sends proper ``Content-Length`` and ``Content-Disposition`` headers.

0 commit comments

Comments
 (0)