Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created August 30, 2008 22:54
Show Gist options
  • Save thinkerbot/8149 to your computer and use it in GitHub Desktop.
Save thinkerbot/8149 to your computer and use it in GitHub Desktop.
A basic cgi script
#!/usr/bin/env ruby
################################################################
# A basic cgi script. To run, set up the following:
#
# /dir
# |- cgi-bin
# | `- this_script.cgi
# `- the_webrick_script.rb
#
# (Of course the_webrick_script is this commented-out bit)
# The only trick is that this_script.cgi must have execution
# priviledges; else you will get a 'Premature end of script
# headers' error. Note the shebang line too...
#
# Assembled from:
# - {Gnomes Guide to Webrick}[http://microjet.ath.cx/webrickguide/html/CGIHandler.html]
# - {CGI documentation}[http://stdlib.rubyonrails.org/libdoc/cgi/rdoc/classes/CGI.html]
################################################################
# require 'webrick'
#
# include WEBrick
#
# def start_webrick(config = {})
# # always listen on port 8080
# config.update(:Port => 8080)
# server = HTTPServer.new(config)
# yield server if block_given?
# ['INT', 'TERM'].each {|signal|
# trap(signal) {server.shutdown}
# }
# server.start
#
# end
#
# start_webrick {|server|
# cgi_dir = File.expand_path('./cgi-bin')
# server.mount("/cgi-bin", HTTPServlet::FileHandler, cgi_dir,
# {:FancyIndexing=>true})
# }
require "cgi"
cgi = CGI.new("html3") # add HTML generation methods
cgi.out() do
cgi.html() do
cgi.head{ cgi.title{"TITLE"} } +
cgi.body() do
cgi.form("post") do
cgi.textarea("get_text") +
cgi.br +
cgi.submit
end +
cgi.pre() do
CGI::escapeHTML(
"params: " + cgi.params.inspect + "\n" +
"cookies: " + cgi.cookies.inspect + "\n" +
ENV.collect() do |key, value|
key + " --> " + value + "\n"
end.join("")
)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment