From af0a9f9eefaee3a8120cfd8d05cbc431af376da3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sun, 29 Jul 2012 17:26:07 -0700 Subject: added live responses which can be written and read in separate threads --- actionpack/lib/action_controller.rb | 1 + actionpack/lib/action_controller/metal/live.rb | 42 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 actionpack/lib/action_controller/metal/live.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index 7c10fcbb8a..1d06c83338 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -1,5 +1,6 @@ require 'abstract_controller' require 'action_dispatch' +require 'action_controller/metal/live' module ActionController extend ActiveSupport::Autoload diff --git a/actionpack/lib/action_controller/metal/live.rb b/actionpack/lib/action_controller/metal/live.rb new file mode 100644 index 0000000000..aea00849d3 --- /dev/null +++ b/actionpack/lib/action_controller/metal/live.rb @@ -0,0 +1,42 @@ +require 'action_dispatch/http/response' + +module ActionController + module Live + class Response < ActionDispatch::Response + class Buffer < ActionDispatch::Response::Buffer # :nodoc: + def initialize(response) + @response = response + @buf = Queue.new + end + + def write(string) + unless @response.committed? + @response.headers["Cache-Control"] = "no-cache" + @response.headers.delete("Content-Length") + end + + super + end + + def each + while str = @buf.pop + yield str + end + end + + def close + super + @buf.push nil + end + end + + private + + def build_buffer(response, body) + buf = Buffer.new response + body.each { |part| buf.write part } + buf + end + end + end +end -- cgit v1.2.3