AmpHtmlResponseAttachmentsProcessor produces invalid Link Header

Created on 21 March 2024, 3 months ago

Problem/Motivation

AmpHtmlResponseAttachmentsProcessor strips semicolons from the amphtml http_header value, resulting in a malformed HTTP header.
According to the Link spec:

The link header contains parameters, which are separated with ; and are equivalent to attributes of the
element.

Relevant code:

  protected function processHtmlHeadLink(array $html_head_link) {
    $attached = parent::processHtmlHeadLink($html_head_link);
    if (array_key_exists('http_header', $attached)) {
      // Find the amphtml link and flag it to be displayed as a HTTP header.
      foreach ($attached['http_header'] as $key => $value) {
        if (strpos($value[1], 'rel="amphtml"') !== FALSE) {
          $new_value = str_replace(';', '', $value[1]);
          $attached['http_header'][$key] = ['Link', $new_value, TRUE];
        }
      }
    }
    return $attached;
  }

I'm assuming that semicolons are stripped for a reason, and this is an unintended consequence.

Steps to reproduce

View a non-AMP node which has an AMP version. You should see <link rel="amphtml" href="https://example.com/some/page?amp" /> in the HTML source.
Check the HTTP response headers in your browser's dev tools. You'll see that the Link header as a value of <https://example.com/some/page?amp> rel="amphtml". This should be <https://example.com/some/page?amp>; rel="amphtml"

Proposed resolution

Modify AmpHtmlResponseAttachmentsProcessor so that it no longer strips semicolons.

Patch will follow shortly.

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom reevo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024