Skip to content

Commit

Permalink
forced all responses to have a Date header (RFC2616 - 14.18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 16, 2011
1 parent 122a61b commit e7e5304
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -82,10 +82,13 @@ class Response
*/
public function __construct($content = '', $status = 200, $headers = array())
{
$this->headers = new ResponseHeaderBag($headers);
$this->setContent($content);
$this->setStatusCode($status);
$this->setProtocolVersion('1.0');
$this->headers = new ResponseHeaderBag($headers);
if (!$this->headers->has('Date')) {
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
}
$this->charset = 'UTF-8';
}

Expand Down Expand Up @@ -329,20 +332,24 @@ public function mustRevalidate()
/**
* Returns the Date header as a DateTime instance.
*
* When no Date header is present, the current time is returned.
*
* @return \DateTime A \DateTime instance
*
* @throws \RuntimeException when the header is not parseable
*/
public function getDate()
{
if (null === $date = $this->headers->getDate('Date')) {
$date = new \DateTime(null, new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
}
return $this->headers->getDate('Date');
}

return $date;
/**
* Sets the Date header.
*
* @param \DateTime $date A \DateTime instance
*/
public function setDate(\DateTime $date)
{
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Expand Up @@ -182,6 +182,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ

$this->restoreResponseBody($request, $response);

$response->setDate(new \DateTime(null, new \DateTimeZone('UTC')));

if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
$response->headers->set('X-Symfony-Cache', $this->getLog());
}
Expand Down Expand Up @@ -340,9 +342,8 @@ protected function validate(Request $request, Response $entry, $catch = false)
}

$entry = clone $entry;
$entry->headers->remove('Date');

foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
foreach (array('Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
if ($response->headers->has($name)) {
$entry->headers->set($name, $response->headers->get($name));
}
Expand Down

0 comments on commit e7e5304

Please sign in to comment.