Skip to content

Commit 1bf9e88

Browse files
Refactor content-type header parsing logic
1 parent fb9220f commit 1bf9e88

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/wp-includes/template.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -926,26 +926,28 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
926926
$is_html_content_type = null;
927927
$html_content_types = array( 'text/html', 'application/xhtml+xml' );
928928
foreach ( headers_list() as $header ) {
929-
$header_parts = explode( ':', strtolower( $header ), 2 );
930-
if (
931-
count( $header_parts ) === 2 &&
932-
'content-type' === $header_parts[0]
933-
) {
934-
/*
935-
* This is looking for very specific content types, therefore it
936-
* doesn’t need to fully parse the header’s value. Instead, it needs
937-
* only assert that the content type is one of the static HTML types.
938-
*
939-
* Example:
940-
*
941-
* Content-Type: text/html; charset=utf8
942-
* Content-Type: text/html ;charset=latin4
943-
* Content-Type:application/xhtml+xml
944-
*/
945-
$media_type = trim( strtok( $header_parts[1], ';' ), " \t" );
946-
$is_html_content_type = in_array( $media_type, $html_content_types, true );
947-
break; // PHP only sends the first Content-Type header in the list.
929+
$header_parts = explode( ':', $header, 2 );
930+
if ( count( $header_parts ) !== 2 ) {
931+
continue;
932+
}
933+
934+
$name = strtolower( trim( $header_parts[0] ) );
935+
if ( 'content-type' !== $name ) {
936+
continue;
948937
}
938+
939+
/*
940+
* This is looking for very specific content types, therefore it
941+
* doesn’t need to fully parse the header’s value. Instead, it needs
942+
* only assert that the content type is one of the static HTML types.
943+
*
944+
* Content-Type: text/html; charset=utf8
945+
* Content-Type: text/html ;charset=latin4
946+
* Content-Type:application/xhtml+xml
947+
*/
948+
$media_type = trim( strtok( $header_parts[1], ';' ), " \t" );
949+
$is_html_content_type = in_array( $media_type, $html_content_types, true );
950+
break; // PHP only sends the first Content-Type header in the list.
949951
}
950952
if ( null === $is_html_content_type ) {
951953
$is_html_content_type = in_array( ini_get( 'default_mimetype' ), $html_content_types, true );

0 commit comments

Comments
 (0)