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 = Unicorn::HttpParser
11 # Apps may return this Rack response: AsyncResponse = [ -1, {}, [] ]
12 ASYNC_CALLBACK = "async.callback".freeze
14 ASYNC_CLOSE = "async.close".freeze
20 @state = :headers # [ :body [ :trailers ] ] :app_call :close
23 # graceful exit, like SIGQUIT
29 msg = Rainbows::Error.response(e) and write(msg)
34 # returns whether to enable response chunking for autochunk models
35 def stream_response_headers(status, headers)
36 if headers['Content-Length']
39 rv = !!(headers['Transfer-Encoding'] =~ %r{\Achunked\z}i)
40 rv = false if headers.delete('X-Rainbows-Autochunk') == 'no'
42 write(response_header(status, headers))
46 # TeeInput doesn't map too well to this right now...
53 len = @hp.content_length
56 app_call # common case
58 # since we don't do streaming input, we have no choice but
59 # to take over 100-continue handling from the Rack application
60 if @env[HTTP_EXPECT] =~ /\A100-continue\z/i
61 write(EXPECT_100_RESPONSE)
62 @env.delete(HTTP_EXPECT)
64 @input = CapInput.new(len, self)
65 @hp.filter_body(@buf2 = "", @buf)
74 @hp.filter_body(@buf2, @buf << data)
79 if @hp.trailers(@env, @buf << data)
88 class CapInput < Struct.new(:io, :client, :bytes_left)
89 MAX_BODY = Unicorn::Const::MAX_BODY
90 TmpIO = Unicorn::TmpIO
92 def self.err(client, msg)
93 client.write(Rainbows::Const::ERROR_413_RESPONSE)
96 # zip back up the stack
97 raise IOError, msg, []
100 def self.new(len, client)
101 max = Rainbows.max_bytes
103 if max && (len > max)
104 err(client, "Content-Length too big: #{len} > #{max}")
106 len <= MAX_BODY ? StringIO.new("") : TmpIO.new
108 max ? super(TmpIO.new, client, max) : TmpIO.new
113 if (self.bytes_left -= buf.size) < 0
115 CapInput.err(client, "chunked request body too big")
120 def gets; io.gets; end
121 def each(&block); io.each(&block); end
122 def size; io.size; end
123 def rewind; io.rewind; end
124 def read(*args); io.read(*args); end