为什么 Chrome 会取消 302 下载?

发布于 2024-12-13 20:40:36 字数 3067 浏览 2 评论 0原文

编辑:解决了一些可能或可能不会引起问题的问题后,进一步测试显示受影响的用户无法下载文件,甚至无法直接下载文件。这促使网站管理员 Stackexchange 上提出这个问题。我接受似乎已经解决了问题的答案,如果这一切都与重定向有关的话。


我有一个脚本,用于处理我的基于订阅的视频网站的下载。基本上,该脚本会根据数据库检查会话和/或 IP 以识别用户,然后根据数据库检查请求的视频文件以验证用户是否应该具有访问权限。根据这些检查的结果,脚本使用 header('location: [blah]'); 将用户发送到文件或将他们发送到错误页面。

在 IE、Firefox 和 Chrome 的某些实例中一切正常。在 Chrome 的其他实例中,用户会看到下载完全失败。我发现对于这些用户,Chrome 会自动取消 header(); 调用进行的 302 重定向,从而阻止文件下载。

为什么会发生这种情况?我能做什么呢?


编辑:以下是原始标头,如从下载失败的计算机上的 Fiddler 中看到的。

请求:

GET /download_video.php?t=Nodeg004&format=wmv HTTP/1.1
Host: www.mysite.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.mysite.com/phpBB3/viewtopic.php?f=3&t=6847
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: phpbb3_cmviy_u=698; phpbb3_cmviy_k=; phpbb3_cmviy_sid=0c2fd249ff50c922beb1c7f88437c283; style_cookie=printonly; __utma=174624884.1269868282.1272079241.1320418809.1320423695.199; __utmb=174624884.5.10.1320423695; __utmc=174624884; __utmz=174624884.1319811247.179.134.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/l.php

响应:

HTTP/1.1 302 Moved Temporarily
Date: Fri, 04 Nov 2011 16:22:05 GMT
Server: Apache
location: http://www.mysite.com/videos/zh4ZcnzIWJpni8tG5CzG/Nodeg004.wmv
Content-Length: 0
Connection: close
Content-Type: text/html

请求:

GET /videos/zh4ZcnzIWJpni8tG5CzG/Nodeg004.wmv HTTP/1.1
Host: www.mysite.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.mysite.com/phpBB3/viewtopic.php?f=3&t=6847
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: phpbb3_cmviy_u=698; phpbb3_cmviy_k=; phpbb3_cmviy_sid=0c2fd249ff50c922beb1c7f88437c283; style_cookie=printonly; __utma=174624884.1269868282.1272079241.1320418809.1320423695.199; __utmb=174624884.5.10.1320423695; __utmc=174624884; __utmz=174624884.1319811247.179.134.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/l.php

响应:

HTTP/1.1 200 OK
Date: Fri, 04 Nov 2011 16:22:05 GMT
Server: Apache
Last-Modified: Wed, 28 Sep 2011 13:28:50 GMT
ETag: "180cc25a-5700e91-4ae005f5ce880"
Accept-Ranges: bytes
Content-Length: 91229841
Keep-Alive: timeout=10, max=30
Connection: Keep-Alive
Content-Type: application/octet-stream

Edit: After addressing some issues that may or may not have been causing problems, further testing revealed that the affected users are unable to download files, even directly. That has prompted this question on Webmasters Stackexchange. I am accepting the answer that seems likely to have fixed the problem, were it all about the redirection.


I have a script which processes downloads for my subscription-based video site. Basically, the script checks session and/or IP against the database to identify the user, and then checks the requested video file against the database to verify that the user should have access. Based on the results of these checks, the script uses header('location: [blah]'); to either send the user to the file or to send them to an error page.

Everything works fine in IE, Firefox, and some instances of Chrome. In other instances of Chrome, users see the downloads fail completely. I have found that for these users, Chrome is automatically cancelling the 302 redirect made by the header(); call, preventing the file from downloading.

Why is this happening? What can I do about it?


Edit: Here are the raw headers, as seen from Fiddler on a machine that failed the download.

Request:

GET /download_video.php?t=Nodeg004&format=wmv HTTP/1.1
Host: www.mysite.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.mysite.com/phpBB3/viewtopic.php?f=3&t=6847
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: phpbb3_cmviy_u=698; phpbb3_cmviy_k=; phpbb3_cmviy_sid=0c2fd249ff50c922beb1c7f88437c283; style_cookie=printonly; __utma=174624884.1269868282.1272079241.1320418809.1320423695.199; __utmb=174624884.5.10.1320423695; __utmc=174624884; __utmz=174624884.1319811247.179.134.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/l.php

Response:

HTTP/1.1 302 Moved Temporarily
Date: Fri, 04 Nov 2011 16:22:05 GMT
Server: Apache
location: http://www.mysite.com/videos/zh4ZcnzIWJpni8tG5CzG/Nodeg004.wmv
Content-Length: 0
Connection: close
Content-Type: text/html

Request:

GET /videos/zh4ZcnzIWJpni8tG5CzG/Nodeg004.wmv HTTP/1.1
Host: www.mysite.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.mysite.com/phpBB3/viewtopic.php?f=3&t=6847
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: phpbb3_cmviy_u=698; phpbb3_cmviy_k=; phpbb3_cmviy_sid=0c2fd249ff50c922beb1c7f88437c283; style_cookie=printonly; __utma=174624884.1269868282.1272079241.1320418809.1320423695.199; __utmb=174624884.5.10.1320423695; __utmc=174624884; __utmz=174624884.1319811247.179.134.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/l.php

Response:

HTTP/1.1 200 OK
Date: Fri, 04 Nov 2011 16:22:05 GMT
Server: Apache
Last-Modified: Wed, 28 Sep 2011 13:28:50 GMT
ETag: "180cc25a-5700e91-4ae005f5ce880"
Accept-Ranges: bytes
Content-Length: 91229841
Keep-Alive: timeout=10, max=30
Connection: Keep-Alive
Content-Type: application/octet-stream

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

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

发布评论

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

评论(2

耳钉梦 2024-12-20 20:40:36

令人惊讶的是,问题似乎是您发送的是“位置”标头而不是标准的“位置”标头。
我做了一个快速测试,似乎证实了这一点,但我仍然不敢相信,所以我快速进行了网络搜索,发现其他人确认 Chrome 确实是个顽固分子,它不会接受大小写错误的 HTTP 标头。
PHP 手册上的评论提到 IE7 也有同样的怪癖。

Rather surprisingly, the problem seems to be that you are sending a 'location' header instead of the standard 'Location' header.
I did a quick test that seemed to confirm this, but I still couldn't believe it, so I did a quick web search and found someone else confirming that Chrome is indeed such a stickler that it won't accept an HTTP header with the wrong case.
A comment on the PHP manual mentions IE7 having the same quirk.

笑饮青盏花 2024-12-20 20:40:36

正如 php.net 评论我在 stackoverflow 上的回复

Chrome 不会执行 Location: 指令,除非它首先获得 Status: 指令。它对大小写也很敏感。

As mentioned in php.net comments and my response on stackoverflow:

Chrome doesn't perform a Location: instruction unless it gets a Status: first. It's also sensitive to capitalisation.

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