xref: /PHP-7.4/ext/standard/tests/http/bug60570.phpt (revision 94d37a5d)
1--TEST--
2Bug #60570 (Stream context leaks when http request fails)
3--SKIPIF--
4<?php require 'server.inc'; http_server_skipif('tcp://127.0.0.1:12342'); ?>
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 = http_server("tcp://127.0.0.1:12342", $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('http://127.0.0.1:12342/', 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--EXPECTF--
41Warning: file_get_contents(http://127.0.0.1:12342/): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
42 in %s on line %d
43
44Warning: file_get_contents(http://127.0.0.1:12342/): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
45 in %s on line %d
46
47Warning: file_get_contents(http://127.0.0.1:12342/): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
48 in %s on line %d
49leak? penultimate iteration: %d, last one: %d
50bool(true)
51