Skip to content

Shift responsibility for keeping Date header to ResponseHeaderBag #22124

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Guard Date header in ResponseHeaderBag instead of Response
  • Loading branch information
mpdude committed Jun 16, 2017
commit d4918f13cfa6cca146b7a85cf61d4e04ea8e8045
19 changes: 0 additions & 19 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,6 @@ public function __construct($content = '', $status = 200, $headers = array())
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');

/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}
}

/**
Expand Down Expand Up @@ -334,11 +329,6 @@ public function sendHeaders()
return $this;
}

/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}

// headers
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
foreach ($values as $value) {
Expand Down Expand Up @@ -648,15 +638,6 @@ public function mustRevalidate()
*/
public function getDate()
{
/*
RFC2616 - 14.18 says all Responses need to have a Date.
Make sure we provide one even if it the header
has been removed in the meantime.
*/
if (!$this->headers->has('Date')) {
$this->setDate(\DateTime::createFromFormat('U', time()));
}

return $this->headers->getDate('Date');
}

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function __construct(array $headers = array())
if (!isset($this->headers['cache-control'])) {
$this->set('Cache-Control', '');
}

/* RFC2616 - 14.18 says all Responses need to have a Date */
if (!isset($this->headers['date'])) {
$this->initDate();
}
}

/**
Expand Down Expand Up @@ -90,6 +95,10 @@ public function replace(array $headers = array())
if (!isset($this->headers['cache-control'])) {
$this->set('Cache-Control', '');
}

if (!isset($this->headers['date'])) {
$this->initDate();
}
}

/**
Expand Down Expand Up @@ -156,6 +165,10 @@ public function remove($key)
if ('cache-control' === $uniqueKey) {
$this->computedCacheControl = array();
}

if ('date' === $uniqueKey) {
$this->initDate();
}
}

/**
Expand Down Expand Up @@ -338,4 +351,11 @@ protected function computeCacheControlValue()

return $header;
}

private function initDate()
{
$now = \DateTime::createFromFormat('U', time());
$now->setTimezone(new \DateTimeZone('UTC'));
$this->set('Date', $now->format('D, d M Y H:i:s').' GMT');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,43 @@ public function provideMakeDispositionFail()
);
}

public function testDateHeaderAddedOnCreation()
{
$now = time();

$bag = new ResponseHeaderBag();
$this->assertTrue($bag->has('Date'));

$this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
}

public function testDateHeaderCanBeSetOnCreation()
{
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
$bag = new ResponseHeaderBag(array('Date' => $someDate));

$this->assertEquals($someDate, $bag->get('Date'));
}

public function testDateHeaderWillBeRecreatedWhenRemoved()
{
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
$bag = new ResponseHeaderBag(array('Date' => $someDate));
$bag->remove('Date');

// a (new) Date header is still present
$this->assertTrue($bag->has('Date'));
$this->assertNotEquals($someDate, $bag->get('Date'));
}

public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
{
$bag = new ResponseHeaderBag();
$bag->replace(array());

$this->assertTrue($bag->has('Date'));
}

private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
{
$this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));
Expand Down