@@ -78,17 +78,20 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
78
78
79
79
$ syRequest = $ this ->mapRequest ($ request );
80
80
81
- //start buffering the output, so cgi is not sending any http headers
82
- //this is necessary because it would break session handling since
83
- //headers_sent() returns true if any unbuffered output reaches cgi stdout.
84
- ob_start ();
85
-
86
81
try {
82
+ // start buffering the output, so cgi is not sending any http headers
83
+ // this is necessary because it would break session handling since
84
+ // headers_sent() returns true if any unbuffered output reaches cgi stdout.
85
+ ob_start ();
86
+
87
87
if ($ this ->bootstrap instanceof HooksInterface) {
88
88
$ this ->bootstrap ->preHandle ($ this ->application );
89
89
}
90
90
91
91
$ syResponse = $ this ->application ->handle ($ syRequest );
92
+
93
+ // should not receive output from application->handle()
94
+ ob_end_clean ();
92
95
} catch (\Exception $ exception ) {
93
96
$ response ->writeHead (500 ); // internal server error
94
97
$ response ->end ();
@@ -170,15 +173,10 @@ protected function mapRequest(ReactRequest $reactRequest)
170
173
*/
171
174
protected function mapResponse (HttpResponse $ reactResponse , SymfonyResponse $ syResponse )
172
175
{
173
- //end active session
176
+ // end active session
174
177
if (PHP_SESSION_ACTIVE === session_status ()) {
175
178
session_write_close ();
176
- session_unset (); //reset $_SESSION
177
- }
178
-
179
- $ content = $ syResponse ->getContent ();
180
- if ($ syResponse instanceof SymfonyStreamedResponse) {
181
- $ syResponse ->sendContent ();
179
+ session_unset (); // reset $_SESSION
182
180
}
183
181
184
182
$ nativeHeaders = [];
@@ -200,8 +198,8 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
200
198
}
201
199
}
202
200
203
- //after reading all headers we need to reset it, so next request
204
- //operates on a clean header.
201
+ // after reading all headers we need to reset it, so next request
202
+ // operates on a clean header.
205
203
header_remove ();
206
204
207
205
$ headers = array_merge ($ nativeHeaders , $ syResponse ->headers ->allPreserveCase ());
@@ -240,12 +238,22 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
240
238
241
239
$ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
242
240
243
- $ stdOut = '' ;
244
- while ($ buffer = @ob_get_clean ()) {
245
- $ stdOut .= $ buffer ;
241
+ // asynchronously get content
242
+ ob_start (function ($ buffer ) use ($ reactResponse ) {
243
+ $ reactResponse ->write ($ buffer );
244
+ return '' ;
245
+ }, 4096 );
246
+
247
+ if ($ syResponse instanceof SymfonyStreamedResponse) {
248
+ $ syResponse ->sendContent ();
249
+ }
250
+ else {
251
+ echo ($ syResponse ->getContent ());
246
252
}
247
253
248
- $ reactResponse ->end ($ stdOut . $ content );
254
+ // flush remaining content
255
+ @ob_end_flush ();
256
+ $ reactResponse ->end ();
249
257
}
250
258
251
259
/**
0 commit comments