warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: cgi-lib is deprecated after Ruby 1.8.1; use cgi instead"
= simple CGI support library
query['field'] # <== value of 'field'
query.keys # <== array of fields
and query has Hash class methods
query.cookie['name'] # <== cookie value of 'name'
query.cookie.keys # <== all cookie names
and query.cookie has Hash class methods
== print HTTP header and HTML string to $>
CGI::tag("HEAD"){ CGI::tag("TITLE"){"TITLE"} } +
CGI::tag("FORM", {"ACTION"=>"test.rb", "METHOD"=>"POST"}){
CGI::tag("INPUT", {"TYPE"=>"submit", "VALUE"=>"submit"})
== make raw cookie string
cookie1 = CGI::cookie({'name' => 'name',
'path' => 'path', # optional
'domain' => 'domain', # optional
'expires' => Time.now, # optional
'secure' => true # optional
CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
== print HTTP header and string to $>
# == CGI::print("Content-Type: text/html"){ "string" }
CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
=== NPH (no-parse-header) mode
CGI::print("nph"){ "string" }
# == CGI::print("nph", "Content-Type: text/html"){ "string" }
CGI::print("nph", "Content-Type: text/html", cookie1, cookie2){ "string" }
CGI::tag("element", {"attribute_name"=>"attribute_value"}){"content"}
== make HTTP header string
CGI::header # == CGI::header("Content-Type: text/html")
CGI::header("Content-Type: text/html", cookie1, cookie2)
=== NPH (no-parse-header) mode
CGI::header("nph") # == CGI::header("nph", "Content-Type: text/html")
CGI::header("nph", "Content-Type: text/html", cookie1, cookie2)
url_encoded_string = CGI::escape("string")
string = CGI::unescape("url encoded string")
CGI::escapeHTML("string")
class CGI < SimpleDelegator
RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
# make rfc1123 date string
def CGI::rfc1123_date(time)
return format("%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
str.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
str.gsub(/\+/, ' ').gsub(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
str.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
# offline mode. read name=value pairs on standard input.
words = Shellwords.shellwords(
STDERR.print "(offline mode: enter name=value pairs on standard input)\n" if STDIN.tty?
readlines.join(' ').gsub(/\n/, '')
end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26'))
if words.find{|x| x =~ /=/} then words.join('&') else words.join('+') end
def initialize(input = $stdin)
case ENV['REQUEST_METHOD']
ENV['QUERY_STRING'] or ""
input.read(Integer(ENV['CONTENT_LENGTH'])) or ""
end.split(/[&;]/).each do |x|
key, val = x.split(/=/,2).collect{|x|CGI::unescape(x)}
@inputs[key] += "\0" + (val or "")
@inputs[key] = (val or "")
if ENV.has_key?('HTTP_COOKIE') or ENV.has_key?('COOKIE')
(ENV['HTTP_COOKIE'] or ENV['COOKIE']).split(/; /).each do |x|
key, val = x.split(/=/,2)
val = val.split(/&/).collect{|x|CGI::unescape(x)}.join("\0")
@cookie[key] += "\0" + val
def CGI::tag(element, attributes = {})
"<" + escapeHTML(element) + attributes.collect{|name, value|
" " + escapeHTML(name) + '="' + escapeHTML(value) + '"'
(iterator? ? yield.to_s + "</" + escapeHTML(element) + ">" : "")
"Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
(options['domain'] ? '; domain=' + options['domain'] : '') +
(options['path'] ? '; path=' + options['path'] : '') +
(options['expires'] ? '; expires=' + rfc1123_date(options['expires']) : '') +
(options['secure'] ? '; secure' : '')
# make HTTP header string
def CGI::header(*options)
option.sub(/(.*?): (.*)/){
Apache::request.headers_out[$1] = $2
Apache::request.send_http_header
if options.delete("nph") or (ENV['SERVER_SOFTWARE'] =~ /IIS/)
[(ENV['SERVER_PROTOCOL'] or "HTTP/1.0") + " 200 OK",
"Date: " + rfc1123_date(Time.now),
"Server: " + (ENV['SERVER_SOFTWARE'] or ""),
(options.empty? ? ["Content-Type: text/html"] : options)
options.empty? ? ["Content-Type: text/html"] : options
end.join(EOL) + EOL + EOL
# print HTTP header and string to $>
$>.print CGI::header(*options) + yield.to_s
def CGI::message(message, title = "", header = ["Content-Type: text/html"])
if message.kind_of?(Hash)
header = message['header']
message = message['body']
CGI::tag("HEAD"){ CGI.tag("TITLE"){ title } } +
CGI::tag("BODY"){ message }
# print error message to $> and exit
CGI::message({'title'=>'ERROR', 'body'=>
"ERROR: " + CGI::tag("STRONG"){ escapeHTML($!.to_s) } + "\n" + escapeHTML($@.join("\n"))