rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob cf97c4dba2f38a06ad907327c1819ae9df7135da 926 bytes (raw)
$ git show v3.2.0:lib/rainbows/reverse_proxy/ev_client.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
29
30
31
32
33
34
35
36
37
38
39
 
# -*- encoding: binary -*-
# :enddoc:
require 'tempfile'
module Rainbows::ReverseProxy::EvClient
  include Rainbows::ReverseProxy::Synchronous
  AsyncCallback = "async.callback"
  CBB = Unicorn::TeeInput.client_body_buffer_size
  Content_Length = "Content-Length"
  Transfer_Encoding = "Transfer-Encoding"

  def receive_data(buf)
    if @body
      @body << buf
    else
      response = @parser.headers(@headers, @rbuf << buf) or return
      if (cl = @headers[Content_Length] && cl.to_i > CBB) ||
         (%r{\bchunked\b} =~ @headers[Transfer_Encoding])
        @body = LargeBody.new("")
        @body << @rbuf
        @response = response << @body
      else
        @body = @rbuf.dup
        @response = response << [ @body ]
      end
    end
  end

  class LargeBody < Tempfile
    def each
      buf = ""
      rewind
      while read(16384, buf)
        yield buf
      end
    end

    alias close close!
  end
end

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