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

Issue #2585899 by willzyx, mr.baileys, esod, dawehner:...

Issue #2585899 by willzyx, mr.baileys, esod, dawehner: HtmlResponseAttachmentsProcessor::processHtmlHeadLink produces invalid HTTP Link header
parent f6b3dd94
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -419,9 +419,13 @@ protected function processHtmlHeadLink(array $html_head_link) {
if ($should_add_header) {
// Also add a HTTP header "Link:".
$href = '<' . Html::escape($attributes['href'] . '>');
$href = '<' . Html::escape($attributes['href']) . '>';
unset($attributes['href']);
$attached['http_header'][] = ['Link', $href . drupal_http_header_attributes($attributes), TRUE];
if ($param = drupal_http_header_attributes($attributes)) {
$href .= ';' . $param;
}
$attached['http_header'][] = ['Link', $href, FALSE];
}
}
return $attached;
......
......@@ -2,6 +2,8 @@
namespace Drupal\node\Tests;
use Drupal\Component\Utility\Html;
/**
* Tests the node/{node} page.
*
......@@ -63,6 +65,24 @@ public function testHtmlHeadLinks() {
$this->assertEqual($result[0]['href'], $node->url('edit-form'));
}
/**
* Tests the Link header.
*/
public function testLinkHeader() {
$node = $this->drupalCreateNode();
$expected = [
'<' . Html::escape($node->url('canonical')) . '>; rel="canonical"',
'<' . Html::escape($node->url('canonical'), ['alias' => TRUE]) . '>; rel="shortlink"',
'<' . Html::escape($node->url('revision')) . '>; rel="revision"',
];
$this->drupalGet($node->urlInfo());
$links = explode(',', $this->drupalGetHeader('Link'));
$this->assertEqual($links, $expected);
}
/**
* Tests that we store and retrieve multi-byte UTF-8 characters correctly.
*/
......
......@@ -54,6 +54,14 @@ public function testAttachments() {
// Repeat for the cache.
$this->drupalGet('/render_attached_test/head');
$this->assertHeader('X-Drupal-Cache', 'HIT');
// Test ['#attached']['html_head_link'] when outputted as HTTP header.
$this->drupalGet('/render_attached_test/html_header_link');
$expected_link_headers = [
'</foo?bar=&lt;baz&gt;&amp;baz=false>; rel="alternate"',
'</foo/bar>; hreflang="nl"; rel="alternate"',
];
$this->assertEqual($this->drupalGetHeader('link'), implode(',', $expected_link_headers));
}
/**
......
......@@ -19,6 +19,13 @@ render_attached.head:
requirements:
_access: 'TRUE'
render_attached.html_header_link:
path: '/render_attached_test/html_header_link'
defaults:
_controller: '\Drupal\render_attached_test\Controller\RenderAttachedTestController::htmlHeaderLink'
requirements:
_access: 'TRUE'
render_attached.feed_single:
path: '/render_attached_test/feed'
defaults:
......
......@@ -69,4 +69,18 @@ public function feed() {
return $render;
}
/**
* Test HTTP header rendering for link.
*
* @return array
* A render array using the 'html_head_link' directive.
*/
public function htmlHeaderLink() {
$render = [];
$render['#attached']['html_head_link'][] = [['href' => '/foo?bar=<baz>&baz=false', 'rel' => 'alternate'], TRUE];
$render['#attached']['html_head_link'][] = [['href' => '/not-added-to-http-headers', 'rel' => 'alternate'], FALSE];
$render['#attached']['html_head_link'][] = [['href' => '/foo/bar', 'hreflang' => 'nl', 'rel' => 'alternate'], TRUE];
return $render;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment