1313
1414namespace CodeIgniter \HTTP ;
1515
16- use Closure ;
1716use JsonException ;
1817
1918/**
2019 * HTTP response for Server-Sent Events (SSE) streaming.
2120 *
2221 * @see \CodeIgniter\HTTP\SSEResponseTest
2322 */
24- class SSEResponse extends Response implements NonBufferedResponseInterface
23+ class SSEResponse extends StreamResponse
2524{
2625 /**
2726 * Constructor.
2827 *
29- * @param Closure (SSEResponse): void $callback
28+ * @param callable (SSEResponse): void $callback
3029 */
31- public function __construct (private readonly Closure $ callback )
30+ public function __construct (callable $ callback )
3231 {
33- parent ::__construct ();
32+ parent ::__construct ($ callback );
3433 }
3534
3635 /**
@@ -42,7 +41,7 @@ public function __construct(private readonly Closure $callback)
4241 */
4342 public function event (array |string $ data , ?string $ event = null , ?string $ id = null ): bool
4443 {
45- if ($ this ->isConnectionAborted ()) {
44+ if (! $ this ->isClientConnected ()) {
4645 return false ;
4746 }
4847
@@ -76,10 +75,6 @@ public function event(array|string $data, ?string $event = null, ?string $id = n
7675 */
7776 public function comment (string $ text ): bool
7877 {
79- if ($ this ->isConnectionAborted ()) {
80- return false ;
81- }
82-
8378 return $ this ->write ($ this ->formatMultiline ('' , $ text ));
8479 }
8580
@@ -90,21 +85,9 @@ public function comment(string $text): bool
9085 */
9186 public function retry (int $ milliseconds ): bool
9287 {
93- if ($ this ->isConnectionAborted ()) {
94- return false ;
95- }
96-
9788 return $ this ->write ("retry: {$ milliseconds }\n\n" );
9889 }
9990
100- /**
101- * Check if the client connection has been lost.
102- */
103- private function isConnectionAborted (): bool
104- {
105- return connection_status () !== CONNECTION_NORMAL || connection_aborted () === 1 ;
106- }
107-
10891 /**
10992 * Strip newlines from a single-line SSE field (event, id).
11093 */
@@ -130,75 +113,33 @@ private function formatMultiline(string $prefix, string $value): string
130113 return $ output . "\n" ;
131114 }
132115
133- /**
134- * Write raw SSE output and flush.
135- */
136- private function write (string $ output ): bool
137- {
138- echo $ output ;
139-
140- if (! service ('environment ' )->isTesting ()) {
141- if (ob_get_level () > 0 ) {
142- ob_flush ();
143- }
144-
145- flush ();
146- }
147-
148- return true ;
149- }
150-
151116 /**
152117 * {@inheritDoc}
153118 *
154- * @return $this
119+ * SSE headers are fixed by the protocol, so they override
120+ * anything set on the response.
155121 */
156- public function send ()
122+ protected function prepareStreamHeaders (): void
157123 {
158- // Turn off output buffering completely, even if php.ini output_buffering is not off
159- if (! service ('environment ' )->isTesting ()) {
160- set_time_limit (0 );
161- ini_set ('zlib.output_compression ' , 'Off ' );
162-
163- while (ob_get_level () > 0 ) {
164- ob_end_clean ();
165- }
166- }
167-
168- // Close session if active to prevent blocking other requests
169- if (session_status () === PHP_SESSION_ACTIVE ) {
170- session_write_close ();
171- }
172-
173124 $ this ->setContentType ('text/event-stream ' , 'UTF-8 ' );
174125 $ this ->removeHeader ('Cache-Control ' );
175126 $ this ->setHeader ('Cache-Control ' , 'no-cache ' );
176- $ this ->setHeader ('Content-Encoding ' , 'identity ' );
177127 $ this ->setHeader ('X-Accel-Buffering ' , 'no ' );
178128
179129 // Connection: keep-alive is only valid for HTTP/1.x
180130 if (version_compare ($ this ->getProtocolVersion (), '2.0 ' , '< ' )) {
181131 $ this ->setHeader ('Connection ' , 'keep-alive ' );
182132 }
183-
184- // Intentionally skip CSP finalize: no HTML/JS execution in SSE streams.
185- $ this ->sendHeaders ();
186- $ this ->sendCookies ();
187-
188- ($ this ->callback )($ this );
189-
190- return $ this ;
191133 }
192134
193135 /**
194136 * {@inheritDoc}
195137 *
196- * No-op — body is streamed via the callback, not stored.
197- *
198- * @return $this
138+ * CSP is not finalized for SSE responses, as Content Security
139+ * Policy does not apply to event streams.
199140 */
200- public function sendBody ()
141+ protected function shouldFinalizeCsp (): bool
201142 {
202- return $ this ;
143+ return false ;
203144 }
204145}
0 commit comments