Skip to content

Commit e7e5304

Browse files
committed
forced all responses to have a Date header (RFC2616 - 14.18)
1 parent 122a61b commit e7e5304

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/Symfony/Component/HttpFoundation/Response.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ class Response
8282
*/
8383
public function __construct($content = '', $status = 200, $headers = array())
8484
{
85+
$this->headers = new ResponseHeaderBag($headers);
8586
$this->setContent($content);
8687
$this->setStatusCode($status);
8788
$this->setProtocolVersion('1.0');
88-
$this->headers = new ResponseHeaderBag($headers);
89+
if (!$this->headers->has('Date')) {
90+
$this->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
91+
}
8992
$this->charset = 'UTF-8';
9093
}
9194

@@ -329,20 +332,24 @@ public function mustRevalidate()
329332
/**
330333
* Returns the Date header as a DateTime instance.
331334
*
332-
* When no Date header is present, the current time is returned.
333-
*
334335
* @return \DateTime A \DateTime instance
335336
*
336337
* @throws \RuntimeException when the header is not parseable
337338
*/
338339
public function getDate()
339340
{
340-
if (null === $date = $this->headers->getDate('Date')) {
341-
$date = new \DateTime(null, new \DateTimeZone('UTC'));
342-
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
343-
}
341+
return $this->headers->getDate('Date');
342+
}
344343

345-
return $date;
344+
/**
345+
* Sets the Date header.
346+
*
347+
* @param \DateTime $date A \DateTime instance
348+
*/
349+
public function setDate(\DateTime $date)
350+
{
351+
$date->setTimezone(new \DateTimeZone('UTC'));
352+
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
346353
}
347354

348355
/**

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
182182

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

185+
$response->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
186+
185187
if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
186188
$response->headers->set('X-Symfony-Cache', $this->getLog());
187189
}
@@ -340,9 +342,8 @@ protected function validate(Request $request, Response $entry, $catch = false)
340342
}
341343

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

345-
foreach (array('Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
346+
foreach (array('Expires', 'Cache-Control', 'ETag', 'Last-Modified') as $name) {
346347
if ($response->headers->has($name)) {
347348
$entry->headers->set($name, $response->headers->get($name));
348349
}

0 commit comments

Comments
 (0)