1999-08-13 09:45:20 +04:00
|
|
|
=begin
|
|
|
|
|
|
|
|
= simple CGI support library
|
|
|
|
|
|
|
|
= example
|
|
|
|
|
|
|
|
== get form values
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
query = CGI.new
|
|
|
|
query['field'] # <== value of 'field'
|
|
|
|
query.keys # <== array of fields
|
|
|
|
|
|
|
|
and query has Hash class methods
|
|
|
|
|
|
|
|
|
|
|
|
== get cookie values
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
query = CGI.new
|
|
|
|
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 $>
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
CGI::print{
|
|
|
|
CGI::tag("HTML"){
|
|
|
|
CGI::tag("HEAD"){ CGI::tag("TITLE"){"TITLE"} } +
|
|
|
|
CGI::tag("BODY"){
|
|
|
|
CGI::tag("FORM", {"ACTION"=>"test.rb", "METHOD"=>"POST"}){
|
|
|
|
CGI::tag("INPUT", {"TYPE"=>"submit", "VALUE"=>"submit"})
|
|
|
|
} +
|
|
|
|
CGI::tag("HR")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
== make raw cookie string
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
cookie1 = CGI::cookie({'name' => 'name',
|
|
|
|
'value' => 'value',
|
|
|
|
'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 $>
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
CGI::print{ "string" }
|
|
|
|
# == CGI::print("Content-Type: text/html"){ "string" }
|
|
|
|
CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
|
|
|
|
|
|
|
|
|
|
|
|
=== NPH (no-parse-header) mode
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
CGI::print("nph"){ "string" }
|
|
|
|
# == CGI::print("nph", "Content-Type: text/html"){ "string" }
|
|
|
|
CGI::print("nph", "Content-Type: text/html", cookie1, cookie2){ "string" }
|
|
|
|
|
|
|
|
|
|
|
|
== make HTML tag string
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
CGI::tag("element", {"attribute_name"=>"attribute_value"}){"content"}
|
|
|
|
|
|
|
|
|
|
|
|
== make HTTP header string
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
== escape url encode
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
url_encoded_string = CGI::escape("string")
|
|
|
|
|
|
|
|
|
|
|
|
== unescape url encoded
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
string = CGI::unescape("url encoded string")
|
|
|
|
|
|
|
|
|
|
|
|
== escape HTML &"<>
|
|
|
|
|
|
|
|
require "cgi-lib.rb"
|
|
|
|
CGI::escapeHTML("string")
|
|
|
|
|
|
|
|
|
|
|
|
=end
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
require "delegate"
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
class CGI < SimpleDelegator
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
CR = "\015"
|
|
|
|
LF = "\012"
|
|
|
|
EOL = CR + LF
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-10-12 08:53:36 +04:00
|
|
|
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
|
2000-07-19 12:04:52 +04:00
|
|
|
def CGI::rfc1123_date(time)
|
1999-10-12 08:53:36 +04:00
|
|
|
t = time.clone.gmtime
|
|
|
|
return format("%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
|
|
|
|
RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
|
|
|
|
t.hour, t.min, t.sec)
|
|
|
|
end
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
# escape url encode
|
2000-07-19 12:04:52 +04:00
|
|
|
def CGI::escape(str)
|
1999-08-13 09:45:20 +04:00
|
|
|
str.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
|
1998-01-16 15:19:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
# unescape url encoded
|
2000-07-19 12:04:52 +04:00
|
|
|
def CGI::unescape(str)
|
1999-08-13 09:45:20 +04:00
|
|
|
str.gsub(/\+/, ' ').gsub(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
|
|
|
|
end
|
|
|
|
|
|
|
|
# escape HTML
|
2000-07-19 12:04:52 +04:00
|
|
|
def CGI::escapeHTML(str)
|
1999-08-13 09:45:20 +04:00
|
|
|
str.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
|
|
|
|
end
|
|
|
|
|
|
|
|
# offline mode. read name=value pairs on standard input.
|
|
|
|
def read_from_cmdline
|
|
|
|
require "shellwords.rb"
|
|
|
|
words = Shellwords.shellwords(
|
|
|
|
if not ARGV.empty?
|
|
|
|
ARGV.join(' ')
|
|
|
|
else
|
|
|
|
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
|
1998-01-16 15:19:22 +03:00
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def initialize(input = $stdin)
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
@inputs = {}
|
1999-08-13 09:45:20 +04:00
|
|
|
@cookie = {}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
case ENV['REQUEST_METHOD']
|
|
|
|
when "GET"
|
|
|
|
ENV['QUERY_STRING'] or ""
|
|
|
|
when "POST"
|
1999-08-13 09:45:20 +04:00
|
|
|
input.read(Integer(ENV['CONTENT_LENGTH'])) or ""
|
1998-01-16 15:19:22 +03:00
|
|
|
else
|
|
|
|
read_from_cmdline
|
1999-10-12 08:53:36 +04:00
|
|
|
end.split(/[&;]/).each do |x|
|
2000-07-19 12:04:52 +04:00
|
|
|
key, val = x.split(/=/,2).collect{|x|CGI::unescape(x)}
|
1999-01-20 07:59:39 +03:00
|
|
|
if @inputs.include?(key)
|
|
|
|
@inputs[key] += "\0" + (val or "")
|
|
|
|
else
|
|
|
|
@inputs[key] = (val or "")
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
super(@inputs)
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
if ENV.has_key?('HTTP_COOKIE') or ENV.has_key?('COOKIE')
|
2002-12-24 17:42:38 +03:00
|
|
|
(ENV['HTTP_COOKIE'] or ENV['COOKIE']).split(/; /).each do |x|
|
1999-08-13 09:45:20 +04:00
|
|
|
key, val = x.split(/=/,2)
|
2000-07-19 12:04:52 +04:00
|
|
|
key = CGI::unescape(key)
|
|
|
|
val = val.split(/&/).collect{|x|CGI::unescape(x)}.join("\0")
|
1999-08-13 09:45:20 +04:00
|
|
|
if @cookie.include?(key)
|
|
|
|
@cookie[key] += "\0" + val
|
|
|
|
else
|
|
|
|
@cookie[key] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
attr("inputs")
|
|
|
|
attr("cookie")
|
|
|
|
|
|
|
|
# make HTML tag string
|
|
|
|
def CGI::tag(element, attributes = {})
|
|
|
|
"<" + escapeHTML(element) + attributes.collect{|name, value|
|
|
|
|
" " + escapeHTML(name) + '="' + escapeHTML(value) + '"'
|
|
|
|
}.to_s + ">" +
|
|
|
|
(iterator? ? yield.to_s + "</" + escapeHTML(element) + ">" : "")
|
|
|
|
end
|
|
|
|
|
|
|
|
# make raw cookie string
|
|
|
|
def CGI::cookie(options)
|
|
|
|
"Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
|
|
|
|
(options['domain'] ? '; domain=' + options['domain'] : '') +
|
|
|
|
(options['path'] ? '; path=' + options['path'] : '') +
|
1999-10-12 08:53:36 +04:00
|
|
|
(options['expires'] ? '; expires=' + rfc1123_date(options['expires']) : '') +
|
1999-08-13 09:45:20 +04:00
|
|
|
(options['secure'] ? '; secure' : '')
|
|
|
|
end
|
|
|
|
|
|
|
|
# make HTTP header string
|
|
|
|
def CGI::header(*options)
|
2002-05-20 23:56:52 +04:00
|
|
|
if defined?(MOD_RUBY)
|
1999-08-13 09:45:20 +04:00
|
|
|
options.each{|option|
|
|
|
|
option.sub(/(.*?): (.*)/){
|
2002-05-20 23:56:52 +04:00
|
|
|
Apache::request.headers_out[$1] = $2
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Apache::request.send_http_header
|
|
|
|
''
|
|
|
|
else
|
|
|
|
if options.delete("nph") or (ENV['SERVER_SOFTWARE'] =~ /IIS/)
|
|
|
|
[(ENV['SERVER_PROTOCOL'] or "HTTP/1.0") + " 200 OK",
|
1999-10-12 08:53:36 +04:00
|
|
|
"Date: " + rfc1123_date(Time.now),
|
1999-08-13 09:45:20 +04:00
|
|
|
"Server: " + (ENV['SERVER_SOFTWARE'] or ""),
|
|
|
|
"Connection: close"] +
|
|
|
|
(options.empty? ? ["Content-Type: text/html"] : options)
|
|
|
|
else
|
|
|
|
options.empty? ? ["Content-Type: text/html"] : options
|
|
|
|
end.join(EOL) + EOL + EOL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# print HTTP header and string to $>
|
|
|
|
def CGI::print(*options)
|
|
|
|
$>.print CGI::header(*options) + yield.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# print message to $>
|
|
|
|
def CGI::message(message, title = "", header = ["Content-Type: text/html"])
|
|
|
|
if message.kind_of?(Hash)
|
|
|
|
title = message['title']
|
|
|
|
header = message['header']
|
|
|
|
message = message['body']
|
|
|
|
end
|
|
|
|
CGI::print(*header){
|
|
|
|
CGI::tag("HTML"){
|
|
|
|
CGI::tag("HEAD"){ CGI.tag("TITLE"){ title } } +
|
|
|
|
CGI::tag("BODY"){ message }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
# print error message to $> and exit
|
|
|
|
def CGI::error
|
|
|
|
CGI::message({'title'=>'ERROR', 'body'=>
|
|
|
|
CGI::tag("PRE"){
|
|
|
|
"ERROR: " + CGI::tag("STRONG"){ escapeHTML($!.to_s) } + "\n" + escapeHTML($@.join("\n"))
|
|
|
|
}
|
|
|
|
})
|
1998-01-16 15:19:22 +03:00
|
|
|
exit
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|