lib/reel/rack/server.rb in reel-rack-0.2.0 vs lib/reel/rack/server.rb in reel-rack-0.2.1

- old
+ new

@@ -29,24 +29,33 @@ route_request request end end end + # Compile the regex once + CONTENT_LENGTH_HEADER = %r{^content-length$}i + def route_request(request) options = { :method => request.method, :input => request.body.to_s, "REMOTE_ADDR" => request.remote_addr }.merge(convert_headers(request.headers)) status, headers, body = app.call ::Rack::MockRequest.env_for(request.url, options) - if body.respond_to?(:to_str) - request.respond status_symbol(status), headers, body.to_str - elsif body.respond_to?(:each) - request.respond status_symbol(status), headers.merge(:transfer_encoding => :chunked) - body.each { |chunk| request << chunk } - request.finish_response + if body.respond_to? :each + # If Content-Length was specified we can send the response all at once + if headers.keys.detect { |h| h =~ CONTENT_LENGTH_HEADER } + # Can't use collect here because Rack::BodyProxy/Rack::Lint isn't a real Enumerable + full_body = '' + body.each { |b| full_body << b } + request.respond status_symbol(status), headers, full_body + else + request.respond status_symbol(status), headers.merge(:transfer_encoding => :chunked) + body.each { |chunk| request << chunk } + request.finish_response + end else Logger.error("don't know how to render: #{body.inspect}") request.respond :internal_server_error, "An error occurred processing your request" end