Apache 2.0 + 206 Partial Content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Apache 2.0 + 206 Partial Content

Post by quocbao »

Hi , i found this script on a site that support section download and speed limit

Code: Select all

<?php


function send_file($file, $speed = 100,$resume=false) {
    
    //First, see if the file exists  
    if (!is_file($file)) {
        die("<b>404 File not found!</b>");
    }
    //Gather relevent info about file
    $filename = basename($file);
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    // This will set the Content-Type to the appropriate setting for the file
    switch( $file_extension ) {
        case "exe":
            $ctype="application/octet-stream";
            break;
        case "zip":
            $ctype="application/zip";
            break;
        case "mp3":
            $ctype="audio/mpeg";
            break;
        case "mpg":
            $ctype="video/mpeg";
            break;
        case "avi":
            $ctype="video/x-msvideo";
            break;
       
        //  The following are for extensions that shouldn't be downloaded
        // (sensitive stuff, like php files)
        case "php":
        case "htm":
        case "html":
        case "txt":
            die("<b>Cannot be used for ". $file_extension ." files!</b>");
            break;
        default:
            $ctype="application/force-download";
    }

    //  Begin writing headers
    //header("Cache-Control:");
    //header("Cache-Control: public");
    
    header("Content-Type: $ctype");

    $filespaces = str_replace("_", " ", $filename);
    // if your filename contains underscores, replace them with spaces

    $header='Content-Disposition: attachment; filename='.$filespaces;
    header($header);
    header("Accept-Ranges: bytes");
   
    $size = filesize($file);
    
    //  check if http_range is sent by browser (or download manager)  
    if($resume && isset($_SERVER['HTTP_RANGE'])) {
        // if yes, download missing part    

        $seek_range = substr($_SERVER['HTTP_RANGE'] , 6);
        $range = explode( '-', $seek_range);
        if($range[0] > 0) { $seek_start = intval($range[0]); }
        if($range[1] > 0) { $seek_end  =  intval($range[1]); }
        else $seek_end = $size - 1;
           
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: " . ($seek_end - $seek_start + 1));
        header("Content-Range: bytes $seek_start-$seek_end/$size");
    } else {
        //header("Content-Range: bytes 0-$seek_end/$size");
        header("Content-Length: $size");
    }
    
    //open the file
    $fp = fopen("$file","rb");
   
    //seek to start of missing part  
    fseek($fp,$seek_start);
  
    //start buffered download
    while(!feof($fp)) {      
        //reset time limit for big files
        set_time_limit(0);      
        print(fread($fp,1024*$speed));
        flush();
        sleep(1);
    }
    fclose($fp);
    exit;
}

?>

Code: Select all

<?

//download file.zip at 100kbs and no resume support
send_file("file.zip" , 100 , false);

?>
This work fine when i enable resume
But when i disable resume , it work strangely :(

I except Apahce will return Response HTTP/1.1 200 OK , so download program can detect that this download cannot be resumed . But Apache doesn't return anything ( see below )

PHP is installed as CGI module

I try with Apache 1.3 and IIS , they work correctly :(

What happens with Apache response ?

Code: Select all

Status	17	9:38:19	2/8/2006	Download state changed to [Download]
Info	18	9:38:19	2/8/2006	One more section started
Info	19	9:38:19	2/8/2006	Connecting to localhost (127.0.0.1:80)
Output	20	9:38:19	2/8/2006	GET /dev/phpClassCollection/HttpDownload/download.php HTTP/1.0
Output	20			User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)
Output	20			Accept: */*
Output	20			Range: bytes=1755306-
Output	20			Referer: http://localhost/dev/phpClassCollection/HttpDownload/
Output	20			Host: localhost
Info	21	9:38:36	2/8/2006	Section finished

** Where is HTTP Response ??? **

Info	22	9:38:36	2/8/2006	One more section started
Info	23	9:38:36	2/8/2006	Connecting to localhost (127.0.0.1:80)
Output	24	9:38:36	2/8/2006	GET /dev/phpClassCollection/HttpDownload/download.php HTTP/1.0
Output	24			Accept: */*
Output	24			Range: bytes=2660888-
Output	24			Referer: http://localhost/dev/phpClassCollection/HttpDownload/
Output	24			Host: localhost
Info	25	9:38:44	2/8/2006	Section finished

** Where is HTTP Response ??? **

Info	26	9:38:54	2/8/2006	Section finished
Status	27	9:38:54	2/8/2006	Download state changed to [Completed]
in IIS

Code: Select all

Status	17	9:54:11	2/8/2006	Download state changed to [Download]
Info	18	9:54:11	2/8/2006	One more section started
Info	19	9:54:12	2/8/2006	Connecting to localhost (127.0.0.1:8080)
Output	20	9:54:12	2/8/2006	GET /download.php HTTP/1.0
Output	20			User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)
Output	20			Accept: */*
Output	20			Range: bytes=1756959-
Output	20			Referer: http://localhost:8080/
Output	20			Host: localhost:8080


** HTTP Response here , thanks IIS **


Input	21	9:54:12	2/8/2006	HTTP/1.1 200 OK
Input	21			Server: Microsoft-IIS/5.1
Input	21			Date: Wed, 08 Feb 2006 02:54:11 GMT
Input	21			X-Powered-By: ASP.NET
Input	21			Content-Type: application/force-download
Input	21			X-Powered-By: PHP/4.3.7
Input	21			Content-Disposition: attachment; filename=php5ts.dll
Input	21			Accept-Ranges: bytes
Input	21			Content-Length: 3502136
Error	22	9:54:12	2/8/2006	Resuming is not supported by the destination server, closing connection
Info	23	9:54:12	2/8/2006	Section finished
Info	24	9:54:48	2/8/2006	Section finished
Status	25	9:54:48	2/8/2006	Download state changed to [Completed]
Download log when using Apache 2.0 View
Download log when using IIS 6.0 View
Post Reply