Skip to content

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

Closed

Description

@TimWolla

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

Activity

  1. ndossche commented on Nov 21, 2024

    @ndossche
    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;
    
  2. added a commit that references this issue on Jan 26, 2025
    7f6c6a6
  3. added a commit that references this issue on Jan 27, 2025
    f926c5c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions