Ruby CGI 在向 Opera 发送数据时无法继续下载过程

Ruby CGI is unable to continue the downloading process when sending data to Opera

我写了一个小 Ruby CGI 脚本 - 让我们调用 downloader.rb - 来帮助我的朋友轻松地从文件托管服务下载文件。我的脚本如下所示:

#!/usr/bin/ruby

require "cgi"

cgi=CGI.new(:accept_charset => "UTF-8")
file_id=cgi["id"] #get the file id from the GET request variable
url="http://www.yetanotherfilehost.com/file/#{file_id}"

range=ENV["HTTP_RANGE"]==nil ? "" : "-r #{ENV["HTTP_RANGE"].split("=")[1]}" #get the range HTTP request header which in fact is passed in the request header, but can be accessed through an environment variable called HTTP_RANGE

header=%x{curl #{range} -s -b ./cookie.txt "#{url}" -L -I}.force_encoding("utf-8").encode("utf-8").split("\r\n").last(7).join("\r\n")
#I simply call curl with option -I to dump the response headers and -L
# to follow redirection which occurs in this case, and then keeping only 
# the last 7 lines of headers, to send them back later to my users, 
# thus emulating a download process. Response headers looks like follows:

=begin

HTTP/1.1 302 Found
Date: Sat, 20 Jun 2015 06:25:19 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: auth=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; expires=Sun, 20-Dec-2015 07:25:19 GMT; path=/; domain=.yetanotherfilehost.com
Location: http://server007.yetanotherfilehost.com/get/p/33fi3trctwtl/c50fc7dbb8824cfe/gandahar.avi
Connection: close
Content-Type: text/html; charset=utf-8

HTTP/1.1 200 OK
Server: nginx
Date: Sat, 20 Jun 2015 06:25:20 GMT
*Content-Type: application/force-download
*Content-Length: 1468299264
*Last-Modified: Fri, 12 Sep 2014 11:43:06 GMT
*Connection: close
*Content-Disposition: attachment; filename="gandahar.avi"
*ETag: "5412dc4a-57847800"
*Accept-Ranges: bytes

=end

puts header # sending back the last 7 lines of headers marked with asterisks, to my users.
puts
system "curl #{range} -s -b ./cookie.txt \"#{ff_url}\" -L -o -" #download the file and writing the content to stdout

他们可以使用我的脚本,例如通过在他们的浏览器中粘贴 link 来调用它: http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl 然后开始下载,感谢支持的 cookie 文件。

我的问题是,当我使用 cURL 或 Wget 下载文件时,我可以在按 Ctrl+C 中断下载后继续下载(例如 "curl -C - http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl" 或 "wget -c http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl" ) 两个程序都可以继续下载过程,并且根据它们的 md5 总和,文件是完整的。但是,当我使用 Opera 浏览器下载文件并停止下载过程时,我无法继续下载。我该怎么做才能让它与 Opera 一起工作,当然还有其他能够继续中断下载的浏览器?

好的,问题解决了。我不得不在第一个名为 "Status: 206 Partial Content" 的地方添加一个额外的 header 行,但只有当范围请求 header 不为空时才行。