Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
}
ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response);
} else {
php_stream_close(stream);
stream = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While merging this up into 7.4, I found that we have 41c4816 there, which should have much the same effect -- the main difference I see is that it does not close and null the stream. The EOF branch below also doesn't do this. I'm wondering if it's necessary?

Copy link
Contributor Author

@fancyweb fancyweb Aug 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to be coherent with lines 851 & 852 (

php_stream_close(stream);
)

Removing both lines doesn't work because the stream is not technically EOF, so it still goes through the second call, thus applying 2 times the timeout.

Only php_stream_close(stream) -> seg fault
Only stream = null -> seems to work

php_stream_wrapper_log_error(wrapper, options, "HTTP request failed!");
goto out;
}
} else {
php_stream_wrapper_log_error(wrapper, options, "HTTP request failed, unexpected end of socket!");
Expand Down
33 changes: 33 additions & 0 deletions ext/standard/tests/http/bug76342.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Bug #76342 (file_get_contents waits twice specified timeout)
--INI--
allow_url_fopen=1
--SKIPIF--
<?php require 'server.inc'; http_server_skipif('tcp://127.0.0.1:12342'); ?>
--FILE--
<?php
require 'server.inc';

$options = [
'http' => [
'timeout' => '0.1',
],
];

$ctx = stream_context_create($options);

$pid = http_server_sleep('tcp://127.0.0.1:12342');

$start = microtime(true);
file_get_contents('http://127.0.0.1:12342/', false, $ctx);
if (microtime(true) - $start >= 0.2) {
echo 'FAIL';
}

http_server_kill($pid);

?>
DONE
--EXPECTF--
Warning: file_get_contents(http://127.0.0.1:12342/): failed to open stream: HTTP request failed! in %s on line %d
DONE
42 changes: 34 additions & 8 deletions ext/standard/tests/http/server.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ function http_server_skipif($socket_string) {
if (!stream_socket_server($socket_string)) die('skip stream_socket_server() failed');
}

/* Minimal HTTP server with predefined responses.
*
* $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
* $files is an array of files containing N responses for N expected requests. Server dies after N requests.
* $output is a stream on which everything sent by clients is written to
*/
function http_server($socket_string, array $files, &$output = null) {

function http_server_init($socket_string, &$output = null) {
pcntl_alarm(60);

$server = stream_socket_server($socket_string, $errno, $errstr);
Expand All @@ -36,6 +29,21 @@ function http_server($socket_string, array $files, &$output = null) {
return $pid;
}

return $server;
}

/* Minimal HTTP server with predefined responses.
*
* $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
* $files is an array of files containing N responses for N expected requests. Server dies after N requests.
* $output is a stream on which everything sent by clients is written to
*/
function http_server($socket_string, array $files, &$output = null) {

if (!is_resource($server = http_server_init($socket_string, $output))) {
return $server;
}

foreach($files as $file) {

$sock = stream_socket_accept($server);
Expand Down Expand Up @@ -84,6 +92,24 @@ function http_server($socket_string, array $files, &$output = null) {
exit(0);
}

function http_server_sleep($socket_string, $micro_seconds = 500000)
{
if (!is_resource($server = http_server_init($socket_string, $output))) {
return $server;
}

$sock = stream_socket_accept($server);
if (!$sock) {
exit(1);
}

usleep($micro_seconds);

fclose($sock);

exit(0);
}

function http_server_kill($pid) {
posix_kill($pid, SIGTERM);
pcntl_waitpid($pid, $status);
Expand Down