diff options
Diffstat (limited to 'lib/cgi-lib.rb')
-rw-r--r-- | lib/cgi-lib.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/cgi-lib.rb b/lib/cgi-lib.rb new file mode 100644 index 0000000000..afadbff3b6 --- /dev/null +++ b/lib/cgi-lib.rb @@ -0,0 +1,56 @@ +#!/usr/local/bin/ruby +# +# Get CGI String +# +# EXAMPLE: +# require "cgi-lib.rb" +# foo = CGI.new +# foo['field'] <== value of 'field' +# foo.keys <== array of fields +# foo.inputs <== hash of { <field> => <value> } + +class CGI + attr("inputs") + + def initialize + str = if ENV['REQUEST_METHOD'] == "GET" + ENV['QUERY_STRING'] + elsif ENV['REQUEST_METHOD'] == "POST" + $stdin.read ENV['CONTENT_LENGTH'].to_i + else + "" + end + arr = str.split(/&/) + @inputs = {} + arr.each do |x| + x.gsub!(/\+/, ' ') + key, val = x.split(/=/, 2) + val = "" unless val + + key.gsub!(/%(..)/) { [$1.hex].pack("c") } + val.gsub!(/%(..)/) { [$1.hex].pack("c") } + + @inputs[key] += "\0" if @inputs[key] + @inputs[key] += val + end + end + + def keys + @inputs.keys + end + + def [](key) + @inputs[key] + end + + def CGI.message(msg, title = "") + print "Content-type: text/html\n\n" + print "<html><head><title>" + print title + print "</title></head><body>\n" + print msg + print "</body></html>\n" + TRUE + end + +end |