Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[WebProfilerBundle] Imply forward request by a new X-Previous-Debug-Token header #22447

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ FrameworkBundle
---------------

* A `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.
* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

HttpFoundation
--------------
Expand Down
1 change: 1 addition & 0 deletions 1 UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ FrameworkBundle
---------------

* Using a `RouterInterface` that does not implement the `WarmableInterface` is not supported anymore.
* The `RequestDataCollector` class has been removed. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

HttpFoundation
--------------
Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CHANGELOG
* Allowed the `Router` to work with any PSR-11 container
* Added option in workflow dump command to label graph with a custom label
* Using a `RouterInterface` that does not implement the `WarmableInterface` is deprecated and will not be supported in Symfony 5.0.

* The `RequestDataCollector` class has been deprecated and will be removed in Symfony 5.0. Use the `Symfony\Component\HttpKernel\DataCollector\RequestDataCollector` class instead.

4.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected function generateUrl(string $route, array $parameters = array(), int $
protected function forward(string $controller, array $path = array(), array $query = array()): Response
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
$path['_controller'] = $controller;
$subRequest = $request->duplicate($query, null, $path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,17 @@

namespace Symfony\Bundle\FrameworkBundle\DataCollector;

use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestCollector;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector as BaseRequestDataCollector;

@trigger_error(sprintf('The "%s" class is deprecated since version 4.1 and will be removed in Symfony 5.0. Use %s instead.', RequestDataCollector::class, BaseRequestDataCollector::class), E_USER_DEPRECATED);

/**
* RequestDataCollector.
*
* @author Jules Pietri <jusles@heahprod.com>
*
* @deprecated since version 4.1, to be removed in Symfony 5.0
*/
class RequestDataCollector extends BaseRequestCollector implements EventSubscriberInterface
class RequestDataCollector extends BaseRequestDataCollector
{
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
parent::collect($request, $response, $exception);

if ($parentRequestAttributes = $request->attributes->get('_forwarded')) {
if ($parentRequestAttributes instanceof ParameterBag) {
$parentRequestAttributes->set('_forward_token', $response->headers->get('x-debug-token'));
}
}
if ($request->attributes->has('_forward_controller')) {
$this->data['forward'] = array(
'token' => $request->attributes->get('_forward_token'),
'controller' => $this->parseController($request->attributes->get('_forward_controller')),
);
}
}

/**
* Gets the parsed forward controller.
*
* @return array|bool An array with keys 'token' the forward profile token, and
* 'controller' the parsed forward controller, false otherwise
*/
public function getForward()
{
return isset($this->data['forward']) ? $this->data['forward'] : false;
}

public function onKernelController(FilterControllerEvent $event)
{
$this->controllers[$event->getRequest()] = $event->getController();

if ($parentRequestAttributes = $event->getRequest()->attributes->get('_forwarded')) {
if ($parentRequestAttributes instanceof ParameterBag) {
$parentRequestAttributes->set('_forward_controller', $event->getController());
}
}
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'request';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<call method="setKernel"><argument type="service" id="kernel" on-invalid="ignore" /></call>
</service>

<service id="data_collector.request" class="Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector">
<service id="data_collector.request" class="Symfony\Component\HttpKernel\DataCollector\RequestDataCollector">
<tag name="kernel.event_subscriber" />
<tag name="data_collector" template="@WebProfiler/Collector/request.html.twig" id="request" priority="335" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class SubRequestServiceResolutionController implements ContainerAwareInterface
public function indexAction()
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
$path['_controller'] = 'TestBundle:SubRequestServiceResolution:fragment';
$subRequest = $request->duplicate(array(), null, $path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
{% endset %}
{% endif %}

{% if collector.forward|default(false) %}
{% if collector.forwardtoken %}
{% set forward_profile = profile.childByToken(collector.forwardtoken) %}
{% set forward_handler %}
{{ helper.set_handler(collector.forward.controller) }}
{{ helper.set_handler(forward_profile ? forward_profile.collector('request').controller : 'n/a') }}
{% endset %}
{% endif %}

Expand All @@ -24,7 +25,7 @@
<span class="sf-toolbar-status sf-toolbar-status-{{ request_status_code_color }}">{{ collector.statuscode }}</span>
{% if collector.route %}
{% if collector.redirect %}{{ include('@WebProfiler/Icon/redirect.svg') }}{% endif %}
{% if collector.forward|default(false) %}{{ include('@WebProfiler/Icon/forward.svg') }}{% endif %}
{% if collector.forwardtoken %}{{ include('@WebProfiler/Icon/forward.svg') }}{% endif %}
<span class="sf-toolbar-label">{{ 'GET' != collector.method ? collector.method }} @</span>
<span class="sf-toolbar-value sf-toolbar-info-piece-additional">{{ collector.route }}</span>
{% endif %}
Expand Down Expand Up @@ -81,7 +82,7 @@
<b>Forwarded to</b>
<span>
{{ forward_handler }}
(<a href="{{ path('_profiler', { token: collector.forward.token }) }}">{{ collector.forward.token }}</a>)
(<a href="{{ path('_profiler', { token: collector.forwardtoken }) }}">{{ collector.forwardtoken }}</a>)
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,22 @@
</dl>
{%- endif %}

{% if request_collector and request_collector.forward|default(false) and request_collector.forward.controller.class is defined -%}
{%- set forward = request_collector.forward -%}
{%- set controller = forward.controller -%}
{% if request_collector and request_collector.forwardtoken -%}
{% set forward_profile = profile.childByToken(request_collector.forwardtoken) %}
{% set controller = forward_profile ? forward_profile.collector('request').controller : 'n/a' %}
<dl class="metadata">
<dt>Forwarded to</dt>
<dd>
{% set link = controller.file|file_link(controller.line) -%}
{% set link = controller.file is defined ? controller.file|file_link(controller.line) : null -%}
{%- if link %}<a href="{{ link }}" title="{{ controller.file }}">{% endif -%}
{{- controller.class|abbr_class|striptags -}}
{{- controller.method ? ' :: ' ~ controller.method }}
{% if controller.class is defined %}
{{- controller.class|abbr_class|striptags -}}
{{- controller.method ? ' :: ' ~ controller.method -}}
{% else %}
{{- controller -}}
{% endif %}
{%- if link %}</a>{% endif %}
(<a href="{{ path('_profiler', { token: forward.token }) }}">{{ forward.token }}</a>)
(<a href="{{ path('_profiler', { token: request_collector.forwardtoken }) }}">{{ request_collector.forwardtoken }}</a>)
</dd>
</dl>
{%- endif %}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bundle/WebProfilerBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7.1.3",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/http-kernel": "~4.1",
"symfony/routing": "~3.4|~4.0",
"symfony/twig-bridge": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public function collect(Request $request, Response $response, \Exception $except
}

$this->data['identifier'] = $this->data['route'] ?: (is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']);

if ($response->headers->has('x-previous-debug-token')) {
$this->data['forward_token'] = $response->headers->get('x-previous-debug-token');
}
}

public function lateCollect()
Expand Down Expand Up @@ -322,6 +326,11 @@ public function getRedirect()
return isset($this->data['redirect']) ? $this->data['redirect'] : false;
}

public function getForwardToken()
{
return isset($this->data['forward_token']) ? $this->data['forward_token'] : null;
}

public function onKernelController(FilterControllerEvent $event)
{
$this->controllers[$event->getRequest()] = $event->getController();
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpKernel/Profiler/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ public function addChild(Profile $child)
$child->setParent($this);
}

public function getChildByToken(string $token): ?self
{
foreach ($this->children as $child) {
if ($token === $child->getToken()) {
return $child;
}
}

return null;
}

/**
* Gets a Collector by name.
*
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public function collect(Request $request, Response $response, \Exception $except
$profile->setIp('Unknown');
}

if ($prevToken = $response->headers->get('X-Debug-Token')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this make any sub request a forwarded one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :) basically if the response has a x-debug-token header already, we assume a forwarded request. Thats the trick here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And thus if we have a X-Previous-Debug-Token in RequestDataCollector we know it was forwarded (it had a x-debug-token previously).

$response->headers->set('X-Previous-Debug-Token', $prevToken);
}

$response->headers->set('X-Debug-Token', $profile->getToken());

foreach ($this->collectors as $collector) {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.