Created
May 23, 2012 15:24
-
-
Save aschoerk/2775878 to your computer and use it in GitHub Desktop.
Allows to debug a cgi script in ruby with minimal effort inside an IDE.
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
#!/usr/bin/env ruby | |
$:.unshift File.join(File.dirname(__FILE__),'.','') | |
require 'webrick' | |
require 'cgi' | |
require 'stringio' | |
$cgi_input = nil | |
class CGI | |
def stdoutput | |
@str_output = StringIO.new if not defined? @str_output | |
@str_output | |
end | |
def stdinput | |
$cgi_input | |
end | |
def to_s | |
return @str_output.string if defined? @str_output | |
"EMPTY" | |
end | |
end | |
class CgiTester < WEBrick::HTTPServlet::AbstractServlet | |
def initialize(server) | |
super | |
@script_filename = "inprocess_script" | |
@tempdir = server[:TempDir] | |
end | |
def do_GET(req, res) | |
if RUBY_VERSION >= "1.9" | |
load "#{ARGF.argv[0]}" | |
else | |
load "#{$ARGV[0]}" | |
end | |
cgi_in = Tempfile.new("webrick.cgi_in.", @tempdir) | |
meta = req.meta_vars | |
ENV.keys.each{|name| ENV.delete(name) } | |
meta.each{|k, v| ENV[k] = v if v } | |
if req.body and req.body.size > 0 | |
cgi_in.write(req.body) | |
end | |
cgi_in.close | |
$cgi_input = File.open(cgi_in.path) | |
cgi = initCGI(CGI.new("html3")) | |
$cgi_input.close | |
content = cgi.to_s | |
res.status = 200 | |
ix = content.index("\r\n\r\n") | |
res.content_type = 'text/html' | |
res.content_length = content.length - ix | |
res.body = content[ix..-1] | |
end | |
alias do_POST do_GET | |
end | |
include WEBrick | |
s = HTTPServer.new( | |
:Port => 8081 | |
) | |
s.mount "/cgitester", CgiTester | |
trap("INT") { s.shutdown } | |
s.start | |
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 'cgi' | |
def initCGI(cgi) | |
cgi.out { | |
cgi.head { | |
cgi.title {"test title"} | |
} + | |
cgi.body { | |
cgi.h1 {"Test Header"} | |
} | |
} | |
return cgi | |
end | |
if __FILE__ == $0 | |
cgi = CGI.new("html3") | |
initCGI(cgi) | |
end | |
Hi, aschoerk. Tell me why this block needs? Thanks!
if FILE == $0
cgi = CGI.new("html3")
initCGI(cgi)
end
@JunjiUmeno, that __FILE__ == $0
check returns true when the file is being executed on it's own, and not loaded into the cgitester script. When deploying to the real webserver, Apache, or whatever webserver you are using, will invoke the script directly, instead of invoking the initCGI
method like the cgitester script. Since the function as a whole is executed by the webserver the script needs to invoke initCGI
on its own.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I searched for a method to be able to easily debug cgi-scripts
Start with: ruby cgitester.rb examplecgi.rb
Open in Browser: http://localhost:8081/cgitester
Tested with ruby 1.8.7, don't use it as server, it's not thread safe