-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
MergeHttpHeadersListener.php
170 lines (149 loc) · 3.99 KB
/
MergeHttpHeadersListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao\CoreBundle\EventListener;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\CoreBundle\HttpKernel\Header\HeaderStorageInterface;
use Contao\CoreBundle\HttpKernel\Header\NativeHeaderStorage;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
/**
* Adds HTTP headers sent by Contao to the Symfony response.
*
* @author Yanick Witschi <https://github.com/toflar>
* @author Andreas Schempp <https://github.com/aschempp>
*/
class MergeHttpHeadersListener
{
/**
* @var ContaoFrameworkInterface
*/
private $framework;
/**
* @var HeaderStorageInterface
*/
private $headerStorage;
/**
* @var array
*/
private $headers = [];
/**
* @var array
*/
private $multiHeaders = [
'set-cookie',
'link',
'vary',
'pragma',
'cache-control',
];
/**
* Constructor.
*
* @param ContaoFrameworkInterface $framework
* @param HeaderStorageInterface|null $headerStorage
*/
public function __construct(ContaoFrameworkInterface $framework, HeaderStorageInterface $headerStorage = null)
{
$this->framework = $framework;
$this->headerStorage = $headerStorage ?: new NativeHeaderStorage();
}
/**
* Returns the multi-value headers.
*
* @return array
*/
public function getMultiHeaders()
{
return array_values($this->multiHeaders);
}
/**
* Sets the multi-value headers.
*
* @param array $headers
*/
public function setMultiHeader(array $headers)
{
$this->multiHeaders = $headers;
}
/**
* Adds a multi-value header.
*
* @param string $name
*/
public function addMultiHeader($name)
{
$uniqueKey = $this->getUniqueKey($name);
if (!in_array($uniqueKey, $this->multiHeaders, true)) {
$this->multiHeaders[] = $uniqueKey;
}
}
/**
* Removes a multi-value header.
*
* @param string $name
*/
public function removeMultiHeader($name)
{
if (false !== ($i = array_search($this->getUniqueKey($name), $this->multiHeaders, true))) {
unset($this->multiHeaders[$i]);
}
}
/**
* Adds the Contao headers to the Symfony response.
*
* @param FilterResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
{
if (!$this->framework->isInitialized()) {
return;
}
// Fetch remaining headers and add them to the response
$this->fetchHttpHeaders();
$this->setResponseHeaders($event->getResponse());
}
/**
* Fetches and stores HTTP headers from PHP.
*/
private function fetchHttpHeaders()
{
$this->headers = array_merge($this->headers, $this->headerStorage->all());
$this->headerStorage->clear();
}
/**
* Sets the response headers.
*
* @param Response $response
*/
private function setResponseHeaders(Response $response)
{
$allowOverrides = [];
foreach ($this->headers as $header) {
list($name, $content) = explode(':', $header, 2);
$uniqueKey = $this->getUniqueKey($name);
if (in_array($uniqueKey, $this->multiHeaders, true)) {
$response->headers->set($uniqueKey, trim($content), false);
} elseif (isset($allowOverrides[$uniqueKey]) || !$response->headers->has($uniqueKey)) {
$allowOverrides[$uniqueKey] = true;
$response->headers->set($uniqueKey, trim($content));
}
}
}
/**
* Returns the unique header key.
*
* @param string $name
*
* @return string
*/
private function getUniqueKey($name)
{
return str_replace('_', '-', strtolower($name));
}
}