Skip to content

Core request fix #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpFoundation][HttpKernel] allows headers to be set from a core.req…
…uest listener
  • Loading branch information
schmittjoh committed Feb 21, 2011
commit e807cfb57530f9071b9b030cb7e6db6fe5db1345
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Request
*/
public $headers;

/**
* @var \Symfony\Component\HttpFoundation\ResponseHeaderBag
*/
public $responseHeaders;

protected $content;
protected $languages;
protected $charsets;
Expand Down Expand Up @@ -108,6 +113,7 @@ public function initialize(array $query = array(), array $request = array(), arr
$this->files = new FileBag($files);
$this->server = new ServerBag($server);
$this->headers = new HeaderBag($this->server->getHeaders());
$this->responseHeaders = new ResponseHeaderBag();

$this->content = $content;
$this->languages = null;
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpKernel/ResponseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel;

use Symfony\Component\EventDispatcher\EventInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -38,6 +39,8 @@ public function __construct($charset)
*/
public function filter(EventInterface $event, Response $response)
{
$this->mergeHeadersFromRequest($event->get('request'), $response);

if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
return $response;
}
Expand All @@ -58,4 +61,15 @@ public function filter(EventInterface $event, Response $response)

return $response;
}

protected function mergeHeadersFromRequest(Request $request, Response $response)
{
foreach ($request->responseHeaders->all() as $key => $value) {
$response->headers->set($key, $value, true);
}

foreach ($request->responseHeaders->getCookies() as $cookie) {
$response->setCookie($cookie);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class ResponseListenerTest extends \PHPUnit_Framework_TestCase
{
public function testFilterDoesNothingForSubRequests()
{
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST));
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::SUB_REQUEST, 'request' => new Request()));
$this->getDispatcher()->filter($event, $response = new Response('foo'));

$this->assertEquals('', $response->headers->get('content-type'));
}

public function testFilterDoesNothingIfContentTypeIsSet()
{
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST));
$event = new Event(null, 'core.response', array('request_type' => HttpKernelInterface::MASTER_REQUEST, 'request' => new Request()));
$response = new Response('foo');
$response->headers->set('Content-Type', 'text/plain');
$this->getDispatcher()->filter($event, $response);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy