General: Improve parsing of sent HTTP Content-Type header to detect HTML response.

This improves adherence to the HTTP spec in extracting the header name and value.

Developed in https://github.com/WordPress/wordpress-develop/pull/10293

Follow-up to [60936].

Props dmsnell, westonruter.
See #43258.

Built from https://develop.svn.wordpress.org/trunk@60973


git-svn-id: http://core.svn.wordpress.org/trunk@60309 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter
2025-10-19 00:21:26 +00:00
parent fd3520f7ec
commit 0ea651a73e
2 changed files with 16 additions and 5 deletions

View File

@@ -926,13 +926,24 @@ 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 = preg_split( '/\s*[:;]\s*/', strtolower( $header ) );
$header_parts = explode( ':', strtolower( $header ), 2 );
if (
is_array( $header_parts ) &&
count( $header_parts ) >= 2 &&
count( $header_parts ) === 2 &&
'content-type' === $header_parts[0]
) {
$is_html_content_type = in_array( $header_parts[1], $html_content_types, true );
/*
* This is looking for very specific content types, therefore it
* doesnt need to fully parse the headers 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.
}
}

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.9-alpha-60972';
$wp_version = '6.9-alpha-60973';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.