preliminary reverse proxy Rack application
[rainbows.git] / lib / rainbows / reverse_proxy / ev_client.rb
blob94e7f8223e695c53c87d1eae5b756f1b1849b7bd
1 # -*- encoding: binary -*-
2 # :enddoc:
3 require 'tempfile'
4 module Rainbows::ReverseProxy::EvClient
5   include Rainbows::ReverseProxy::Synchronous
6   AsyncCallback = "async.callback"
7   CBB = Unicorn::TeeInput.client_body_buffer_size
8   Content_Length = "Content-Length"
9   Transfer_Encoding = "Transfer-Encoding"
11   def receive_data(buf)
12     if @body
13       @body << buf
14     else
15       response = @parser.headers(@headers, @rbuf << buf) or return
16       if (cl = @headers[Content_Length] && cl.to_i > CBB) ||
17          (%r{\bchunked\b} =~ @headers[Transfer_Encoding])
18         @body = LargeBody.new("")
19         @body << @rbuf
20         @response = response << @body
21       else
22         @body = @rbuf.dup
23         @response = response << [ @body ]
24       end
25     end
26   end
28   class LargeBody < Tempfile
29     def each(&block)
30       buf = ""
31       rewind
32       while read(16384, buf)
33         yield buf
34       end
35     end
37     alias close close!
38   end
39 end