1999-11-04 03:39:57 -05:00
|
|
|
=begin
|
|
|
|
|
2000-09-11 18:02:37 -04:00
|
|
|
== NAME
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-09-11 18:02:37 -04:00
|
|
|
cgi.rb - cgi support library
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-06-19 17:18:41 -04:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2002-05-29 00:48:32 -04:00
|
|
|
Wakou Aoyama <wakou@ruby-lang.org>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
== EXAMPLE
|
|
|
|
|
|
|
|
=== GET FORM VALUES
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
values = cgi['field_name'] # <== array of 'field_name'
|
|
|
|
# if not 'field_name' included, then return [].
|
|
|
|
fields = cgi.keys # <== array of field names
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
# returns true if form has 'field_name'
|
|
|
|
cgi.has_key?('field_name')
|
|
|
|
cgi.has_key?('field_name')
|
|
|
|
cgi.include?('field_name')
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
=== GET FORM VALUES AS HASH
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
params = cgi.params
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
cgi.params is a hash.
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
cgi.params['new_field_name'] = ["value"] # add new param
|
|
|
|
cgi.params['field_name'] = ["new_value"] # change value
|
|
|
|
cgi.params.delete('field_name') # delete param
|
|
|
|
cgi.params.clear # delete all params
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
=== SAVE FORM VALUES TO FILE
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "pstore"
|
|
|
|
db = PStore.new("query.db")
|
|
|
|
db.transaction do
|
|
|
|
db["params"] = cgi.params
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
=== RESTORE FORM VALUES FROM FILE
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "pstore"
|
|
|
|
db = PStore.new("query.db")
|
|
|
|
db.transaction do
|
|
|
|
cgi.params = db["params"]
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
=== GET MULTIPART FORM VALUES
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
values = cgi['field_name'] # <== array of 'field_name'
|
|
|
|
values[0].read # <== body of values[0]
|
|
|
|
values[0].local_path # <== path to local file of values[0]
|
|
|
|
values[0].original_filename # <== original filename of values[0]
|
|
|
|
values[0].content_type # <== content_type of values[0]
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
and values[0] has Tempfile class methods.
|
|
|
|
(Tempfile class object has File class methods)
|
|
|
|
|
|
|
|
|
|
|
|
=== GET COOKIE VALUES
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
values = cgi.cookies['name'] # <== array of 'name'
|
|
|
|
# if not 'name' included, then return [].
|
|
|
|
names = cgi.cookies.keys # <== array of cookie names
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
and cgi.cookies is a hash.
|
|
|
|
|
|
|
|
|
|
|
|
=== GET COOKIE OBJECTS
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
for name, cookie in cgi.cookies
|
|
|
|
cookie.expires = Time.now + 30
|
|
|
|
end
|
|
|
|
cgi.out("cookie" => cgi.cookies){"string"}
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
cgi.cookies['name'].expires = Time.now + 30
|
|
|
|
cgi.out("cookie" => cgi.cookies['name']){"string"}
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
and see MAKE COOKIE OBJECT.
|
|
|
|
|
|
|
|
|
|
|
|
=== GET ENVIRONMENT VALUE
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
require "cgi"
|
|
|
|
cgi = CGI.new
|
|
|
|
value = cgi.auth_type
|
|
|
|
# ENV["AUTH_TYPE"]
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
see http://www.w3.org/CGI/
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
AUTH_TYPE CONTENT_LENGTH CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
|
|
|
|
PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST REMOTE_IDENT
|
|
|
|
REMOTE_USER REQUEST_METHOD SCRIPT_NAME SERVER_NAME SERVER_PORT
|
|
|
|
SERVER_PROTOCOL SERVER_SOFTWARE
|
|
|
|
|
|
|
|
content_length and server_port return Integer. and the others return String.
|
|
|
|
|
|
|
|
and HTTP_COOKIE, HTTP_COOKIE2
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
value = cgi.raw_cookie
|
|
|
|
# ENV["HTTP_COOKIE"]
|
|
|
|
value = cgi.raw_cookie2
|
|
|
|
# ENV["HTTP_COOKIE2"]
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
and other HTTP_*
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
value = cgi.accept
|
|
|
|
# ENV["HTTP_ACCEPT"]
|
|
|
|
value = cgi.accept_charset
|
|
|
|
# ENV["HTTP_ACCEPT_CHARSET"]
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING HTTP_ACCEPT_LANGUAGE
|
|
|
|
HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST HTTP_NEGOTIATE HTTP_PRAGMA
|
|
|
|
HTTP_REFERER HTTP_USER_AGENT
|
|
|
|
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
=== PRINT HTTP HEADER AND HTML STRING TO $DEFAULT_OUTPUT ($>)
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
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() 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("")
|
|
|
|
)
|
2000-06-17 13:52:49 -04:00
|
|
|
end
|
2000-06-19 17:18:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# add HTML generation methods
|
|
|
|
CGI.new("html3") # html3.2
|
2001-09-06 18:18:11 -04:00
|
|
|
CGI.new("html4") # html4.01 (Strict)
|
|
|
|
CGI.new("html4Tr") # html4.01 Transitional
|
|
|
|
CGI.new("html4Fr") # html4.01 Frameset
|
2000-06-17 13:52:49 -04:00
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-09-11 18:02:37 -04:00
|
|
|
raise "Please, use ruby1.5.4 or later." if RUBY_VERSION < "1.5.4"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
require 'English'
|
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
class CGI
|
|
|
|
|
|
|
|
CR = "\015"
|
|
|
|
LF = "\012"
|
|
|
|
EOL = CR + LF
|
2001-02-25 14:57:56 -05:00
|
|
|
REVISION = '$Id$'
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
NEEDS_BINMODE = true if /WIN/ni.match(RUBY_PLATFORM)
|
1999-11-04 03:39:57 -05:00
|
|
|
PATH_SEPARATOR = {'UNIX'=>'/', 'WINDOWS'=>'\\', 'MACINTOSH'=>':'}
|
|
|
|
|
|
|
|
HTTP_STATUS = {
|
|
|
|
"OK" => "200 OK",
|
|
|
|
"PARTIAL_CONTENT" => "206 Partial Content",
|
|
|
|
"MULTIPLE_CHOICES" => "300 Multiple Choices",
|
|
|
|
"MOVED" => "301 Moved Permanently",
|
|
|
|
"REDIRECT" => "302 Found",
|
|
|
|
"NOT_MODIFIED" => "304 Not Modified",
|
|
|
|
"BAD_REQUEST" => "400 Bad Request",
|
|
|
|
"AUTH_REQUIRED" => "401 Authorization Required",
|
|
|
|
"FORBIDDEN" => "403 Forbidden",
|
|
|
|
"NOT_FOUND" => "404 Not Found",
|
|
|
|
"METHOD_NOT_ALLOWED" => "405 Method Not Allowed",
|
|
|
|
"NOT_ACCEPTABLE" => "406 Not Acceptable",
|
|
|
|
"LENGTH_REQUIRED" => "411 Length Required",
|
|
|
|
"PRECONDITION_FAILED" => "412 Rrecondition Failed",
|
|
|
|
"SERVER_ERROR" => "500 Internal Server Error",
|
|
|
|
"NOT_IMPLEMENTED" => "501 Method Not Implemented",
|
|
|
|
"BAD_GATEWAY" => "502 Bad Gateway",
|
|
|
|
"VARIANT_ALSO_VARIES" => "506 Variant Also Negotiates"
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ]
|
|
|
|
|
|
|
|
def env_table
|
|
|
|
ENV
|
|
|
|
end
|
|
|
|
|
|
|
|
def stdinput
|
|
|
|
$stdin
|
|
|
|
end
|
|
|
|
|
|
|
|
def stdoutput
|
2000-05-09 00:53:16 -04:00
|
|
|
$DEFAULT_OUTPUT
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private :env_table, :stdinput, :stdoutput
|
|
|
|
|
|
|
|
=begin
|
|
|
|
== METHODS
|
|
|
|
=end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== ESCAPE URL ENCODE
|
2000-06-19 17:18:41 -04:00
|
|
|
url_encoded_string = CGI::escape("string")
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::escape(string)
|
2000-09-11 18:02:37 -04:00
|
|
|
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
|
2000-12-24 15:20:49 -05:00
|
|
|
'%' + $1.unpack('H2' * $1.size).join('%').upcase
|
2000-09-11 18:02:37 -04:00
|
|
|
end.tr(' ', '+')
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== UNESCAPE URL ENCODED
|
2000-06-19 17:18:41 -04:00
|
|
|
string = CGI::unescape("url encoded string")
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::unescape(string)
|
2000-09-11 18:02:37 -04:00
|
|
|
string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
|
2000-12-24 15:20:49 -05:00
|
|
|
[$1.delete('%')].pack('H*')
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
2000-10-10 03:03:36 -04:00
|
|
|
=== ESCAPE HTML &\"<>
|
2000-06-19 17:18:41 -04:00
|
|
|
CGI::escapeHTML("string")
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::escapeHTML(string)
|
2000-05-09 00:53:16 -04:00
|
|
|
string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(/</n, '<')
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== UNESCAPE HTML
|
2000-06-19 17:18:41 -04:00
|
|
|
CGI::unescapeHTML("HTML escaped string")
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::unescapeHTML(string)
|
2000-05-09 00:53:16 -04:00
|
|
|
string.gsub(/&(.*?);/n) do
|
2000-12-24 15:20:49 -05:00
|
|
|
match = $1.dup
|
1999-11-04 03:39:57 -05:00
|
|
|
case match
|
|
|
|
when /\Aamp\z/ni then '&'
|
|
|
|
when /\Aquot\z/ni then '"'
|
|
|
|
when /\Agt\z/ni then '>'
|
|
|
|
when /\Alt\z/ni then '<'
|
2000-12-24 15:20:49 -05:00
|
|
|
when /\A#0*(\d+)\z/n then
|
|
|
|
if Integer($1) < 256
|
|
|
|
Integer($1).chr
|
2000-05-01 05:42:38 -04:00
|
|
|
else
|
2000-12-24 15:20:49 -05:00
|
|
|
if Integer($1) < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
|
|
|
|
[Integer($1)].pack("U")
|
2000-05-01 05:42:38 -04:00
|
|
|
else
|
2000-12-24 15:20:49 -05:00
|
|
|
"&##{$1};"
|
2000-09-11 18:02:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
when /\A#x([0-9a-f]+)\z/ni then
|
2000-12-24 15:20:49 -05:00
|
|
|
if $1.hex < 256
|
|
|
|
$1.hex.chr
|
2000-09-11 18:02:37 -04:00
|
|
|
else
|
2000-12-24 15:20:49 -05:00
|
|
|
if $1.hex < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
|
|
|
|
[$1.hex].pack("U")
|
2000-09-11 18:02:37 -04:00
|
|
|
else
|
2000-12-24 15:20:49 -05:00
|
|
|
"&#x#{$1};"
|
2000-05-01 05:42:38 -04:00
|
|
|
end
|
|
|
|
end
|
2000-09-11 18:02:37 -04:00
|
|
|
else
|
2000-12-24 15:20:49 -05:00
|
|
|
"&#{match};"
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== ESCAPE ELEMENT
|
2000-09-11 18:02:37 -04:00
|
|
|
print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
|
2000-06-19 17:18:41 -04:00
|
|
|
# "<BR><A HREF="url"></A>"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-09-11 18:02:37 -04:00
|
|
|
print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
|
2000-06-19 17:18:41 -04:00
|
|
|
# "<BR><A HREF="url"></A>"
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
2000-12-24 15:20:49 -05:00
|
|
|
def CGI::escapeElement(string, *elements)
|
|
|
|
elements = elements[0] if elements[0].kind_of?(Array)
|
|
|
|
unless elements.empty?
|
|
|
|
string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/ni) do
|
|
|
|
CGI::escapeHTML($&)
|
2000-09-11 18:02:37 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
string
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== UNESCAPE ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
print CGI::unescapeElement(
|
2000-09-11 18:02:37 -04:00
|
|
|
CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
|
2000-06-19 17:18:41 -04:00
|
|
|
# "<BR><A HREF="url"></A>"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
print CGI::unescapeElement(
|
2000-09-11 18:02:37 -04:00
|
|
|
CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
|
2000-06-19 17:18:41 -04:00
|
|
|
# "<BR><A HREF="url"></A>"
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
2000-12-24 15:20:49 -05:00
|
|
|
def CGI::unescapeElement(string, *elements)
|
|
|
|
elements = elements[0] if elements[0].kind_of?(Array)
|
|
|
|
unless elements.empty?
|
|
|
|
string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/ni) do
|
|
|
|
CGI::unescapeHTML($&)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
string
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== MAKE RFC1123 DATE STRING
|
2000-06-19 17:18:41 -04:00
|
|
|
CGI::rfc1123_date(Time.now)
|
2001-03-17 19:01:57 -05:00
|
|
|
# Sat, 01 Jan 2000 00:00:00 GMT
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::rfc1123_date(time)
|
|
|
|
t = time.clone.gmtime
|
2001-03-17 19:01:57 -05:00
|
|
|
return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
|
1999-11-04 03:39:57 -05:00
|
|
|
RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
|
|
|
|
t.hour, t.min, t.sec)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== MAKE HTTP HEADER STRING
|
2000-06-19 17:18:41 -04:00
|
|
|
header
|
|
|
|
# Content-Type: text/html
|
|
|
|
|
|
|
|
header("text/plain")
|
|
|
|
# Content-Type: text/plain
|
|
|
|
|
|
|
|
header({"nph" => true,
|
|
|
|
"status" => "OK", # == "200 OK"
|
|
|
|
# "status" => "200 GOOD",
|
|
|
|
"server" => ENV['SERVER_SOFTWARE'],
|
|
|
|
"connection" => "close",
|
|
|
|
"type" => "text/html",
|
|
|
|
"charset" => "iso-2022-jp",
|
|
|
|
# Content-Type: text/html; charset=iso-2022-jp
|
|
|
|
"language" => "ja",
|
|
|
|
"expires" => Time.now + 30,
|
|
|
|
"cookie" => [cookie1, cookie2],
|
|
|
|
"my_header1" => "my_value"
|
|
|
|
"my_header2" => "my_value"})
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
header will not convert charset.
|
|
|
|
|
|
|
|
status:
|
2000-06-19 17:18:41 -04:00
|
|
|
|
|
|
|
"OK" --> "200 OK"
|
|
|
|
"PARTIAL_CONTENT" --> "206 Partial Content"
|
|
|
|
"MULTIPLE_CHOICES" --> "300 Multiple Choices"
|
|
|
|
"MOVED" --> "301 Moved Permanently"
|
|
|
|
"REDIRECT" --> "302 Found"
|
|
|
|
"NOT_MODIFIED" --> "304 Not Modified"
|
|
|
|
"BAD_REQUEST" --> "400 Bad Request"
|
|
|
|
"AUTH_REQUIRED" --> "401 Authorization Required"
|
|
|
|
"FORBIDDEN" --> "403 Forbidden"
|
|
|
|
"NOT_FOUND" --> "404 Not Found"
|
|
|
|
"METHOD_NOT_ALLOWED" --> "405 Method Not Allowed"
|
|
|
|
"NOT_ACCEPTABLE" --> "406 Not Acceptable"
|
|
|
|
"LENGTH_REQUIRED" --> "411 Length Required"
|
|
|
|
"PRECONDITION_FAILED" --> "412 Rrecondition Failed"
|
|
|
|
"SERVER_ERROR" --> "500 Internal Server Error"
|
|
|
|
"NOT_IMPLEMENTED" --> "501 Method Not Implemented"
|
|
|
|
"BAD_GATEWAY" --> "502 Bad Gateway"
|
|
|
|
"VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
=end
|
|
|
|
def header(options = "text/html")
|
|
|
|
|
|
|
|
buf = ""
|
|
|
|
|
2001-09-06 18:18:11 -04:00
|
|
|
case options
|
|
|
|
when String
|
1999-11-04 03:39:57 -05:00
|
|
|
options = { "type" => options }
|
2001-09-06 18:18:11 -04:00
|
|
|
when Hash
|
|
|
|
options = options.dup
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
unless options.has_key?("type")
|
1999-11-04 03:39:57 -05:00
|
|
|
options["type"] = "text/html"
|
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("charset")
|
2001-09-06 18:18:11 -04:00
|
|
|
options["type"] += "; charset=" + options.delete("charset")
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2001-03-17 20:20:56 -05:00
|
|
|
options.delete("nph") if defined?(MOD_RUBY)
|
2001-03-17 19:01:57 -05:00
|
|
|
if options.delete("nph") or /IIS/n.match(env_table['SERVER_SOFTWARE'])
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += (env_table["SERVER_PROTOCOL"] or "HTTP/1.0") + " " +
|
|
|
|
(HTTP_STATUS[options["status"]] or options["status"] or "200 OK") +
|
|
|
|
EOL +
|
|
|
|
"Date: " + CGI::rfc1123_date(Time.now) + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
unless options.has_key?("server")
|
1999-11-04 03:39:57 -05:00
|
|
|
options["server"] = (env_table['SERVER_SOFTWARE'] or "")
|
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
unless options.has_key?("connection")
|
1999-11-04 03:39:57 -05:00
|
|
|
options["connection"] = "close"
|
|
|
|
end
|
|
|
|
|
2000-09-12 17:16:09 -04:00
|
|
|
options.delete("status")
|
|
|
|
end
|
|
|
|
|
|
|
|
if options.has_key?("status")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Status: " +
|
|
|
|
(HTTP_STATUS[options["status"]] or options["status"]) + EOL
|
2001-02-25 14:57:56 -05:00
|
|
|
options.delete("status")
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("server")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Server: " + options.delete("server") + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("connection")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Connection: " + options.delete("connection") + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Content-Type: " + options.delete("type") + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("length")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Content-Length: " + options.delete("length").to_s + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("language")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Content-Language: " + options.delete("language") + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("expires")
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Expires: " + CGI::rfc1123_date( options.delete("expires") ) + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("cookie")
|
1999-11-04 03:39:57 -05:00
|
|
|
if options["cookie"].kind_of?(String) or
|
|
|
|
options["cookie"].kind_of?(Cookie)
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Set-Cookie: " + options.delete("cookie").to_s + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
elsif options["cookie"].kind_of?(Array)
|
|
|
|
options.delete("cookie").each{|cookie|
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Set-Cookie: " + cookie.to_s + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
|
|
|
elsif options["cookie"].kind_of?(Hash)
|
|
|
|
options.delete("cookie").each_value{|cookie|
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Set-Cookie: " + cookie.to_s + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if @output_cookies
|
|
|
|
for cookie in @output_cookies
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += "Set-Cookie: " + cookie.to_s + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
options.each{|key, value|
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += key + ": " + value + EOL
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if defined?(MOD_RUBY)
|
2001-01-24 01:01:47 -05:00
|
|
|
table = Apache::request.headers_out
|
2001-02-25 14:57:56 -05:00
|
|
|
buf.scan(/([^:]+): (.+)#{EOL}/n){ |name, value|
|
|
|
|
$stderr.printf("name:%s value:%s\n", name, value) if $DEBUG
|
|
|
|
case name
|
|
|
|
when 'Set-Cookie'
|
|
|
|
table.add($1, $2)
|
|
|
|
when /^status$/ni
|
|
|
|
Apache::request.status_line = value
|
2001-11-18 15:08:14 -05:00
|
|
|
Apache::request.status = value.to_i
|
2001-02-25 14:57:56 -05:00
|
|
|
when /^content-type$/ni
|
|
|
|
Apache::request.content_type = value
|
|
|
|
when /^content-encoding$/ni
|
|
|
|
Apache::request.content_encoding = value
|
2001-11-18 15:08:14 -05:00
|
|
|
when /^location$/ni
|
2001-11-20 22:30:22 -05:00
|
|
|
if Apache::request.status == 200
|
|
|
|
Apache::request.status = 302
|
|
|
|
end
|
2001-11-18 15:08:14 -05:00
|
|
|
Apache::request.headers_out[name] = value
|
2001-02-25 14:57:56 -05:00
|
|
|
else
|
|
|
|
Apache::request.headers_out[name] = value
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
}
|
|
|
|
Apache::request.send_http_header
|
|
|
|
''
|
|
|
|
else
|
|
|
|
buf + EOL
|
|
|
|
end
|
|
|
|
|
|
|
|
end # header()
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
2000-05-09 00:53:16 -04:00
|
|
|
=== PRINT HTTP HEADER AND STRING TO $DEFAULT_OUTPUT ($>)
|
2000-06-19 17:18:41 -04:00
|
|
|
cgi = CGI.new
|
|
|
|
cgi.out{ "string" }
|
|
|
|
# Content-Type: text/html
|
|
|
|
# Content-Length: 6
|
|
|
|
#
|
|
|
|
# string
|
|
|
|
|
|
|
|
cgi.out("text/plain"){ "string" }
|
|
|
|
# Content-Type: text/plain
|
|
|
|
# Content-Length: 6
|
|
|
|
#
|
|
|
|
# string
|
|
|
|
|
|
|
|
cgi.out({"nph" => true,
|
|
|
|
"status" => "OK", # == "200 OK"
|
|
|
|
"server" => ENV['SERVER_SOFTWARE'],
|
|
|
|
"connection" => "close",
|
|
|
|
"type" => "text/html",
|
|
|
|
"charset" => "iso-2022-jp",
|
|
|
|
# Content-Type: text/html; charset=iso-2022-jp
|
|
|
|
"language" => "ja",
|
|
|
|
"expires" => Time.now + (3600 * 24 * 30),
|
|
|
|
"cookie" => [cookie1, cookie2],
|
|
|
|
"my_header1" => "my_value",
|
|
|
|
"my_header2" => "my_value"}){ "string" }
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-30 15:18:06 -04:00
|
|
|
if "HEAD" == REQUEST_METHOD then output only HTTP header.
|
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
if charset is "iso-2022-jp" or "euc-jp" or "shift_jis" then
|
|
|
|
convert string charset, and set language to "ja".
|
|
|
|
|
|
|
|
=end
|
|
|
|
def out(options = "text/html")
|
|
|
|
|
|
|
|
options = { "type" => options } if options.kind_of?(String)
|
|
|
|
content = yield
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if options.has_key?("charset")
|
1999-11-04 03:39:57 -05:00
|
|
|
require "nkf"
|
|
|
|
case options["charset"]
|
|
|
|
when /iso-2022-jp/ni
|
|
|
|
content = NKF::nkf('-j', content)
|
1999-12-07 04:25:55 -05:00
|
|
|
options["language"] = "ja" unless options.has_key?("language")
|
1999-11-04 03:39:57 -05:00
|
|
|
when /euc-jp/ni
|
|
|
|
content = NKF::nkf('-e', content)
|
1999-12-07 04:25:55 -05:00
|
|
|
options["language"] = "ja" unless options.has_key?("language")
|
1999-11-04 03:39:57 -05:00
|
|
|
when /shift_jis/ni
|
|
|
|
content = NKF::nkf('-s', content)
|
1999-12-07 04:25:55 -05:00
|
|
|
options["language"] = "ja" unless options.has_key?("language")
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
options["length"] = content.length.to_s
|
|
|
|
output = stdoutput
|
|
|
|
output.binmode if defined? output.binmode
|
|
|
|
output.print header(options)
|
2000-05-30 15:18:06 -04:00
|
|
|
output.print content unless "HEAD" == env_table['REQUEST_METHOD']
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== PRINT
|
2000-06-19 17:18:41 -04:00
|
|
|
cgi = CGI.new
|
|
|
|
cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def print(*options)
|
|
|
|
stdoutput.print(*options)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== MAKE COOKIE OBJECT
|
2000-06-19 17:18:41 -04:00
|
|
|
cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
|
|
|
|
cookie1 = CGI::Cookie::new({"name" => "name", "value" => "value"})
|
|
|
|
cookie1 = CGI::Cookie::new({'name' => 'name',
|
|
|
|
'value' => ['value1', 'value2', ...],
|
|
|
|
'path' => 'path', # optional
|
|
|
|
'domain' => 'domain', # optional
|
|
|
|
'expires' => Time.now, # optional
|
|
|
|
'secure' => true # optional
|
|
|
|
})
|
|
|
|
|
|
|
|
cgi.out({"cookie" => [cookie1, cookie2]}){ "string" }
|
|
|
|
|
|
|
|
name = cookie1.name
|
|
|
|
values = cookie1.value
|
|
|
|
path = cookie1.path
|
|
|
|
domain = cookie1.domain
|
|
|
|
expires = cookie1.expires
|
|
|
|
secure = cookie1.secure
|
|
|
|
|
|
|
|
cookie1.name = 'name'
|
|
|
|
cookie1.value = ['value1', 'value2', ...]
|
|
|
|
cookie1.path = 'path'
|
|
|
|
cookie1.domain = 'domain'
|
|
|
|
cookie1.expires = Time.now + 30
|
|
|
|
cookie1.secure = true
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
require "delegate"
|
|
|
|
class Cookie < SimpleDelegator
|
|
|
|
|
|
|
|
def initialize(name = "", *value)
|
|
|
|
options = if name.kind_of?(String)
|
|
|
|
{ "name" => name, "value" => value }
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
1999-12-07 04:25:55 -05:00
|
|
|
unless options.has_key?("name")
|
1999-11-04 03:39:57 -05:00
|
|
|
raise ArgumentError, "`name' required"
|
|
|
|
end
|
|
|
|
|
|
|
|
@name = options["name"]
|
|
|
|
@value = Array(options["value"])
|
2000-05-01 05:42:38 -04:00
|
|
|
# simple support for IE
|
|
|
|
if options["path"]
|
|
|
|
@path = options["path"]
|
|
|
|
else
|
2001-04-17 15:43:27 -04:00
|
|
|
%r|^(.*/)|.match(ENV["SCRIPT_NAME"])
|
|
|
|
@path = ($1 or "")
|
2000-05-01 05:42:38 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
@domain = options["domain"]
|
|
|
|
@expires = options["expires"]
|
|
|
|
@secure = options["secure"] == true ? true : false
|
|
|
|
|
|
|
|
super(@value)
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor("name", "value", "path", "domain", "expires")
|
|
|
|
attr_reader("secure")
|
|
|
|
def secure=(val)
|
|
|
|
@secure = val if val == true or val == false
|
|
|
|
@secure
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
buf = ""
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += @name + '='
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
if @value.kind_of?(String)
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += CGI::escape(@value)
|
1999-11-04 03:39:57 -05:00
|
|
|
else
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += @value.collect{|v| CGI::escape(v) }.join("&")
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if @domain
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += '; domain=' + @domain
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if @path
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += '; path=' + @path
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if @expires
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += '; expires=' + CGI::rfc1123_date(@expires)
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if @secure == true
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += '; secure'
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
end # class Cookie
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== PARSE RAW COOKIE STRING
|
2000-06-19 17:18:41 -04:00
|
|
|
cookies = CGI::Cookie::parse("raw_cookie_string")
|
|
|
|
# { "name1" => cookie1, "name2" => cookie2, ... }
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def Cookie::parse(raw_cookie)
|
|
|
|
cookies = Hash.new([])
|
2000-03-05 23:15:42 -05:00
|
|
|
return cookies unless raw_cookie
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
raw_cookie.split('; ').each do |pairs|
|
|
|
|
name, values = pairs.split('=',2)
|
|
|
|
name = CGI::unescape(name)
|
2000-03-05 23:15:42 -05:00
|
|
|
values ||= ""
|
2000-05-09 00:53:16 -04:00
|
|
|
values = values.split('&').collect{|v| CGI::unescape(v) }
|
2001-10-30 12:48:24 -05:00
|
|
|
unless cookies.has_key?(name)
|
1999-11-04 03:39:57 -05:00
|
|
|
cookies[name] = Cookie::new({ "name" => name, "value" => values })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
cookies
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== PARSE QUERY STRING
|
2000-06-19 17:18:41 -04:00
|
|
|
params = CGI::parse("query_string")
|
|
|
|
# {"name1" => ["value1", "value2", ...],
|
|
|
|
# "name2" => ["value1", "value2", ...], ... }
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::parse(query)
|
|
|
|
params = Hash.new([])
|
|
|
|
|
|
|
|
query.split(/[&;]/n).each do |pairs|
|
2000-05-09 00:53:16 -04:00
|
|
|
key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) }
|
1999-12-07 04:25:55 -05:00
|
|
|
if params.has_key?(key)
|
1999-11-04 03:39:57 -05:00
|
|
|
params[key].push(value)
|
|
|
|
else
|
|
|
|
params[key] = [value]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module QueryExtension
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
for env in %w[ CONTENT_LENGTH SERVER_PORT ]
|
1999-11-04 03:39:57 -05:00
|
|
|
eval( <<-END )
|
|
|
|
def #{env.sub(/^HTTP_/n, '').downcase}
|
|
|
|
env_table["#{env}"] && Integer(env_table["#{env}"])
|
|
|
|
end
|
|
|
|
END
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
for env in %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
|
1999-11-04 03:39:57 -05:00
|
|
|
PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
|
|
|
|
REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
|
|
|
|
SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
|
|
|
|
|
|
|
|
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
|
|
|
|
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
|
2000-05-09 00:53:16 -04:00
|
|
|
HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ]
|
1999-11-04 03:39:57 -05:00
|
|
|
eval( <<-END )
|
|
|
|
def #{env.sub(/^HTTP_/n, '').downcase}
|
|
|
|
env_table["#{env}"]
|
|
|
|
end
|
|
|
|
END
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
def raw_cookie
|
|
|
|
env_table["HTTP_COOKIE"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_cookie2
|
|
|
|
env_table["HTTP_COOKIE2"]
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor("cookies")
|
|
|
|
attr("params")
|
|
|
|
def params=(hash)
|
|
|
|
@params.clear
|
|
|
|
@params.update(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_multipart(boundary, content_length)
|
|
|
|
params = Hash.new([])
|
|
|
|
boundary = "--" + boundary
|
|
|
|
buf = ""
|
|
|
|
bufsize = 10 * 1024
|
|
|
|
|
|
|
|
# start multipart/form-data
|
|
|
|
stdinput.binmode
|
2000-06-13 11:53:19 -04:00
|
|
|
boundary_size = boundary.size + EOL.size
|
|
|
|
content_length -= boundary_size
|
|
|
|
status = stdinput.read(boundary_size)
|
|
|
|
if nil == status
|
|
|
|
raise EOFError, "no content body"
|
2002-05-29 00:48:32 -04:00
|
|
|
elsif boundary + EOL != status
|
|
|
|
raise EOFError, "bad content body"
|
2000-06-13 11:53:19 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
require "tempfile"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
until -1 == content_length
|
|
|
|
head = nil
|
|
|
|
body = Tempfile.new("CGI")
|
|
|
|
body.binmode
|
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
until head and /#{boundary}(?:#{EOL}|--)/n.match(buf)
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
if (not head) and /#{EOL}#{EOL}/n.match(buf)
|
2000-05-09 00:53:16 -04:00
|
|
|
buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
|
2000-12-24 15:20:49 -05:00
|
|
|
head = $1.dup
|
1999-11-04 03:39:57 -05:00
|
|
|
""
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if head and ( (EOL + boundary + EOL).size < buf.size )
|
|
|
|
body.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
|
|
|
|
buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
c = if bufsize < content_length
|
|
|
|
stdinput.read(bufsize) or ''
|
|
|
|
else
|
|
|
|
stdinput.read(content_length) or ''
|
|
|
|
end
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += c
|
1999-11-04 03:39:57 -05:00
|
|
|
content_length -= c.size
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
buf = buf.sub(/\A((?:.|\n)*?)(?:#{EOL})?#{boundary}(#{EOL}|--)/n) do
|
2000-12-24 15:20:49 -05:00
|
|
|
body.print $1
|
|
|
|
if "--" == $2
|
1999-11-04 03:39:57 -05:00
|
|
|
content_length = -1
|
|
|
|
end
|
|
|
|
""
|
2000-05-09 00:53:16 -04:00
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
body.rewind
|
|
|
|
|
|
|
|
eval <<-END
|
|
|
|
def body.local_path
|
|
|
|
#{body.path.dump}
|
|
|
|
end
|
|
|
|
END
|
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
/Content-Disposition:.* filename="?([^\";]*)"?/ni.match(head)
|
1999-11-04 03:39:57 -05:00
|
|
|
eval <<-END
|
|
|
|
def body.original_filename
|
|
|
|
#{
|
2000-12-22 04:00:23 -05:00
|
|
|
filename = ($1 or "").dup
|
2001-03-17 19:01:57 -05:00
|
|
|
if /Mac/ni.match(env_table['HTTP_USER_AGENT']) and
|
|
|
|
/Mozilla/ni.match(env_table['HTTP_USER_AGENT']) and
|
|
|
|
(not /MSIE/ni.match(env_table['HTTP_USER_AGENT']))
|
1999-11-04 03:39:57 -05:00
|
|
|
CGI::unescape(filename)
|
|
|
|
else
|
|
|
|
filename
|
2000-12-13 19:26:05 -05:00
|
|
|
end.dump.untaint
|
|
|
|
}.taint
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
END
|
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
/Content-Type: (.*)/ni.match(head)
|
1999-11-04 03:39:57 -05:00
|
|
|
eval <<-END
|
|
|
|
def body.content_type
|
2000-12-24 15:20:49 -05:00
|
|
|
#{($1 or "").dump.untaint}.taint
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
END
|
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
/Content-Disposition:.* name="?([^\";]*)"?/ni.match(head)
|
2000-12-24 15:20:49 -05:00
|
|
|
name = $1.dup
|
1999-11-04 03:39:57 -05:00
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if params.has_key?(name)
|
1999-11-04 03:39:57 -05:00
|
|
|
params[name].push(body)
|
|
|
|
else
|
|
|
|
params[name] = [body]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
params
|
|
|
|
end # read_multipart
|
|
|
|
private :read_multipart
|
|
|
|
|
|
|
|
# offline mode. read name=value pairs on standard input.
|
|
|
|
def read_from_cmdline
|
2000-05-09 00:53:16 -04:00
|
|
|
require "shellwords"
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
string = unless ARGV.empty?
|
|
|
|
ARGV.join(' ')
|
1999-11-04 03:39:57 -05:00
|
|
|
else
|
|
|
|
if STDIN.tty?
|
|
|
|
STDERR.print(
|
|
|
|
%|(offline mode: enter name=value pairs on standard input)\n|
|
|
|
|
)
|
|
|
|
end
|
2000-05-09 00:53:16 -04:00
|
|
|
readlines.join(' ').gsub(/\n/n, '')
|
|
|
|
end.gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
words = Shellwords.shellwords(string)
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2001-03-17 19:01:57 -05:00
|
|
|
if words.find{|x| /=/n.match(x) }
|
1999-11-04 03:39:57 -05:00
|
|
|
words.join('&')
|
|
|
|
else
|
|
|
|
words.join('+')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :read_from_cmdline
|
|
|
|
|
|
|
|
def initialize_query()
|
|
|
|
if ("POST" == env_table['REQUEST_METHOD']) and
|
2001-03-17 20:07:00 -05:00
|
|
|
%r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(env_table['CONTENT_TYPE'])
|
2000-12-24 15:20:49 -05:00
|
|
|
boundary = $1.dup
|
1999-11-04 03:39:57 -05:00
|
|
|
@params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
|
|
|
|
else
|
|
|
|
@params = CGI::parse(
|
|
|
|
case env_table['REQUEST_METHOD']
|
|
|
|
when "GET", "HEAD"
|
1999-12-07 04:25:55 -05:00
|
|
|
if defined?(MOD_RUBY)
|
1999-11-04 03:39:57 -05:00
|
|
|
Apache::request.args or ""
|
|
|
|
else
|
|
|
|
env_table['QUERY_STRING'] or ""
|
|
|
|
end
|
|
|
|
when "POST"
|
|
|
|
stdinput.binmode
|
|
|
|
stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or ''
|
|
|
|
else
|
|
|
|
read_from_cmdline
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2000-03-05 23:15:42 -05:00
|
|
|
@cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE']))
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
private :initialize_query
|
|
|
|
|
|
|
|
def [](*args)
|
|
|
|
@params[*args]
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys(*args)
|
|
|
|
@params.keys(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_key?(*args)
|
|
|
|
@params.has_key?(*args)
|
|
|
|
end
|
|
|
|
alias key? has_key?
|
|
|
|
alias include? has_key?
|
|
|
|
|
|
|
|
end # QueryExtension
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== HTML PRETTY FORMAT
|
2000-06-19 17:18:41 -04:00
|
|
|
print CGI::pretty("<HTML><BODY></BODY></HTML>")
|
|
|
|
# <HTML>
|
|
|
|
# <BODY>
|
|
|
|
# </BODY>
|
|
|
|
# </HTML>
|
|
|
|
|
|
|
|
print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
|
|
|
|
# <HTML>
|
|
|
|
# <BODY>
|
|
|
|
# </BODY>
|
|
|
|
# </HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def CGI::pretty(string, shift = " ")
|
2000-06-03 14:22:23 -04:00
|
|
|
lines = string.gsub(/(?!\A)<(?:.|\n)*?>/n, "\n\\0").gsub(/<(?:.|\n)*?>(?!\n)/n, "\\0\n")
|
|
|
|
end_pos = 0
|
|
|
|
while end_pos = lines.index(/^<\/(\w+)/n, end_pos)
|
2000-12-24 15:20:49 -05:00
|
|
|
element = $1.dup
|
2000-06-03 14:22:23 -04:00
|
|
|
start_pos = lines.rindex(/^\s*<#{element}/ni, end_pos)
|
|
|
|
lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/n, "\n" + shift) + "__"
|
|
|
|
end
|
2000-08-09 00:32:24 -04:00
|
|
|
lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/n, '\1')
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
== HTML ELEMENTS
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
cgi = CGI.new("html3") # add HTML generation methods
|
|
|
|
cgi.element
|
|
|
|
cgi.element{ "string" }
|
2001-02-25 14:57:56 -05:00
|
|
|
cgi.element({ "ATTRIBUTE1" => "value1", "ATTRIBUTE2" => "value2" })
|
|
|
|
cgi.element({ "ATTRIBUTE1" => "value1", "ATTRIBUTE2" => "value2" }){ "string" }
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
# add HTML generation methods
|
|
|
|
CGI.new("html3") # html3.2
|
|
|
|
CGI.new("html4") # html4.0 (Strict)
|
|
|
|
CGI.new("html4Tr") # html4.0 Transitional
|
|
|
|
CGI.new("html4Fr") # html4.0 Frameset
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
|
|
module TagMaker
|
|
|
|
|
|
|
|
# - -
|
|
|
|
def nn_element_def(element)
|
|
|
|
<<-END.gsub(/element\.downcase/n, element.downcase).gsub(/element\.upcase/n, element.upcase)
|
|
|
|
"<element.upcase" + attributes.collect{|name, value|
|
2000-05-01 05:42:38 -04:00
|
|
|
next unless value
|
1999-11-04 03:39:57 -05:00
|
|
|
" " + CGI::escapeHTML(name) +
|
|
|
|
if true == value
|
|
|
|
""
|
|
|
|
else
|
|
|
|
'="' + CGI::escapeHTML(value) + '"'
|
|
|
|
end
|
|
|
|
}.to_s + ">" +
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
yield.to_s
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end +
|
|
|
|
"</element.upcase>"
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
# - O EMPTY
|
|
|
|
def nOE_element_def(element)
|
|
|
|
<<-END.gsub(/element\.downcase/n, element.downcase).gsub(/element\.upcase/n, element.upcase)
|
|
|
|
"<element.upcase" + attributes.collect{|name, value|
|
2000-05-01 05:42:38 -04:00
|
|
|
next unless value
|
1999-11-04 03:39:57 -05:00
|
|
|
" " + CGI::escapeHTML(name) +
|
|
|
|
if true == value
|
|
|
|
""
|
|
|
|
else
|
|
|
|
'="' + CGI::escapeHTML(value) + '"'
|
|
|
|
end
|
|
|
|
}.to_s + ">"
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
# O O or - O
|
|
|
|
def nO_element_def(element)
|
|
|
|
<<-END.gsub(/element\.downcase/n, element.downcase).gsub(/element\.upcase/n, element.upcase)
|
1999-12-01 04:24:48 -05:00
|
|
|
"<element.upcase" + attributes.collect{|name, value|
|
2000-05-01 05:42:38 -04:00
|
|
|
next unless value
|
1999-11-04 03:39:57 -05:00
|
|
|
" " + CGI::escapeHTML(name) +
|
|
|
|
if true == value
|
|
|
|
""
|
|
|
|
else
|
|
|
|
'="' + CGI::escapeHTML(value) + '"'
|
|
|
|
end
|
|
|
|
}.to_s + ">" +
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
yield.to_s + "</element.upcase>"
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
end # TagMaker
|
|
|
|
|
|
|
|
|
|
|
|
module HtmlExtension
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== A ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
a("url")
|
|
|
|
# = a({ "HREF" => "url" })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def a(href = "")
|
|
|
|
attributes = if href.kind_of?(String)
|
|
|
|
{ "HREF" => href }
|
|
|
|
else
|
|
|
|
href
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
super(attributes){ yield }
|
|
|
|
else
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== BASE ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
base("url")
|
|
|
|
# = base({ "HREF" => "url" })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def base(href = "")
|
|
|
|
attributes = if href.kind_of?(String)
|
|
|
|
{ "HREF" => href }
|
|
|
|
else
|
|
|
|
href
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
super(attributes){ yield }
|
|
|
|
else
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== BLOCKQUOTE ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
blockquote("url"){ "string" }
|
|
|
|
# = blockquote({ "CITE" => "url" }){ "string" }
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def blockquote(cite = nil)
|
|
|
|
attributes = if cite.kind_of?(String)
|
|
|
|
{ "CITE" => cite }
|
|
|
|
else
|
|
|
|
cite or ""
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
super(attributes){ yield }
|
|
|
|
else
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== CAPTION ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
caption("align"){ "string" }
|
|
|
|
# = caption({ "ALIGN" => "align" }){ "string" }
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def caption(align = nil)
|
|
|
|
attributes = if align.kind_of?(String)
|
|
|
|
{ "ALIGN" => align }
|
|
|
|
else
|
|
|
|
align or ""
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
super(attributes){ yield }
|
|
|
|
else
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== CHECKBOX
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox("name")
|
|
|
|
# = checkbox({ "NAME" => "name" })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox("name", "value")
|
|
|
|
# = checkbox({ "NAME" => "name", "VALUE" => "value" })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox("name", "value", true)
|
|
|
|
# = checkbox({ "NAME" => "name", "VALUE" => "value", "CHECKED" => true })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def checkbox(name = "", value = nil, checked = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "checkbox", "NAME" => name,
|
|
|
|
"VALUE" => value, "CHECKED" => checked }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "checkbox"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== CHECKBOX_GROUP
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group("name", "foo", "bar", "baz")
|
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
|
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="bar">bar
|
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group("name", ["foo"], ["bar", true], "baz")
|
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="bar">bar
|
2000-06-19 17:18:41 -04:00
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
|
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="1">Foo
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="2">Bar
|
2000-06-19 17:18:41 -04:00
|
|
|
# <INPUT TYPE="checkbox" NAME="name" VALUE="Baz">Baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group({ "NAME" => "name",
|
|
|
|
"VALUES" => ["foo", "bar", "baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group({ "NAME" => "name",
|
|
|
|
"VALUES" => [["foo"], ["bar", true], "baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
checkbox_group({ "NAME" => "name",
|
|
|
|
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def checkbox_group(name = "", *values)
|
|
|
|
if name.kind_of?(Hash)
|
|
|
|
values = name["VALUES"]
|
|
|
|
name = name["NAME"]
|
|
|
|
end
|
|
|
|
values.collect{|value|
|
|
|
|
if value.kind_of?(String)
|
|
|
|
checkbox(name, value) + value
|
|
|
|
else
|
|
|
|
if value[value.size - 1] == true
|
|
|
|
checkbox(name, value[0], true) +
|
|
|
|
value[value.size - 2]
|
|
|
|
else
|
|
|
|
checkbox(name, value[0]) +
|
|
|
|
value[value.size - 1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== FILE_FIELD
|
2000-06-19 17:18:41 -04:00
|
|
|
file_field("name")
|
|
|
|
# <INPUT TYPE="file" NAME="name" SIZE="20">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
file_field("name", 40)
|
|
|
|
# <INPUT TYPE="file" NAME="name" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
file_field("name", 40, 100)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="file" NAME="name" SIZE="40" MAXLENGTH="100">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
file_field({ "NAME" => "name", "SIZE" => 40 })
|
|
|
|
# <INPUT TYPE="file" NAME="name" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def file_field(name = "", size = 20, maxlength = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "file", "NAME" => name,
|
|
|
|
"SIZE" => size.to_s }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "file"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
attributes["MAXLENGTH"] = maxlength.to_s if maxlength
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== FORM ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
form{ "string" }
|
|
|
|
# <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
form("get"){ "string" }
|
|
|
|
# <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
form("get", "url"){ "string" }
|
|
|
|
# <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2001-02-25 14:57:56 -05:00
|
|
|
form({"METHOD" => "post", "ENCTYPE" => "enctype"}){ "string" }
|
2000-06-19 17:18:41 -04:00
|
|
|
# <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
|
2001-02-25 14:57:56 -05:00
|
|
|
|
|
|
|
The hash keys are case sensitive. Ask the samples.
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def form(method = "post", action = nil, enctype = "application/x-www-form-urlencoded")
|
|
|
|
attributes = if method.kind_of?(String)
|
|
|
|
{ "METHOD" => method, "ACTION" => action,
|
|
|
|
"ENCTYPE" => enctype }
|
|
|
|
else
|
1999-12-07 04:25:55 -05:00
|
|
|
unless method.has_key?("METHOD")
|
2001-02-25 14:57:56 -05:00
|
|
|
method["METHOD"] = "post"
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
1999-12-07 04:25:55 -05:00
|
|
|
unless method.has_key?("ENCTYPE")
|
1999-11-04 03:39:57 -05:00
|
|
|
method["ENCTYPE"] = enctype
|
|
|
|
end
|
|
|
|
method
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
2000-06-17 13:52:49 -04:00
|
|
|
body = yield
|
1999-11-04 03:39:57 -05:00
|
|
|
else
|
|
|
|
body = ""
|
|
|
|
end
|
|
|
|
if @output_hidden
|
2000-06-17 13:52:49 -04:00
|
|
|
hidden = @output_hidden.collect{|k,v|
|
|
|
|
"<INPUT TYPE=HIDDEN NAME=\"#{k}\" VALUE=\"#{v}\">"
|
|
|
|
}.to_s
|
2001-09-06 18:18:11 -04:00
|
|
|
body += hidden
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
super(attributes){body}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== HIDDEN FIELD
|
2000-06-19 17:18:41 -04:00
|
|
|
hidden("name")
|
|
|
|
# <INPUT TYPE="hidden" NAME="name">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
hidden("name", "value")
|
|
|
|
# <INPUT TYPE="hidden" NAME="name" VALUE="value">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
hidden({ "NAME" => "name", "VALUE" => "reset", "ID" => "foo" })
|
|
|
|
# <INPUT TYPE="hidden" NAME="name" VALUE="value" ID="foo">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def hidden(name = "", value = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "hidden"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== HTML ELEMENT
|
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html{ "string" }
|
|
|
|
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>string</HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html({ "LANG" => "ja" }){ "string" }
|
|
|
|
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML LANG="ja">string</HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html({ "DOCTYPE" => false }){ "string" }
|
|
|
|
# <HTML>string</HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html({ "DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">' }){ "string" }
|
|
|
|
# <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>string</HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html({ "PRETTY" => " " }){ "<BODY></BODY>" }
|
|
|
|
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
|
|
# <HTML>
|
|
|
|
# <BODY>
|
|
|
|
# </BODY>
|
|
|
|
# </HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html({ "PRETTY" => "\t" }){ "<BODY></BODY>" }
|
|
|
|
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
|
|
# <HTML>
|
|
|
|
# <BODY>
|
|
|
|
# </BODY>
|
|
|
|
# </HTML>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html("PRETTY"){ "<BODY></BODY>" }
|
|
|
|
# = html({ "PRETTY" => " " }){ "<BODY></BODY>" }
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
html(if $VERBOSE then "PRETTY" end){ "HTML string" }
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
=end
|
|
|
|
def html(attributes = {})
|
|
|
|
if nil == attributes
|
|
|
|
attributes = {}
|
|
|
|
elsif "PRETTY" == attributes
|
|
|
|
attributes = { "PRETTY" => true }
|
|
|
|
end
|
|
|
|
pretty = attributes.delete("PRETTY")
|
2000-10-11 12:28:15 -04:00
|
|
|
pretty = " " if true == pretty
|
1999-11-04 03:39:57 -05:00
|
|
|
buf = ""
|
|
|
|
|
1999-12-07 04:25:55 -05:00
|
|
|
if attributes.has_key?("DOCTYPE")
|
1999-11-04 03:39:57 -05:00
|
|
|
if attributes["DOCTYPE"]
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += attributes.delete("DOCTYPE")
|
1999-11-04 03:39:57 -05:00
|
|
|
else
|
|
|
|
attributes.delete("DOCTYPE")
|
|
|
|
end
|
|
|
|
else
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += doctype
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += super(attributes){ yield }
|
1999-11-04 03:39:57 -05:00
|
|
|
else
|
2001-09-06 18:18:11 -04:00
|
|
|
buf += super(attributes)
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if pretty
|
|
|
|
CGI::pretty(buf, pretty)
|
|
|
|
else
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== IMAGE_BUTTON
|
2000-06-19 17:18:41 -04:00
|
|
|
image_button("url")
|
|
|
|
# <INPUT TYPE="image" SRC="url">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
image_button("url", "name", "string")
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
image_button({ "SRC" => "url", "ATL" => "strng" })
|
|
|
|
# <INPUT TYPE="image" SRC="url" ALT="string">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def image_button(src = "", name = nil, alt = nil)
|
|
|
|
attributes = if src.kind_of?(String)
|
|
|
|
{ "TYPE" => "image", "SRC" => src, "NAME" => name,
|
|
|
|
"ALT" => alt }
|
|
|
|
else
|
2000-05-01 05:42:38 -04:00
|
|
|
src["TYPE"] = "image"
|
|
|
|
src["SRC"] ||= ""
|
|
|
|
src
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== IMG ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
img("src", "alt", 100, 50)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
img({ "SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50 })
|
2002-04-11 06:03:01 -04:00
|
|
|
# <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def img(src = "", alt = "", width = nil, height = nil)
|
|
|
|
attributes = if src.kind_of?(String)
|
|
|
|
{ "SRC" => src, "ALT" => alt }
|
|
|
|
else
|
|
|
|
src
|
|
|
|
end
|
|
|
|
attributes["WIDTH"] = width.to_s if width
|
|
|
|
attributes["HEIGHT"] = height.to_s if height
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== MULTIPART FORM
|
2000-06-19 17:18:41 -04:00
|
|
|
multipart_form{ "string" }
|
|
|
|
# <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
multipart_form("url"){ "string" }
|
|
|
|
# <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def multipart_form(action = nil, enctype = "multipart/form-data")
|
|
|
|
attributes = if action == nil
|
|
|
|
{ "METHOD" => "post", "ENCTYPE" => enctype }
|
|
|
|
elsif action.kind_of?(String)
|
|
|
|
{ "METHOD" => "post", "ACTION" => action,
|
|
|
|
"ENCTYPE" => enctype }
|
|
|
|
else
|
1999-12-07 04:25:55 -05:00
|
|
|
unless action.has_key?("METHOD")
|
1999-11-04 03:39:57 -05:00
|
|
|
action["METHOD"] = "post"
|
|
|
|
end
|
1999-12-07 04:25:55 -05:00
|
|
|
unless action.has_key?("ENCTYPE")
|
1999-11-04 03:39:57 -05:00
|
|
|
action["ENCTYPE"] = enctype
|
|
|
|
end
|
|
|
|
action
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
form(attributes){ yield }
|
|
|
|
else
|
|
|
|
form(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== PASSWORD_FIELD
|
2000-06-19 17:18:41 -04:00
|
|
|
password_field("name")
|
|
|
|
# <INPUT TYPE="password" NAME="name" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
password_field("name", "value")
|
|
|
|
# <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
password_field("password", "value", 80, 200)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
password_field({ "NAME" => "name", "VALUE" => "value" })
|
|
|
|
# <INPUT TYPE="password" NAME="name" VALUE="value">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def password_field(name = "", value = nil, size = 40, maxlength = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "password", "NAME" => name,
|
|
|
|
"VALUE" => value, "SIZE" => size.to_s }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "password"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
attributes["MAXLENGTH"] = maxlength.to_s if maxlength
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== POPUP_MENU
|
2000-06-19 17:18:41 -04:00
|
|
|
popup_menu("name", "foo", "bar", "baz")
|
|
|
|
# <SELECT NAME="name">
|
|
|
|
# <OPTION VALUE="foo">foo</OPTION>
|
|
|
|
# <OPTION VALUE="bar">bar</OPTION>
|
|
|
|
# <OPTION VALUE="baz">baz</OPTION>
|
|
|
|
# </SELECT>
|
|
|
|
|
|
|
|
popup_menu("name", ["foo"], ["bar", true], "baz")
|
|
|
|
# <SELECT NAME="name">
|
|
|
|
# <OPTION VALUE="foo">foo</OPTION>
|
|
|
|
# <OPTION VALUE="bar" SELECTED>bar</OPTION>
|
|
|
|
# <OPTION VALUE="baz">baz</OPTION>
|
|
|
|
# </SELECT>
|
|
|
|
|
|
|
|
popup_menu("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
|
|
|
|
# <SELECT NAME="name">
|
|
|
|
# <OPTION VALUE="1">Foo</OPTION>
|
|
|
|
# <OPTION SELECTED VALUE="2">Bar</OPTION>
|
|
|
|
# <OPTION VALUE="Baz">Baz</OPTION>
|
|
|
|
# </SELECT>
|
|
|
|
|
|
|
|
popup_menu({"NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
|
|
|
|
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
|
|
|
|
# <SELECT NAME="name" MULTIPLE SIZE="2">
|
|
|
|
# <OPTION VALUE="1">Foo</OPTION>
|
|
|
|
# <OPTION SELECTED VALUE="2">Bar</OPTION>
|
|
|
|
# <OPTION VALUE="Baz">Baz</OPTION>
|
|
|
|
# </SELECT>
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def popup_menu(name = "", *values)
|
|
|
|
|
|
|
|
if name.kind_of?(Hash)
|
|
|
|
values = name["VALUES"]
|
|
|
|
size = name["SIZE"].to_s if name["SIZE"]
|
|
|
|
multiple = name["MULTIPLE"]
|
|
|
|
name = name["NAME"]
|
|
|
|
else
|
|
|
|
size = nil
|
|
|
|
multiple = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
select({ "NAME" => name, "SIZE" => size,
|
|
|
|
"MULTIPLE" => multiple }){
|
|
|
|
values.collect{|value|
|
|
|
|
if value.kind_of?(String)
|
|
|
|
option({ "VALUE" => value }){ value }
|
|
|
|
else
|
|
|
|
if value[value.size - 1] == true
|
|
|
|
option({ "VALUE" => value[0], "SELECTED" => true }){
|
|
|
|
value[value.size - 2]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
option({ "VALUE" => value[0] }){
|
|
|
|
value[value.size - 1]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.to_s
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== RADIO_BUTTON
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_button("name", "value")
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="value">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_button("name", "value", true)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_button({ "NAME" => "name", "VALUE" => "value", "ID" => "foo" })
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def radio_button(name = "", value = nil, checked = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "radio", "NAME" => name,
|
|
|
|
"VALUE" => value, "CHECKED" => checked }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "radio"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== RADIO_GROUP
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group("name", "foo", "bar", "baz")
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="bar">bar
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group("name", ["foo"], ["bar", true], "baz")
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="radio" CHECKED NAME="name" VALUE="bar">bar
|
2000-06-19 17:18:41 -04:00
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
|
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="1">Foo
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="radio" CHECKED NAME="name" VALUE="2">Bar
|
2000-06-19 17:18:41 -04:00
|
|
|
# <INPUT TYPE="radio" NAME="name" VALUE="Baz">Baz
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group({ "NAME" => "name",
|
|
|
|
"VALUES" => ["foo", "bar", "baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group({ "NAME" => "name",
|
|
|
|
"VALUES" => [["foo"], ["bar", true], "baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
radio_group({ "NAME" => "name",
|
|
|
|
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def radio_group(name = "", *values)
|
|
|
|
if name.kind_of?(Hash)
|
|
|
|
values = name["VALUES"]
|
|
|
|
name = name["NAME"]
|
|
|
|
end
|
|
|
|
values.collect{|value|
|
|
|
|
if value.kind_of?(String)
|
|
|
|
radio_button(name, value) + value
|
|
|
|
else
|
|
|
|
if value[value.size - 1] == true
|
|
|
|
radio_button(name, value[0], true) +
|
|
|
|
value[value.size - 2]
|
|
|
|
else
|
|
|
|
radio_button(name, value[0]) +
|
|
|
|
value[value.size - 1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== RESET BUTTON
|
2000-06-19 17:18:41 -04:00
|
|
|
reset
|
|
|
|
# <INPUT TYPE="reset">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
reset("reset")
|
|
|
|
# <INPUT TYPE="reset" VALUE="reset">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
reset({ "VALUE" => "reset", "ID" => "foo" })
|
|
|
|
# <INPUT TYPE="reset" VALUE="reset" ID="foo">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def reset(value = nil, name = nil)
|
|
|
|
attributes = if (not value) or value.kind_of?(String)
|
|
|
|
{ "TYPE" => "reset", "VALUE" => value, "NAME" => name }
|
|
|
|
else
|
|
|
|
value["TYPE"] = "reset"
|
|
|
|
value
|
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== SCROLLING_LIST
|
2000-06-19 17:18:41 -04:00
|
|
|
scrolling_list({"NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
|
|
|
|
"VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"] })
|
|
|
|
# <SELECT NAME="name" MULTIPLE SIZE="2">
|
|
|
|
# <OPTION VALUE="1">Foo</OPTION>
|
|
|
|
# <OPTION SELECTED VALUE="2">Bar</OPTION>
|
|
|
|
# <OPTION VALUE="Baz">Baz</OPTION>
|
|
|
|
# </SELECT>
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
alias scrolling_list popup_menu
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== SUBMIT BUTTON
|
2000-06-19 17:18:41 -04:00
|
|
|
submit
|
|
|
|
# <INPUT TYPE="submit">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
submit("ok")
|
|
|
|
# <INPUT TYPE="submit" VALUE="ok">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
submit("ok", "button1")
|
|
|
|
# <INPUT TYPE="submit" VALUE="ok" NAME="button1">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
submit({ "VALUE" => "ok", "NAME" => "button1", "ID" => "foo" })
|
|
|
|
# <INPUT TYPE="submit" VALUE="ok" NAME="button1" ID="foo">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def submit(value = nil, name = nil)
|
|
|
|
attributes = if (not value) or value.kind_of?(String)
|
|
|
|
{ "TYPE" => "submit", "VALUE" => value, "NAME" => name }
|
|
|
|
else
|
|
|
|
value["TYPE"] = "submit"
|
|
|
|
value
|
|
|
|
end
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== TEXT_FIELD
|
2000-06-19 17:18:41 -04:00
|
|
|
text_field("name")
|
|
|
|
# <INPUT TYPE="text" NAME="name" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
text_field("name", "value")
|
|
|
|
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
text_field("name", "value", 80)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
text_field("name", "value", 80, 200)
|
2002-04-11 06:03:01 -04:00
|
|
|
# <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
text_field({ "NAME" => "name", "VALUE" => "value" })
|
|
|
|
# <INPUT TYPE="text" NAME="name" VALUE="value">
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def text_field(name = "", value = nil, size = 40, maxlength = nil)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "TYPE" => "text", "NAME" => name, "VALUE" => value,
|
|
|
|
"SIZE" => size.to_s }
|
|
|
|
else
|
|
|
|
name["TYPE"] = "text"
|
|
|
|
name
|
|
|
|
end
|
|
|
|
attributes["MAXLENGTH"] = maxlength.to_s if maxlength
|
|
|
|
input(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
=== TEXTAREA ELEMENT
|
2000-06-19 17:18:41 -04:00
|
|
|
textarea("name")
|
|
|
|
# = textarea({ "NAME" => "name", "COLS" => 70, "ROWS" => 10 })
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-06-19 17:18:41 -04:00
|
|
|
textarea("name", 40, 5)
|
|
|
|
# = textarea({ "NAME" => "name", "COLS" => 40, "ROWS" => 5 })
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
|
|
|
def textarea(name = "", cols = 70, rows = 10)
|
|
|
|
attributes = if name.kind_of?(String)
|
|
|
|
{ "NAME" => name, "COLS" => cols.to_s,
|
|
|
|
"ROWS" => rows.to_s }
|
|
|
|
else
|
|
|
|
name
|
|
|
|
end
|
2000-06-23 03:05:59 -04:00
|
|
|
if block_given?
|
1999-11-04 03:39:57 -05:00
|
|
|
super(attributes){ yield }
|
|
|
|
else
|
|
|
|
super(attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # HtmlExtension
|
|
|
|
|
|
|
|
|
|
|
|
module Html3
|
|
|
|
|
|
|
|
def doctype
|
|
|
|
%|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">|
|
|
|
|
end
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
def element_init
|
|
|
|
extend TagMaker
|
|
|
|
methods = ""
|
|
|
|
# - -
|
|
|
|
for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG
|
|
|
|
DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV center MAP
|
|
|
|
APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT table TITLE
|
|
|
|
STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE
|
|
|
|
CAPTION ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nn_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# - O EMPTY
|
|
|
|
for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
|
|
|
|
ISINDEX META ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nOE_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# O O or - O
|
|
|
|
for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION tr
|
|
|
|
th td ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nO_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
eval(methods)
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
end # Html3
|
|
|
|
|
|
|
|
|
|
|
|
module Html4
|
|
|
|
|
|
|
|
def doctype
|
2000-09-11 18:02:37 -04:00
|
|
|
%|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
def element_init
|
|
|
|
extend TagMaker
|
|
|
|
methods = ""
|
|
|
|
# - -
|
|
|
|
for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD
|
|
|
|
VAR CITE ABBR ACRONYM SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT
|
|
|
|
H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP
|
|
|
|
FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
|
|
|
|
TEXTAREA FORM A BLOCKQUOTE CAPTION ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nn_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# - O EMPTY
|
|
|
|
for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nOE_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# O O or - O
|
|
|
|
for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
|
|
|
|
COLGROUP TR TH TD HEAD]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nO_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
eval(methods)
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
end # Html4
|
|
|
|
|
|
|
|
|
|
|
|
module Html4Tr
|
|
|
|
|
|
|
|
def doctype
|
2000-09-11 18:02:37 -04:00
|
|
|
%|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
def element_init
|
|
|
|
extend TagMaker
|
|
|
|
methods = ""
|
|
|
|
# - -
|
|
|
|
for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN
|
|
|
|
CODE SAMP KBD VAR CITE ABBR ACRONYM FONT SUB SUP SPAN BDO
|
|
|
|
ADDRESS DIV CENTER MAP OBJECT APPLET H1 H2 H3 H4 H5 H6 PRE Q
|
|
|
|
INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET
|
|
|
|
LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT
|
|
|
|
NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nn_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# - O EMPTY
|
|
|
|
for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
|
|
|
|
COL ISINDEX META ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nOE_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# O O or - O
|
|
|
|
for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
|
|
|
|
COLGROUP TR TH TD HEAD ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nO_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
eval(methods)
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
end # Html4Tr
|
|
|
|
|
|
|
|
|
|
|
|
module Html4Fr
|
|
|
|
|
|
|
|
def doctype
|
2000-09-11 18:02:37 -04:00
|
|
|
%|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
def element_init
|
|
|
|
methods = ""
|
|
|
|
# - -
|
|
|
|
for element in %w[ FRAMESET ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nn_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
# - O EMPTY
|
|
|
|
for element in %w[ FRAME ]
|
2001-09-06 18:18:11 -04:00
|
|
|
methods += <<-BEGIN + nOE_element_def(element) + <<-END
|
2000-05-09 00:53:16 -04:00
|
|
|
def #{element.downcase}(attributes = {})
|
|
|
|
BEGIN
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
eval(methods)
|
|
|
|
end
|
1999-11-04 03:39:57 -05:00
|
|
|
|
|
|
|
end # Html4Fr
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(type = "query")
|
1999-12-07 04:25:55 -05:00
|
|
|
extend QueryExtension
|
2002-05-29 00:48:32 -04:00
|
|
|
if "POST" != env_table['REQUEST_METHOD']
|
1999-12-07 04:25:55 -05:00
|
|
|
initialize_query() # set @params, @cookies
|
2002-05-29 00:48:32 -04:00
|
|
|
else
|
|
|
|
if defined?(CGI_PARAMS)
|
|
|
|
@params = CGI_PARAMS.nil? ? nil : CGI_PARAMS.dup
|
|
|
|
@cookies = CGI_COOKIES.nil? ? nil : CGI_COOKIES.dup
|
|
|
|
else
|
|
|
|
initialize_query() # set @params, @cookies
|
|
|
|
eval "CGI_PARAMS = @params.nil? ? nil : @params.dup"
|
|
|
|
eval "CGI_COOKIES = @cookies.nil? ? nil : @cookies.dup"
|
|
|
|
if defined?(MOD_RUBY) and (RUBY_VERSION < "1.4.3")
|
|
|
|
raise "Please, use ruby1.4.3 or later."
|
|
|
|
else
|
|
|
|
at_exit() do
|
|
|
|
if defined?(CGI_PARAMS)
|
|
|
|
remove_const(:CGI_PARAMS)
|
|
|
|
remove_const(:CGI_COOKIES)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
1999-12-07 04:25:55 -05:00
|
|
|
end
|
1999-12-01 04:24:48 -05:00
|
|
|
@output_cookies = nil
|
|
|
|
@output_hidden = nil
|
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
case type
|
|
|
|
when "html3"
|
|
|
|
extend Html3
|
2000-05-09 00:53:16 -04:00
|
|
|
element_init()
|
1999-11-04 03:39:57 -05:00
|
|
|
extend HtmlExtension
|
|
|
|
when "html4"
|
|
|
|
extend Html4
|
2000-05-09 00:53:16 -04:00
|
|
|
element_init()
|
1999-11-04 03:39:57 -05:00
|
|
|
extend HtmlExtension
|
|
|
|
when "html4Tr"
|
|
|
|
extend Html4Tr
|
2000-05-09 00:53:16 -04:00
|
|
|
element_init()
|
1999-11-04 03:39:57 -05:00
|
|
|
extend HtmlExtension
|
|
|
|
when "html4Fr"
|
2001-09-06 18:18:11 -04:00
|
|
|
extend Html4Tr
|
|
|
|
element_init()
|
1999-11-04 03:39:57 -05:00
|
|
|
extend Html4Fr
|
2000-05-09 00:53:16 -04:00
|
|
|
element_init()
|
1999-11-04 03:39:57 -05:00
|
|
|
extend HtmlExtension
|
|
|
|
end
|
2000-05-09 00:53:16 -04:00
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
== HISTORY
|
|
|
|
|
2001-02-25 14:57:56 -05:00
|
|
|
delete. see cvs log.
|
|
|
|
|
|
|
|
|
1999-11-04 03:39:57 -05:00
|
|
|
=end
|
2002-05-29 00:48:32 -04:00
|
|
|
|
|
|
|
# vi:set tw=0:
|