Parsing HTTP_RANGE header in PHP

前端 未结 3 882
广开言路
广开言路 2020-12-03 02:28

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

相关标签:
3条回答
  • 2020-12-03 02:37

    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!

    0 讨论(0)
  • 2020-12-03 02:44

    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

    0 讨论(0)
  • 2020-12-03 02:54

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

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

    0 讨论(0)
提交回复
热议问题