8000 feature #22124 Shift responsibility for keeping Date header to Respon… · symfony/symfony@0478ecd · GitHub
[go: up one dir, main page]

Skip to content

Commit 0478ecd

Browse files
committed
feature #22124 Shift responsibility for keeping Date header to ResponseHeaderBag (mpdude)
This PR was squashed before being merged into the 3.4 branch (closes #22124). Discussion ---------- Shift responsibility for keeping Date header to ResponseHeaderBag | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This is an improvement over #22036. It shifts responsibility for preserving a `Date` header to the `ResponseHeaderBag`. We already have similar logic there for the `Cache-Control` header. Commits ------- 5d83836 Shift responsibility for keeping Date header to ResponseHeaderBag
2 parents 1cdbb7d + 5d83836 commit 0478ecd

File tree

3 files changed

+72
-58
lines changed

3 files changed

+72
-58
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ public function __construct($content = '', $status = 200, $headers = array())
201201
$this->setContent($content);
202202
$this->setStatusCode($status);
203203
$this->setProtocolVersion('1.0');
204-
205-
/* RFC2616 - 14.18 says all Responses need to have a Date */
206-
if (!$this->headers->has('Date')) {
207-
$this->setDate(\DateTime::createFromFormat('U', time()));
208-
}
209204
}
210205

211206
/**
@@ -334,11 +329,6 @@ public function sendHeaders()
334329
return $this;
335330
}
336331

337-
/* RFC2616 - 14.18 says all Responses need to have a Date */
338-
if (!$this->headers->has('Date')) {
339-
$this->setDate(\DateTime::createFromFormat('U', time()));
340-
}
341-
342332
// headers
343333
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
344334
foreach ($values as $value) {
@@ -648,15 +638,6 @@ public function mustRevalidate()
648638
*/
649639
public function getDate()
650640
{
651-
/*
652-
RFC2616 - 14.18 says all Responses need to have a Date.
653-
Make sure we provide one even if it the header
654-
has been removed in the meantime.
655-
*/
656-
if (!$this->headers->has('Date')) {
657-
$this->setDate(\DateTime::createFromFormat('U', time()));
658-
}
659-
660641
return $this->headers->getDate('Date');
661642
}
662643

src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function __construct(array $headers = array())
5151
if (!isset($this->headers['cache-control'])) {
5252
$this->set('Cache-Control', '');
5353
}
54+
55+
/* RFC2616 - 14.18 says all Responses need to have a Date */
56+
if (!isset($this->headers['date'])) {
57+
$this->initDate();
58+
}
5459
}
5560

5661
/**
@@ -90,6 +95,10 @@ public function replace(array $headers = array())
9095
if (!isset($this->headers['cache-control'])) {
9196
$this->set('Cache-Control', '');
9297
}
98+
99+
if (!isset($this->headers['date'])) {
100+
$this->initDate();
101+
}
93102
}
94103

95104
/**
@@ -156,6 +165,10 @@ public function remove($key)
156165
if ('cache-control' === $uniqueKey) {
157166
$this->computedCacheControl = array();
158167
}
168+
169+
if ('date' === $uniqueKey) {
170+
$this->initDate();
171+
}
159172
}
160173

161174
/**
@@ -338,4 +351,11 @@ protected function computeCacheControlValue()
338351

339352
return $header;
340353
}
354+
355+
private function initDate()
356+
{
357+
$now = \DateTime::createFromFormat('U', time());
358+
$now->setTimezone(new \DateTimeZone('UTC'));
359+
$this->set('Date', $now->format('D, d M Y H:i:s').' GMT');
360+
}
341361
}

src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,24 @@
2020
*/
2121
class ResponseHeaderBagTest extends TestCase
2222
{
23-
/**
24-
* @dataProvider provideAllPreserveCase
25-
*/
26-
public function testAllPreserveCase($headers, $expected)
23+
public function testAllPreserveCase()
2724
{
25+
$headers = array(
26+
'fOo' => 'BAR',
27+
'ETag' => 'xyzzy',
28+
'Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ==',
29+
'P3P' => 'CP="CAO PSA OUR"',
30+
'WWW-Authenticate' => 'Basic realm="WallyWorld"',
31+
'X-UA-Compatible' => 'IE=edge,chrome=1',
32+
'X-XSS-Protection' => '1; mode=block',
33+
);
34+
2835
$bag = new ResponseHeaderBag($headers);
36+
$allPreservedCase = $bag->allPreserveCase();
2937

30-
$this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
31-
}
32-
33-
public function provideAllPreserveCase()
34-
{
35-
return array(
36-
array(
37-
array('fOo' => 'BAR'),
38-
array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache, private')),
39-
),
40-
array(
41-
array('ETag' => 'xyzzy'),
42-
array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate')),
43-
),
44-
array(
45-
array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
46-
array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache, private')),
47-
),
48-
array(
49-
array('P3P' => 'CP="CAO PSA OUR"'),
50-
array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache, private')),
51-
),
52-
array(
53-
array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
54-
array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache, private')),
55-
),
56-
array(
57-
array('X-UA-Compatible' => 'IE=edge,chrome=1'),
58-
array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache, private')),
59-
),
60-
array(
61-
array('X-XSS-Protection' => '1; mode=block'),
62-
array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache, private')),
63-
),
64-
);
38+
foreach (array_keys($headers) as $headerName) {
39+
$this->assertArrayHasKey($headerName, $allPreservedCase, '->allPreserveCase() gets all input keys in original case');
40+
}
6541
}
6642

6743
public function testCacheControlHeader()
@@ -332,6 +308,43 @@ public function provideMakeDispositionFail()
332308
);
333309
}
334310

311+
public function testDateHeaderAddedOnCreation()
312+
{
313+
$now = time();
314+
315+
$bag = new ResponseHeaderBag();
316+
$this->assertTrue($bag->has('Date'));
317+
318+
$this->assertEquals($now, $bag->getDate('Date')->getTimestamp());
319+
}
320+
321+
public function testDateHeaderCanBeSetOnCreation()
322+
{
323+
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
324+
$bag = new ResponseHeaderBag(array('Date' => $someDate));
325+
326+
$this->assertEquals($someDate, $bag->get('Date'));
327+
}
328+
329+
public function testDateHeaderWillBeRecreatedWhenRemoved()
330+
{
331+
$someDate = 'Thu, 23 Mar 2017 09:15:12 GMT';
332+
$bag = new ResponseHeaderBag(array('Date' => $someDate));
333+
$bag->remove('Date');
334+
335+
// a (new) Date header is still present
336+
$this->assertTrue($bag->has('Date'));
337+
$this->assertNotEquals($someDate, $bag->get('Date'));
338+
}
339+
340+
public function testDateHeaderWillBeRecreatedWhenHeadersAreReplaced()
341+
{
342+
$bag = new ResponseHeaderBag();
343+
$bag->replace(array());
344+
345+
$this->assertTrue($bag->has('Date'));
346+
}
347+
335348
private function assertSetCookieHeader($expected, ResponseHeaderBag $actual)
336349
{
337350
$this->assertRegExp('#^Set-Cookie:\s+'.preg_quote($expected, '#').'$#m', str_replace("\r\n", "\n", (string) $actual));

0 commit comments

Comments
 (0)
0