-
Notifications
You must be signed in to change notification settings - Fork 24
EZP-28183: Requests (incl from REST) with X-Location-Id not tagged #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
spec/EventSubscriber/XLocationIdResponseSubscriberSpec.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
| /** | ||
| * @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| namespace spec\EzSystems\PlatformHttpCacheBundle\EventSubscriber; | ||
|
|
||
| use PhpSpec\ObjectBehavior; | ||
| use Prophecy\Argument; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
| use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
|
|
||
| class XLocationIdResponseSubscriberSpec extends ObjectBehavior | ||
| { | ||
| public function let( | ||
| FilterResponseEvent $event, | ||
| Response $response, | ||
| ResponseHeaderBag $responseHeaders | ||
| ) { | ||
| $response->headers = $responseHeaders; | ||
| $event->getResponse()->willReturn($response); | ||
|
|
||
| $this->beConstructedWith('Surrogate-Key'); | ||
| } | ||
|
|
||
| public function it_does_not_rewrite_header_if_there_is_none( | ||
| FilterResponseEvent $event, | ||
| ResponseHeaderBag $responseHeaders | ||
| ) { | ||
| $responseHeaders->has('X-Location-Id')->willReturn(false); | ||
| $responseHeaders->set()->shouldNotBecalled(); | ||
|
|
||
| $this->rewriteCacheHeader($event); | ||
| } | ||
|
|
||
| public function it_rewrite_header_if_there( | ||
| FilterResponseEvent $event, | ||
| ResponseHeaderBag $responseHeaders | ||
| ) { | ||
| $responseHeaders->has('X-Location-Id')->willReturn(true); | ||
| $responseHeaders->get('X-Location-Id')->willReturn('123'); | ||
| $responseHeaders->has('Surrogate-Key')->willReturn(false); | ||
|
|
||
| $responseHeaders->set('Surrogate-Key', ['location-123'])->willReturn(null); | ||
| $responseHeaders->remove('X-Location-Id')->willReturn(null); | ||
|
|
||
| $this->rewriteCacheHeader($event); | ||
| } | ||
|
|
||
| public function it_rewrite_header_also_in_unofficial_plural_form_and_merges_exisitng_value( | ||
| FilterResponseEvent $event, | ||
| ResponseHeaderBag $responseHeaders | ||
| ) { | ||
| $responseHeaders->has('X-Location-Id')->willReturn(true); | ||
| $responseHeaders->get('X-Location-Id')->willReturn('123,34'); | ||
| $responseHeaders->has('Surrogate-Key')->willReturn(true); | ||
| $responseHeaders->get('Surrogate-Key', null, false)->willReturn(['content-44']); | ||
|
|
||
| $responseHeaders->set('Surrogate-Key', ['content-44', 'location-123', 'location-34'])->willReturn(null); | ||
| $responseHeaders->remove('X-Location-Id')->willReturn(null); | ||
|
|
||
| $this->rewriteCacheHeader($event); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
| /** | ||
| * @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| namespace EzSystems\PlatformHttpCacheBundle\EventSubscriber; | ||
|
|
||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
| use Symfony\Component\HttpKernel\KernelEvents; | ||
|
|
||
| /** | ||
| * Rewrites the X-Location-Id HTTP header. | ||
| * | ||
| * This is a BC layer for custom controllers (including REST server) still | ||
| * using X-Location-Id header which is now deprecated. For | ||
| * full value of tagging, see docs/using_tags.md for how to take advantage of the | ||
| * system. | ||
| */ | ||
| class XLocationIdResponseSubscriber implements EventSubscriberInterface | ||
| { | ||
| const LOCATION_ID_HEADER = 'X-Location-Id'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $tagHeader; | ||
|
|
||
| public function __construct($tagHeader) | ||
| { | ||
| $this->tagHeader = $tagHeader; | ||
| } | ||
|
|
||
| public static function getSubscribedEvents() | ||
| { | ||
| return [KernelEvents::RESPONSE => ['rewriteCacheHeader', -5]]; | ||
| } | ||
|
|
||
| public function rewriteCacheHeader(FilterResponseEvent $event) | ||
| { | ||
| $response = $event->getResponse(); | ||
| if (!$response->headers->has(static::LOCATION_ID_HEADER)) { | ||
| return; | ||
| } | ||
|
|
||
| @trigger_error( | ||
| 'X-Location-Id is no longer preferred way to tag content responses, see ezplatform-http-cache/docs/using_tags.md', | ||
| E_USER_DEPRECATED | ||
| ); | ||
|
|
||
| // Map the tag, even if not officially supported, handle comma separated values as was possible with Varnish | ||
| $tags = array_map( | ||
| function ($id) { | ||
| return 'location-' . trim($id); | ||
| }, | ||
| explode(',', $response->headers->get(static::LOCATION_ID_HEADER)) | ||
| ); | ||
|
|
||
| if ($response->headers->has($this->tagHeader)) { | ||
| $tags = array_merge($response->headers->get($this->tagHeader, null, false) , $tags); | ||
| } | ||
|
|
||
| // @todo we need to use abstract tag writer to also be able to support Fastly | ||
| // FOS has some stuff around this in 2.x but not in a good way in 1.x | ||
| $response->headers->set($this->tagHeader, array_unique($tags)); | ||
| $response->headers->remove(static::LOCATION_ID_HEADER); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| parameters: | ||
| ezplatform.http_cache.store.root: "%kernel.cache_dir%/http_cache" | ||
| ezplatform.http_cache.tags.header: 'xkey' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this. Now we have logic for setting xkey both here and in
ezplatform-http-cache/src/ResponseConfigurator/ConfigurableResponseCacheConfigurator.php
Line 63 in a4ed9b6
This should be done in one place.
I fancy to replace the whole ConfigurableResponseCacheConfigurator with FOS\HttpCacheBundle\Handler\TagHandler, but that is not quite straight forward.
So instead I am thinking just adding a TagHandler and make sure ConfigurableResponseCacheConfigurator and XLocationIdResponseSubscriber uses that one for setting the headers. What do you say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, Adding our own TagHandler based on FOS\HttpCacheBundle\Handler was not a good idea for several reasons.
One was that we are not able to run it's constructor without also adding a CacheInvalidator ( kinda replaces our PurgeClient) and that it's tag handling doesn't really suit our needs in regards to either xkey nor fastly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll then create our abstraction for this
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, thread marked 🏷 as obsolete 😏