From 336348328c07b663c09eff79c40e4d9dd363b607 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Mon, 15 Feb 1999 07:31:51 +0000 Subject: This commit was manufactured by cvs2svn to create tag 'v1_3_1_990215'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_3_1_990215@403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/cgi-lib.rb | 125 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 108 insertions(+), 17 deletions(-) (limited to 'lib/cgi-lib.rb') diff --git a/lib/cgi-lib.rb b/lib/cgi-lib.rb index 7033f0f8c1..83ea6118b9 100644 --- a/lib/cgi-lib.rb +++ b/lib/cgi-lib.rb @@ -7,6 +7,43 @@ # foo['field'] <== value of 'field' # foo.keys <== array of fields # and foo has Hash class methods +# +# foo.cookie['name'] <== cookie value of 'name' +# foo.cookie.keys <== all cookie names +# and foo.cookie has Hash class methods +# +# make raw cookie string +# cookie1 = CGI.cookie({'name' => 'name', +# 'value' => 'value', +# 'path' => 'path', # optional +# 'domain' => 'domain', # optional +# 'expires' => Time.now, # optional +# 'secure' => true # optional +# }) +# +# print CGI.header("Content-Type: text/html", cookie1, cookie2) +# +# print CGI.header("HTTP/1.0 200 OK", "Content-Type: text/html") +# print CGI.header # == print CGI.header("Content-Type: text/html") +# +# make HTML tag string +# CGI.tag("element", {"attribute_name"=>"attribute_value"}){"content"} +# +# 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") +# } +# } +# +# print HTTP header and strings to STDOUT +# CGI.print{ "string" } # add HTTP header "Content-Type: text/html" +# CGI.print("Content-Type: text/plain"){ "string" } +# CGI.print("HTTP/1.0 200 OK", "Content-Type: text/html"){ "string" } + # if running on Windows(IIS or PWS) then change cwd. if ENV['SERVER_SOFTWARE'] =~ /^Microsoft-/ then @@ -17,7 +54,12 @@ require "delegate" class CGI < SimpleDelegator + CR = "\015" + LF = "\012" + EOL = CR + LF + attr("inputs") + attr("cookie") # original is CGI.pm def read_from_cmdline @@ -40,11 +82,17 @@ class CGI < SimpleDelegator # unescape url encoded def unescape(str) - str.gsub! /\+/, ' ' + str.gsub!(/\+/, ' ') str.gsub!(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") } str end - module_function :escape, :unescape + + # escape HTML + def escapeHTML(str) + str.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/" + + (iterator? ? yield.to_s + "" : "") end - def CGI.message(msg, title = "") - print "Content-type: text/html\n\n" - print "" - print title - print "\n" - print msg - print "\n" + def CGI.print(*header) + header.push("Content-Type: text/html") if header.empty? + STDOUT.print CGI.header(*header) + yield.to_s + end + + 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 end def CGI.error - m = $!.to_s.dup - m.gsub!(/&/, '&') - m.gsub!(//, '>') - msgs = ["
ERROR: #{m}"]
-    msgs << $@
-    msgs << "
" - CGI.message(msgs.join("\n"), "ERROR") + CGI.message({'title'=>'ERROR', 'body'=> + CGI.tag("PRE"){ + "ERROR: " + CGI.tag("STRONG"){ escapeHTML($!.to_s) } + "\n" + + escapeHTML($@.join("\n")) + } + }) exit end end -- cgit v1.2.3