123456789_123456789_123456789_123456789_123456789_

Class: Rackup::Handler::CGI

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, ::Rack
Inherits: Object
Defined in: lib/rackup/handler/cgi.rb

Constant Summary

::Rack - Included

Handler, Server

Class Method Summary

Class Method Details

.run(app, **options)

[ GitHub ]

  
# File 'lib/rackup/handler/cgi.rb', line 11

def self.run(app, **options)
  $stdin.binmode
  serve app
end

.send_body(body)

[ GitHub ]

  
# File 'lib/rackup/handler/cgi.rb', line 51

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

.send_headers(status, headers)

[ GitHub ]

  
# File 'lib/rackup/handler/cgi.rb', line 40

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

.serve(app)

[ GitHub ]

  
# File 'lib/rackup/handler/cgi.rb', line 16

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

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

  env.update(
    RACK_INPUT        => $stdin,
    RACK_ERRORS       => $stderr,
    RACK_URL_SCHEME   => ["yes", "on", "1"].include?(ENV[HTTPS]) ? "https" : "http"
  )

  env[QUERY_STRING] ||= ""
  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