Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gzopen() does not use the default stream context when opening HTTP URLs #16883

Open
TimWolla opened this issue Nov 21, 2024 · 1 comment
Open

Comments

@TimWolla
Copy link
Member

Description

The following code:

<?php
stream_context_set_default([
	'http' => array(
		'user_agent' => 'dummy',
	)
]);

$f = fopen("http://example.com", 'r');
var_dump(stream_get_contents($f));

$f = gzopen("http://example.com", 'r');
var_dump(stream_get_contents($f));

Resulted in these HTTP requests being sent:

sendto(4, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUser-Agent: dummy\r\n\r\n", 75, MSG_DONTWAIT, NULL, 0) = 75
[…]
sendto(5, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n", 56, MSG_DONTWAIT, NULL, 0) = 56

But I expected these HTTP requests to be sent:

sendto(4, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUser-Agent: dummy\r\n\r\n", 75, MSG_DONTWAIT, NULL, 0) = 75
[…]
sendto(5, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUser-Agent: dummy\r\n\r\n", 75, MSG_DONTWAIT, NULL, 0) = 75

The user-agent is missing from the gzopen() call.

PHP Version

PHP 8.3.13

Operating System

Ubuntu 24.04

@nielsdos
Copy link
Member

Right, this makes sense to me. It's a behaviour change though so not sure if it can go in stable branches.
Perhaps an optional context argument like fopen has is also a good idea?

Anyway, fixing the report as-is is not hard at all:

diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index df3d270217d..d4905b920e5 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -28,6 +28,7 @@
 #include "ext/standard/info.h"
 #include "php_zlib.h"
 #include "zlib_arginfo.h"
+#include "ext/standard/file.h"
 
 /*
  * zlib include files can define the following preprocessor defines which rename
@@ -659,7 +660,7 @@ PHP_FUNCTION(gzopen)
 		flags |= USE_PATH;
 	}
 
-	stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, NULL STREAMS_CC);
+	stream = php_stream_gzopen(NULL, filename, mode, flags, NULL, php_stream_context_from_zval(NULL, false) STREAMS_CC);
 
 	if (!stream) {
 		RETURN_FALSE;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants