PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ruby/1.8/cgi.rb

https://bitbucket.org/nicksieger/jruby
Ruby | 2313 lines | 1497 code | 79 blank | 737 comment | 70 complexity | 90b5535f0198505cb39901c4bc8d8640 MD5 | raw file
Possible License(s): GPL-3.0, JSON
  1. #
  2. # cgi.rb - cgi support library
  3. #
  4. # Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
  5. #
  6. # Copyright (C) 2000 Information-technology Promotion Agency, Japan
  7. #
  8. # Author: Wakou Aoyama <wakou@ruby-lang.org>
  9. #
  10. # Documentation: Wakou Aoyama (RDoc'd and embellished by William Webber)
  11. #
  12. # == Overview
  13. #
  14. # The Common Gateway Interface (CGI) is a simple protocol
  15. # for passing an HTTP request from a web server to a
  16. # standalone program, and returning the output to the web
  17. # browser. Basically, a CGI program is called with the
  18. # parameters of the request passed in either in the
  19. # environment (GET) or via $stdin (POST), and everything
  20. # it prints to $stdout is returned to the client.
  21. #
  22. # This file holds the +CGI+ class. This class provides
  23. # functionality for retrieving HTTP request parameters,
  24. # managing cookies, and generating HTML output. See the
  25. # class documentation for more details and examples of use.
  26. #
  27. # The file cgi/session.rb provides session management
  28. # functionality; see that file for more details.
  29. #
  30. # See http://www.w3.org/CGI/ for more information on the CGI
  31. # protocol.
  32. raise "Please, use ruby 1.5.4 or later." if RUBY_VERSION < "1.5.4"
  33. require 'English'
  34. # CGI class. See documentation for the file cgi.rb for an overview
  35. # of the CGI protocol.
  36. #
  37. # == Introduction
  38. #
  39. # CGI is a large class, providing several categories of methods, many of which
  40. # are mixed in from other modules. Some of the documentation is in this class,
  41. # some in the modules CGI::QueryExtension and CGI::HtmlExtension. See
  42. # CGI::Cookie for specific information on handling cookies, and cgi/session.rb
  43. # (CGI::Session) for information on sessions.
  44. #
  45. # For queries, CGI provides methods to get at environmental variables,
  46. # parameters, cookies, and multipart request data. For responses, CGI provides
  47. # methods for writing output and generating HTML.
  48. #
  49. # Read on for more details. Examples are provided at the bottom.
  50. #
  51. # == Queries
  52. #
  53. # The CGI class dynamically mixes in parameter and cookie-parsing
  54. # functionality, environmental variable access, and support for
  55. # parsing multipart requests (including uploaded files) from the
  56. # CGI::QueryExtension module.
  57. #
  58. # === Environmental Variables
  59. #
  60. # The standard CGI environmental variables are available as read-only
  61. # attributes of a CGI object. The following is a list of these variables:
  62. #
  63. #
  64. # AUTH_TYPE HTTP_HOST REMOTE_IDENT
  65. # CONTENT_LENGTH HTTP_NEGOTIATE REMOTE_USER
  66. # CONTENT_TYPE HTTP_PRAGMA REQUEST_METHOD
  67. # GATEWAY_INTERFACE HTTP_REFERER SCRIPT_NAME
  68. # HTTP_ACCEPT HTTP_USER_AGENT SERVER_NAME
  69. # HTTP_ACCEPT_CHARSET PATH_INFO SERVER_PORT
  70. # HTTP_ACCEPT_ENCODING PATH_TRANSLATED SERVER_PROTOCOL
  71. # HTTP_ACCEPT_LANGUAGE QUERY_STRING SERVER_SOFTWARE
  72. # HTTP_CACHE_CONTROL REMOTE_ADDR
  73. # HTTP_FROM REMOTE_HOST
  74. #
  75. #
  76. # For each of these variables, there is a corresponding attribute with the
  77. # same name, except all lower case and without a preceding HTTP_.
  78. # +content_length+ and +server_port+ are integers; the rest are strings.
  79. #
  80. # === Parameters
  81. #
  82. # The method #params() returns a hash of all parameters in the request as
  83. # name/value-list pairs, where the value-list is an Array of one or more
  84. # values. The CGI object itself also behaves as a hash of parameter names
  85. # to values, but only returns a single value (as a String) for each
  86. # parameter name.
  87. #
  88. # For instance, suppose the request contains the parameter
  89. # "favourite_colours" with the multiple values "blue" and "green". The
  90. # following behaviour would occur:
  91. #
  92. # cgi.params["favourite_colours"] # => ["blue", "green"]
  93. # cgi["favourite_colours"] # => "blue"
  94. #
  95. # If a parameter does not exist, the former method will return an empty
  96. # array, the latter an empty string. The simplest way to test for existence
  97. # of a parameter is by the #has_key? method.
  98. #
  99. # === Cookies
  100. #
  101. # HTTP Cookies are automatically parsed from the request. They are available
  102. # from the #cookies() accessor, which returns a hash from cookie name to
  103. # CGI::Cookie object.
  104. #
  105. # === Multipart requests
  106. #
  107. # If a request's method is POST and its content type is multipart/form-data,
  108. # then it may contain uploaded files. These are stored by the QueryExtension
  109. # module in the parameters of the request. The parameter name is the name
  110. # attribute of the file input field, as usual. However, the value is not
  111. # a string, but an IO object, either an IOString for small files, or a
  112. # Tempfile for larger ones. This object also has the additional singleton
  113. # methods:
  114. #
  115. # #local_path():: the path of the uploaded file on the local filesystem
  116. # #original_filename():: the name of the file on the client computer
  117. # #content_type():: the content type of the file
  118. #
  119. # == Responses
  120. #
  121. # The CGI class provides methods for sending header and content output to
  122. # the HTTP client, and mixes in methods for programmatic HTML generation
  123. # from CGI::HtmlExtension and CGI::TagMaker modules. The precise version of HTML
  124. # to use for HTML generation is specified at object creation time.
  125. #
  126. # === Writing output
  127. #
  128. # The simplest way to send output to the HTTP client is using the #out() method.
  129. # This takes the HTTP headers as a hash parameter, and the body content
  130. # via a block. The headers can be generated as a string using the #header()
  131. # method. The output stream can be written directly to using the #print()
  132. # method.
  133. #
  134. # === Generating HTML
  135. #
  136. # Each HTML element has a corresponding method for generating that
  137. # element as a String. The name of this method is the same as that
  138. # of the element, all lowercase. The attributes of the element are
  139. # passed in as a hash, and the body as a no-argument block that evaluates
  140. # to a String. The HTML generation module knows which elements are
  141. # always empty, and silently drops any passed-in body. It also knows
  142. # which elements require matching closing tags and which don't. However,
  143. # it does not know what attributes are legal for which elements.
  144. #
  145. # There are also some additional HTML generation methods mixed in from
  146. # the CGI::HtmlExtension module. These include individual methods for the
  147. # different types of form inputs, and methods for elements that commonly
  148. # take particular attributes where the attributes can be directly specified
  149. # as arguments, rather than via a hash.
  150. #
  151. # == Examples of use
  152. #
  153. # === Get form values
  154. #
  155. # require "cgi"
  156. # cgi = CGI.new
  157. # value = cgi['field_name'] # <== value string for 'field_name'
  158. # # if not 'field_name' included, then return "".
  159. # fields = cgi.keys # <== array of field names
  160. #
  161. # # returns true if form has 'field_name'
  162. # cgi.has_key?('field_name')
  163. # cgi.has_key?('field_name')
  164. # cgi.include?('field_name')
  165. #
  166. # CAUTION! cgi['field_name'] returned an Array with the old
  167. # cgi.rb(included in ruby 1.6)
  168. #
  169. # === Get form values as hash
  170. #
  171. # require "cgi"
  172. # cgi = CGI.new
  173. # params = cgi.params
  174. #
  175. # cgi.params is a hash.
  176. #
  177. # cgi.params['new_field_name'] = ["value"] # add new param
  178. # cgi.params['field_name'] = ["new_value"] # change value
  179. # cgi.params.delete('field_name') # delete param
  180. # cgi.params.clear # delete all params
  181. #
  182. #
  183. # === Save form values to file
  184. #
  185. # require "pstore"
  186. # db = PStore.new("query.db")
  187. # db.transaction do
  188. # db["params"] = cgi.params
  189. # end
  190. #
  191. #
  192. # === Restore form values from file
  193. #
  194. # require "pstore"
  195. # db = PStore.new("query.db")
  196. # db.transaction do
  197. # cgi.params = db["params"]
  198. # end
  199. #
  200. #
  201. # === Get multipart form values
  202. #
  203. # require "cgi"
  204. # cgi = CGI.new
  205. # value = cgi['field_name'] # <== value string for 'field_name'
  206. # value.read # <== body of value
  207. # value.local_path # <== path to local file of value
  208. # value.original_filename # <== original filename of value
  209. # value.content_type # <== content_type of value
  210. #
  211. # and value has StringIO or Tempfile class methods.
  212. #
  213. # === Get cookie values
  214. #
  215. # require "cgi"
  216. # cgi = CGI.new
  217. # values = cgi.cookies['name'] # <== array of 'name'
  218. # # if not 'name' included, then return [].
  219. # names = cgi.cookies.keys # <== array of cookie names
  220. #
  221. # and cgi.cookies is a hash.
  222. #
  223. # === Get cookie objects
  224. #
  225. # require "cgi"
  226. # cgi = CGI.new
  227. # for name, cookie in cgi.cookies
  228. # cookie.expires = Time.now + 30
  229. # end
  230. # cgi.out("cookie" => cgi.cookies) {"string"}
  231. #
  232. # cgi.cookies # { "name1" => cookie1, "name2" => cookie2, ... }
  233. #
  234. # require "cgi"
  235. # cgi = CGI.new
  236. # cgi.cookies['name'].expires = Time.now + 30
  237. # cgi.out("cookie" => cgi.cookies['name']) {"string"}
  238. #
  239. # === Print http header and html string to $DEFAULT_OUTPUT ($>)
  240. #
  241. # require "cgi"
  242. # cgi = CGI.new("html3") # add HTML generation methods
  243. # cgi.out() do
  244. # cgi.html() do
  245. # cgi.head{ cgi.title{"TITLE"} } +
  246. # cgi.body() do
  247. # cgi.form() do
  248. # cgi.textarea("get_text") +
  249. # cgi.br +
  250. # cgi.submit
  251. # end +
  252. # cgi.pre() do
  253. # CGI::escapeHTML(
  254. # "params: " + cgi.params.inspect + "\n" +
  255. # "cookies: " + cgi.cookies.inspect + "\n" +
  256. # ENV.collect() do |key, value|
  257. # key + " --> " + value + "\n"
  258. # end.join("")
  259. # )
  260. # end
  261. # end
  262. # end
  263. # end
  264. #
  265. # # add HTML generation methods
  266. # CGI.new("html3") # html3.2
  267. # CGI.new("html4") # html4.01 (Strict)
  268. # CGI.new("html4Tr") # html4.01 Transitional
  269. # CGI.new("html4Fr") # html4.01 Frameset
  270. #
  271. class CGI
  272. # :stopdoc:
  273. # String for carriage return
  274. CR = "\015"
  275. # String for linefeed
  276. LF = "\012"
  277. # Standard internet newline sequence
  278. EOL = CR + LF
  279. REVISION = '$Id$' #:nodoc:
  280. NEEDS_BINMODE = true if /WIN/ni.match(RUBY_PLATFORM)
  281. # Path separators in different environments.
  282. PATH_SEPARATOR = {'UNIX'=>'/', 'WINDOWS'=>'\\', 'MACINTOSH'=>':'}
  283. # HTTP status codes.
  284. HTTP_STATUS = {
  285. "OK" => "200 OK",
  286. "PARTIAL_CONTENT" => "206 Partial Content",
  287. "MULTIPLE_CHOICES" => "300 Multiple Choices",
  288. "MOVED" => "301 Moved Permanently",
  289. "REDIRECT" => "302 Found",
  290. "NOT_MODIFIED" => "304 Not Modified",
  291. "BAD_REQUEST" => "400 Bad Request",
  292. "AUTH_REQUIRED" => "401 Authorization Required",
  293. "FORBIDDEN" => "403 Forbidden",
  294. "NOT_FOUND" => "404 Not Found",
  295. "METHOD_NOT_ALLOWED" => "405 Method Not Allowed",
  296. "NOT_ACCEPTABLE" => "406 Not Acceptable",
  297. "LENGTH_REQUIRED" => "411 Length Required",
  298. "PRECONDITION_FAILED" => "412 Precondition Failed",
  299. "SERVER_ERROR" => "500 Internal Server Error",
  300. "NOT_IMPLEMENTED" => "501 Method Not Implemented",
  301. "BAD_GATEWAY" => "502 Bad Gateway",
  302. "VARIANT_ALSO_VARIES" => "506 Variant Also Negotiates"
  303. }
  304. # Abbreviated day-of-week names specified by RFC 822
  305. RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
  306. # Abbreviated month names specified by RFC 822
  307. RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
  308. # :startdoc:
  309. def env_table
  310. ENV
  311. end
  312. def stdinput
  313. $stdin
  314. end
  315. def stdoutput
  316. $DEFAULT_OUTPUT
  317. end
  318. private :env_table, :stdinput, :stdoutput
  319. # URL-encode a string.
  320. # url_encoded_string = CGI::escape("'Stop!' said Fred")
  321. # # => "%27Stop%21%27+said+Fred"
  322. def CGI::escape(string)
  323. string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
  324. '%' + $1.unpack('H2' * $1.size).join('%').upcase
  325. end.tr(' ', '+')
  326. end
  327. # URL-decode a string.
  328. # string = CGI::unescape("%27Stop%21%27+said+Fred")
  329. # # => "'Stop!' said Fred"
  330. def CGI::unescape(string)
  331. string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
  332. [$1.delete('%')].pack('H*')
  333. end
  334. end
  335. # Escape special characters in HTML, namely &\"<>
  336. # CGI::escapeHTML('Usage: foo "bar" <baz>')
  337. # # => "Usage: foo &quot;bar&quot; &lt;baz&gt;"
  338. def CGI::escapeHTML(string)
  339. string.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
  340. end
  341. # Unescape a string that has been HTML-escaped
  342. # CGI::unescapeHTML("Usage: foo &quot;bar&quot; &lt;baz&gt;")
  343. # # => "Usage: foo \"bar\" <baz>"
  344. def CGI::unescapeHTML(string)
  345. string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/n) do
  346. match = $1.dup
  347. case match
  348. when 'amp' then '&'
  349. when 'quot' then '"'
  350. when 'gt' then '>'
  351. when 'lt' then '<'
  352. when /\A#0*(\d+)\z/n then
  353. if Integer($1) < 256
  354. Integer($1).chr
  355. else
  356. if Integer($1) < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
  357. [Integer($1)].pack("U")
  358. else
  359. "&##{$1};"
  360. end
  361. end
  362. when /\A#x([0-9a-f]+)\z/ni then
  363. if $1.hex < 128
  364. $1.hex.chr
  365. else
  366. if $1.hex < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
  367. [$1.hex].pack("U")
  368. else
  369. "&#x#{$1};"
  370. end
  371. end
  372. else
  373. "&#{match};"
  374. end
  375. end
  376. end
  377. # Escape only the tags of certain HTML elements in +string+.
  378. #
  379. # Takes an element or elements or array of elements. Each element
  380. # is specified by the name of the element, without angle brackets.
  381. # This matches both the start and the end tag of that element.
  382. # The attribute list of the open tag will also be escaped (for
  383. # instance, the double-quotes surrounding attribute values).
  384. #
  385. # print CGI::escapeElement('<BR><A HREF="url"></A>', "A", "IMG")
  386. # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
  387. #
  388. # print CGI::escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"])
  389. # # "<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt"
  390. def CGI::escapeElement(string, *elements)
  391. elements = elements[0] if elements[0].kind_of?(Array)
  392. unless elements.empty?
  393. string.gsub(/<\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?>/ni) do
  394. CGI::escapeHTML($&)
  395. end
  396. else
  397. string
  398. end
  399. end
  400. # Undo escaping such as that done by CGI::escapeElement()
  401. #
  402. # print CGI::unescapeElement(
  403. # CGI::escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG")
  404. # # "&lt;BR&gt;<A HREF="url"></A>"
  405. #
  406. # print CGI::unescapeElement(
  407. # CGI::escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"])
  408. # # "&lt;BR&gt;<A HREF="url"></A>"
  409. def CGI::unescapeElement(string, *elements)
  410. elements = elements[0] if elements[0].kind_of?(Array)
  411. unless elements.empty?
  412. string.gsub(/&lt;\/?(?:#{elements.join("|")})(?!\w)(?:.|\n)*?&gt;/ni) do
  413. CGI::unescapeHTML($&)
  414. end
  415. else
  416. string
  417. end
  418. end
  419. # Format a +Time+ object as a String using the format specified by RFC 1123.
  420. #
  421. # CGI::rfc1123_date(Time.now)
  422. # # Sat, 01 Jan 2000 00:00:00 GMT
  423. def CGI::rfc1123_date(time)
  424. t = time.clone.gmtime
  425. return format("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
  426. RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
  427. t.hour, t.min, t.sec)
  428. end
  429. # Create an HTTP header block as a string.
  430. #
  431. # Includes the empty line that ends the header block.
  432. #
  433. # +options+ can be a string specifying the Content-Type (defaults
  434. # to text/html), or a hash of header key/value pairs. The following
  435. # header keys are recognized:
  436. #
  437. # type:: the Content-Type header. Defaults to "text/html"
  438. # charset:: the charset of the body, appended to the Content-Type header.
  439. # nph:: a boolean value. If true, prepend protocol string and status code, and
  440. # date; and sets default values for "server" and "connection" if not
  441. # explicitly set.
  442. # status:: the HTTP status code, returned as the Status header. See the
  443. # list of available status codes below.
  444. # server:: the server software, returned as the Server header.
  445. # connection:: the connection type, returned as the Connection header (for
  446. # instance, "close".
  447. # length:: the length of the content that will be sent, returned as the
  448. # Content-Length header.
  449. # language:: the language of the content, returned as the Content-Language
  450. # header.
  451. # expires:: the time on which the current content expires, as a +Time+
  452. # object, returned as the Expires header.
  453. # cookie:: a cookie or cookies, returned as one or more Set-Cookie headers.
  454. # The value can be the literal string of the cookie; a CGI::Cookie
  455. # object; an Array of literal cookie strings or Cookie objects; or a
  456. # hash all of whose values are literal cookie strings or Cookie objects.
  457. # These cookies are in addition to the cookies held in the
  458. # @output_cookies field.
  459. #
  460. # Other header lines can also be set; they are appended as key: value.
  461. #
  462. # header
  463. # # Content-Type: text/html
  464. #
  465. # header("text/plain")
  466. # # Content-Type: text/plain
  467. #
  468. # header("nph" => true,
  469. # "status" => "OK", # == "200 OK"
  470. # # "status" => "200 GOOD",
  471. # "server" => ENV['SERVER_SOFTWARE'],
  472. # "connection" => "close",
  473. # "type" => "text/html",
  474. # "charset" => "iso-2022-jp",
  475. # # Content-Type: text/html; charset=iso-2022-jp
  476. # "length" => 103,
  477. # "language" => "ja",
  478. # "expires" => Time.now + 30,
  479. # "cookie" => [cookie1, cookie2],
  480. # "my_header1" => "my_value"
  481. # "my_header2" => "my_value")
  482. #
  483. # The status codes are:
  484. #
  485. # "OK" --> "200 OK"
  486. # "PARTIAL_CONTENT" --> "206 Partial Content"
  487. # "MULTIPLE_CHOICES" --> "300 Multiple Choices"
  488. # "MOVED" --> "301 Moved Permanently"
  489. # "REDIRECT" --> "302 Found"
  490. # "NOT_MODIFIED" --> "304 Not Modified"
  491. # "BAD_REQUEST" --> "400 Bad Request"
  492. # "AUTH_REQUIRED" --> "401 Authorization Required"
  493. # "FORBIDDEN" --> "403 Forbidden"
  494. # "NOT_FOUND" --> "404 Not Found"
  495. # "METHOD_NOT_ALLOWED" --> "405 Method Not Allowed"
  496. # "NOT_ACCEPTABLE" --> "406 Not Acceptable"
  497. # "LENGTH_REQUIRED" --> "411 Length Required"
  498. # "PRECONDITION_FAILED" --> "412 Precondition Failed"
  499. # "SERVER_ERROR" --> "500 Internal Server Error"
  500. # "NOT_IMPLEMENTED" --> "501 Method Not Implemented"
  501. # "BAD_GATEWAY" --> "502 Bad Gateway"
  502. # "VARIANT_ALSO_VARIES" --> "506 Variant Also Negotiates"
  503. #
  504. # This method does not perform charset conversion.
  505. #
  506. def header(options = "text/html")
  507. buf = ""
  508. case options
  509. when String
  510. options = { "type" => options }
  511. when Hash
  512. options = options.dup
  513. end
  514. unless options.has_key?("type")
  515. options["type"] = "text/html"
  516. end
  517. if options.has_key?("charset")
  518. options["type"] += "; charset=" + options.delete("charset")
  519. end
  520. options.delete("nph") if defined?(MOD_RUBY)
  521. if options.delete("nph") or
  522. (/IIS\/(\d+)/n.match(env_table['SERVER_SOFTWARE']) and $1.to_i < 5)
  523. buf += (env_table["SERVER_PROTOCOL"] or "HTTP/1.0") + " " +
  524. (HTTP_STATUS[options["status"]] or options["status"] or "200 OK") +
  525. EOL +
  526. "Date: " + CGI::rfc1123_date(Time.now) + EOL
  527. unless options.has_key?("server")
  528. options["server"] = (env_table['SERVER_SOFTWARE'] or "")
  529. end
  530. unless options.has_key?("connection")
  531. options["connection"] = "close"
  532. end
  533. options.delete("status")
  534. end
  535. if options.has_key?("status")
  536. buf += "Status: " +
  537. (HTTP_STATUS[options["status"]] or options["status"]) + EOL
  538. options.delete("status")
  539. end
  540. if options.has_key?("server")
  541. buf += "Server: " + options.delete("server") + EOL
  542. end
  543. if options.has_key?("connection")
  544. buf += "Connection: " + options.delete("connection") + EOL
  545. end
  546. buf += "Content-Type: " + options.delete("type") + EOL
  547. if options.has_key?("length")
  548. buf += "Content-Length: " + options.delete("length").to_s + EOL
  549. end
  550. if options.has_key?("language")
  551. buf += "Content-Language: " + options.delete("language") + EOL
  552. end
  553. if options.has_key?("expires")
  554. buf += "Expires: " + CGI::rfc1123_date( options.delete("expires") ) + EOL
  555. end
  556. if options.has_key?("cookie")
  557. if options["cookie"].kind_of?(String) or
  558. options["cookie"].kind_of?(Cookie)
  559. buf += "Set-Cookie: " + options.delete("cookie").to_s + EOL
  560. elsif options["cookie"].kind_of?(Array)
  561. options.delete("cookie").each{|cookie|
  562. buf += "Set-Cookie: " + cookie.to_s + EOL
  563. }
  564. elsif options["cookie"].kind_of?(Hash)
  565. options.delete("cookie").each_value{|cookie|
  566. buf += "Set-Cookie: " + cookie.to_s + EOL
  567. }
  568. end
  569. end
  570. if @output_cookies
  571. for cookie in @output_cookies
  572. buf += "Set-Cookie: " + cookie.to_s + EOL
  573. end
  574. end
  575. options.each{|key, value|
  576. buf += key + ": " + value.to_s + EOL
  577. }
  578. if defined?(MOD_RUBY)
  579. table = Apache::request.headers_out
  580. buf.scan(/([^:]+): (.+)#{EOL}/n){ |name, value|
  581. warn sprintf("name:%s value:%s\n", name, value) if $DEBUG
  582. case name
  583. when 'Set-Cookie'
  584. table.add(name, value)
  585. when /^status$/ni
  586. Apache::request.status_line = value
  587. Apache::request.status = value.to_i
  588. when /^content-type$/ni
  589. Apache::request.content_type = value
  590. when /^content-encoding$/ni
  591. Apache::request.content_encoding = value
  592. when /^location$/ni
  593. if Apache::request.status == 200
  594. Apache::request.status = 302
  595. end
  596. Apache::request.headers_out[name] = value
  597. else
  598. Apache::request.headers_out[name] = value
  599. end
  600. }
  601. Apache::request.send_http_header
  602. ''
  603. else
  604. buf + EOL
  605. end
  606. end # header()
  607. # Print an HTTP header and body to $DEFAULT_OUTPUT ($>)
  608. #
  609. # The header is provided by +options+, as for #header().
  610. # The body of the document is that returned by the passed-
  611. # in block. This block takes no arguments. It is required.
  612. #
  613. # cgi = CGI.new
  614. # cgi.out{ "string" }
  615. # # Content-Type: text/html
  616. # # Content-Length: 6
  617. # #
  618. # # string
  619. #
  620. # cgi.out("text/plain") { "string" }
  621. # # Content-Type: text/plain
  622. # # Content-Length: 6
  623. # #
  624. # # string
  625. #
  626. # cgi.out("nph" => true,
  627. # "status" => "OK", # == "200 OK"
  628. # "server" => ENV['SERVER_SOFTWARE'],
  629. # "connection" => "close",
  630. # "type" => "text/html",
  631. # "charset" => "iso-2022-jp",
  632. # # Content-Type: text/html; charset=iso-2022-jp
  633. # "language" => "ja",
  634. # "expires" => Time.now + (3600 * 24 * 30),
  635. # "cookie" => [cookie1, cookie2],
  636. # "my_header1" => "my_value",
  637. # "my_header2" => "my_value") { "string" }
  638. #
  639. # Content-Length is automatically calculated from the size of
  640. # the String returned by the content block.
  641. #
  642. # If ENV['REQUEST_METHOD'] == "HEAD", then only the header
  643. # is outputted (the content block is still required, but it
  644. # is ignored).
  645. #
  646. # If the charset is "iso-2022-jp" or "euc-jp" or "shift_jis" then
  647. # the content is converted to this charset, and the language is set
  648. # to "ja".
  649. def out(options = "text/html") # :yield:
  650. options = { "type" => options } if options.kind_of?(String)
  651. content = yield
  652. if options.has_key?("charset")
  653. require "nkf"
  654. case options["charset"]
  655. when /iso-2022-jp/ni
  656. content = NKF::nkf('-m0 -x -j', content)
  657. options["language"] = "ja" unless options.has_key?("language")
  658. when /euc-jp/ni
  659. content = NKF::nkf('-m0 -x -e', content)
  660. options["language"] = "ja" unless options.has_key?("language")
  661. when /shift_jis/ni
  662. content = NKF::nkf('-m0 -x -s', content)
  663. options["language"] = "ja" unless options.has_key?("language")
  664. end
  665. end
  666. options["length"] = content.length.to_s
  667. output = stdoutput
  668. output.binmode if defined? output.binmode
  669. output.print header(options)
  670. output.print content unless "HEAD" == env_table['REQUEST_METHOD']
  671. end
  672. # Print an argument or list of arguments to the default output stream
  673. #
  674. # cgi = CGI.new
  675. # cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
  676. def print(*options)
  677. stdoutput.print(*options)
  678. end
  679. require "delegate"
  680. # Class representing an HTTP cookie.
  681. #
  682. # In addition to its specific fields and methods, a Cookie instance
  683. # is a delegator to the array of its values.
  684. #
  685. # See RFC 2965.
  686. #
  687. # == Examples of use
  688. # cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
  689. # cookie1 = CGI::Cookie::new("name" => "name", "value" => "value")
  690. # cookie1 = CGI::Cookie::new('name' => 'name',
  691. # 'value' => ['value1', 'value2', ...],
  692. # 'path' => 'path', # optional
  693. # 'domain' => 'domain', # optional
  694. # 'expires' => Time.now, # optional
  695. # 'secure' => true # optional
  696. # )
  697. #
  698. # cgi.out("cookie" => [cookie1, cookie2]) { "string" }
  699. #
  700. # name = cookie1.name
  701. # values = cookie1.value
  702. # path = cookie1.path
  703. # domain = cookie1.domain
  704. # expires = cookie1.expires
  705. # secure = cookie1.secure
  706. #
  707. # cookie1.name = 'name'
  708. # cookie1.value = ['value1', 'value2', ...]
  709. # cookie1.path = 'path'
  710. # cookie1.domain = 'domain'
  711. # cookie1.expires = Time.now + 30
  712. # cookie1.secure = true
  713. class Cookie < DelegateClass(Array)
  714. # Create a new CGI::Cookie object.
  715. #
  716. # The contents of the cookie can be specified as a +name+ and one
  717. # or more +value+ arguments. Alternatively, the contents can
  718. # be specified as a single hash argument. The possible keywords of
  719. # this hash are as follows:
  720. #
  721. # name:: the name of the cookie. Required.
  722. # value:: the cookie's value or list of values.
  723. # path:: the path for which this cookie applies. Defaults to the
  724. # base directory of the CGI script.
  725. # domain:: the domain for which this cookie applies.
  726. # expires:: the time at which this cookie expires, as a +Time+ object.
  727. # secure:: whether this cookie is a secure cookie or not (default to
  728. # false). Secure cookies are only transmitted to HTTPS
  729. # servers.
  730. #
  731. # These keywords correspond to attributes of the cookie object.
  732. def initialize(name = "", *value)
  733. if name.kind_of?(String)
  734. @name = name
  735. @value = value
  736. %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
  737. @path = ($1 or "")
  738. @secure = false
  739. return super(@value)
  740. end
  741. options = name
  742. unless options.has_key?("name")
  743. raise ArgumentError, "`name' required"
  744. end
  745. @name = options["name"]
  746. @value = Array(options["value"])
  747. # simple support for IE
  748. if options["path"]
  749. @path = options["path"]
  750. else
  751. %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
  752. @path = ($1 or "")
  753. end
  754. @domain = options["domain"]
  755. @expires = options["expires"]
  756. @secure = options["secure"] == true ? true : false
  757. super(@value)
  758. end
  759. attr_accessor("name", "path", "domain", "expires")
  760. attr_reader("secure", "value")
  761. # Set whether the Cookie is a secure cookie or not.
  762. #
  763. # +val+ must be a boolean.
  764. def secure=(val)
  765. @secure = val if val == true or val == false
  766. @secure
  767. end
  768. def value=(val)
  769. @value.replace(Array(val))
  770. end
  771. # Convert the Cookie to its string representation.
  772. def to_s
  773. buf = ""
  774. buf += @name + '='
  775. buf += @value.map { |v| CGI::escape(v) }.join("&")
  776. if @domain
  777. buf += '; domain=' + @domain
  778. end
  779. if @path
  780. buf += '; path=' + @path
  781. end
  782. if @expires
  783. buf += '; expires=' + CGI::rfc1123_date(@expires)
  784. end
  785. if @secure == true
  786. buf += '; secure'
  787. end
  788. buf
  789. end
  790. end # class Cookie
  791. # Parse a raw cookie string into a hash of cookie-name=>Cookie
  792. # pairs.
  793. #
  794. # cookies = CGI::Cookie::parse("raw_cookie_string")
  795. # # { "name1" => cookie1, "name2" => cookie2, ... }
  796. #
  797. def Cookie::parse(raw_cookie)
  798. cookies = Hash.new([])
  799. return cookies unless raw_cookie
  800. raw_cookie.split(/[;,]\s?/).each do |pairs|
  801. name, values = pairs.split('=',2)
  802. next unless name and values
  803. name = CGI::unescape(name)
  804. values ||= ""
  805. values = values.split('&').collect{|v| CGI::unescape(v) }
  806. if cookies.has_key?(name)
  807. values = cookies[name].value + values
  808. end
  809. cookies[name] = Cookie::new(name, *values)
  810. end
  811. cookies
  812. end
  813. # Parse an HTTP query string into a hash of key=>value pairs.
  814. #
  815. # params = CGI::parse("query_string")
  816. # # {"name1" => ["value1", "value2", ...],
  817. # # "name2" => ["value1", "value2", ...], ... }
  818. #
  819. def CGI::parse(query)
  820. params = Hash.new([].freeze)
  821. query.split(/[&;]/n).each do |pairs|
  822. key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) }
  823. if params.has_key?(key)
  824. params[key].push(value)
  825. else
  826. params[key] = [value]
  827. end
  828. end
  829. params
  830. end
  831. # Mixin module. It provides the follow functionality groups:
  832. #
  833. # 1. Access to CGI environment variables as methods. See
  834. # documentation to the CGI class for a list of these variables.
  835. #
  836. # 2. Access to cookies, including the cookies attribute.
  837. #
  838. # 3. Access to parameters, including the params attribute, and overloading
  839. # [] to perform parameter value lookup by key.
  840. #
  841. # 4. The initialize_query method, for initialising the above
  842. # mechanisms, handling multipart forms, and allowing the
  843. # class to be used in "offline" mode.
  844. #
  845. module QueryExtension
  846. %w[ CONTENT_LENGTH SERVER_PORT ].each do |env|
  847. define_method(env.sub(/^HTTP_/n, '').downcase) do
  848. (val = env_table[env]) && Integer(val)
  849. end
  850. end
  851. %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
  852. PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
  853. REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
  854. SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
  855. HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
  856. HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
  857. HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT ].each do |env|
  858. define_method(env.sub(/^HTTP_/n, '').downcase) do
  859. env_table[env]
  860. end
  861. end
  862. # Get the raw cookies as a string.
  863. def raw_cookie
  864. env_table["HTTP_COOKIE"]
  865. end
  866. # Get the raw RFC2965 cookies as a string.
  867. def raw_cookie2
  868. env_table["HTTP_COOKIE2"]
  869. end
  870. # Get the cookies as a hash of cookie-name=>Cookie pairs.
  871. attr_accessor("cookies")
  872. # Get the parameters as a hash of name=>values pairs, where
  873. # values is an Array.
  874. attr("params")
  875. # Set all the parameters.
  876. def params=(hash)
  877. @params.clear
  878. @params.update(hash)
  879. end
  880. def read_multipart(boundary, content_length)
  881. params = Hash.new([])
  882. boundary = "--" + boundary
  883. quoted_boundary = Regexp.quote(boundary, "n")
  884. buf = ""
  885. bufsize = 10 * 1024
  886. boundary_end=""
  887. # start multipart/form-data
  888. stdinput.binmode if defined? stdinput.binmode
  889. boundary_size = boundary.size + EOL.size
  890. content_length -= boundary_size
  891. status = stdinput.read(boundary_size)
  892. if nil == status
  893. raise EOFError, "no content body"
  894. elsif boundary + EOL != status
  895. raise EOFError, "bad content body"
  896. end
  897. loop do
  898. head = nil
  899. if 10240 < content_length
  900. require "tempfile"
  901. body = Tempfile.new("CGI")
  902. else
  903. begin
  904. require "stringio"
  905. body = StringIO.new
  906. rescue LoadError
  907. require "tempfile"
  908. body = Tempfile.new("CGI")
  909. end
  910. end
  911. body.binmode if defined? body.binmode
  912. until head and /#{quoted_boundary}(?:#{EOL}|--)/n.match(buf)
  913. if (not head) and /#{EOL}#{EOL}/n.match(buf)
  914. buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
  915. head = $1.dup
  916. ""
  917. end
  918. next
  919. end
  920. if head and ( (EOL + boundary + EOL).size < buf.size )
  921. body.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
  922. buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
  923. end
  924. c = if bufsize < content_length
  925. stdinput.read(bufsize)
  926. else
  927. stdinput.read(content_length)
  928. end
  929. if c.nil? || c.empty?
  930. raise EOFError, "bad content body"
  931. end
  932. buf.concat(c)
  933. content_length -= c.size
  934. end
  935. buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do
  936. body.print $1
  937. if "--" == $2
  938. content_length = -1
  939. end
  940. boundary_end = $2.dup
  941. ""
  942. end
  943. body.rewind
  944. /Content-Disposition:.* filename=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni.match(head)
  945. filename = ($1 or $2 or "")
  946. if /Mac/ni.match(env_table['HTTP_USER_AGENT']) and
  947. /Mozilla/ni.match(env_table['HTTP_USER_AGENT']) and
  948. (not /MSIE/ni.match(env_table['HTTP_USER_AGENT']))
  949. filename = CGI::unescape(filename)
  950. end
  951. /Content-Type: ([^\s]*)/ni.match(head)
  952. content_type = ($1 or "")
  953. (class << body; self; end).class_eval do
  954. alias local_path path
  955. define_method(:original_filename) {filename.dup.taint}
  956. define_method(:content_type) {content_type.dup.taint}
  957. end
  958. /Content-Disposition:.* name="?([^\";\s]*)"?/ni.match(head)
  959. name = $1.dup
  960. if params.has_key?(name)
  961. params[name].push(body)
  962. else
  963. params[name] = [body]
  964. end
  965. break if buf.size == 0
  966. break if content_length == -1
  967. end
  968. raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
  969. params
  970. end # read_multipart
  971. private :read_multipart
  972. # offline mode. read name=value pairs on standard input.
  973. def read_from_cmdline
  974. require "shellwords"
  975. string = unless ARGV.empty?
  976. ARGV.join(' ')
  977. else
  978. if STDIN.tty?
  979. STDERR.print(
  980. %|(offline mode: enter name=value pairs on standard input)\n|
  981. )
  982. end
  983. array = readlines rescue nil
  984. if not array.nil?
  985. array.join(' ').gsub(/\n/n, '')
  986. else
  987. ""
  988. end
  989. end.gsub(/\\=/n, '%3D').gsub(/\\&/n, '%26')
  990. words = Shellwords.shellwords(string)
  991. if words.find{|x| /=/n.match(x) }
  992. words.join('&')
  993. else
  994. words.join('+')
  995. end
  996. end
  997. private :read_from_cmdline
  998. # Initialize the data from the query.
  999. #
  1000. # Handles multipart forms (in particular, forms that involve file uploads).
  1001. # Reads query parameters in the @params field, and cookies into @cookies.
  1002. def initialize_query()
  1003. if ("POST" == env_table['REQUEST_METHOD']) and
  1004. %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(env_table['CONTENT_TYPE'])
  1005. boundary = $1.dup
  1006. @multipart = true
  1007. @params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
  1008. else
  1009. @multipart = false
  1010. @params = CGI::parse(
  1011. case env_table['REQUEST_METHOD']
  1012. when "GET", "HEAD"
  1013. if defined?(MOD_RUBY)
  1014. Apache::request.args or ""
  1015. else
  1016. env_table['QUERY_STRING'] or ""
  1017. end
  1018. when "POST"
  1019. stdinput.binmode if defined? stdinput.binmode
  1020. stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or ''
  1021. else
  1022. read_from_cmdline
  1023. end
  1024. )
  1025. end
  1026. @cookies = CGI::Cookie::parse((env_table['HTTP_COOKIE'] or env_table['COOKIE']))
  1027. end
  1028. private :initialize_query
  1029. def multipart?
  1030. @multipart
  1031. end
  1032. module Value # :nodoc:
  1033. def set_params(params)
  1034. @params = params
  1035. end
  1036. def [](idx, *args)
  1037. if args.size == 0
  1038. warn "#{caller(1)[0]}:CAUTION! cgi['key'] == cgi.params['key'][0]; if want Array, use cgi.params['key']"
  1039. @params[idx]
  1040. else
  1041. super[idx,*args]
  1042. end
  1043. end
  1044. def first
  1045. warn "#{caller(1)[0]}:CAUTION! cgi['key'] == cgi.params['key'][0]; if want Array, use cgi.params['key']"
  1046. self
  1047. end
  1048. alias last first
  1049. def to_a
  1050. @params || [self]
  1051. end
  1052. alias to_ary to_a # to be rhs of multiple assignment
  1053. end
  1054. # Get the value for the parameter with a given key.
  1055. #
  1056. # If the parameter has multiple values, only the first will be
  1057. # retrieved; use #params() to get the array of values.
  1058. def [](key)
  1059. params = @params[key]
  1060. return '' unless params
  1061. value = params[0]
  1062. if @multipart
  1063. if value
  1064. return value
  1065. elsif defined? StringIO
  1066. StringIO.new("")
  1067. else
  1068. Tempfile.new("CGI")
  1069. end
  1070. else
  1071. str = if value then value.dup else "" end
  1072. str.extend(Value)
  1073. str.set_params(params)
  1074. str
  1075. end
  1076. end
  1077. # Return all parameter keys as an array.
  1078. def keys(*args)
  1079. @params.keys(*args)
  1080. end
  1081. # Returns true if a given parameter key exists in the query.
  1082. def has_key?(*args)
  1083. @params.has_key?(*args)
  1084. end
  1085. alias key? has_key?
  1086. alias include? has_key?
  1087. end # QueryExtension
  1088. # Prettify (indent) an HTML string.
  1089. #
  1090. # +string+ is the HTML string to indent. +shift+ is the indentation
  1091. # unit to use; it defaults to two spaces.
  1092. #
  1093. # print CGI::pretty("<HTML><BODY></BODY></HTML>")
  1094. # # <HTML>
  1095. # # <BODY>
  1096. # # </BODY>
  1097. # # </HTML>
  1098. #
  1099. # print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
  1100. # # <HTML>
  1101. # # <BODY>
  1102. # # </BODY>
  1103. # # </HTML>
  1104. #
  1105. def CGI::pretty(string, shift = " ")
  1106. lines = string.gsub(/(?!\A)<(?:.|\n)*?>/n, "\n\\0").gsub(/<(?:.|\n)*?>(?!\n)/n, "\\0\n")
  1107. end_pos = 0
  1108. while end_pos = lines.index(/^<\/(\w+)/n, end_pos)
  1109. element = $1.dup
  1110. start_pos = lines.rindex(/^\s*<#{element}/ni, end_pos)
  1111. lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/n, "\n" + shift) + "__"
  1112. end
  1113. lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/n, '\1')
  1114. end
  1115. # Base module for HTML-generation mixins.
  1116. #
  1117. # Provides methods for code generation for tags following
  1118. # the various DTD element types.
  1119. module TagMaker # :nodoc:
  1120. # Generate code for an element with required start and end tags.
  1121. #
  1122. # - -
  1123. def nn_element_def(element)
  1124. nOE_element_def(element, <<-END)
  1125. if block_given?
  1126. yield.to_s
  1127. else
  1128. ""
  1129. end +
  1130. "</#{element.upcase}>"
  1131. END
  1132. end
  1133. # Generate code for an empty element.
  1134. #
  1135. # - O EMPTY
  1136. def nOE_element_def(element, append = nil)
  1137. s = <<-END
  1138. "<#{element.upcase}" + attributes.collect{|name, value|
  1139. next unless value
  1140. " " + CGI::escapeHTML(name) +
  1141. if true == value
  1142. ""
  1143. else
  1144. '="' + CGI::escapeHTML(value) + '"'
  1145. end
  1146. }.to_s + ">"
  1147. END
  1148. s.sub!(/\Z/, " +") << append if append
  1149. s
  1150. end
  1151. # Generate code for an element for which the end (and possibly the
  1152. # start) tag is optional.
  1153. #
  1154. # O O or - O
  1155. def nO_element_def(element)
  1156. nOE_element_def(element, <<-END)
  1157. if block_given?
  1158. yield.to_s + "</#{element.upcase}>"
  1159. else
  1160. ""
  1161. end
  1162. END
  1163. end
  1164. end # TagMaker
  1165. #
  1166. # Mixin module providing HTML generation methods.
  1167. #
  1168. # For example,
  1169. # cgi.a("http://www.example.com") { "Example" }
  1170. # # => "<A HREF=\"http://www.example.com\">Example</A>"
  1171. #
  1172. # Modules Http3, Http4, etc., contain more basic HTML-generation methods
  1173. # (:title, :center, etc.).
  1174. #
  1175. # See class CGI for a detailed example.
  1176. #
  1177. module HtmlExtension
  1178. # Generate an Anchor element as a string.
  1179. #
  1180. # +href+ can either be a string, giving the URL
  1181. # for the HREF attribute, or it can be a hash of
  1182. # the element's attributes.
  1183. #
  1184. # The body of the element is the string returned by the no-argument
  1185. # block passed in.
  1186. #
  1187. # a("http://www.example.com") { "Example" }
  1188. # # => "<A HREF=\"http://www.example.com\">Example</A>"
  1189. #
  1190. # a("HREF" => "http://www.example.com", "TARGET" => "_top") { "Example" }
  1191. # # => "<A HREF=\"http://www.example.com\" TARGET=\"_top\">Example</A>"
  1192. #
  1193. def a(href = "") # :yield:
  1194. attributes = if href.kind_of?(String)
  1195. { "HREF" => href }
  1196. else
  1197. href
  1198. end
  1199. if block_given?
  1200. super(attributes){ yield }
  1201. else
  1202. super(attributes)
  1203. end
  1204. end
  1205. # Generate a Document Base URI element as a String.
  1206. #
  1207. # +href+ can either by a string, giving the base URL for the HREF
  1208. # attribute, or it can be a has of the element's attributes.
  1209. #
  1210. # The passed-in no-argument block is ignored.
  1211. #
  1212. # base("http://www.example.com/cgi")
  1213. # # => "<BASE HREF=\"http://www.example.com/cgi\">"
  1214. def base(href = "") # :yield:
  1215. attributes = if href.kind_of?(String)
  1216. { "HREF" => href }
  1217. else
  1218. href
  1219. end
  1220. if block_given?
  1221. super(attributes){ yield }
  1222. else
  1223. super(attributes)
  1224. end
  1225. end
  1226. # Generate a BlockQuote element as a string.
  1227. #
  1228. # +cite+ can either be a string, give the URI for the source of
  1229. # the quoted text, or a hash, giving all attributes of the element,
  1230. # or it can be omitted, in which case the element has no attributes.
  1231. #
  1232. # The body is provided by the passed-in no-argument block
  1233. #
  1234. # blockquote("http://www.example.com/quotes/foo.html") { "Foo!" }
  1235. # #=> "<BLOCKQUOTE CITE=\"http://www.example.com/quotes/foo.html\">Foo!</BLOCKQUOTE>
  1236. def blockquote(cite = nil) # :yield:
  1237. attributes = if cite.kind_of?(String)
  1238. { "CITE" => cite }
  1239. else
  1240. cite or ""
  1241. end
  1242. if block_given?
  1243. super(attributes){ yield }
  1244. else
  1245. super(attributes)
  1246. end
  1247. end
  1248. # Generate a Table Caption element as a string.
  1249. #
  1250. # +align+ can be a string, giving the alignment of the caption
  1251. # (one of top, bottom, left, or right). It can be a hash of
  1252. # all the attributes of the element. Or it can be omitted.
  1253. #
  1254. # The body of the element is provided by the passed-in no-argument block.
  1255. #
  1256. # caption("left") { "Capital Cities" }
  1257. # # => <CAPTION ALIGN=\"left\">Capital Cities</CAPTION>
  1258. def caption(align = nil) # :yield:
  1259. attributes = if align.kind_of?(String)
  1260. { "ALIGN" => align }
  1261. else
  1262. align or ""
  1263. end
  1264. if block_given?
  1265. super(attributes){ yield }
  1266. else
  1267. super(attributes)
  1268. end
  1269. end
  1270. # Generate a Checkbox Input element as a string.
  1271. #
  1272. # The attributes of the element can be specified as three arguments,
  1273. # +name+, +value+, and +checked+. +checked+ is a boolean value;
  1274. # if true, the CHECKED attribute will be included in the element.
  1275. #
  1276. # Alternatively, the attributes can be specified as a hash.
  1277. #
  1278. # checkbox("name")
  1279. # # = checkbox("NAME" => "name")
  1280. #
  1281. # checkbox("name", "value")
  1282. # # = checkbox("NAME" => "name", "VALUE" => "value")
  1283. #
  1284. # checkbox("name", "value", true)
  1285. # # = checkbox("NAME" => "name", "VALUE" => "value", "CHECKED" => true)
  1286. def checkbox(name = "", value = nil, checked = nil)
  1287. attributes = if name.kind_of?(String)
  1288. { "TYPE" => "checkbox", "NAME" => name,
  1289. "VALUE" => value, "CHECKED" => checked }
  1290. else
  1291. name["TYPE"] = "checkbox"
  1292. name
  1293. end
  1294. input(attributes)
  1295. end
  1296. # Generate a sequence of checkbox elements, as a String.
  1297. #
  1298. # The checkboxes will all have the same +name+ attribute.
  1299. # Each checkbox is followed by a label.
  1300. # There will be one checkbox for each value. Each value
  1301. # can be specified as a String, which will be used both
  1302. # as the value of the VALUE attribute and as the label
  1303. # for that checkbox. A single-element array has the
  1304. # same effect.
  1305. #
  1306. # Each value can also be specified as a three-element array.
  1307. # The first element is the VALUE attribute; the second is the
  1308. # label; and the third is a boolean specifying whether this
  1309. # checkbox is CHECKED.
  1310. #
  1311. # Each value can also be specified as a two-element
  1312. # array, by omitting either the value element (defaults
  1313. # to the same as the label), or the boolean checked element
  1314. # (defaults to false).
  1315. #
  1316. # checkbox_group("name", "foo", "bar", "baz")
  1317. # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
  1318. # # <INPUT TYPE="checkbox" NAME="name" VALUE="bar">bar
  1319. # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
  1320. #
  1321. # checkbox_group("name", ["foo"], ["bar", true], "baz")
  1322. # # <INPUT TYPE="checkbox" NAME="name" VALUE="foo">foo
  1323. # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="bar">bar
  1324. # # <INPUT TYPE="checkbox" NAME="name" VALUE="baz">baz
  1325. #
  1326. # checkbox_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
  1327. # # <INPUT TYPE="checkbox" NAME="name" VALUE="1">Foo
  1328. # # <INPUT TYPE="checkbox" CHECKED NAME="name" VALUE="2">Bar
  1329. # # <INPUT TYPE="checkbox" NAME="name" VALUE="Baz">Baz
  1330. #
  1331. # checkbox_group("NAME" => "name",
  1332. # "VALUES" => ["foo", "bar", "baz"])
  1333. #
  1334. # checkbox_group("NAME" => "name",
  1335. # "VALUES" => [["foo"], ["bar", true], "baz"])
  1336. #
  1337. # checkbox_group("NAME" => "name",
  1338. # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
  1339. def checkbox_group(name = "", *values)
  1340. if name.kind_of?(Hash)
  1341. values = name["VALUES"]
  1342. name = name["NAME"]
  1343. end
  1344. values.collect{|value|
  1345. if value.kind_of?(String)
  1346. checkbox(name, value) + value
  1347. else
  1348. if value[value.size - 1] == true
  1349. checkbox(name, value[0], true) +
  1350. value[value.size - 2]
  1351. else
  1352. checkbox(name, value[0]) +
  1353. value[value.size - 1]
  1354. end
  1355. end
  1356. }.to_s
  1357. end
  1358. # Generate an File Upload Input element as a string.
  1359. #
  1360. # The attributes of the element can be specified as three arguments,
  1361. # +name+, +size+, and +maxlength+. +maxlength+ is the maximum length
  1362. # of the file's _name_, not of the file's _contents_.
  1363. #
  1364. # Alternatively, the attributes can be specified as a hash.
  1365. #
  1366. # See #multipart_form() for forms that include file uploads.
  1367. #
  1368. # file_field("name")
  1369. # # <INPUT TYPE="file" NAME="name" SIZE="20">
  1370. #
  1371. # file_field("name", 40)
  1372. # # <INPUT TYPE="file" NAME="name" SIZE="40">
  1373. #
  1374. # file_field("name", 40, 100)
  1375. # # <INPUT TYPE="file" NAME="name" SIZE="40" MAXLENGTH="100">
  1376. #
  1377. # file_field("NAME" => "name", "SIZE" => 40)
  1378. # # <INPUT TYPE="file" NAME="name" SIZE="40">
  1379. def file_field(name = "", size = 20, maxlength = nil)
  1380. attributes = if name.kind_of?(String)
  1381. { "TYPE" => "file", "NAME" => name,
  1382. "SIZE" => size.to_s }
  1383. else
  1384. name["TYPE"] = "file"
  1385. name
  1386. end
  1387. attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  1388. input(attributes)
  1389. end
  1390. # Generate a Form element as a string.
  1391. #
  1392. # +method+ should be either "get" or "post", and defaults to the latter.
  1393. # +action+ defaults to the current CGI script name. +enctype+
  1394. # defaults to "application/x-www-form-urlencoded".
  1395. #
  1396. # Alternatively, the attributes can be specified as a hash.
  1397. #
  1398. # See also #multipart_form() for forms that include file uploads.
  1399. #
  1400. # form{ "string" }
  1401. # # <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
  1402. #
  1403. # form("get") { "string" }
  1404. # # <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
  1405. #
  1406. # form("get", "url") { "string" }
  1407. # # <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>
  1408. #
  1409. # form("METHOD" => "post", "ENCTYPE" => "enctype") { "string" }
  1410. # # <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
  1411. def form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded")
  1412. attributes = if method.kind_of?(String)
  1413. { "METHOD" => method, "ACTION" => action,
  1414. "ENCTYPE" => enctype }
  1415. else
  1416. unless method.has_key?("METHOD")
  1417. method["METHOD"] = "post"
  1418. end
  1419. unless method.has_key?("ENCTYPE")
  1420. method["ENCTYPE"] = enctype
  1421. end
  1422. method
  1423. end
  1424. if block_given?
  1425. body = yield
  1426. else
  1427. body = ""
  1428. end
  1429. if @output_hidden
  1430. body += @output_hidden.collect{|k,v|
  1431. "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
  1432. }.to_s
  1433. end
  1434. super(attributes){body}
  1435. end
  1436. # Generate a Hidden Input element as a string.
  1437. #
  1438. # The attributes of the element can be specified as two arguments,
  1439. # +name+ and +value+.
  1440. #
  1441. # Alternatively, the attributes can be specified as a hash.
  1442. #
  1443. # hidden("name")
  1444. # # <INPUT TYPE="hidden" NAME="name">
  1445. #
  1446. # hidden("name", "value")
  1447. # # <INPUT TYPE="hidden" NAME="name" VALUE="value">
  1448. #
  1449. # hidden("NAME" => "name", "VALUE" => "reset", "ID" => "foo")
  1450. # # <INPUT TYPE="hidden" NAME="name" VALUE="value" ID="foo">
  1451. def hidden(name = "", value = nil)
  1452. attributes = if name.kind_of?(String)
  1453. { "TYPE" => "hidden", "NAME" => name, "VALUE" => value }
  1454. else
  1455. name["TYPE"] = "hidden"
  1456. name
  1457. end
  1458. input(attributes)
  1459. end
  1460. # Generate a top-level HTML element as a string.
  1461. #
  1462. # The attributes of the element are specified as a hash. The
  1463. # pseudo-attribute "PRETTY" can be used to specify that the generated
  1464. # HTML string should be indented. "PRETTY" can also be specified as
  1465. # a string as the sole argument to this method. The pseudo-attribute
  1466. # "DOCTYPE", if given, is used as the leading DOCTYPE SGML tag; it
  1467. # should include the entire text of this tag, including angle brackets.
  1468. #
  1469. # The body of the html element is supplied as a block.
  1470. #
  1471. # html{ "string" }
  1472. # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>string</HTML>
  1473. #
  1474. # html("LANG" => "ja") { "string" }
  1475. # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML LANG="ja">string</HTML>
  1476. #
  1477. # html("DOCTYPE" => false) { "string" }
  1478. # # <HTML>string</HTML>
  1479. #
  1480. # html("DOCTYPE" => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">') { "string" }
  1481. # # <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><HTML>string</HTML>
  1482. #
  1483. # html("PRETTY" => " ") { "<BODY></BODY>" }
  1484. # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  1485. # # <HTML>
  1486. # # <BODY>
  1487. # # </BODY>
  1488. # # </HTML>
  1489. #
  1490. # html("PRETTY" => "\t") { "<BODY></BODY>" }
  1491. # # <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  1492. # # <HTML>
  1493. # # <BODY>
  1494. # # </BODY>
  1495. # # </HTML>
  1496. #
  1497. # html("PRETTY") { "<BODY></BODY>" }
  1498. # # = html("PRETTY" => " ") { "<BODY></BODY>" }
  1499. #
  1500. # html(if $VERBOSE then "PRETTY" end) { "HTML string" }
  1501. #
  1502. def html(attributes = {}) # :yield:
  1503. if nil == attributes
  1504. attributes = {}
  1505. elsif "PRETTY" == attributes
  1506. attributes = { "PRETTY" => true }
  1507. end
  1508. pretty = attributes.delete("PRETTY")
  1509. pretty = " " if true == pretty
  1510. buf = ""
  1511. if attributes.has_key?("DOCTYPE")
  1512. if attributes["DOCTYPE"]
  1513. buf += attributes.delete("DOCTYPE")
  1514. else
  1515. attributes.delete("DOCTYPE")
  1516. end
  1517. else
  1518. buf += doctype
  1519. end
  1520. if block_given?
  1521. buf += super(attributes){ yield }
  1522. else
  1523. buf += super(attributes)
  1524. end
  1525. if pretty
  1526. CGI::pretty(buf, pretty)
  1527. else
  1528. buf
  1529. end
  1530. end
  1531. # Generate an Image Button Input element as a string.
  1532. #
  1533. # +src+ is the URL of the image to use for the button. +name+
  1534. # is the input name. +alt+ is the alternative text for the image.
  1535. #
  1536. # Alternatively, the attributes can be specified as a hash.
  1537. #
  1538. # image_button("url")
  1539. # # <INPUT TYPE="image" SRC="url">
  1540. #
  1541. # image_button("url", "name", "string")
  1542. # # <INPUT TYPE="image" SRC="url" NAME="name" ALT="string">
  1543. #
  1544. # image_button("SRC" => "url", "ATL" => "strng")
  1545. # # <INPUT TYPE="image" SRC="url" ALT="string">
  1546. def image_button(src = "", name = nil, alt = nil)
  1547. attributes = if src.kind_of?(String)
  1548. { "TYPE" => "image", "SRC" => src, "NAME" => name,
  1549. "ALT" => alt }
  1550. else
  1551. src["TYPE"] = "image"
  1552. src["SRC"] ||= ""
  1553. src
  1554. end
  1555. input(attributes)
  1556. end
  1557. # Generate an Image element as a string.
  1558. #
  1559. # +src+ is the URL of the image. +alt+ is the alternative text for
  1560. # the image. +width+ is the width of the image, and +height+ is
  1561. # its height.
  1562. #
  1563. # Alternatively, the attributes can be specified as a hash.
  1564. #
  1565. # img("src", "alt", 100, 50)
  1566. # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
  1567. #
  1568. # img("SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50)
  1569. # # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">
  1570. def img(src = "", alt = "", width = nil, height = nil)
  1571. attributes = if src.kind_of?(String)
  1572. { "SRC" => src, "ALT" => alt }
  1573. else
  1574. src
  1575. end
  1576. attributes["WIDTH"] = width.to_s if width
  1577. attributes["HEIGHT"] = height.to_s if height
  1578. super(attributes)
  1579. end
  1580. # Generate a Form element with multipart encoding as a String.
  1581. #
  1582. # Multipart encoding is used for forms that include file uploads.
  1583. #
  1584. # +action+ is the action to perform. +enctype+ is the encoding
  1585. # type, which defaults to "multipart/form-data".
  1586. #
  1587. # Alternatively, the attributes can be specified as a hash.
  1588. #
  1589. # multipart_form{ "string" }
  1590. # # <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM>
  1591. #
  1592. # multipart_form("url") { "string" }
  1593. # # <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
  1594. def multipart_form(action = nil, enctype = "multipart/form-data")
  1595. attributes = if action == nil
  1596. { "METHOD" => "post", "ENCTYPE" => enctype }
  1597. elsif action.kind_of?(String)
  1598. { "METHOD" => "post", "ACTION" => action,
  1599. "ENCTYPE" => enctype }
  1600. else
  1601. unless action.has_key?("METHOD")
  1602. action["METHOD"] = "post"
  1603. end
  1604. unless action.has_key?("ENCTYPE")
  1605. action["ENCTYPE"] = enctype
  1606. end
  1607. action
  1608. end
  1609. if block_given?
  1610. form(attributes){ yield }
  1611. else
  1612. form(attributes)
  1613. end
  1614. end
  1615. # Generate a Password Input element as a string.
  1616. #
  1617. # +name+ is the name of the input field. +value+ is its default
  1618. # value. +size+ is the size of the input field display. +maxlength+
  1619. # is the maximum length of the inputted password.
  1620. #
  1621. # Alternatively, attributes can be specified as a hash.
  1622. #
  1623. # password_field("name")
  1624. # # <INPUT TYPE="password" NAME="name" SIZE="40">
  1625. #
  1626. # password_field("name", "value")
  1627. # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40">
  1628. #
  1629. # password_field("password", "value", 80, 200)
  1630. # # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
  1631. #
  1632. # password_field("NAME" => "name", "VALUE" => "value")
  1633. # # <INPUT TYPE="password" NAME="name" VALUE="value">
  1634. def password_field(name = "", value = nil, size = 40, maxlength = nil)
  1635. attributes = if name.kind_of?(String)
  1636. { "TYPE" => "password", "NAME" => name,
  1637. "VALUE" => value, "SIZE" => size.to_s }
  1638. else
  1639. name["TYPE"] = "password"
  1640. name
  1641. end
  1642. attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  1643. input(attributes)
  1644. end
  1645. # Generate a Select element as a string.
  1646. #
  1647. # +name+ is the name of the element. The +values+ are the options that
  1648. # can be selected from the Select menu. Each value can be a String or
  1649. # a one, two, or three-element Array. If a String or a one-element
  1650. # Array, this is both the value of that option and the text displayed for
  1651. # it. If a three-element Array, the elements are the option value, displayed
  1652. # text, and a boolean value specifying whether this option starts as selected.
  1653. # The two-element version omits either the option value (defaults to the same
  1654. # as the display text) or the boolean selected specifier (defaults to false).
  1655. #
  1656. # The attributes and options can also be specified as a hash. In this
  1657. # case, options are specified as an array of values as described above,
  1658. # with the hash key of "VALUES".
  1659. #
  1660. # popup_menu("name", "foo", "bar", "baz")
  1661. # # <SELECT NAME="name">
  1662. # # <OPTION VALUE="foo">foo</OPTION>
  1663. # # <OPTION VALUE="bar">bar</OPTION>
  1664. # # <OPTION VALUE="baz">baz</OPTION>
  1665. # # </SELECT>
  1666. #
  1667. # popup_menu("name", ["foo"], ["bar", true], "baz")
  1668. # # <SELECT NAME="name">
  1669. # # <OPTION VALUE="foo">foo</OPTION>
  1670. # # <OPTION VALUE="bar" SELECTED>bar</OPTION>
  1671. # # <OPTION VALUE="baz">baz</OPTION>
  1672. # # </SELECT>
  1673. #
  1674. # popup_menu("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
  1675. # # <SELECT NAME="name">
  1676. # # <OPTION VALUE="1">Foo</OPTION>
  1677. # # <OPTION SELECTED VALUE="2">Bar</OPTION>
  1678. # # <OPTION VALUE="Baz">Baz</OPTION>
  1679. # # </SELECT>
  1680. #
  1681. # popup_menu("NAME" => "name", "SIZE" => 2, "MULTIPLE" => true,
  1682. # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
  1683. # # <SELECT NAME="name" MULTIPLE SIZE="2">
  1684. # # <OPTION VALUE="1">Foo</OPTION>
  1685. # # <OPTION SELECTED VALUE="2">Bar</OPTION>
  1686. # # <OPTION VALUE="Baz">Baz</OPTION>
  1687. # # </SELECT>
  1688. def popup_menu(name = "", *values)
  1689. if name.kind_of?(Hash)
  1690. values = name["VALUES"]
  1691. size = name["SIZE"].to_s if name["SIZE"]
  1692. multiple = name["MULTIPLE"]
  1693. name = name["NAME"]
  1694. else
  1695. size = nil
  1696. multiple = nil
  1697. end
  1698. select({ "NAME" => name, "SIZE" => size,
  1699. "MULTIPLE" => multiple }){
  1700. values.collect{|value|
  1701. if value.kind_of?(String)
  1702. option({ "VALUE" => value }){ value }
  1703. else
  1704. if value[value.size - 1] == true
  1705. option({ "VALUE" => value[0], "SELECTED" => true }){
  1706. value[value.size - 2]
  1707. }
  1708. else
  1709. option({ "VALUE" => value[0] }){
  1710. value[value.size - 1]
  1711. }
  1712. end
  1713. end
  1714. }.to_s
  1715. }
  1716. end
  1717. # Generates a radio-button Input element.
  1718. #
  1719. # +name+ is the name of the input field. +value+ is the value of
  1720. # the field if checked. +checked+ specifies whether the field
  1721. # starts off checked.
  1722. #
  1723. # Alternatively, the attributes can be specified as a hash.
  1724. #
  1725. # radio_button("name", "value")
  1726. # # <INPUT TYPE="radio" NAME="name" VALUE="value">
  1727. #
  1728. # radio_button("name", "value", true)
  1729. # # <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED>
  1730. #
  1731. # radio_button("NAME" => "name", "VALUE" => "value", "ID" => "foo")
  1732. # # <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
  1733. def radio_button(name = "", value = nil, checked = nil)
  1734. attributes = if name.kind_of?(String)
  1735. { "TYPE" => "radio", "NAME" => name,
  1736. "VALUE" => value, "CHECKED" => checked }
  1737. else
  1738. name["TYPE"] = "radio"
  1739. name
  1740. end
  1741. input(attributes)
  1742. end
  1743. # Generate a sequence of radio button Input elements, as a String.
  1744. #
  1745. # This works the same as #checkbox_group(). However, it is not valid
  1746. # to have more than one radiobutton in a group checked.
  1747. #
  1748. # radio_group("name", "foo", "bar", "baz")
  1749. # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
  1750. # # <INPUT TYPE="radio" NAME="name" VALUE="bar">bar
  1751. # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
  1752. #
  1753. # radio_group("name", ["foo"], ["bar", true], "baz")
  1754. # # <INPUT TYPE="radio" NAME="name" VALUE="foo">foo
  1755. # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="bar">bar
  1756. # # <INPUT TYPE="radio" NAME="name" VALUE="baz">baz
  1757. #
  1758. # radio_group("name", ["1", "Foo"], ["2", "Bar", true], "Baz")
  1759. # # <INPUT TYPE="radio" NAME="name" VALUE="1">Foo
  1760. # # <INPUT TYPE="radio" CHECKED NAME="name" VALUE="2">Bar
  1761. # # <INPUT TYPE="radio" NAME="name" VALUE="Baz">Baz
  1762. #
  1763. # radio_group("NAME" => "name",
  1764. # "VALUES" => ["foo", "bar", "baz"])
  1765. #
  1766. # radio_group("NAME" => "name",
  1767. # "VALUES" => [["foo"], ["bar", true], "baz"])
  1768. #
  1769. # radio_group("NAME" => "name",
  1770. # "VALUES" => [["1", "Foo"], ["2", "Bar", true], "Baz"])
  1771. def radio_group(name = "", *values)
  1772. if name.kind_of?(Hash)
  1773. values = name["VALUES"]
  1774. name = name["NAME"]
  1775. end
  1776. values.collect{|value|
  1777. if value.kind_of?(String)
  1778. radio_button(name, value) + value
  1779. else
  1780. if value[value.size - 1] == true
  1781. radio_button(name, value[0], true) +
  1782. value[value.size - 2]
  1783. else
  1784. radio_button(name, value[0]) +
  1785. value[value.size - 1]
  1786. end
  1787. end
  1788. }.to_s
  1789. end
  1790. # Generate a reset button Input element, as a String.
  1791. #
  1792. # This resets the values on a form to their initial values. +value+
  1793. # is the text displayed on the button. +name+ is the name of this button.
  1794. #
  1795. # Alternatively, the attributes can be specified as a hash.
  1796. #
  1797. # reset
  1798. # # <INPUT TYPE="reset">
  1799. #
  1800. # reset("reset")
  1801. # # <INPUT TYPE="reset" VALUE="reset">
  1802. #
  1803. # reset("VALUE" => "reset", "ID" => "foo")
  1804. # # <INPUT TYPE="reset" VALUE="reset" ID="foo">
  1805. def reset(value = nil, name = nil)
  1806. attributes = if (not value) or value.kind_of?(String)
  1807. { "TYPE" => "reset", "VALUE" => value, "NAME" => name }
  1808. else
  1809. value["TYPE"] = "reset"
  1810. value
  1811. end
  1812. input(attributes)
  1813. end
  1814. alias scrolling_list popup_menu
  1815. # Generate a submit button Input element, as a String.
  1816. #
  1817. # +value+ is the text to display on the button. +name+ is the name
  1818. # of the input.
  1819. #
  1820. # Alternatively, the attributes can be specified as a hash.
  1821. #
  1822. # submit
  1823. # # <INPUT TYPE="submit">
  1824. #
  1825. # submit("ok")
  1826. # # <INPUT TYPE="submit" VALUE="ok">
  1827. #
  1828. # submit("ok", "button1")
  1829. # # <INPUT TYPE="submit" VALUE="ok" NAME="button1">
  1830. #
  1831. # submit("VALUE" => "ok", "NAME" => "button1", "ID" => "foo")
  1832. # # <INPUT TYPE="submit" VALUE="ok" NAME="button1" ID="foo">
  1833. def submit(value = nil, name = nil)
  1834. attributes = if (not value) or value.kind_of?(String)
  1835. { "TYPE" => "submit", "VALUE" => value, "NAME" => name }
  1836. else
  1837. value["TYPE"] = "submit"
  1838. value
  1839. end
  1840. input(attributes)
  1841. end
  1842. # Generate a text field Input element, as a String.
  1843. #
  1844. # +name+ is the name of the input field. +value+ is its initial
  1845. # value. +size+ is the size of the input area. +maxlength+
  1846. # is the maximum length of input accepted.
  1847. #
  1848. # Alternatively, the attributes can be specified as a hash.
  1849. #
  1850. # text_field("name")
  1851. # # <INPUT TYPE="text" NAME="name" SIZE="40">
  1852. #
  1853. # text_field("name", "value")
  1854. # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">
  1855. #
  1856. # text_field("name", "value", 80)
  1857. # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">
  1858. #
  1859. # text_field("name", "value", 80, 200)
  1860. # # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">
  1861. #
  1862. # text_field("NAME" => "name", "VALUE" => "value")
  1863. # # <INPUT TYPE="text" NAME="name" VALUE="value">
  1864. def text_field(name = "", value = nil, size = 40, maxlength = nil)
  1865. attributes = if name.kind_of?(String)
  1866. { "TYPE" => "text", "NAME" => name, "VALUE" => value,
  1867. "SIZE" => size.to_s }
  1868. else
  1869. name["TYPE"] = "text"
  1870. name
  1871. end
  1872. attributes["MAXLENGTH"] = maxlength.to_s if maxlength
  1873. input(attributes)
  1874. end
  1875. # Generate a TextArea element, as a String.
  1876. #
  1877. # +name+ is the name of the textarea. +cols+ is the number of
  1878. # columns and +rows+ is the number of rows in the display.
  1879. #
  1880. # Alternatively, the attributes can be specified as a hash.
  1881. #
  1882. # The body is provided by the passed-in no-argument block
  1883. #
  1884. # textarea("name")
  1885. # # = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10)
  1886. #
  1887. # textarea("name", 40, 5)
  1888. # # = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
  1889. def textarea(name = "", cols = 70, rows = 10) # :yield:
  1890. attributes = if name.kind_of?(String)
  1891. { "NAME" => name, "COLS" => cols.to_s,
  1892. "ROWS" => rows.to_s }
  1893. else
  1894. name
  1895. end
  1896. if block_given?
  1897. super(attributes){ yield }
  1898. else
  1899. super(attributes)
  1900. end
  1901. end
  1902. end # HtmlExtension
  1903. # Mixin module for HTML version 3 generation methods.
  1904. module Html3 # :nodoc:
  1905. # The DOCTYPE declaration for this version of HTML
  1906. def doctype
  1907. %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">|
  1908. end
  1909. # Initialise the HTML generation methods for this version.
  1910. def element_init
  1911. extend TagMaker
  1912. methods = ""
  1913. # - -
  1914. for element in %w[ A TT I B U STRIKE BIG SMALL SUB SUP EM STRONG
  1915. DFN CODE SAMP KBD VAR CITE FONT ADDRESS DIV center MAP
  1916. APPLET PRE XMP LISTING DL OL UL DIR MENU SELECT table TITLE
  1917. STYLE SCRIPT H1 H2 H3 H4 H5 H6 TEXTAREA FORM BLOCKQUOTE
  1918. CAPTION ]
  1919. methods += <<-BEGIN + nn_element_def(element) + <<-END
  1920. def #{element.downcase}(attributes = {})
  1921. BEGIN
  1922. end
  1923. END
  1924. end
  1925. # - O EMPTY
  1926. for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
  1927. ISINDEX META ]
  1928. methods += <<-BEGIN + nOE_element_def(element) + <<-END
  1929. def #{element.downcase}(attributes = {})
  1930. BEGIN
  1931. end
  1932. END
  1933. end
  1934. # O O or - O
  1935. for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD LI OPTION tr
  1936. th td ]
  1937. methods += <<-BEGIN + nO_element_def(element) + <<-END
  1938. def #{element.downcase}(attributes = {})
  1939. BEGIN
  1940. end
  1941. END
  1942. end
  1943. eval(methods)
  1944. end
  1945. end # Html3
  1946. # Mixin module for HTML version 4 generation methods.
  1947. module Html4 # :nodoc:
  1948. # The DOCTYPE declaration for this version of HTML
  1949. def doctype
  1950. %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|
  1951. end
  1952. # Initialise the HTML generation methods for this version.
  1953. def element_init
  1954. extend TagMaker
  1955. methods = ""
  1956. # - -
  1957. for element in %w[ TT I B BIG SMALL EM STRONG DFN CODE SAMP KBD
  1958. VAR CITE ABBR ACRONYM SUB SUP SPAN BDO ADDRESS DIV MAP OBJECT
  1959. H1 H2 H3 H4 H5 H6 PRE Q INS DEL DL OL UL LABEL SELECT OPTGROUP
  1960. FIELDSET LEGEND BUTTON TABLE TITLE STYLE SCRIPT NOSCRIPT
  1961. TEXTAREA FORM A BLOCKQUOTE CAPTION ]
  1962. methods += <<-BEGIN + nn_element_def(element) + <<-END
  1963. def #{element.downcase}(attributes = {})
  1964. BEGIN
  1965. end
  1966. END
  1967. end
  1968. # - O EMPTY
  1969. for element in %w[ IMG BASE BR AREA LINK PARAM HR INPUT COL META ]
  1970. methods += <<-BEGIN + nOE_element_def(element) + <<-END
  1971. def #{element.downcase}(attributes = {})
  1972. BEGIN
  1973. end
  1974. END
  1975. end
  1976. # O O or - O
  1977. for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
  1978. COLGROUP TR TH TD HEAD]
  1979. methods += <<-BEGIN + nO_element_def(element) + <<-END
  1980. def #{element.downcase}(attributes = {})
  1981. BEGIN
  1982. end
  1983. END
  1984. end
  1985. eval(methods)
  1986. end
  1987. end # Html4
  1988. # Mixin module for HTML version 4 transitional generation methods.
  1989. module Html4Tr # :nodoc:
  1990. # The DOCTYPE declaration for this version of HTML
  1991. def doctype
  1992. %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|
  1993. end
  1994. # Initialise the HTML generation methods for this version.
  1995. def element_init
  1996. extend TagMaker
  1997. methods = ""
  1998. # - -
  1999. for element in %w[ TT I B U S STRIKE BIG SMALL EM STRONG DFN
  2000. CODE SAMP KBD VAR CITE ABBR ACRONYM FONT SUB SUP SPAN BDO
  2001. ADDRESS DIV CENTER MAP OBJECT APPLET H1 H2 H3 H4 H5 H6 PRE Q
  2002. INS DEL DL OL UL DIR MENU LABEL SELECT OPTGROUP FIELDSET
  2003. LEGEND BUTTON TABLE IFRAME NOFRAMES TITLE STYLE SCRIPT
  2004. NOSCRIPT TEXTAREA FORM A BLOCKQUOTE CAPTION ]
  2005. methods += <<-BEGIN + nn_element_def(element) + <<-END
  2006. def #{element.downcase}(attributes = {})
  2007. BEGIN
  2008. end
  2009. END
  2010. end
  2011. # - O EMPTY
  2012. for element in %w[ IMG BASE BASEFONT BR AREA LINK PARAM HR INPUT
  2013. COL ISINDEX META ]
  2014. methods += <<-BEGIN + nOE_element_def(element) + <<-END
  2015. def #{element.downcase}(attributes = {})
  2016. BEGIN
  2017. end
  2018. END
  2019. end
  2020. # O O or - O
  2021. for element in %w[ HTML BODY P DT DD LI OPTION THEAD TFOOT TBODY
  2022. COLGROUP TR TH TD HEAD ]
  2023. methods += <<-BEGIN + nO_element_def(element) + <<-END
  2024. def #{element.downcase}(attributes = {})
  2025. BEGIN
  2026. end
  2027. END
  2028. end
  2029. eval(methods)
  2030. end
  2031. end # Html4Tr
  2032. # Mixin module for generating HTML version 4 with framesets.
  2033. module Html4Fr # :nodoc:
  2034. # The DOCTYPE declaration for this version of HTML
  2035. def doctype
  2036. %|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|
  2037. end
  2038. # Initialise the HTML generation methods for this version.
  2039. def element_init
  2040. methods = ""
  2041. # - -
  2042. for element in %w[ FRAMESET ]
  2043. methods += <<-BEGIN + nn_element_def(element) + <<-END
  2044. def #{element.downcase}(attributes = {})
  2045. BEGIN
  2046. end
  2047. END
  2048. end
  2049. # - O EMPTY
  2050. for element in %w[ FRAME ]
  2051. methods += <<-BEGIN + nOE_element_def(element) + <<-END
  2052. def #{element.downcase}(attributes = {})
  2053. BEGIN
  2054. end
  2055. END
  2056. end
  2057. eval(methods)
  2058. end
  2059. end # Html4Fr
  2060. # Creates a new CGI instance.
  2061. #
  2062. # +type+ specifies which version of HTML to load the HTML generation
  2063. # methods for. The following versions of HTML are supported:
  2064. #
  2065. # html3:: HTML 3.x
  2066. # html4:: HTML 4.0
  2067. # html4Tr:: HTML 4.0 Transitional
  2068. # html4Fr:: HTML 4.0 with Framesets
  2069. #
  2070. # If not specified, no HTML generation methods will be loaded.
  2071. #
  2072. # If the CGI object is not created in a standard CGI call environment
  2073. # (that is, it can't locate REQUEST_METHOD in its environment), then
  2074. # it will run in "offline" mode. In this mode, it reads its parameters
  2075. # from the command line or (failing that) from standard input. Otherwise,
  2076. # cookies and other parameters are parsed automatically from the standard
  2077. # CGI locations, which varies according to the REQUEST_METHOD.
  2078. def initialize(type = "query")
  2079. if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
  2080. Apache.request.setup_cgi_env
  2081. end
  2082. extend QueryExtension
  2083. @multipart = false
  2084. if defined?(CGI_PARAMS)
  2085. warn "do not use CGI_PARAMS and CGI_COOKIES"
  2086. @params = CGI_PARAMS.dup
  2087. @cookies = CGI_COOKIES.dup
  2088. else
  2089. initialize_query() # set @params, @cookies
  2090. end
  2091. @output_cookies = nil
  2092. @output_hidden = nil
  2093. case type
  2094. when "html3"
  2095. extend Html3
  2096. element_init()
  2097. extend HtmlExtension
  2098. when "html4"
  2099. extend Html4
  2100. element_init()
  2101. extend HtmlExtension
  2102. when "html4Tr"
  2103. extend Html4Tr
  2104. element_init()
  2105. extend HtmlExtension
  2106. when "html4Fr"
  2107. extend Html4Tr
  2108. element_init()
  2109. extend Html4Fr
  2110. element_init()
  2111. extend HtmlExtension
  2112. end
  2113. end
  2114. end # class CGI