[HttpKernel] Add a \"short\" trace header format, make header configurable
This commit is contained in:
parent
65b46a532c
commit
9a2fcc9392
@ -24,6 +24,7 @@ CHANGELOG
|
||||
* renamed `GetResponseForExceptionEvent` to `ExceptionEvent`
|
||||
* renamed `PostResponseEvent` to `TerminateEvent`
|
||||
* added `HttpClientKernel` for handling requests with an `HttpClientInterface` instance
|
||||
* added `trace_header` and `trace_level` configuration options to `HttpCache`
|
||||
|
||||
4.2.0
|
||||
-----
|
||||
|
@ -40,7 +40,14 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
*
|
||||
* The available options are:
|
||||
*
|
||||
* * debug: If true, the traces are added as a HTTP header to ease debugging
|
||||
* * debug If true, exceptions are thrown when things go wrong. Otherwise, the cache
|
||||
* will try to carry on and deliver a meaningful response.
|
||||
*
|
||||
* * trace_level May be one of 'none', 'short' and 'full'. For 'short', a concise trace of the
|
||||
* master request will be added as an HTTP header. 'full' will add traces for all
|
||||
* requests (including ESI subrequests). (default: 'full' if in debug; 'none' otherwise)
|
||||
*
|
||||
* * trace_header Header name to use for traces. (default: X-Symfony-Cache)
|
||||
*
|
||||
* * default_ttl The number of seconds that a cache entry should be considered
|
||||
* fresh when no explicit freshness information is provided in
|
||||
@ -87,7 +94,13 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
'allow_revalidate' => false,
|
||||
'stale_while_revalidate' => 2,
|
||||
'stale_if_error' => 60,
|
||||
'trace_level' => 'none',
|
||||
'trace_header' => 'X-Symfony-Cache',
|
||||
], $options);
|
||||
|
||||
if (!isset($options['trace_level']) && $this->options['debug']) {
|
||||
$this->options['trace_level'] = 'full';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,6 +123,23 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
return $this->traces;
|
||||
}
|
||||
|
||||
private function addTraces(Response $response)
|
||||
{
|
||||
$traceString = null;
|
||||
|
||||
if ('full' === $this->options['trace_level']) {
|
||||
$traceString = $this->getLog();
|
||||
}
|
||||
|
||||
if ('short' === $this->options['trace_level'] && $masterId = array_key_first($this->traces)) {
|
||||
$traceString = implode('/', $this->traces[$masterId]);
|
||||
}
|
||||
|
||||
if (null !== $traceString) {
|
||||
$response->headers->add([$this->options['trace_header'] => $traceString]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a log message for the events of the last request processing.
|
||||
*
|
||||
@ -194,8 +224,8 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
|
||||
|
||||
$this->restoreResponseBody($request, $response);
|
||||
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) {
|
||||
$response->headers->set('X-Symfony-Cache', $this->getLog());
|
||||
if (HttpKernelInterface::MASTER_REQUEST === $type) {
|
||||
$this->addTraces($response);
|
||||
}
|
||||
|
||||
if (null !== $this->surrogate) {
|
||||
|
@ -1508,6 +1508,44 @@ class HttpCacheTest extends HttpCacheTestCase
|
||||
// Surrogate request
|
||||
$cache->handle($request, HttpKernelInterface::SUB_REQUEST);
|
||||
}
|
||||
|
||||
public function testTraceHeaderNameCanBeChanged()
|
||||
{
|
||||
$this->cacheConfig['trace_header'] = 'X-My-Header';
|
||||
$this->setNextResponse();
|
||||
$this->request('GET', '/');
|
||||
|
||||
$this->assertTrue($this->response->headers->has('X-My-Header'));
|
||||
}
|
||||
|
||||
public function testTraceLevelDefaultsToFullIfDebug()
|
||||
{
|
||||
$this->setNextResponse();
|
||||
$this->request('GET', '/');
|
||||
|
||||
$this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
|
||||
$this->assertEquals('GET /: miss', $this->response->headers->get('X-Symfony-Cache'));
|
||||
}
|
||||
|
||||
public function testTraceLevelDefaultsToNoneIfNotDebug()
|
||||
{
|
||||
$this->cacheConfig['debug'] = false;
|
||||
$this->setNextResponse();
|
||||
$this->request('GET', '/');
|
||||
|
||||
$this->assertFalse($this->response->headers->has('X-Symfony-Cache'));
|
||||
}
|
||||
|
||||
public function testTraceLevelShort()
|
||||
{
|
||||
$this->cacheConfig['trace_level'] = 'short';
|
||||
|
||||
$this->setNextResponse();
|
||||
$this->request('GET', '/');
|
||||
|
||||
$this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
|
||||
$this->assertEquals('miss', $this->response->headers->get('X-Symfony-Cache'));
|
||||
}
|
||||
}
|
||||
|
||||
class TestKernel implements HttpKernelInterface
|
||||
|
@ -122,7 +122,9 @@ class HttpCacheTestCase extends TestCase
|
||||
|
||||
$this->store = new Store(sys_get_temp_dir().'/http_cache');
|
||||
|
||||
$this->cacheConfig['debug'] = true;
|
||||
if (!isset($this->cacheConfig['debug'])) {
|
||||
$this->cacheConfig['debug'] = true;
|
||||
}
|
||||
|
||||
$this->esi = $esi ? new Esi() : null;
|
||||
$this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
|
||||
|
@ -22,6 +22,7 @@
|
||||
"symfony/http-foundation": "^4.1.1",
|
||||
"symfony/debug": "~3.4|~4.0",
|
||||
"symfony/polyfill-ctype": "~1.8",
|
||||
"symfony/polyfill-php73": "^1.9",
|
||||
"psr/log": "~1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
|
Reference in New Issue
Block a user