Skip to content

Commit 9bd468d

Browse files
committed
Fix GH-7953: ob_clean() only does not set Content-Encoding
If an output handler has not yet been started, calling `ob_clean()` causes it to start. If that happens, we must not forget to set the `Content-Encoding` and `Vary` headers. Closes GH-7960.
1 parent 8a46311 commit 9bd468d

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2022, PHP 8.0.17
44

5+
- Iconv:
6+
. Fixed bug GH-7953 (ob_clean() only does not set Content-Encoding). (cmb)
7+
8+
- Zlib:
9+
. Fixed bug GH-7953 (ob_clean() only does not set Content-Encoding). (cmb)
510

611
17 Feb 2022, PHP 8.0.16
712

ext/iconv/iconv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static int php_iconv_output_handler(void **nothing, php_output_context *output_c
311311
mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE;
312312
}
313313

314-
if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) {
314+
if (mimetype != NULL && (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START))) {
315315
size_t len;
316316
char *p = strstr(get_output_encoding(), "//");
317317

ext/iconv/tests/gh7953.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-7953 (ob_clean() only may not set Content-* header)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("iconv")) die("skip iconv extension not available");
6+
?>
7+
--INI--
8+
input_encoding=UTF-8
9+
output_encoding=ISO-8859-1
10+
--CGI--
11+
--FILE--
12+
<?php
13+
ob_start('ob_iconv_handler');
14+
ob_clean();
15+
16+
echo 'Hello World';
17+
?>
18+
--EXPECTF--
19+
%a
20+
--EXPECTHEADERS--
21+
Content-Type: text/html; charset=ISO-8859-1

ext/zlib/tests/gh7953.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-7953 (ob_clean() only may not set Content-* header)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("zlib")) die("skip zlib extension not available");
6+
?>
7+
--CGI--
8+
--ENV--
9+
HTTP_ACCEPT_ENCODING=gzip
10+
--FILE--
11+
<?php
12+
ob_start('ob_gzhandler');
13+
ob_clean();
14+
15+
echo 'Hello World';
16+
?>
17+
--EXPECTF--
18+
%a
19+
--EXPECTHEADERS--
20+
Content-Encoding: gzip
21+
Vary: Accept-Encoding

ext/zlib/zlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static int php_zlib_output_handler(void **handler_context, php_output_context *o
281281
return FAILURE;
282282
}
283283

284-
if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) {
284+
if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START)) {
285285
int flags;
286286

287287
if (SUCCESS == php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags)) {

0 commit comments

Comments
 (0)