lib/falcon/adapters/rack.rb in falcon-0.15.2 vs lib/falcon/adapters/rack.rb in falcon-0.16.0

- old
+ new

@@ -17,23 +17,23 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require_relative 'input' +require_relative 'response' -require 'async/http/body/buffered' require 'async/logger' module Falcon module Adapters class Rack def initialize(app, logger = Async.logger) @app = app @logger = logger end - def call(request, peer: nil, address: nil) + def call(request) request_path, query_string = request.path.split('?', 2) server_name, server_port = (request.authority || '').split(':', 2) env = { 'rack.version' => [2, 0, 0], @@ -76,30 +76,39 @@ request.headers.each do |key, value| env["HTTP_#{key.upcase.tr('-', '_')}"] = value end - if peer + if remote_address = request.remote_address + env['REMOTE_ADDR'] = remote_address.ip_address if remote_address.ip? + end + + if request.hijack? env['rack.hijack?'] = true + env['rack.hijack'] = lambda do - env['rack.hijack_io'] = peer + env['rack.hijack_io'] = request.hijack end - - if remote_address = peer.remote_address - env['REMOTE_ADDR'] = remote_address.ip_address if remote_address.ip? - end + else + env['rack.hijack?'] = false end status, headers, body = @app.call(env) + # if hijack = headers.delete('rack.hijack') + # body = Async::HTTP::Body::Writable.new + # + # Task.current.async do + # hijack.call(body) + # end + # return nil + # end + if env['rack.hijack_io'] - throw :hijack - else - # We normalize headers to be lower case. - headers = headers.map{|key, value| [key.downcase, value]}.to_h - - return Async::HTTP::Response[status, headers, Async::HTTP::Body::Buffered.wrap(body)] + return nil end + + return Response.new(status, headers, body) rescue => exception @logger.error "#{exception.class}: #{exception.message}\n\t#{$!.backtrace.join("\n\t")}" return failure_response(exception) end