X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fsymfony%2Fhttp-foundation%2FResponseHeaderBag.php;h=00cff7af087443215b6ac24628c95f3c4f5fe799;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=3223691eb6fe4ef5f79b245fdc734558a85d0e05;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/vendor/symfony/http-foundation/ResponseHeaderBag.php b/vendor/symfony/http-foundation/ResponseHeaderBag.php index 3223691eb..00cff7af0 100644 --- a/vendor/symfony/http-foundation/ResponseHeaderBag.php +++ b/vendor/symfony/http-foundation/ResponseHeaderBag.php @@ -24,26 +24,10 @@ class ResponseHeaderBag extends HeaderBag const DISPOSITION_ATTACHMENT = 'attachment'; const DISPOSITION_INLINE = 'inline'; - /** - * @var array - */ protected $computedCacheControl = array(); - - /** - * @var array - */ protected $cookies = array(); - - /** - * @var array - */ protected $headerNames = array(); - /** - * Constructor. - * - * @param array $headers An array of HTTP headers - */ public function __construct(array $headers = array()) { parent::__construct($headers); @@ -51,21 +35,11 @@ class ResponseHeaderBag extends HeaderBag if (!isset($this->headers['cache-control'])) { $this->set('Cache-Control', ''); } - } - /** - * {@inheritdoc} - */ - public function __toString() - { - $cookies = ''; - foreach ($this->getCookies() as $cookie) { - $cookies .= 'Set-Cookie: '.$cookie."\r\n"; + /* RFC2616 - 14.18 says all Responses need to have a Date */ + if (!isset($this->headers['date'])) { + $this->initDate(); } - - ksort($this->headerNames); - - return parent::__toString().$cookies; } /** @@ -75,7 +49,22 @@ class ResponseHeaderBag extends HeaderBag */ public function allPreserveCase() { - return array_combine($this->headerNames, $this->headers); + $headers = array(); + foreach ($this->all() as $name => $value) { + $headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value; + } + + return $headers; + } + + public function allPreserveCaseWithoutCookies() + { + $headers = $this->allPreserveCase(); + if (isset($this->headerNames['set-cookie'])) { + unset($headers[$this->headerNames['set-cookie']]); + } + + return $headers; } /** @@ -90,20 +79,50 @@ class ResponseHeaderBag extends HeaderBag if (!isset($this->headers['cache-control'])) { $this->set('Cache-Control', ''); } + + if (!isset($this->headers['date'])) { + $this->initDate(); + } } /** * {@inheritdoc} */ - public function set($key, $values, $replace = true) + public function all() { - parent::set($key, $values, $replace); + $headers = parent::all(); + foreach ($this->getCookies() as $cookie) { + $headers['set-cookie'][] = (string) $cookie; + } + return $headers; + } + + /** + * {@inheritdoc} + */ + public function set($key, $values, $replace = true) + { $uniqueKey = str_replace('_', '-', strtolower($key)); + + if ('set-cookie' === $uniqueKey) { + if ($replace) { + $this->cookies = array(); + } + foreach ((array) $values as $cookie) { + $this->setCookie(Cookie::fromString($cookie)); + } + $this->headerNames[$uniqueKey] = $key; + + return; + } + $this->headerNames[$uniqueKey] = $key; + parent::set($key, $values, $replace); + // ensure the cache-control header has sensible defaults - if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) { + if (\in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'), true)) { $computed = $this->computeCacheControlValue(); $this->headers['cache-control'] = array($computed); $this->headerNames['cache-control'] = 'Cache-Control'; @@ -116,14 +135,24 @@ class ResponseHeaderBag extends HeaderBag */ public function remove($key) { - parent::remove($key); - $uniqueKey = str_replace('_', '-', strtolower($key)); unset($this->headerNames[$uniqueKey]); + if ('set-cookie' === $uniqueKey) { + $this->cookies = array(); + + return; + } + + parent::remove($key); + if ('cache-control' === $uniqueKey) { $this->computedCacheControl = array(); } + + if ('date' === $uniqueKey) { + $this->initDate(); + } } /** @@ -142,14 +171,10 @@ class ResponseHeaderBag extends HeaderBag return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null; } - /** - * Sets a cookie. - * - * @param Cookie $cookie - */ public function setCookie(Cookie $cookie) { $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; + $this->headerNames['set-cookie'] = 'Set-Cookie'; } /** @@ -174,6 +199,10 @@ class ResponseHeaderBag extends HeaderBag unset($this->cookies[$domain]); } } + + if (empty($this->cookies)) { + unset($this->headerNames['set-cookie']); + } } /** @@ -181,13 +210,13 @@ class ResponseHeaderBag extends HeaderBag * * @param string $format * - * @return array + * @return Cookie[] * * @throws \InvalidArgumentException When the $format is invalid */ public function getCookies($format = self::COOKIES_FLAT) { - if (!in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) { + if (!\in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) { throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY)))); } @@ -238,7 +267,7 @@ class ResponseHeaderBag extends HeaderBag */ public function makeDisposition($disposition, $filename, $filenameFallback = '') { - if (!in_array($disposition, array(self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE))) { + if (!\in_array($disposition, array(self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE))) { throw new \InvalidArgumentException(sprintf('The disposition must be either "%s" or "%s".', self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE)); } @@ -281,7 +310,7 @@ class ResponseHeaderBag extends HeaderBag protected function computeCacheControlValue() { if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) { - return 'no-cache'; + return 'no-cache, private'; } if (!$this->cacheControl) { @@ -301,4 +330,11 @@ class ResponseHeaderBag extends HeaderBag return $header; } + + private function initDate() + { + $now = \DateTime::createFromFormat('U', time()); + $now->setTimezone(new \DateTimeZone('UTC')); + $this->set('Date', $now->format('D, d M Y H:i:s').' GMT'); + } }