gpt4 book ai didi

apache - mod_xsendfile Firefox 恢复问题

转载 作者:行者123 更新时间:2023-12-02 06:53:36 32 4
gpt4 key购买 nike

我们正在尝试将 mod_xsendfile 与 Apache 结合使用来有效处理大文件下载(> 1 GB)。安装后配置如下:

<IfModule mod_xsendfile.c>
<Directory "/abs_path/to/dl">
XSendFile on
XSendFilePath /abs_path/to/files_dir
</Directory>
</IfModule>

下载脚本没有做任何花哨的事情,只是检查要下载的文件是否存在并根据文档设置 header ,如下所示:

header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header("X-Sendfile: " . $file);

不间断下载适用于我们测试过的任何用户代理,并且 HTTP-Range 确实适用于除 Firefox 之外的所有用户代理(测试版本 27 和 28)。 Firefox 可以暂停下载,但每次恢复都会失败。

这些是使用 Live HTTP headers 扩展捕获的 http headers:

初始下载:

http://www.oursite.com/dl/test-xs.php?ID=TestFileID

GET /dl/test-xs.php?ID=TestFileID HTTP/1.1
Host: www.oursite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: some cookie string...
Connection: keep-alive

HTTP/1.1 200 OK
Date: Tue, 25 Mar 2014 10:22:46 GMT
Server: Apache
X-Powered-By: PHP/5.3.28
Content-Disposition: attachment; filename="TestFile.ext"
Last-Modified: Sun, 02 Mar 2014 18:20:36 GMT
Content-Length: 84406272
Connection: close
Content-Type: application/octet-stream

...当 Firefox 尝试恢复时:

http://www.oursite.com/dl/test-xs.php?ID=TestFileID

GET /dl/test-xs.php?ID=TestFileID HTTP/1.1
Host: www.oursite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: same cookie string...
Connection: keep-alive
Range: bytes=11238434-
If-Unmodified-Since: Sun, 02 Mar 2014 18:20:36 GMT

...服务器返回 404

HTTP/1.1 404 Not Found
Date: Tue, 25 Mar 2014 10:23:03 GMT
Server: Apache
X-Powered-By: PHP/5.3.28, PHP/5.3.28
Content-Disposition: attachment; filename="TestFile.ext"
X-Sendfile: /abs_path/to/files_dir/TestFile.ext
X-Pingback: http://www.oursite.com/xmlrpc.php
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

...这显然会导致 Firefox 无法恢复下载(现在只能取消并从头开始重新启动)。

考虑到在我们尝试过的其他浏览器和下载管理器中一切都按预期工作:

  1. 有人经历过类似的行为吗?
  2. 任何人都可以在我们的文章中解释或指出其潜在原因吗?下载脚本或配置代码?

编辑:

经过更多测试后,发现问题是由于 Firefox 在恢复下载时发送的 If-Unmodified-Since header 造成的。尽管此 header 已正确设置为从 Apache 接收到的 Last-Modified 响应 header 的值,但服务器由于某种原因不喜欢它,并以 404 进行响应。通过更改 .htaccess 从请求中剥离 If-Unmodified-Since header 后:

<Files test-xs.php>
RequestHeader unset If-Unmodified-Since
</Files>

...简历在任何地方都可以正常工作,包括 Firefox。

如果要下载的文件同时被修改,这种方法当然是不正确的,但它对我们来说很有效,因为我们使用不同的约定来提供同一文件的较新版本。

这显然感觉更像是一种黑客攻击,而不是正确的实现,因此不确定是否应该将其标记为答案,我将其作为原始问题的补充。

显然出现了新问题:

  • 有更好的方法来解决这个问题吗?
  • 这是 mod_xsendfile 中的错误吗?

最佳答案

@alternize's solution大部分是正确的,但提供的代码片段:

SetEnvIf Range .+ HAS_RANGE_HEADER
RequestHeader unset If-Range env=!HAS_RANGE_HEADER
RequestHeader unset If-Unmodified-Since env=!HAS_RANGE_HEADER

实际上不会为包含 Range header 的请求取消设置指定的 header 。

!”否定匹配,因此上面的代码片段实际上会为所有请求取消设置这些 header ,除了具有 Range header 的请求。

要从带有 Range header 的请求中正确删除 If-RangeIf-Unmodified-Since header ,您可以使用相同的指令,但是删除这样的否定:

SetEnvIf Range .+ HAS_RANGE_HEADER
RequestHeader unset If-Range env=HAS_RANGE_HEADER
RequestHeader unset If-Unmodified-Since env=HAS_RANGE_HEADER

这已在 apache 2.2.15 上得到证实。

关于apache - mod_xsendfile Firefox 恢复问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22632094/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com