php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49838 feof() reached end while reading big HTTP response from socket using fgets.
Submitted: 2009-10-11 17:08 UTC Modified: 2009-11-02 01:00 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:1 (33.3%)
From: travian dot utils at gmail dot com Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 5.2.11 OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: travian dot utils at gmail dot com
New email:
PHP Version: OS:

 

 [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;
  }
...


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-12 11:51 UTC] gmc at sonologic dot nl
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.
 [2009-10-12 19:17 UTC] travian dot utils at gmail dot com
Hello, Sjoerd.

Sorry for not fully clear description.

Thank you for interest to this problem.
gmc(at)sonologic(dot)nl understood my description and put code for reproducing this problem. Thanks also to him.

Problem occurred when we updated PHP from 5.2.10 to 5.2.11 on our production.
On PHP 5.2.10 problem code on the same server worked properly.
We tested problem code on the same server using PHP version 5.2.8 and also code worked properly.

Now we rewrote our code for downloading big HTTP files without using fgets(using socket_create, socket_bind, socket_connect, socket_select, socket_write, socket_read and socket_close).
It works fine on PHP 5.2.8 and 5.2.11.

And it working fine in production on PHP 5.2.11.
So this is bug, not a configuration issue.

--
With best regards, 
Andrei.
 [2009-10-12 21:42 UTC] lbarnaud@php.net
Verified (with gmc at sonologic dot nl's script), 5.2.11 only
 [2009-10-24 00:58 UTC] srinatar@php.net
this issue is no longer reproducible with latest svn snapshot (http://snaps.php.net). pl. verify with latest snapshot and let us know. 
 [2009-10-25 20:31 UTC] jani@php.net
Please try using this snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-11-02 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC