在 PHP 中解析 HTTP_RANGE 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:31:07  来源:igfitidea点击:

Parsing HTTP_RANGE header in PHP

phpapachehttprangehttp-headers

提问by

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

是否有现有的方法可以HTTP_RANGE在 PHP 中正确解析标头?以为我会在重新发明轮子之前问这里。

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.

提前致谢。

采纳答案by BalusC

Rather use regex to testit before sending a 416. Then just parseit by exploding on the comma ,and the hyphen -. I also see that you used \d+in your regex, but those are actually notrequired. 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 spechow you're supposed to handle it.

而是使用正则表达式来测试发送之前416。然后只需通过在逗号和连字符上爆炸来解析它。我还看到您在正则表达式中使用了这些内容,但实际上并不是必需的。当任何一个范围索引被省略时,它只表示“第一个字节”或“最后一个字节”。你也应该在你的正则表达式中涵盖它。另请参阅HTTP 规范中Range 标头,您应该如何处理它。,-\d+

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

编辑: $start 必须始终小于 $end

回答by powtac

Taken from the PEAR Package HTTP_Download:

取自PEAR 包 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 thispackagesfor stuff like this!

这个用于这样的东西也是一个好主意!

回答by powtac

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

页面上有一个实现 HTTP 范围支持的片段fread()

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

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