Skip to content

Commit f926c5c

Browse files
committed
Fix GH-16883: gzopen() does not use the default stream context when opening HTTP URLs
Otherwise it's not possible to control the context; it's also consistent with how the standard open functions work. Closes GH-17589.
1 parent a7df6a7 commit f926c5c

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ PHP NEWS
149149
- Zlib:
150150
. gzfile, gzopen and readgzfile, their "use_include_path" argument
151151
is now a boolean. (David Carlier)
152+
. Fixed bug GH-16883 (gzopen() does not use the default stream context when
153+
opening HTTP URLs). (nielsdos)
152154

153155

154156
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ PHP 8.5 UPGRADE NOTES
166166
. The "use_include_path" argument for the
167167
gzfile, gzopen and readgzfile functions had been changed
168168
from int to boolean.
169+
. gzfile, gzopen and readgzfile functions now respect the default
170+
stream context.
169171

170172
========================================
171173
6. New Functions

ext/zlib/tests/gh16883.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
GH-16883 (gzopen() does not use the default stream context when opening HTTP URLs)
3+
--EXTENSIONS--
4+
zlib
5+
--INI--
6+
allow_url_fopen=1
7+
--SKIPIF--
8+
<?php
9+
if (!file_exists(__DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc")) {
10+
echo "skip sapi/cli/tests/php_cli_server.inc required but not found";
11+
}
12+
?>
13+
--FILE--
14+
<?php
15+
require __DIR__ . "/../../../sapi/cli/tests/php_cli_server.inc";
16+
17+
$code = <<<'PHP'
18+
echo $_SERVER['HTTP_USER_AGENT'], "\n";
19+
PHP;
20+
21+
php_cli_server_start($code);
22+
23+
stream_context_set_default([
24+
'http' => array(
25+
'user_agent' => 'dummy',
26+
)
27+
]);
28+
29+
$f = gzopen('http://'.PHP_CLI_SERVER_HOSTNAME.':'.PHP_CLI_SERVER_PORT, 'r');
30+
var_dump(stream_get_contents($f));
31+
32+
var_dump(gzfile('http://'.PHP_CLI_SERVER_HOSTNAME.':'.PHP_CLI_SERVER_PORT, 'r'));
33+
34+
var_dump(readgzfile('http://'.PHP_CLI_SERVER_HOSTNAME.':'.PHP_CLI_SERVER_PORT, 'r'));
35+
36+
?>
37+
--EXPECT--
38+
string(6) "dummy
39+
"
40+
array(1) {
41+
[0]=>
42+
string(6) "dummy
43+
"
44+
}
45+
dummy
46+
int(6)

ext/zlib/zlib.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "SAPI.h"
2727
#include "php_ini.h"
2828
#include "ext/standard/info.h"
29+
#include "ext/standard/file.h"
2930
#include "php_zlib.h"
3031
#include "zlib_arginfo.h"
3132

@@ -621,7 +622,7 @@ PHP_FUNCTION(gzfile)
621622
}
622623

623624
/* using a stream here is a bit more efficient (resource wise) than php_gzopen_wrapper */
624-
stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
625+
stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC);
625626

626627
if (!stream) {
627628
/* Error reporting is already done by stream code */
@@ -659,7 +660,7 @@ PHP_FUNCTION(gzopen)
659660
flags |= USE_PATH;
660661
}
661662

662-
stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, NULL STREAMS_CC);
663+
stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC);
663664

664665
if (!stream) {
665666
RETURN_FALSE;
@@ -686,7 +687,7 @@ PHP_FUNCTION(readgzfile)
686687
flags |= USE_PATH;
687688
}
688689

689-
stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, NULL STREAMS_CC);
690+
stream = php_stream_gzopen(NULL, filename, "rb", flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC);
690691

691692
if (!stream) {
692693
RETURN_FALSE;

0 commit comments

Comments
 (0)