在 PHP 中解析 HTTP_RANGE 标头

发布于 2024-08-20 14:09:23 字数 253 浏览 4 评论 0 原文

PHP 中是否有正确解析 HTTP_RANGE 标头的现有方法?我想在重新发明轮子之前我应该​​在这里问一下。

我目前正在使用

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

解析标头,但这并没有涵盖标头的所有可能值,所以我想知道是否有一个函数或库可以做到这一点?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十年不长 2024-08-27 14:09:23

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

启动示例:

if (isset($_SERVER['HTTP_RANGE'])) {
    if (!preg_match('^bytes=\d*-\d*(,\d*-\d*)*

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

, $_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; } // ... } }

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

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*)*

Edit: $start must always be less than $end

, $_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

别闹i 2024-08-27 14:09:23

取自 PEAR 包 HTTP_Download

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

使用 this 此类内容的包

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!

魔法唧唧 2024-08-27 14:09:23

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文