1
warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: cgi-lib is deprecated after Ruby 1.8.1; use cgi instead"
5
= simple CGI support library
13
query['field'] # <== value of 'field'
14
query.keys # <== array of fields
16
and query has Hash class methods
23
query.cookie['name'] # <== cookie value of 'name'
24
query.cookie.keys # <== all cookie names
26
and query.cookie has Hash class methods
29
== print HTTP header and HTML string to $>
34
CGI::tag("HEAD"){ CGI::tag("TITLE"){"TITLE"} } +
36
CGI::tag("FORM", {"ACTION"=>"test.rb", "METHOD"=>"POST"}){
37
CGI::tag("INPUT", {"TYPE"=>"submit", "VALUE"=>"submit"})
45
== make raw cookie string
48
cookie1 = CGI::cookie({'name' => 'name',
50
'path' => 'path', # optional
51
'domain' => 'domain', # optional
52
'expires' => Time.now, # optional
53
'secure' => true # optional
56
CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
59
== print HTTP header and string to $>
62
CGI::print{ "string" }
63
# == CGI::print("Content-Type: text/html"){ "string" }
64
CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
67
=== NPH (no-parse-header) mode
70
CGI::print("nph"){ "string" }
71
# == CGI::print("nph", "Content-Type: text/html"){ "string" }
72
CGI::print("nph", "Content-Type: text/html", cookie1, cookie2){ "string" }
75
== make HTML tag string
78
CGI::tag("element", {"attribute_name"=>"attribute_value"}){"content"}
81
== make HTTP header string
84
CGI::header # == CGI::header("Content-Type: text/html")
85
CGI::header("Content-Type: text/html", cookie1, cookie2)
88
=== NPH (no-parse-header) mode
90
CGI::header("nph") # == CGI::header("nph", "Content-Type: text/html")
91
CGI::header("nph", "Content-Type: text/html", cookie1, cookie2)
97
url_encoded_string = CGI::escape("string")
100
== unescape url encoded
103
string = CGI::unescape("url encoded string")
109
CGI::escapeHTML("string")
116
class CGI < SimpleDelegator
122
RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
123
RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
125
# make rfc1123 date string
126
def CGI::rfc1123_date(time)
127
t = time.clone.gmtime
128
return format("%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
129
RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
130
t.hour, t.min, t.sec)
135
str.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
138
# unescape url encoded
139
def CGI::unescape(str)
140
str.gsub(/\+/, ' ').gsub(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
144
def CGI::escapeHTML(str)
145
str.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
148
# offline mode. read name=value pairs on standard input.
149
def read_from_cmdline
150
require "shellwords.rb"
151
words = Shellwords.shellwords(
155
STDERR.print "(offline mode: enter name=value pairs on standard input)\n" if STDIN.tty?
156
readlines.join(' ').gsub(/\n/, '')
157
end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26'))
159
if words.find{|x| x =~ /=/} then words.join('&') else words.join('+') end
162
def initialize(input = $stdin)
167
case ENV['REQUEST_METHOD']
169
ENV['QUERY_STRING'] or ""
171
input.read(Integer(ENV['CONTENT_LENGTH'])) or ""
174
end.split(/[&;]/).each do |x|
175
key, val = x.split(/=/,2).collect{|x|CGI::unescape(x)}
176
if @inputs.include?(key)
177
@inputs[key] += "\0" + (val or "")
179
@inputs[key] = (val or "")
185
if ENV.has_key?('HTTP_COOKIE') or ENV.has_key?('COOKIE')
186
(ENV['HTTP_COOKIE'] or ENV['COOKIE']).split(/; /).each do |x|
187
key, val = x.split(/=/,2)
188
key = CGI::unescape(key)
189
val = val.split(/&/).collect{|x|CGI::unescape(x)}.join("\0")
190
if @cookie.include?(key)
191
@cookie[key] += "\0" + val
202
# make HTML tag string
203
def CGI::tag(element, attributes = {})
204
"<" + escapeHTML(element) + attributes.collect{|name, value|
205
" " + escapeHTML(name) + '="' + escapeHTML(value) + '"'
207
(iterator? ? yield.to_s + "</" + escapeHTML(element) + ">" : "")
210
# make raw cookie string
211
def CGI::cookie(options)
212
"Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
213
(options['domain'] ? '; domain=' + options['domain'] : '') +
214
(options['path'] ? '; path=' + options['path'] : '') +
215
(options['expires'] ? '; expires=' + rfc1123_date(options['expires']) : '') +
216
(options['secure'] ? '; secure' : '')
219
# make HTTP header string
220
def CGI::header(*options)
221
if defined?(MOD_RUBY)
222
options.each{|option|
223
option.sub(/(.*?): (.*)/){
224
Apache::request.headers_out[$1] = $2
227
Apache::request.send_http_header
230
if options.delete("nph") or (ENV['SERVER_SOFTWARE'] =~ /IIS/)
231
[(ENV['SERVER_PROTOCOL'] or "HTTP/1.0") + " 200 OK",
232
"Date: " + rfc1123_date(Time.now),
233
"Server: " + (ENV['SERVER_SOFTWARE'] or ""),
234
"Connection: close"] +
235
(options.empty? ? ["Content-Type: text/html"] : options)
237
options.empty? ? ["Content-Type: text/html"] : options
238
end.join(EOL) + EOL + EOL
242
# print HTTP header and string to $>
243
def CGI::print(*options)
244
$>.print CGI::header(*options) + yield.to_s
247
# print message to $>
248
def CGI::message(message, title = "", header = ["Content-Type: text/html"])
249
if message.kind_of?(Hash)
250
title = message['title']
251
header = message['header']
252
message = message['body']
256
CGI::tag("HEAD"){ CGI.tag("TITLE"){ title } } +
257
CGI::tag("BODY"){ message }
263
# print error message to $> and exit
265
CGI::message({'title'=>'ERROR', 'body'=>
267
"ERROR: " + CGI::tag("STRONG"){ escapeHTML($!.to_s) } + "\n" + escapeHTML($@.join("\n"))