1 # -*- encoding: binary -*-
3 # base module for evented models like Rev and EventMachine
4 module Rainbows::EvCore
5 include Rainbows::Const
6 include Rainbows::Response
8 NULL_IO = Unicorn::HttpRequest::NULL_IO
9 HttpParser = Rainbows::HttpParser
10 autoload :CapInput, 'rainbows/ev_core/cap_input'
12 # Apps may return this Rack response: AsyncResponse = [ -1, {}, [] ]
13 ASYNC_CALLBACK = "async.callback".freeze
15 ASYNC_CLOSE = "async.close".freeze
21 @state = :headers # [ :body [ :trailers ] ] :app_call :close
24 # graceful exit, like SIGQUIT
33 msg = Rainbows::Error.response(e) and write(msg)
38 # returns whether to enable response chunking for autochunk models
39 def stream_response_headers(status, headers, alive)
40 headers = Rack::Utils::HeaderHash.new(headers)
41 if headers['Content-Length']
44 rv = !!(headers['Transfer-Encoding'] =~ %r{\Achunked\z}i)
45 rv = false if headers.delete('X-Rainbows-Autochunk') == 'no'
47 write_headers(status, headers, alive)
51 def prepare_request_body
52 # since we don't do streaming input, we have no choice but
53 # to take over 100-continue handling from the Rack application
54 if @env[HTTP_EXPECT] =~ /\A100-continue\z/i
55 write(EXPECT_100_RESPONSE)
56 @env.delete(HTTP_EXPECT)
59 @hp.filter_body(@buf2 = "", @buf)
64 # TeeInput doesn't map too well to this right now...
69 @hp.parse or return want_more
71 if 0 == @hp.content_length
73 app_call # common case
87 @hp.filter_body(@buf2, @buf << data)
94 if @hp.trailers(@env, @buf << data)
106 write(Rainbows::Const::ERROR_413_RESPONSE)
108 # zip back up the stack
109 raise IOError, msg, []
112 TmpIO = Unicorn::TmpIO
115 bytes <= CBB ? StringIO.new("") : TmpIO.new
119 max = Rainbows.max_bytes
120 len = @hp.content_length
122 if max && (len > max)
123 err_413("Content-Length too big: #{len} > #{max}")
127 max ? CapInput.new(io_for(max), self, max) : TmpIO.new
132 const_set :CBB, Unicorn::TeeInput.client_body_buffer_size