Sha256: f2981ce6133fc595f574ebeadccfd49d9336899198f8ba02bf1a27528d661dde

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'm2r'
require 'rack'

module M2R
  # Handle Mongrel2 requests using Rack application
  # @private
  class RackHandler < Handler
    attr_accessor :app

    def initialize(app, connection_factory, parser)
      @app = app
      super(connection_factory, parser)

      trap('INT') { stop }
    end

    def process(request)
      script_name = request.pattern.split('(', 2).first.gsub(/\/$/, '')

      env = {
        'REQUEST_METHOD'    => request.method,
        'SCRIPT_NAME'       => script_name,
        'PATH_INFO'         => request.path.gsub(script_name, ''),
        'QUERY_STRING'      => request.query || "",
        'rack.version'      => ::Rack::VERSION,
        'rack.errors'       => $stderr,
        'rack.multithread'  => false,
        'rack.multiprocess' => true,
        'rack.run_once'     => false,
        'rack.url_scheme'   => request.scheme,
        'rack.input'        => request.body_io
      }
      env['SERVER_NAME'], env['SERVER_PORT'] = request.headers['Host'].split(':', 2)
      request.headers.rackify(env)

      status, headers, body = @app.call(env)
      buffer = ""
      body.each { |part| buffer << part }
      return Response.new.status(status).headers(headers).body(buffer)
    end

    def after_all(request, response)
      request.free!
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
m2r-2.0.1 lib/m2r/rack_handler.rb
m2r-2.0.0 lib/m2r/rack_handler.rb