@@ -78,26 +78,29 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
78
78
79
79
$ syRequest = $ this ->mapRequest ($ request );
80
80
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 ();
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 ();
86
85
86
+ try {
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 ();
95
92
} catch (\Exception $ exception ) {
96
93
$ response ->writeHead (500 ); // internal server error
97
94
$ response ->end ();
95
+
96
+ // end buffering if we need to throw
97
+ @ob_end_clean ();
98
98
throw $ exception ;
99
99
}
100
100
101
+ // should not receive output from application->handle()
102
+ @ob_end_clean ();
103
+
101
104
$ this ->mapResponse ($ response , $ syResponse );
102
105
103
106
if ($ this ->application instanceof TerminableInterface) {
@@ -236,24 +239,33 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
236
239
$ headers ['Set-Cookie ' ] = $ cookies ;
237
240
}
238
241
239
- $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
242
+ if ($ syResponse instanceof SymfonyStreamedResponse) {
243
+ $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
240
244
241
- // asynchronously get content
242
- ob_start (function ($ buffer ) use ($ reactResponse ) {
243
- $ reactResponse ->write ($ buffer );
244
- return '' ;
245
- }, 4096 );
245
+ // asynchronously get content
246
+ ob_start (function ($ buffer ) use ($ reactResponse ) {
247
+ $ reactResponse ->write ($ buffer );
248
+ return '' ;
249
+ }, 4096 );
246
250
247
- if ($ syResponse instanceof SymfonyStreamedResponse) {
248
251
$ syResponse ->sendContent ();
252
+
253
+ // flush remaining content
254
+ @ob_end_flush ();
255
+ $ reactResponse ->end ();
249
256
}
250
257
else {
251
- echo ($ syResponse ->getContent ());
252
- }
258
+ ob_start ();
259
+ $ content = $ syResponse ->getContent ();
260
+ @ob_end_flush ();
253
261
254
- // flush remaining content
255
- @ob_end_flush ();
256
- $ reactResponse ->end ();
262
+ if (!isset ($ headers ['Content-Length ' ])) {
263
+ $ headers ['Content-Length ' ] = strlen ($ content );
264
+ }
265
+
266
+ $ reactResponse ->writeHead ($ syResponse ->getStatusCode (), $ headers );
267
+ $ reactResponse ->end ($ content );
268
+ }
257
269
}
258
270
259
271
/**
0 commit comments