From 7aca5eb372f05e2b92278c742218e9875694f8d6 Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 28 Apr 2023 10:56:52 +0800 Subject: [PATCH 1/4] Fix the use of SSE in the built-in webserver --- sapi/cli/php_cli_server.c | 2 +- sapi/cli/tests/gh11146.phpt | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 sapi/cli/tests/gh11146.phpt diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index e59037ac2e8d3..14f55ff64e6cf 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -2167,7 +2167,7 @@ static zend_result php_cli_server_begin_send_static(php_cli_server *server, php_ if (mime_type) { smart_str_appendl_ex(&buffer, "Content-Type: ", sizeof("Content-Type: ") - 1, 1); smart_str_appends_ex(&buffer, mime_type, 1); - if (strncmp(mime_type, "text/", 5) == 0) { + if (strncmp(mime_type, "text/", 5) == 0 && strcmp(mime_type, "text/event-stream") != 0) { smart_str_appends_ex(&buffer, "; charset=UTF-8", 1); } smart_str_appendl_ex(&buffer, "\r\n", 2, 1); diff --git a/sapi/cli/tests/gh11146.phpt b/sapi/cli/tests/gh11146.phpt new file mode 100644 index 0000000000000..57654b437cf30 --- /dev/null +++ b/sapi/cli/tests/gh11146.phpt @@ -0,0 +1,24 @@ +--TEST-- +GH-11146: The built-in webserver will add ";charset=UTF-8" to the end of the Content-Type response header of the SSE response. +--SKIPIF-- + +--FILE-- + +--EXPECT-- +data: 0 + +data: 1 + +data: 2 From 07e735c66df64870d1633eab627fa3582d376f96 Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 28 Apr 2023 15:34:12 +0800 Subject: [PATCH 2/4] Fix test --- sapi/cli/tests/gh11146.phpt | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/sapi/cli/tests/gh11146.phpt b/sapi/cli/tests/gh11146.phpt index 57654b437cf30..d335d0a7a0bfa 100644 --- a/sapi/cli/tests/gh11146.phpt +++ b/sapi/cli/tests/gh11146.phpt @@ -2,21 +2,42 @@ GH-11146: The built-in webserver will add ";charset=UTF-8" to the end of the Content-Type response header of the SSE response. --SKIPIF-- --FILE-- --EXPECT-- +HTTP/1.1 200 OK +Host: localhost +Date: %s +Connection: close +X-Powered-By: %s +Content-type: text/event-stream + data: 0 data: 1 From 3f0f74e250fe3b01f66144ecfee40868f6cc1841 Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 28 Apr 2023 15:57:20 +0800 Subject: [PATCH 3/4] Fix test --- sapi/cli/tests/gh11146.phpt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sapi/cli/tests/gh11146.phpt b/sapi/cli/tests/gh11146.phpt index d335d0a7a0bfa..20edbcec1a1ca 100644 --- a/sapi/cli/tests/gh11146.phpt +++ b/sapi/cli/tests/gh11146.phpt @@ -30,13 +30,13 @@ HEADER)) { } } ?> ---EXPECT-- +--EXPECTF-- HTTP/1.1 200 OK -Host: localhost +Host: %s Date: %s Connection: close -X-Powered-By: %s -Content-type: text/event-stream +X-Powered-By: PHP/%s +Content-Type: text/event-stream data: 0 From b2d234b95741e173b9e95c4ebf4f71d8f42c7d5a Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 28 Apr 2023 16:32:14 +0800 Subject: [PATCH 4/4] Fix --- main/SAPI.c | 2 +- sapi/cli/php_cli_server.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main/SAPI.c b/main/SAPI.c index 2e697f9779669..a2bd456d9baf6 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -371,7 +371,7 @@ SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len) charset = SG(default_charset) ? SG(default_charset) : SAPI_DEFAULT_CHARSET; if (*mimetype != NULL) { - if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) { + if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL && strcmp(*mimetype, "text/event-stream") != 0) { newlen = len + (sizeof(";charset=")-1) + strlen(charset); newtype = emalloc(newlen + 1); PHP_STRLCPY(newtype, *mimetype, newlen + 1, len); diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 14f55ff64e6cf..e59037ac2e8d3 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -2167,7 +2167,7 @@ static zend_result php_cli_server_begin_send_static(php_cli_server *server, php_ if (mime_type) { smart_str_appendl_ex(&buffer, "Content-Type: ", sizeof("Content-Type: ") - 1, 1); smart_str_appends_ex(&buffer, mime_type, 1); - if (strncmp(mime_type, "text/", 5) == 0 && strcmp(mime_type, "text/event-stream") != 0) { + if (strncmp(mime_type, "text/", 5) == 0) { smart_str_appends_ex(&buffer, "; charset=UTF-8", 1); } smart_str_appendl_ex(&buffer, "\r\n", 2, 1);