From ed14b9bdbb35fa18dc283ba2d048a33d10759b2d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jul 2010 10:10:04 +0000 Subject: rev: split out deferred chunk logic from unchunked Similar to what we do in EM, this avoid unnecessary conditional logic inside more frequently used code paths. --- lib/rainbows/rev.rb | 1 + lib/rainbows/rev/client.rb | 4 ++-- lib/rainbows/rev/deferred_chunk_response.rb | 16 ++++++++++++++++ lib/rainbows/rev/deferred_response.rb | 7 ++----- 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 lib/rainbows/rev/deferred_chunk_response.rb diff --git a/lib/rainbows/rev.rb b/lib/rainbows/rev.rb index b670ad7..1ee705c 100644 --- a/lib/rainbows/rev.rb +++ b/lib/rainbows/rev.rb @@ -37,6 +37,7 @@ module Rainbows end autoload :DeferredResponse,'rainbows/rev/deferred_response' + autoload :DeferredChunkResponse,'rainbows/rev/deferred_chunk_response' include Core # :startdoc: end diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb index 5c61109..4d1aaec 100644 --- a/lib/rainbows/rev/client.rb +++ b/lib/rainbows/rev/client.rb @@ -62,10 +62,10 @@ module Rainbows # used for streaming sockets and pipes def stream_response(status, headers, io, body) - do_chunk = stream_response_headers(status, headers) if headers + c = stream_response_headers(status, headers) if headers # we only want to attach to the Rev::Loop belonging to the # main thread in Ruby 1.9 - io = DeferredResponse.new(io, self, do_chunk, body) + io = (c ? DeferredChunkResponse : DeferredResponse).new(io, self, body) defer_body(io.attach(Server::LOOP)) end diff --git a/lib/rainbows/rev/deferred_chunk_response.rb b/lib/rainbows/rev/deferred_chunk_response.rb new file mode 100644 index 0000000..35991d1 --- /dev/null +++ b/lib/rainbows/rev/deferred_chunk_response.rb @@ -0,0 +1,16 @@ +# -*- encoding: binary -*- +# :enddoc: +# +# this is class is specific to Rev for proxying IO-derived objects +class Rainbows::Rev::DeferredChunkResponse < Rainbows::Rev::DeferredResponse + def on_read(data) + @client.write("#{data.size.to_s(16)}\r\n") + @client.write(data) + @client.write("\r\n") + end + + def on_close + @client.write("0\r\n\r\n") + super + end +end diff --git a/lib/rainbows/rev/deferred_response.rb b/lib/rainbows/rev/deferred_response.rb index f0172d6..13396d8 100644 --- a/lib/rainbows/rev/deferred_response.rb +++ b/lib/rainbows/rev/deferred_response.rb @@ -4,19 +4,16 @@ # this is class is specific to Rev for writing large static files # or proxying IO-derived objects class Rainbows::Rev::DeferredResponse < ::Rev::IO - def initialize(io, client, do_chunk, body) + def initialize(io, client, body) super(io) - @client, @do_chunk, @body = client, do_chunk, body + @client, @body = client, body end def on_read(data) - @do_chunk and @client.write("#{data.size.to_s(16)}\r\n") @client.write(data) - @do_chunk and @client.write("\r\n") end def on_close - @do_chunk and @client.write("0\r\n\r\n") @client.next @body.respond_to?(:close) and @body.close end -- cgit v1.2.3-24-ge0c7