Sha256: 80ffcf37e0cb2fbda820f1ce2a5e006f4428558e1d1335e371e6cf26e38d7683

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

require 'scgi'
require 'stringio'
require 'rack/content_length'
require 'rack/chunked'

module Rack
  module Handler
    class SCGI < ::SCGI::Processor
      attr_accessor :app
      
      def self.run(app, options=nil)
        new(options.merge(:app=>app,
                          :host=>options[:Host],
                          :port=>options[:Port],
                          :socket=>options[:Socket])).listen
      end
      
      def initialize(settings = {})
        @app = Rack::Chunked.new(Rack::ContentLength.new(settings[:app]))
        @log = Object.new
        def @log.info(*args); end
        def @log.error(*args); end
        super(settings)
      end
        
      def process_request(request, input_body, socket)
        env = {}.replace(request)
        env.delete "HTTP_CONTENT_TYPE"
        env.delete "HTTP_CONTENT_LENGTH"
        env["REQUEST_PATH"], env["QUERY_STRING"] = env["REQUEST_URI"].split('?', 2)
        env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
        env["PATH_INFO"] = env["REQUEST_PATH"]
        env["QUERY_STRING"] ||= ""
        env["SCRIPT_NAME"] = ""
        env.update({"rack.version" => [1,0],
                     "rack.input" => StringIO.new(input_body),
                     "rack.errors" => $stderr,

                     "rack.multithread" => true,
                     "rack.multiprocess" => true,
                     "rack.run_once" => false,

                     "rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
                   })
        status, headers, body = app.call(env)
        begin
          socket.write("Status: #{status}\r\n")
          headers.each do |k, vs|
            vs.split("\n").each { |v| socket.write("#{k}: #{v}\r\n")}
          end
          socket.write("\r\n")
          body.each {|s| socket.write(s)}
        ensure
          body.close if body.respond_to? :close
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
colouringcode-passenger-0.1 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb
passenger-2.2.7 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb
passenger-2.2.6 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb
passenger-2.2.5 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb
passenger-2.2.3 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb
passenger-2.2.4 vendor/rack-1.0.0-git/lib/rack/handler/scgi.rb