2010-05-03 10:40:23 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\HttpFoundation;
|
2010-05-03 10:40:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-06-23 15:24:24 +01:00
|
|
|
* HeaderBag is a container for HTTP headers.
|
2010-05-03 10:40:23 +01:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-05-03 10:40:23 +01:00
|
|
|
*/
|
2010-06-23 15:24:24 +01:00
|
|
|
class HeaderBag
|
2010-05-03 10:40:23 +01:00
|
|
|
{
|
2010-06-23 15:24:24 +01:00
|
|
|
protected $headers;
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $cacheControl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
* @param array $headers An array of HTTP headers
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
public function __construct(array $headers = array())
|
2010-05-03 10:40:23 +01:00
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$this->cacheControl = array();
|
2010-06-23 15:24:24 +01:00
|
|
|
$this->replace($headers);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
/**
|
|
|
|
* Returns the headers.
|
|
|
|
*
|
|
|
|
* @return array An array of headers
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
2010-08-26 09:56:46 +01:00
|
|
|
/**
|
|
|
|
* Returns the parameter keys.
|
|
|
|
*
|
|
|
|
* @return array An array of parameter keys
|
|
|
|
*/
|
|
|
|
public function keys()
|
|
|
|
{
|
|
|
|
return array_keys($this->headers);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Replaces the current HTTP headers by a new set.
|
|
|
|
*
|
2010-06-23 15:24:24 +01:00
|
|
|
* @param array $headers An array of HTTP headers
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-23 15:24:24 +01:00
|
|
|
public function replace(array $headers = array())
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-06-23 15:24:24 +01:00
|
|
|
$this->headers = array();
|
|
|
|
foreach ($headers as $key => $values) {
|
|
|
|
$this->set($key, $values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a header value by name.
|
|
|
|
*
|
2010-10-27 21:27:33 +01:00
|
|
|
* @param string $key The header name
|
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param Boolean $first Whether to return the first value or all header values
|
2010-06-23 15:24:24 +01:00
|
|
|
*
|
|
|
|
* @return string|array The first header value if $first is true, an array of values otherwise
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-10-27 21:27:33 +01:00
|
|
|
public function get($key, $default = null, $first = true)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$key = strtr(strtolower($key), '_', '-');
|
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
if (!array_key_exists($key, $this->headers)) {
|
2010-10-27 21:27:33 +01:00
|
|
|
if (null === $default) {
|
|
|
|
return $first ? null : array();
|
|
|
|
} else {
|
|
|
|
return $first ? $default : array($default);
|
|
|
|
}
|
2010-06-23 15:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($first) {
|
2010-10-27 21:27:33 +01:00
|
|
|
return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
|
2010-06-23 15:24:24 +01:00
|
|
|
} else {
|
|
|
|
return $this->headers[$key];
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a header by name.
|
|
|
|
*
|
2010-06-23 15:24:24 +01:00
|
|
|
* @param string $key The key
|
|
|
|
* @param string|array $values The value or an array of values
|
|
|
|
* @param Boolean $replace Whether to replace the actual value of not (true by default)
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-23 15:24:24 +01:00
|
|
|
public function set($key, $values, $replace = true)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$key = strtr(strtolower($key), '_', '-');
|
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array($values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
if (true === $replace || !isset($this->headers[$key])) {
|
|
|
|
$this->headers[$key] = $values;
|
|
|
|
} else {
|
|
|
|
$this->headers[$key] = array_merge($this->headers[$key], $values);
|
|
|
|
}
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
|
|
|
|
if ('cache-control' === $key) {
|
|
|
|
$this->cacheControl = $this->parseCacheControl($values[0]);
|
|
|
|
}
|
2010-05-03 10:40:23 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the HTTP header is defined.
|
|
|
|
*
|
|
|
|
* @param string $key The HTTP header
|
|
|
|
*
|
|
|
|
* @return Boolean true if the parameter exists, false otherwise
|
|
|
|
*/
|
|
|
|
public function has($key)
|
2010-05-03 17:10:24 +01:00
|
|
|
{
|
2010-06-23 15:24:24 +01:00
|
|
|
return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
|
2010-05-03 17:10:24 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
[HttpKernel] removed Response assertions
They are too magic and they don't really add value:
$this->assertResponseStatusCodeEmpty($client);
$this->assertTrue($client->getResponse()->isEmpty());
$this->assertResponseStatusCodeNotFound($client);
$this->assertTrue($client->getResponse()->isNotFound());
$this->assertResponseStatusCodeForbidden($client);
$this->assertTrue($client->getResponse()->isForbidden());
$this->assertResponseStatusCodeOk($client);
$this->assertTrue($client->getResponse()->isOk());
$this->assertResponseStatusCodeServerError($client);
$this->assertTrue($client->getResponse()->isServerError());
$this->assertResponseStatusCodeClientError($client);
$this->assertTrue($client->getResponse()->isClientError());
$this->assertResponseStatusCodeRedirection($client);
$this->assertTrue($client->getResponse()->isRedirection());
$this->assertResponseStatusCodeSuccessful($client);
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertResponseStatusCodeInformational($client);
$this->assertTrue($client->getResponse()->isInformational());
$this->assertResponseStatusCode(200, $client);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertResponseStatusCodeRedirect('google.com', $client);
$this->assertTrue($client->getResponse()->isRedirected('google.com'));
$this->assertResponseNotRegExp('/foo/', $client);
$this->assertNotRegExp('/foo', $client->getResponse()->getContent());
$this->assertResponseRegExp('/foo/', $client);
$this->assertRegExp('/foo', $client->getResponse()->getContent());
$this->assertResponseNotSelectExists('h1', $client);
$this->assertTrue($crawler->filter('h1')->isEmpty());
$this->assertResponseSelectExists('h1', $client);
$this->assertFalse($crawler->filter('h1')->isEmpty());
$this->assertResponseSelectCount(3, 'h1', $client);
$this->assertEquals(3, $crawler->filter('h1')->count());
$this->assertResponseSelectEquals($expected, $selector, $arguments, $client);
$this->assertEquals($expected, $crawler->filter($selector)->extract($arguments));
$this->assertResponseHeaderEquals($value, $key, $client);
$this->assertTrue($client->getResponse()->headers->contains($key, $value));
$this->assertResponseNotHeaderEquals($value, $key, $client);
$this->assertFalse($client->getResponse()->headers->contains($key, $value));
$this->assertResponseHeaderRegExp($regex, $key, $client);
$this->assertRegExp($regex, $client->getResponse()->headers->get($key));
$this->assertResponseNotHeaderRegExp($regex, $key, $client);
$this->assertNotRegExp($regex, $client->getResponse()->headers->get($key));
$this->assertResponseCookie($value, $attributes, $name, $client);
$this->assertTrue($client->getResponse()->hasCookie($name));
2010-06-23 09:39:33 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given HTTP header contains the given value.
|
|
|
|
*
|
|
|
|
* @param string $key The HTTP header name
|
|
|
|
* @param string $value The HTTP value
|
|
|
|
*
|
|
|
|
* @return Boolean true if the value is contained in the header, false otherwise
|
|
|
|
*/
|
|
|
|
public function contains($key, $value)
|
|
|
|
{
|
2010-10-27 21:27:33 +01:00
|
|
|
return in_array($value, $this->get($key, null, false));
|
[HttpKernel] removed Response assertions
They are too magic and they don't really add value:
$this->assertResponseStatusCodeEmpty($client);
$this->assertTrue($client->getResponse()->isEmpty());
$this->assertResponseStatusCodeNotFound($client);
$this->assertTrue($client->getResponse()->isNotFound());
$this->assertResponseStatusCodeForbidden($client);
$this->assertTrue($client->getResponse()->isForbidden());
$this->assertResponseStatusCodeOk($client);
$this->assertTrue($client->getResponse()->isOk());
$this->assertResponseStatusCodeServerError($client);
$this->assertTrue($client->getResponse()->isServerError());
$this->assertResponseStatusCodeClientError($client);
$this->assertTrue($client->getResponse()->isClientError());
$this->assertResponseStatusCodeRedirection($client);
$this->assertTrue($client->getResponse()->isRedirection());
$this->assertResponseStatusCodeSuccessful($client);
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertResponseStatusCodeInformational($client);
$this->assertTrue($client->getResponse()->isInformational());
$this->assertResponseStatusCode(200, $client);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertResponseStatusCodeRedirect('google.com', $client);
$this->assertTrue($client->getResponse()->isRedirected('google.com'));
$this->assertResponseNotRegExp('/foo/', $client);
$this->assertNotRegExp('/foo', $client->getResponse()->getContent());
$this->assertResponseRegExp('/foo/', $client);
$this->assertRegExp('/foo', $client->getResponse()->getContent());
$this->assertResponseNotSelectExists('h1', $client);
$this->assertTrue($crawler->filter('h1')->isEmpty());
$this->assertResponseSelectExists('h1', $client);
$this->assertFalse($crawler->filter('h1')->isEmpty());
$this->assertResponseSelectCount(3, 'h1', $client);
$this->assertEquals(3, $crawler->filter('h1')->count());
$this->assertResponseSelectEquals($expected, $selector, $arguments, $client);
$this->assertEquals($expected, $crawler->filter($selector)->extract($arguments));
$this->assertResponseHeaderEquals($value, $key, $client);
$this->assertTrue($client->getResponse()->headers->contains($key, $value));
$this->assertResponseNotHeaderEquals($value, $key, $client);
$this->assertFalse($client->getResponse()->headers->contains($key, $value));
$this->assertResponseHeaderRegExp($regex, $key, $client);
$this->assertRegExp($regex, $client->getResponse()->headers->get($key));
$this->assertResponseNotHeaderRegExp($regex, $key, $client);
$this->assertNotRegExp($regex, $client->getResponse()->headers->get($key));
$this->assertResponseCookie($value, $attributes, $name, $client);
$this->assertTrue($client->getResponse()->hasCookie($name));
2010-06-23 09:39:33 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Deletes a header.
|
|
|
|
*
|
|
|
|
* @param string $key The HTTP header name
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
2010-05-03 10:40:23 +01:00
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
$key = strtr(strtolower($key), '_', '-');
|
2010-05-03 10:40:23 +01:00
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
unset($this->headers[$key]);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
if ('cache-control' === $key) {
|
|
|
|
$this->cacheControl = array();
|
|
|
|
}
|
2010-05-03 10:40:23 +01:00
|
|
|
}
|
|
|
|
|
2010-06-23 15:24:24 +01:00
|
|
|
/**
|
|
|
|
* Sets a cookie.
|
|
|
|
*
|
|
|
|
* @param string $name The cookie name
|
|
|
|
* @param string $value The value of the cookie
|
|
|
|
* @param string $domain The domain that the cookie is available
|
|
|
|
* @param string $expire The time the cookie expires
|
|
|
|
* @param string $path The path on the server in which the cookie will be available on
|
|
|
|
* @param bool $secure Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client
|
2010-07-24 17:17:00 +01:00
|
|
|
* @param bool $httponly When TRUE the cookie will not be made accessible to JavaScript, preventing XSS attacks from stealing cookies
|
2010-06-23 15:24:24 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When the cookie expire parameter is not valid
|
|
|
|
*/
|
2010-07-24 17:18:46 +01:00
|
|
|
public function setCookie($name, $value, $domain = null, $expires = null, $path = '/', $secure = false, $httponly = true)
|
2010-06-23 15:24:24 +01:00
|
|
|
{
|
2010-11-21 12:32:58 +00:00
|
|
|
$this->validateCookie($name, $value);
|
2010-06-23 15:24:24 +01:00
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
return $this->set('Cookie', sprintf('%s=%s', $name, urlencode($value)));
|
2010-06-23 15:24:24 +01:00
|
|
|
}
|
|
|
|
|
2010-05-10 18:40:18 +01:00
|
|
|
/**
|
|
|
|
* Returns the HTTP header value converted to a date.
|
|
|
|
*
|
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param \DateTime $default The default value
|
|
|
|
*
|
|
|
|
* @return \DateTime The filtered value
|
|
|
|
*/
|
|
|
|
public function getDate($key, \DateTime $default = null)
|
|
|
|
{
|
|
|
|
if (null === $value = $this->get($key)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
|
|
|
|
throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
public function addCacheControlDirective($key, $value = true)
|
|
|
|
{
|
|
|
|
$this->cacheControl[$key] = $value;
|
|
|
|
|
|
|
|
$this->set('Cache-Control', $this->getCacheControlHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasCacheControlDirective($key)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->cacheControl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheControlDirective($key)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeCacheControlDirective($key)
|
|
|
|
{
|
|
|
|
unset($this->cacheControl[$key]);
|
|
|
|
|
|
|
|
$this->set('Cache-Control', $this->getCacheControlHeader());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getCacheControlHeader()
|
|
|
|
{
|
|
|
|
$parts = array();
|
|
|
|
ksort($this->cacheControl);
|
|
|
|
foreach ($this->cacheControl as $key => $value) {
|
|
|
|
if (true === $value) {
|
|
|
|
$parts[] = $key;
|
|
|
|
} else {
|
|
|
|
if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
|
|
|
|
$value = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts[] = "$key=$value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(', ', $parts);
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
* Parses a Cache-Control HTTP header.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
* @param string $header The value of the Cache-Control HTTP header
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
* @return array An array representing the attribute values
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
protected function parseCacheControl($header)
|
|
|
|
{
|
|
|
|
$cacheControl = array();
|
|
|
|
preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
$cacheControl[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cacheControl;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateCookie($name, $value)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
changed Cache-Control default value behavior
The PHP native cache limiter feature has been disabled as this is now managed
by the HeaderBag class directly instead (see below.)
The HeaderBag class uses the following rules to define a sensible and
convervative default value for the Response 'Cache-Control' header:
* If no cache header is defined ('Cache-Control', 'ETag', 'Last-Modified',
and 'Expires'), 'Cache-Control' is set to 'no-cache';
* If 'Cache-Control' is empty, its value is set to "private, max-age=0,
must-revalidate";
* But if at least one 'Cache-Control' directive is set, and no 'public' or
'private' directives have been explicitely added, Symfony2 adds the
'private' directive automatically (except when 's-maxage' is set.)
So, remember to explicitly add the 'public' directive to 'Cache-Control' when
you want shared caches to store your application resources:
// The Response is private by default
$response->setEtag($etag);
$response->setLastModified($date);
$response->setMaxAge(10);
// Change the Response to be public
$response->setPublic();
// Set cache settings in one call
$response->setCache(array(
'etag' => $etag,
'last_modified' => $date,
'max_age' => 10,
'public' => true,
));
2010-11-10 09:48:22 +00:00
|
|
|
// from PHP source code
|
|
|
|
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match("/[,; \t\r\n\013\014]/", $value)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The cookie value "%s" contains invalid characters.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$name) {
|
|
|
|
throw new \InvalidArgumentException('The cookie name cannot be empty');
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-05-03 10:40:23 +01:00
|
|
|
}
|