comparison vendor/symfony/http-foundation/ResponseHeaderBag.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
22 const COOKIES_ARRAY = 'array'; 22 const COOKIES_ARRAY = 'array';
23 23
24 const DISPOSITION_ATTACHMENT = 'attachment'; 24 const DISPOSITION_ATTACHMENT = 'attachment';
25 const DISPOSITION_INLINE = 'inline'; 25 const DISPOSITION_INLINE = 'inline';
26 26
27 /**
28 * @var array
29 */
30 protected $computedCacheControl = array(); 27 protected $computedCacheControl = array();
31
32 /**
33 * @var array
34 */
35 protected $cookies = array(); 28 protected $cookies = array();
36
37 /**
38 * @var array
39 */
40 protected $headerNames = array(); 29 protected $headerNames = array();
41 30
42 /**
43 * Constructor.
44 *
45 * @param array $headers An array of HTTP headers
46 */
47 public function __construct(array $headers = array()) 31 public function __construct(array $headers = array())
48 { 32 {
49 parent::__construct($headers); 33 parent::__construct($headers);
50 34
51 if (!isset($this->headers['cache-control'])) { 35 if (!isset($this->headers['cache-control'])) {
52 $this->set('Cache-Control', ''); 36 $this->set('Cache-Control', '');
53 } 37 }
54 } 38
55 39 /* RFC2616 - 14.18 says all Responses need to have a Date */
56 /** 40 if (!isset($this->headers['date'])) {
57 * {@inheritdoc} 41 $this->initDate();
58 */ 42 }
59 public function __toString()
60 {
61 $cookies = '';
62 foreach ($this->getCookies() as $cookie) {
63 $cookies .= 'Set-Cookie: '.$cookie."\r\n";
64 }
65
66 ksort($this->headerNames);
67
68 return parent::__toString().$cookies;
69 } 43 }
70 44
71 /** 45 /**
72 * Returns the headers, with original capitalizations. 46 * Returns the headers, with original capitalizations.
73 * 47 *
74 * @return array An array of headers 48 * @return array An array of headers
75 */ 49 */
76 public function allPreserveCase() 50 public function allPreserveCase()
77 { 51 {
78 return array_combine($this->headerNames, $this->headers); 52 $headers = array();
53 foreach ($this->all() as $name => $value) {
54 $headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value;
55 }
56
57 return $headers;
58 }
59
60 public function allPreserveCaseWithoutCookies()
61 {
62 $headers = $this->allPreserveCase();
63 if (isset($this->headerNames['set-cookie'])) {
64 unset($headers[$this->headerNames['set-cookie']]);
65 }
66
67 return $headers;
79 } 68 }
80 69
81 /** 70 /**
82 * {@inheritdoc} 71 * {@inheritdoc}
83 */ 72 */
88 parent::replace($headers); 77 parent::replace($headers);
89 78
90 if (!isset($this->headers['cache-control'])) { 79 if (!isset($this->headers['cache-control'])) {
91 $this->set('Cache-Control', ''); 80 $this->set('Cache-Control', '');
92 } 81 }
82
83 if (!isset($this->headers['date'])) {
84 $this->initDate();
85 }
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function all()
92 {
93 $headers = parent::all();
94 foreach ($this->getCookies() as $cookie) {
95 $headers['set-cookie'][] = (string) $cookie;
96 }
97
98 return $headers;
93 } 99 }
94 100
95 /** 101 /**
96 * {@inheritdoc} 102 * {@inheritdoc}
97 */ 103 */
98 public function set($key, $values, $replace = true) 104 public function set($key, $values, $replace = true)
99 { 105 {
106 $uniqueKey = str_replace('_', '-', strtolower($key));
107
108 if ('set-cookie' === $uniqueKey) {
109 if ($replace) {
110 $this->cookies = array();
111 }
112 foreach ((array) $values as $cookie) {
113 $this->setCookie(Cookie::fromString($cookie));
114 }
115 $this->headerNames[$uniqueKey] = $key;
116
117 return;
118 }
119
120 $this->headerNames[$uniqueKey] = $key;
121
100 parent::set($key, $values, $replace); 122 parent::set($key, $values, $replace);
101 123
102 $uniqueKey = str_replace('_', '-', strtolower($key));
103 $this->headerNames[$uniqueKey] = $key;
104
105 // ensure the cache-control header has sensible defaults 124 // ensure the cache-control header has sensible defaults
106 if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) { 125 if (\in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'), true)) {
107 $computed = $this->computeCacheControlValue(); 126 $computed = $this->computeCacheControlValue();
108 $this->headers['cache-control'] = array($computed); 127 $this->headers['cache-control'] = array($computed);
109 $this->headerNames['cache-control'] = 'Cache-Control'; 128 $this->headerNames['cache-control'] = 'Cache-Control';
110 $this->computedCacheControl = $this->parseCacheControl($computed); 129 $this->computedCacheControl = $this->parseCacheControl($computed);
111 } 130 }
114 /** 133 /**
115 * {@inheritdoc} 134 * {@inheritdoc}
116 */ 135 */
117 public function remove($key) 136 public function remove($key)
118 { 137 {
119 parent::remove($key);
120
121 $uniqueKey = str_replace('_', '-', strtolower($key)); 138 $uniqueKey = str_replace('_', '-', strtolower($key));
122 unset($this->headerNames[$uniqueKey]); 139 unset($this->headerNames[$uniqueKey]);
123 140
141 if ('set-cookie' === $uniqueKey) {
142 $this->cookies = array();
143
144 return;
145 }
146
147 parent::remove($key);
148
124 if ('cache-control' === $uniqueKey) { 149 if ('cache-control' === $uniqueKey) {
125 $this->computedCacheControl = array(); 150 $this->computedCacheControl = array();
126 } 151 }
152
153 if ('date' === $uniqueKey) {
154 $this->initDate();
155 }
127 } 156 }
128 157
129 /** 158 /**
130 * {@inheritdoc} 159 * {@inheritdoc}
131 */ 160 */
140 public function getCacheControlDirective($key) 169 public function getCacheControlDirective($key)
141 { 170 {
142 return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null; 171 return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
143 } 172 }
144 173
145 /**
146 * Sets a cookie.
147 *
148 * @param Cookie $cookie
149 */
150 public function setCookie(Cookie $cookie) 174 public function setCookie(Cookie $cookie)
151 { 175 {
152 $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; 176 $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
177 $this->headerNames['set-cookie'] = 'Set-Cookie';
153 } 178 }
154 179
155 /** 180 /**
156 * Removes a cookie from the array, but does not unset it in the browser. 181 * Removes a cookie from the array, but does not unset it in the browser.
157 * 182 *
171 unset($this->cookies[$domain][$path]); 196 unset($this->cookies[$domain][$path]);
172 197
173 if (empty($this->cookies[$domain])) { 198 if (empty($this->cookies[$domain])) {
174 unset($this->cookies[$domain]); 199 unset($this->cookies[$domain]);
175 } 200 }
201 }
202
203 if (empty($this->cookies)) {
204 unset($this->headerNames['set-cookie']);
176 } 205 }
177 } 206 }
178 207
179 /** 208 /**
180 * Returns an array with all cookies. 209 * Returns an array with all cookies.
299 return $header.', private'; 328 return $header.', private';
300 } 329 }
301 330
302 return $header; 331 return $header;
303 } 332 }
333
334 private function initDate()
335 {
336 $now = \DateTime::createFromFormat('U', time());
337 $now->setTimezone(new \DateTimeZone('UTC'));
338 $this->set('Date', $now->format('D, d M Y H:i:s').' GMT');
339 }
304 } 340 }