Thanks to visit codestin.com
Credit goes to github.com

Skip to content

preload twig function does not append "Link" header in response headers #23737

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

Closed
thomaskrammer opened this issue Aug 1, 2017 · 10 comments
Closed

Comments

@thomaskrammer
Copy link

Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.5

I wanted to use the preload function for twig but I do not get the "Link" headers appended to the server response. The problem is that $event->isMasterRequest() in AddHeaderLinkListener class returns false (https://github.com/symfony/symfony/blob/master/src/Symfony/Component/WebLink/EventListener/AddLinkHeaderListener.php#L38)

Can you help me @dunglas ?

@javiereguiluz
Copy link
Member

Friendly ping to @dunglas to see if he has returned from his holidays. Thanks!

@jvasseur
Copy link
Contributor

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/WebLink/EventListener/AddLinkHeaderListener.php#L38 is not the problem, links sould only be added to the master request since headers from sub-requests are ignored. And since the preload function is always adding links to the master request (https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Extension/WebLinkExtension.php#L60) it should work without problems for sub-requests.

@Simperfit
Copy link
Contributor

@thomas-dotworx What's the issue since @jvasseur is right ?

@thomaskrammer
Copy link
Author

The AddLinkHeaderListener does not append the link header for me.
When I copy the exact piece of code to my custom response listener the headers get appended though.

Here's my code:

class ResponseListener
{
    private $_enabled;
    private $_serviceContainer;
    private $_authChecker;

    public function __construct(Container $serviceContainer, $enabled, AuthorizationCheckerInterface $authChecker)
    {
        $this->_enabled = $enabled;
        $this->_serviceContainer = $serviceContainer;
        $this->_authChecker = $authChecker;
    }

    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();

        if ($event->isMasterRequest()) {
            // Set headers
            $responseHeaders = $response->headers;
            $responseHeaders->set('strict-transport-security', 'max-age=31536000; includeSubDomains');
            $responseHeaders->set('X-XSS-Protection', '1; mode=block');
            $responseHeaders->set('X-Content-Type-Options', 'nosniff');

            $linkProvider = $event->getRequest()->attributes->get('_links');
            if (!$linkProvider instanceof \Psr\Link\LinkProviderInterface || !$links = $linkProvider->getLinks()) {
                return;
            }
            $serializer = new \Symfony\Component\WebLink\HttpHeaderSerializer();
            $responseHeaders->set('Link', $serializer->serialize($links), false);

        }
    }
}

@thomaskrammer
Copy link
Author

My config file:

acme.response_listener:
       class: AcmeBundle\Listener\ResponseListener
       arguments: ["@service_container", '%trim_response_enabled%', '@security.authorization_checker']
       tags:
           - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

@Simperfit
Copy link
Contributor

@thomas-dotworx Do you want to try povided a PR with the fix ?

@Simperfit
Copy link
Contributor

Status: Reviewed

@thomaskrammer
Copy link
Author

Just figured out that I did not enable web_link in my config file. That's missing in the docs or did I just oversee that? Thanks!

@thomaskrammer
Copy link
Author

So I guess this will be done here symfony/symfony-docs#7515

@votemike
Copy link
Contributor

votemike commented Jan 3, 2018

For anyone finding this issue because they think preload isn't working in a 3.3 application (or one not using Flex), as @thomas-dotworx said, you need to enable web_link in your config file.

framework:
    web_link:
        enabled: true

In applications using flex instead of the symfony bundle as a whole, it seems to be enabled by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants