Sha256: cd263ba229c4b23219a3876010ee7eabfe337b8c964f4e14a7974c2419854b32

Contents?: true

Size: 1.53 KB

Versions: 222

Compression:

Stored size: 1.53 KB

Contents

require 'rack/content_length'
require 'rack/rewindable_input'

module Rack
  module Handler
    class CGI
      def self.run(app, options=nil)
        $stdin.binmode
        serve app
      end

      def self.serve(app)
        env = ENV.to_hash
        env.delete "HTTP_CONTENT_LENGTH"

        env["SCRIPT_NAME"] = ""  if env["SCRIPT_NAME"] == "/"

        env.update({"rack.version" => Rack::VERSION,
                     "rack.input" => Rack::RewindableInput.new($stdin),
                     "rack.errors" => $stderr,

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

                     "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
                   })

        env["QUERY_STRING"] ||= ""
        env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
        env["REQUEST_PATH"] ||= "/"

        status, headers, body = app.call(env)
        begin
          send_headers status, headers
          send_body body
        ensure
          body.close  if body.respond_to? :close
        end
      end

      def self.send_headers(status, headers)
        $stdout.print "Status: #{status}\r\n"
        headers.each { |k, vs|
          vs.split("\n").each { |v|
            $stdout.print "#{k}: #{v}\r\n"
          }
        }
        $stdout.print "\r\n"
        $stdout.flush
      end

      def self.send_body(body)
        body.each { |part|
          $stdout.print part
          $stdout.flush
        }
      end
    end
  end
end

Version data entries

222 entries across 190 versions & 35 rubygems

Version Path
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/handler/cgi.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/handler/cgi.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/handler/cgi.rb
classiccms-0.7.5 vendor/bundle/gems/rack-1.4.1/lib/rack/handler/cgi.rb
classiccms-0.7.4 vendor/bundle/gems/rack-1.4.1/lib/rack/handler/cgi.rb
classiccms-0.7.3 vendor/bundle/gems/rack-1.4.1/lib/rack/handler/cgi.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/handler/cgi.rb
rack-1.4.7 lib/rack/handler/cgi.rb
rack-1.5.5 lib/rack/handler/cgi.rb
rack-1.4.6 lib/rack/handler/cgi.rb
rack-1.5.4 lib/rack/handler/cgi.rb
rack-1.5.3 lib/rack/handler/cgi.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rack-1.4.5/lib/rack/handler/cgi.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/cgi.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/cgi.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/handler/cgi.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/handler/cgi.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/handler/cgi.rb
rack-1.6.0.beta lib/rack/handler/cgi.rb
scout_realtime-1.0.5 lib/vendor/rack-1.5.2/lib/rack/handler/cgi.rb