2015-06-20 32 views
0

我編寫了一個小型Ruby CGI腳本 - 讓我們調用downloader.rb - 幫助我的朋友輕鬆地從文件託管服務下載文件。我的腳本如下所示:當向Opera發送數據時,Ruby CGI無法繼續下載過程

#!/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 

他們可以使用我的腳本,例如通過在瀏覽器中粘貼此鏈接調用它: http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl 然後下載開始,THX的支持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協同工作,當然還有其他瀏覽器能夠繼續中斷下載?

回答

0

好的,問題解決了。我必須在第一個地方添加一個額外的標題行,名爲:「狀態:206部分內容」,但僅當範圍請求標頭不爲空時。