1 # -*- encoding: binary -*-
5 # base module for evented models like Rev and EventMachine
8 include Rainbows::Const
9 include Rainbows::Response
11 NULL_IO = Unicorn::HttpRequest::NULL_IO
13 # Apps may return this Rack response: AsyncResponse = [ -1, {}, [] ]
14 ASYNC_CALLBACK = "async.callback".freeze
16 ASYNC_CLOSE = "async.close".freeze
21 @state = :headers # [ :body [ :trailers ] ] :app_call :close
25 # graceful exit, like SIGQUIT
31 msg = Error.response(e) and write(msg)
36 # returns whether to enable response chunking for autochunk models
37 def stream_response_headers(status, headers)
38 if headers['Content-Length']
41 rv = !!(headers['Transfer-Encoding'] =~ %r{\Achunked\z}i)
42 rv = false if headers.delete('X-Rainbows-Autochunk') == 'no'
44 write(response_header(status, headers))
48 # TeeInput doesn't map too well to this right now...
52 @hp.headers(@env, @buf << data) or return
54 len = @hp.content_length
57 app_call # common case
59 # since we don't do streaming input, we have no choice but
60 # to take over 100-continue handling from the Rack application
61 if @env[HTTP_EXPECT] =~ /\A100-continue\z/i
62 write(EXPECT_100_RESPONSE)
63 @env.delete(HTTP_EXPECT)
65 @input = CapInput.new(len, self)
66 @hp.filter_body(@buf2 = "", @buf)
75 @hp.filter_body(@buf2, @buf << data)
80 if @hp.trailers(@env, @buf << data)
89 class CapInput < Struct.new(:io, :client, :bytes_left)
90 MAX_BODY = Unicorn::Const::MAX_BODY
93 def self.err(client, msg)
94 client.write(Const::ERROR_413_RESPONSE)
97 # zip back up the stack
98 raise IOError, msg, []
101 def self.new(len, client)
102 max = Rainbows.max_bytes
104 if max && (len > max)
105 err(client, "Content-Length too big: #{len} > #{max}")
107 len <= MAX_BODY ? StringIO.new("") : Util.tmpio
109 max ? super(Util.tmpio, client, max) : Util.tmpio
114 if (self.bytes_left -= buf.size) < 0
116 CapInput.err(client, "chunked request body too big")
121 def gets; io.gets; end
122 def each(&block); io.each(&block); end
123 def size; io.size; end
124 def rewind; io.rewind; end
125 def read(*args); io.read(*args); end