Skip to content

Commit a26d648

Browse files
committed
Cleanup and add Content-Length
1 parent 786ae84 commit a26d648

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

Bridges/HttpKernel.php

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,29 @@ public function onRequest(ReactRequest $request, HttpResponse $response)
7878

7979
$syRequest = $this->mapRequest($request);
8080

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();
8685

86+
try {
8787
if ($this->bootstrap instanceof HooksInterface) {
8888
$this->bootstrap->preHandle($this->application);
8989
}
9090

9191
$syResponse = $this->application->handle($syRequest);
92-
93-
// should not receive output from application->handle()
94-
ob_end_clean();
9592
} catch (\Exception $exception) {
9693
$response->writeHead(500); // internal server error
9794
$response->end();
95+
96+
// end buffering if we need to throw
97+
@ob_end_clean();
9898
throw $exception;
9999
}
100100

101+
// should not receive output from application->handle()
102+
@ob_end_clean();
103+
101104
$this->mapResponse($response, $syResponse);
102105

103106
if ($this->application instanceof TerminableInterface) {
@@ -236,24 +239,33 @@ protected function mapResponse(HttpResponse $reactResponse, SymfonyResponse $syR
236239
$headers['Set-Cookie'] = $cookies;
237240
}
238241

239-
$reactResponse->writeHead($syResponse->getStatusCode(), $headers);
242+
if ($syResponse instanceof SymfonyStreamedResponse) {
243+
$reactResponse->writeHead($syResponse->getStatusCode(), $headers);
240244

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);
246250

247-
if ($syResponse instanceof SymfonyStreamedResponse) {
248251
$syResponse->sendContent();
252+
253+
// flush remaining content
254+
@ob_end_flush();
255+
$reactResponse->end();
249256
}
250257
else {
251-
echo($syResponse->getContent());
252-
}
258+
ob_start();
259+
$content = $syResponse->getContent();
260+
@ob_end_flush();
253261

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+
}
257269
}
258270

259271
/**

0 commit comments

Comments
 (0)