Bug #76268 [Ver->Ana]: stream_get_contents fail to seek on streams modified by curl_exec

From: Date: Tue, 26 Mar 2019 16:25:00 +0000
Subject: Bug #76268 [Ver->Ana]: stream_get_contents fail to seek on streams modified by curl_exec
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220195@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76268&edit=1 ID: 76268 Updated by: mike@php.net Reported by: divinity76 at gmail dot com Summary: stream_get_contents fail to seek on streams modified by curl_exec -Status: Verified +Status: Analyzed Type: Bug Package: cURL related Operating System: linux, debian 10 x64 k4.15.11-1 PHP Version: 7.1.16 Block user comment: N Private report: N New Comment: stream->position is still zero when curl returns. For all other streams this might be fixable by upgrading ext/curl to streams instead of stdio, looks like streams came a few years later. Previous Comments: ------------------------------------------------------------------------ [2019-03-26 15:54:24] divinity76 at gmail dot com for the record, i have several times written code like $stderrh=tmpfile(); curl_setopt($ch,CURLOPT_STDERR,$stderrh); curl_exec($ch); rewind($stderrh); // https://bugs.php.net/bug.php?id=76268 $stderr=stream_get_contents($stderrh); fclose($stderrh); because of this bug. real examples: https://stackoverflow.com/questions/53128631/curl-php-cannot-display-amazon and https://stackoverflow.com/questions/50051940/very-long-loadtime-php-curl and https://github.com/divinity76/autoit_php/blob/master/src/autoit.class.php#L97 and http://qaru.site/questions/15745219/set-a-global-php-curl-upload-download-speed-limit ------------------------------------------------------------------------ [2018-04-25 14:35:04] divinity76 at gmail dot com Description: ------------ stream_get_contents fail to seek on streams exclusively modified by curl_exec, the optional third parameter of stream_get_contents is "Seek to the specified offset", and is effectively ignored when the stream has only been modified by curl_exec. my best guess is that libcurl calls fwrite directly, and doesn't update php's own stream->position, and stream_get_contents in turn doesn't call fseek if it's not necessary (presumably to save up on syscalls), and determines if it's necessary by checking stream->positioun instead of ftell (presumably to save up on syscalls), then it calls fread() until EOF, which is where libcurl left it, and thus returns an empty string. Test script: --------------- <?php $ch = curl_init ( "http://gstatic.com/generate_204" ); $h = tmpfile (); curl_setopt_array ( $ch, array ( CURLOPT_VERBOSE => 1, CURLOPT_STDERR => $h ) ); curl_exec ( $ch ); $verbose1 = stream_get_contents ( $h, - 1, 0 ); // empty string fseek ( $h, 0, SEEK_SET ); // a manual fseek fixes it $verbose2 = stream_get_contents ( $h, - 1, 0 ); // the actual CURLOPT_VERBOSE log var_dump ( $verbose1 === $verbose2 ); Expected result: ---------------- bool(true) Actual result: -------------- bool(false) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76268&edit=1

« previous php.bugs (#220195) next »