rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob de348bb2e9ca6e3bc78fecfe4b2b87e2e466d707 777 bytes (raw)
$ git show v0.95.1:lib/rainbows/rev/deferred_response.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
# -*- encoding: binary -*-
# :enddoc:
module Rainbows
  module Rev

    # this is class is specific to Rev for writing large static files
    # or proxying IO-derived objects
    class DeferredResponse < ::Rev::IO
      include Rainbows::Const
      def initialize(io, client, do_chunk, body)
        super(io)
        @client, @do_chunk, @body = client, do_chunk, body
      end

      def on_read(data)
        @do_chunk and @client.write(sprintf("%x\r\n", data.size))
        @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.quit
        @body.respond_to?(:close) and @body.close
      end
    end # class DeferredResponse
  end # module Rev
end # module Rainbows

git clone https://yhbt.net/rainbows.git