Skip to content
Snippets Groups Projects
Unverified Commit 88b76677 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3089206 by DamienMcKenna, Wim Leers, ravi.shankar, mr.baileys: Add...

Issue #3089206 by DamienMcKenna, Wim Leers, ravi.shankar, mr.baileys: Add "protocol version" cache context to allow optimizing responses for HTTP/1 vs HTTP/2
parent 66f55381
Branches
Tags
6 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1012Issue #3226887: Hreflang on non-canonical content pages,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10,!596Issue #3046532: deleting an entity reference field, used in a contextual view, makes the whole site unrecoverable,!496Issue #2463967: Use .user.ini file for PHP settings,!144Issue #2666286: Clean up menu_ui to conform to Drupal coding standards
......@@ -49,6 +49,11 @@ services:
arguments: ['@request_stack']
tags:
- { name: cache.context }
cache_context.protocol_version:
class: Drupal\Core\Cache\Context\ProtocolVersionCacheContext
arguments: ['@request_stack']
tags:
- { name: cache.context }
cache_context.headers:
class: Drupal\Core\Cache\Context\HeadersCacheContext
arguments: ['@request_stack']
......
<?php
namespace Drupal\Core\Cache\Context;
use Drupal\Core\Cache\CacheableMetadata;
/**
* Defines the ProtocolVersionCacheContext service, for "per protocol" caching.
*
* Useful to differentiate between HTTP/1.1 and HTTP/2.0 responses for example,
* to allow responses to be optimized for protocol-specific characteristics.
*
* Cache context ID: 'protocol_version'.
*/
class ProtocolVersionCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t('Protocol version');
}
/**
* {@inheritdoc}
*/
public function getContext() {
return $this->requestStack->getCurrentRequest()->getProtocolVersion();
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
return new CacheableMetadata();
}
}
<?php
namespace Drupal\Tests\Core\Cache\Context;
use Drupal\Core\Cache\Context\ProtocolVersionCacheContext;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\Core\Cache\Context\ProtocolVersionCacheContext
* @group Cache
*/
class ProtocolVersionCacheContextTest extends UnitTestCase {
/**
* @covers ::getContext
*
* @dataProvider providerTestGetContext
*/
public function testGetContext($protocol, $context) {
$request_stack = new RequestStack();
$request = Request::create('/');
$request->server->set('SERVER_PROTOCOL', $protocol);
$request_stack->push($request);
$cache_context = new ProtocolVersionCacheContext($request_stack);
$this->assertSame($cache_context->getContext(), $context);
}
/**
* Provides a list of query arguments and expected cache contexts.
*/
public function providerTestGetContext() {
return [
['HTTP/1.0', 'HTTP/1.0'],
['HTTP/1.1', 'HTTP/1.1'],
['HTTP/2.0', 'HTTP/2.0'],
['HTTP/3.0', 'HTTP/3.0'],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment