xref: /PHP-8.1/ext/standard/tests/http/bug60570.phpt (revision 95f25837)
1--TEST--
2Bug #60570 (Stream context leaks when http request fails)
3--SKIPIF--
4<?php require 'server.inc'; http_server_skipif(); ?>
5--INI--
6allow_url_fopen=1
7--FILE--
8<?php
9require 'server.inc';
10
11function do_test() {
12
13    $responses = array(
14        "data://text/plain,HTTP/1.0 404 Not Found\r\n\r\n",
15        "data://text/plain,HTTP/1.0 404 Not Found\r\n\r\n",
16        "data://text/plain,HTTP/1.0 404 Not Found\r\n\r\n"
17    );
18
19    ['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);
20
21    $a = $b = count(get_resources());
22
23    $i = 3;
24    while ($i--) {
25        $context = stream_context_create(array('http'=>array('timeout'=>1)));
26        file_get_contents($uri, 0, $context);
27        unset($context);
28
29        $b = $a;
30        $a = count(get_resources());
31    }
32
33    http_server_kill($pid);
34
35    echo "leak? penultimate iteration: $b, last one: $a\n";
36    var_dump($a == $b);
37}
38
39do_test();
40?>
41--EXPECTF--
42Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
43 in %s on line %d
44
45Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
46 in %s on line %d
47
48Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
49 in %s on line %d
50leak? penultimate iteration: %d, last one: %d
51bool(true)
52