Ruby CGI redirect response without pending end of script

I am developing a video image processing tool in the CGI ruby ​​and want to redirect the user to another place after clicking the start-ffmpeg-for-hour-long-encoding-spree button.

Here is the code:

@cgi.out("status" => "302", "location" => @job.report_url) {''}
@cgi.out{''}
@job.start

Doing this works fine with Safari 5.0.5. But Firefox waits for the script to complete its work before redirecting. And, if your script takes longer than completing the Apache timeout, this may never happen.

I was hoping for the kinf of the cgi.close () method. What exists! But this is the CGI :: Session method, and I have nothing to do.

Here is another similar question ... But not a duplicate! Since I need to use stdin, stdout and stderr after , redirection: Returning a response using Ruby CGI before the script is complete?

So, how can I send the full CGI response before , performing some other tasks with the same script?

+3
source share
4 answers

Well, finally it worked out.

But this seems more like a hack than a real solution:

@cgi.out("status" => "303", "Connection" => "close", "Content-Length" => 1, "Location" => @job.report_url) {' '}
@job.start

There are two keys to make it work, both are needed:

  • Set Content-Lenghtsomething larger to 0. 1 Works well with one content space.
  • Connection close. , HTTP 1.1 Keep Alive. , , Firefox 4.

303 See Other POST (HTTP 303). , 302.

+1

.

  • cgi

, cgi. cgi . , , /.

, , , .

, . ( )/

- .

+1

(, Firefox) .

, 4096 :

@cgi.out("status" => "302", "location" => @job.report_url) { ' ' * 4096 }
@job.start

: :

#!/usr/bin/ruby

require 'cgi'

# change the line below to test; e.g.: buf = ''
buf = ' ' * 4096

cgi = CGI.new
cgi.out('status' => '302', 'location' => 'http://www.example.com') { buf }

sleep 10

puts 'end'

, 'end' , .

buf , Firefox 10 . "" (.. 4K ), . Firefox 4.0 Ubuntu 10.04 Firefox 4.0.1 Windows Seven.

+1

Content-Length 0. Kernel #.

. , . Content-Length cgi.out. , 0.

, , stdout . lighttpd + ree, firefox chrome.

require 'cgi'    
cgi = CGI.new
cgi.out('status' => '302', 'location' => 'http://www.google.com') { "" }
$stdout.close

# here we do a very very long task
sleep 30
exit 0
+1

All Articles