yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

CHttpRequest::sendFile accept range requests (range header) #1589

Closed Ragazzo closed 11 years ago

Ragazzo commented 12 years ago

I think need to be implemented to hold range-requests, i mean $_SERVER['HTTP_RANGE'], i've allready have one working solution, so if needed can make a PR, good enhancement i think. Need opinions of fw devs.

samdark commented 12 years ago

What is your use case exactly?

Ragazzo commented 12 years ago

The same one, jPlayer (or other js) can ask for a part of the file, so i need to handle this one situation, also chrome sends this header and needs it back in correct form. I was mentioned this ench. early in some issue about CHttpRequest, so i just decided to ask if it needed. My opinion yes, because of some browsers also can download file by parts, or am i wrong about it?

samdark commented 12 years ago

Ah, right. Can be useful. What API do you suggest for it?

Ragazzo commented 12 years ago

Hm, API... i was suggesting just to modify CHttpRequest::sendFile() method to check if there is a $_SERVER['HTTP_RANGE'] and serve it correctly, smth like this:

//code from sendFile above
        $fullContentLength = (function_exists('mb_strlen') ? mb_strlen($content,'8bit') : strlen($content));
        if (isset($_SERVER['HTTP_RANGE']))
        {
            $range = $_SERVER['HTTP_RANGE']; 
            $range = str_replace('bytes=', '', $range);
            list($range, $end) = explode('-', $range);
            if (!empty($range))
                $content = substr($content,$range);
        } 
        else
            $range = 0;
        if ($range != 0)
            header($_SERVER['SERVER_PROTOCOL'].' 206 Partial Content');
        else
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
//.... sendFile headers
        header('Accept-Ranges: bytes');
        header('Content-Length: '.(function_exists('mb_strlen') ? mb_strlen($content,'8bit') : strlen($content)));
        header('Content-Range: bytes '.$range.'-'.($fullContentLength).'/'.$fullContentLength);

If it must be like API i think can do it if you will say what kind of API.

samdark commented 12 years ago

No, I think it's good as is. If client is sending range headers than sendfile should definitely respond accordingly.

Ragazzo commented 12 years ago

So, need a PR?

samdark commented 12 years ago

Yes.

Ragazzo commented 12 years ago

Ok, will work on it. Thanks for your replies.