Created
August 17, 2009 13:16
-
-
Save matschaffer/169114 to your computer and use it in GitHub Desktop.
A basic Rack::CGI implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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