10

Is there an existing way to parse the HTTP_RANGE header correctly in PHP? Thought I'd ask here before re-inventing the wheel.

I am currently using

preg_match('/bytes=(\d+)-(\d+)/', $_SERVER['HTTP_RANGE'], $matches);

to parse the header but that does not cover all possible values of the header so I am wondering if there is a function or library that can do this already?

Thanks in advance.

John Rasch
  • 62,489
  • 19
  • 106
  • 139
  • I don't know of a way to do this, but I'd have a crack at it with regular expressions. Take a look at the specification http://tools.ietf.org/html/draft-ietf-http-range-retrieval-00 – The Pixel Developer Feb 05 '10 at 18:01

3 Answers3

10

Rather use regex to test it before sending a 416. Then just parse it by exploding on the comma , and the hyphen -. I also see that you used \d+ in your regex, but those are actually not required. When either of the range indexes is omitted, then it just means "first byte" or "last byte". You should cover that in your regex as well. Also see the Range header in the HTTP spec how you're supposed to handle it.

Kickoff example:

if (isset($_SERVER['HTTP_RANGE'])) {
    if (!preg_match('^bytes=\d*-\d*(,\d*-\d*)*$', $_SERVER['HTTP_RANGE'])) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header('Content-Range: bytes */' . filelength); // Required in 416.
        exit;
    }

    $ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
    foreach ($ranges as $range) {
        $parts = explode('-', $range);
        $start = $parts[0]; // If this is empty, this should be 0.
        $end = $parts[1]; // If this is empty or greater than than filelength - 1, this should be filelength - 1.

        if ($start > $end) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header('Content-Range: bytes */' . filelength); // Required in 416.
            exit;
        }

        // ...
    }
}

Edit: $start must always be less than $end

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    You're welcome. Disclaimer: untested. This is less or more translated in head from my Java Servlet example here: http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html – BalusC Feb 09 '10 at 12:36
  • You saved me hours of work by sharing your Java Servlet implementation! Thanks so much. – shadit Nov 04 '10 at 03:24
  • 2
    The [RFC specification for byte ranges](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35) also allows something like `bytes=-500` which is a valid request for the last 500 bytes of a file. This makes it a bit more complicated to extract the range than just exploding on the `-` character. – Andrew Theis Jun 06 '13 at 17:39
  • 2
    This answer is wrong when it states that when start is missing it should be treated as 0. As Andrew Theis said, when you see something like bytes=-500, it's a request for the end of the file, NOT bytes=0-500. – Brigham Feb 02 '14 at 03:45
2

Taken from the PEAR Package HTTP_Download:

function getRanges()
{
    return preg_match('/^bytes=((\d*-\d*,? ?)+)$/', @$_SERVER['HTTP_RANGE'], $matches) ? $matches[1] : array();
}

It is also a good idea to use this packages for stuff like this!

powtac
  • 40,542
  • 28
  • 115
  • 170
1

There's a snippet implementing HTTP range support on the fread() page:

http://www.php.net/manual/en/function.fread.php#84115