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

127 Views Asked by At

I wrote a small Ruby CGI script - let's call downloader.rb - to help my friends easily download files from a file hosting service. My script looks as follows:

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

They can use my script for example calling it by pasting such a link in their browsers: http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl And then downloading begins, thx to the supported cookie file.

My problem is, when I use cURL or Wget to download files, I am able to continue the downloading after I interrupt it by pressing Ctrl+C (for example with "curl -C - http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl" or "wget -c http://www.example.com/cgi-bin/downloader.rb?id=33fi3trctwtl" ) both program can continue the downloading process, and files are intact according to their md5 sums. However when I am using Opera browser to download files and I stop the downloading process, then I am unable to continue them. What can I do to make it work with Opera and of course other browsers which has the ability to continue interrupted downloads?

1

There are 1 best solutions below

0
On BEST ANSWER

Okay, problem solved. I had to add an extra header line at the first place called: "Status: 206 Partial Content", but only when range request header isn't empty.