Skip to content

Instantly share code, notes, and snippets.

@matschaffer
Created August 17, 2009 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matschaffer/169114 to your computer and use it in GitHub Desktop.
Save matschaffer/169114 to your computer and use it in GitHub Desktop.
A basic Rack::CGI implementation.
require 'open3'
require 'pathname'
module Rack
class CGI
def initialize(script)
@script = Pathname.new(script)
end
def call(env)
env.each { |k, v| ENV[k] = v unless k.include? 'rack' }
res = Response.new
Dir.chdir(@script.dirname) do
Open3.popen3("./#{@script.basename}") do |stdin, stdout, stderr|
begin
stdin.write env['rack.input'].read
stdin.close_write
rescue Errno::EPIPE; end
puts stderr.read
line = stdout.readline
while line.strip.length > 0
header_name, header_value = line.split(': ', 2)
if header_name == "Status"
res.status = header_value.strip.to_i
else
res[header_name] = header_value.rstrip
end
line = stdout.readline
end
res.write stdout.read unless stdout.closed?
end
end
res.finish
end
end
end
run Rack::CGI.new("index.cgi")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment