Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6bca8af

Browse files
committed
bug #20289 Fix edge case with StreamedResponse where headers are sent twice (Nicofuma)
This PR was merged into the 2.7 branch. Discussion ---------- Fix edge case with StreamedResponse where headers are sent twice | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | | License | MIT | Doc PR | If you have PHPs output buffering enabled (`output_buffering=4096` in your php.ini per example), there is an edge case with the StreamedResponse object where the headers are sent twice. Even if it is harmless most of the time, it can be critical sometimes (per example, if an `Access-Control-Allow-Origin` header is duplicated the browser will block the request). Explanation: because the streamed response may need the request in order to generate the content, the `StreamedResponseListener` class calls the send method of the `Response` when the event `kernel.response` is fired. To prevent the content from being duplicated, a state has been introduced in the `sendContent()` method and it works fine. But there is an edge case is the headers of the response. If the content generated by the `sendContent()` method is smaller than the value defined for `output_buffering` in the `php.ini` then the buffer won't be flushed and the `headers_sent()` function will return false. Therefore when `$response->send()` is called for the second time (in the `app.php` file), the headers will be sent a second time. Commits ------- a79991f Fix edge case with StreamedResponse where headers are sent twice
2 parents 7b56cc0 + a79991f commit 6bca8af

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class StreamedResponse extends Response
2828
{
2929
protected $callback;
3030
protected $streamed;
31+
private $headersSent;
3132

3233
/**
3334
* Constructor.
@@ -44,6 +45,7 @@ public function __construct($callback = null, $status = 200, $headers = array())
4445
$this->setCallback($callback);
4546
}
4647
$this->streamed = false;
48+
$this->headersSent = false;
4749
}
4850

4951
/**
@@ -75,6 +77,22 @@ public function setCallback($callback)
7577
$this->callback = $callback;
7678
}
7779

80+
/**
81+
* {@inheritdoc}
82+
*
83+
* This method only sends the headers once.
84+
*/
85+
public function sendHeaders()
86+
{
87+
if ($this->headersSent) {
88+
return;
89+
}
90+
91+
$this->headersSent = true;
92+
93+
parent::sendHeaders();
94+
}
95+
7896
/**
7997
* {@inheritdoc}
8098
*

0 commit comments

Comments
 (0)