|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-11 17:08 UTC] travian dot utils at gmail dot com
Description:
------------
feof() reached end of stream while reading big HTTP response from socket using fgets.
Reproduce code:
---------------
...
$fp = @fsockopen ($sname, 80, $errno, $errstr, 18);
if ($fp) {
fputs ($fp, "GET /".$xxx." HTTP/1.0\r\nHost: ".$sname."\r\nUser-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.0.2) Gecko/2008092313 Firefox/3.1.6\r\n\r\n");
$time='';
$len='';
$substr='';
$upstr='';
$redirect='';
$http_code=0;
while (!feof($fp)) {
$line=fgets($fp,256);
$substr=substr($line,0,15);
$substr2=substr($line,0,10);
$substr3=substr($line,0,16);
if(strpos($line, '404 Not Found')!=false){$http_code=404; break;};
if($line==chr(13).chr(10))break;
if($substr2=='Location: '){$redirect=substr($line,10);break;};
if($substr3=='Content-Length: ')$len=intval(substr($line,16));
if($substr=='Last-Modified: ')$time=substr($line,15);
}
$rlen=0;
unset($lines);
// This cycle reached end while reading big HTTP response
while (!feof($fp)) {
$line=fgets($fp,1024);
$lines[]=$line;
$rlen+=strlen($line);
}
//print('$len='.$len);
//print('$time='.$time);
$dtin=date('Y-m-d',strtotime($time));
$time=strtotime($time);
//print('$dtin='.$dtin);
//print('$time='.$time);
if($http_code==404){
return -10;
}elseif($dtin==$dt){
return -2;
}elseif($time==-1){
return -3;
}elseif($redirect!=''){
return -4;
}elseif($time==''){
return -5;
}elseif(isset($timein)){
if($time<=$timein){
return 0;
}
}
}else{
return -6;
}
...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 05:00:01 2025 UTC |
I'm experiencing the same problem on: FreeBSD 7.1-PRERELEASE FreeBSD 7.1-PRERELEASE #0: Tue Oct 14 11:55:05 CEST 2008 root@postel:/usr/obj/usr/src/sys/GENERIC i386 The problem is that feof($fp) returns TRUE, even though the end of the remote file has not been reached. The below script reproduces the problem. I'm sorry it depends on an external resource, that's just the nature of the problem. AFAICS, the bug is not dependent on the particular file chosen, it occurs for any large text file. <?php $trigger_bug=TRUE; $file = "http://www.mersenneforum.org/txt/43.txt"; if (!($fp = fopen($file, "r"))) { die("could not open XML input from $file"); } $chunkno=1; $total=0; while($data=fgets($fp,10000)) { $total+=strlen($data); if($trigger_bug) { print "chunk $chunkno, total ".$total." (+".strlen($data)."), eof: ".(feof($fp)?1:0)."<br/>\n"; } else { print "chunk $chunkno, total ".$total." (+".strlen($data).")<br/>\n"; } $chunkno++; } fclose($fp); ?> With $trigger_bug set to true, this will terminate before all of the file is read. With $trigger_bug set to false it will read the entire file. Note that the only difference between the two is that the script displays the output of feof($fp). This bug started to bite us when we upgraded from 5.2.1 to 5.2.11, it is not present in 5.2.1 / 5.1.4.