Skip to content
Open
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
40 changes: 21 additions & 19 deletions src/wp-includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,26 +926,28 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
$is_html_content_type = null;
$html_content_types = array( 'text/html', 'application/xhtml+xml' );
foreach ( headers_list() as $header ) {
$header_parts = explode( ':', strtolower( $header ), 2 );
if (
count( $header_parts ) === 2 &&
'content-type' === $header_parts[0]
) {
/*
* This is looking for very specific content types, therefore it
* doesn’t need to fully parse the header’s value. Instead, it needs
* only assert that the content type is one of the static HTML types.
*
* Example:
*
* Content-Type: text/html; charset=utf8
* Content-Type: text/html ;charset=latin4
* Content-Type:application/xhtml+xml
*/
$media_type = trim( strtok( $header_parts[1], ';' ), " \t" );
$is_html_content_type = in_array( $media_type, $html_content_types, true );
break; // PHP only sends the first Content-Type header in the list.
$header_parts = explode( ':', $header, 2 );
if ( count( $header_parts ) !== 2 ) {
continue;
}

$name = strtolower( trim( $header_parts[0] ) );
Copy link
Member

Choose a reason for hiding this comment

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

Trimming of the header name was intentionally removed in accordance of the HTTP spec

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case, can we add an inline comment explaining this behaviour so it doesn’t come up again in future reviews? Just curious what’s the reason behind this difference?

Copy link
Member

Choose a reason for hiding this comment

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

The rationale is in this thread: #10293 (comment)

if ( 'content-type' !== $name ) {
continue;
}

/*
* This is looking for very specific content types, therefore it
* doesn’t need to fully parse the header’s value. Instead, it needs
* only assert that the content type is one of the static HTML types.
*
* Content-Type: text/html; charset=utf8
* Content-Type: text/html ;charset=latin4
* Content-Type:application/xhtml+xml
*/
$media_type = trim( strtok( $header_parts[1], ';' ), " \t" );
$is_html_content_type = in_array( $media_type, $html_content_types, true );
break; // PHP only sends the first Content-Type header in the list.
}
if ( null === $is_html_content_type ) {
$is_html_content_type = in_array( ini_get( 'default_mimetype' ), $html_content_types, true );
Expand Down
Loading