51 if (!isset($this->headers[
'cache-control'])) {
52 $this->
set(
'Cache-Control',
'');
64 foreach ($this->
all() as $name => $value) {
65 $headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value;
74 if (isset($this->headerNames[
'set-cookie'])) {
75 unset(
$headers[$this->headerNames[
'set-cookie']]);
86 $this->headerNames = array();
90 if (!isset($this->headers[
'cache-control'])) {
91 $this->
set(
'Cache-Control',
'');
102 $headers[
'set-cookie'][] = (string) $cookie;
111 public function set($key, $values, $replace =
true)
113 $uniqueKey = str_replace(
'_',
'-', strtolower($key));
115 if (
'set-cookie' === $uniqueKey) {
117 $this->cookies = array();
119 foreach ((array) $values as $cookie) {
122 $this->headerNames[$uniqueKey] = $key;
127 $this->headerNames[$uniqueKey] = $key;
129 parent::set($key, $values, $replace);
132 if (in_array($uniqueKey, array(
'cache-control',
'etag',
'last-modified',
'expires'))) {
134 $this->headers[
'cache-control'] = array($computed);
135 $this->headerNames[
'cache-control'] =
'Cache-Control';
143 public function remove($key)
145 $uniqueKey = str_replace(
'_',
'-', strtolower($key));
146 unset($this->headerNames[$uniqueKey]);
148 if (
'set-cookie' === $uniqueKey) {
149 $this->cookies = array();
154 parent::remove($key);
156 if (
'cache-control' === $uniqueKey) {
157 $this->computedCacheControl = array();
166 return array_key_exists($key, $this->computedCacheControl);
174 return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] :
null;
185 $this->headerNames[
'set-cookie'] =
'Set-Cookie';
195 public function removeCookie($name, $path =
'/', $domain =
null)
197 if (
null === $path) {
201 unset($this->cookies[$domain][$path][$name]);
203 if (empty($this->cookies[$domain][$path])) {
204 unset($this->cookies[$domain][$path]);
206 if (empty($this->cookies[$domain])) {
207 unset($this->cookies[$domain]);
211 if (empty($this->cookies)) {
212 unset($this->headerNames[
'set-cookie']);
225 public function getCookies($format = self::COOKIES_FLAT)
227 if (!in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) {
228 throw new \InvalidArgumentException(sprintf(
'Format "%s" invalid (%s).', $format, implode(
', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY))));
231 if (self::COOKIES_ARRAY === $format) {
235 $flattenedCookies = array();
236 foreach ($this->cookies as $path) {
239 $flattenedCookies[] = $cookie;
244 return $flattenedCookies;
256 public function clearCookie($name, $path =
'/', $domain =
null, $secure =
false, $httpOnly =
true)
258 $this->
setCookie(
new Cookie($name,
null, 1, $path, $domain, $secure, $httpOnly));
276 public function makeDisposition($disposition, $filename, $filenameFallback =
'')
278 if (!in_array($disposition, array(self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE))) {
279 throw new \InvalidArgumentException(sprintf(
'The disposition must be either "%s" or "%s".', self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE));
282 if (
'' == $filenameFallback) {
283 $filenameFallback = $filename;
287 if (!preg_match(
'/^[\x20-\x7e]*$/', $filenameFallback)) {
288 throw new \InvalidArgumentException(
'The filename fallback must only contain ASCII characters.');
292 if (
false !== strpos($filenameFallback,
'%')) {
293 throw new \InvalidArgumentException(
'The filename fallback cannot contain the "%" character.');
297 if (
false !== strpos($filename,
'/') ||
false !== strpos($filename,
'\\') ||
false !== strpos($filenameFallback,
'/') ||
false !== strpos($filenameFallback,
'\\')) {
298 throw new \InvalidArgumentException(
'The filename and the fallback cannot contain the "/" and "\\" characters.');
301 $output = sprintf(
'%s; filename="%s"', $disposition, str_replace(
'"',
'\\"', $filenameFallback));
303 if ($filename !== $filenameFallback) {
304 $output .= sprintf(
"; filename*=utf-8''%s", rawurlencode($filename));
320 if (!$this->cacheControl && !$this->
has(
'ETag') && !$this->
has(
'Last-Modified') && !$this->
has(
'Expires')) {
321 return 'no-cache, private';
324 if (!$this->cacheControl) {
326 return 'private, must-revalidate';
330 if (isset($this->cacheControl[
'public']) || isset($this->cacheControl[
'private'])) {
335 if (!isset($this->cacheControl[
's-maxage'])) {
336 return $header.
', private';