Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bca8af

Browse filesBrowse 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
Copy full SHA for 6bca8af

File tree

Expand file treeCollapse file tree

1 file changed

+18
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-0
lines changed

‎src/Symfony/Component/HttpFoundation/StreamedResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/StreamedResponse.php
+18Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.