PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/1.8/cgi-lib.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 272 lines | 149 code | 9 blank | 114 comment | 0 complexity | 8b68e35442b17546be6922c86e9ac49b MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  1. warn "Warning:#{caller[0].sub(/:in `.*'\z/, '')}: cgi-lib is deprecated after Ruby 1.8.1; use cgi instead"
  2. =begin
  3. = simple CGI support library
  4. = example
  5. == get form values
  6. require "cgi-lib.rb"
  7. query = CGI.new
  8. query['field'] # <== value of 'field'
  9. query.keys # <== array of fields
  10. and query has Hash class methods
  11. == get cookie values
  12. require "cgi-lib.rb"
  13. query = CGI.new
  14. query.cookie['name'] # <== cookie value of 'name'
  15. query.cookie.keys # <== all cookie names
  16. and query.cookie has Hash class methods
  17. == print HTTP header and HTML string to $>
  18. require "cgi-lib.rb"
  19. CGI::print{
  20. CGI::tag("HTML"){
  21. CGI::tag("HEAD"){ CGI::tag("TITLE"){"TITLE"} } +
  22. CGI::tag("BODY"){
  23. CGI::tag("FORM", {"ACTION"=>"test.rb", "METHOD"=>"POST"}){
  24. CGI::tag("INPUT", {"TYPE"=>"submit", "VALUE"=>"submit"})
  25. } +
  26. CGI::tag("HR")
  27. }
  28. }
  29. }
  30. == make raw cookie string
  31. require "cgi-lib.rb"
  32. cookie1 = CGI::cookie({'name' => 'name',
  33. 'value' => 'value',
  34. 'path' => 'path', # optional
  35. 'domain' => 'domain', # optional
  36. 'expires' => Time.now, # optional
  37. 'secure' => true # optional
  38. })
  39. CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
  40. == print HTTP header and string to $>
  41. require "cgi-lib.rb"
  42. CGI::print{ "string" }
  43. # == CGI::print("Content-Type: text/html"){ "string" }
  44. CGI::print("Content-Type: text/html", cookie1, cookie2){ "string" }
  45. === NPH (no-parse-header) mode
  46. require "cgi-lib.rb"
  47. CGI::print("nph"){ "string" }
  48. # == CGI::print("nph", "Content-Type: text/html"){ "string" }
  49. CGI::print("nph", "Content-Type: text/html", cookie1, cookie2){ "string" }
  50. == make HTML tag string
  51. require "cgi-lib.rb"
  52. CGI::tag("element", {"attribute_name"=>"attribute_value"}){"content"}
  53. == make HTTP header string
  54. require "cgi-lib.rb"
  55. CGI::header # == CGI::header("Content-Type: text/html")
  56. CGI::header("Content-Type: text/html", cookie1, cookie2)
  57. === NPH (no-parse-header) mode
  58. CGI::header("nph") # == CGI::header("nph", "Content-Type: text/html")
  59. CGI::header("nph", "Content-Type: text/html", cookie1, cookie2)
  60. == escape url encode
  61. require "cgi-lib.rb"
  62. url_encoded_string = CGI::escape("string")
  63. == unescape url encoded
  64. require "cgi-lib.rb"
  65. string = CGI::unescape("url encoded string")
  66. == escape HTML &"<>
  67. require "cgi-lib.rb"
  68. CGI::escapeHTML("string")
  69. =end
  70. require "delegate"
  71. class CGI < SimpleDelegator
  72. CR = "\015"
  73. LF = "\012"
  74. EOL = CR + LF
  75. RFC822_DAYS = %w[ Sun Mon Tue Wed Thu Fri Sat ]
  76. RFC822_MONTHS = %w[ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ]
  77. # make rfc1123 date string
  78. def CGI::rfc1123_date(time)
  79. t = time.clone.gmtime
  80. return format("%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
  81. RFC822_DAYS[t.wday], t.day, RFC822_MONTHS[t.month-1], t.year,
  82. t.hour, t.min, t.sec)
  83. end
  84. # escape url encode
  85. def CGI::escape(str)
  86. str.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
  87. end
  88. # unescape url encoded
  89. def CGI::unescape(str)
  90. str.gsub(/\+/, ' ').gsub(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
  91. end
  92. # escape HTML
  93. def CGI::escapeHTML(str)
  94. str.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
  95. end
  96. # offline mode. read name=value pairs on standard input.
  97. def read_from_cmdline
  98. require "shellwords.rb"
  99. words = Shellwords.shellwords(
  100. if not ARGV.empty?
  101. ARGV.join(' ')
  102. else
  103. STDERR.print "(offline mode: enter name=value pairs on standard input)\n" if STDIN.tty?
  104. readlines.join(' ').gsub(/\n/, '')
  105. end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26'))
  106. if words.find{|x| x =~ /=/} then words.join('&') else words.join('+') end
  107. end
  108. def initialize(input = $stdin)
  109. @inputs = {}
  110. @cookie = {}
  111. case ENV['REQUEST_METHOD']
  112. when "GET"
  113. ENV['QUERY_STRING'] or ""
  114. when "POST"
  115. input.read(Integer(ENV['CONTENT_LENGTH'])) or ""
  116. else
  117. read_from_cmdline
  118. end.split(/[&;]/).each do |x|
  119. key, val = x.split(/=/,2).collect{|x|CGI::unescape(x)}
  120. if @inputs.include?(key)
  121. @inputs[key] += "\0" + (val or "")
  122. else
  123. @inputs[key] = (val or "")
  124. end
  125. end
  126. super(@inputs)
  127. if ENV.has_key?('HTTP_COOKIE') or ENV.has_key?('COOKIE')
  128. (ENV['HTTP_COOKIE'] or ENV['COOKIE']).split(/; /).each do |x|
  129. key, val = x.split(/=/,2)
  130. key = CGI::unescape(key)
  131. val = val.split(/&/).collect{|x|CGI::unescape(x)}.join("\0")
  132. if @cookie.include?(key)
  133. @cookie[key] += "\0" + val
  134. else
  135. @cookie[key] = val
  136. end
  137. end
  138. end
  139. end
  140. attr("inputs")
  141. attr("cookie")
  142. # make HTML tag string
  143. def CGI::tag(element, attributes = {})
  144. "<" + escapeHTML(element) + attributes.collect{|name, value|
  145. " " + escapeHTML(name) + '="' + escapeHTML(value) + '"'
  146. }.to_s + ">" +
  147. (iterator? ? yield.to_s + "</" + escapeHTML(element) + ">" : "")
  148. end
  149. # make raw cookie string
  150. def CGI::cookie(options)
  151. "Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
  152. (options['domain'] ? '; domain=' + options['domain'] : '') +
  153. (options['path'] ? '; path=' + options['path'] : '') +
  154. (options['expires'] ? '; expires=' + rfc1123_date(options['expires']) : '') +
  155. (options['secure'] ? '; secure' : '')
  156. end
  157. # make HTTP header string
  158. def CGI::header(*options)
  159. if defined?(MOD_RUBY)
  160. options.each{|option|
  161. option.sub(/(.*?): (.*)/){
  162. Apache::request.headers_out[$1] = $2
  163. }
  164. }
  165. Apache::request.send_http_header
  166. ''
  167. else
  168. if options.delete("nph") or (ENV['SERVER_SOFTWARE'] =~ /IIS/)
  169. [(ENV['SERVER_PROTOCOL'] or "HTTP/1.0") + " 200 OK",
  170. "Date: " + rfc1123_date(Time.now),
  171. "Server: " + (ENV['SERVER_SOFTWARE'] or ""),
  172. "Connection: close"] +
  173. (options.empty? ? ["Content-Type: text/html"] : options)
  174. else
  175. options.empty? ? ["Content-Type: text/html"] : options
  176. end.join(EOL) + EOL + EOL
  177. end
  178. end
  179. # print HTTP header and string to $>
  180. def CGI::print(*options)
  181. $>.print CGI::header(*options) + yield.to_s
  182. end
  183. # print message to $>
  184. def CGI::message(message, title = "", header = ["Content-Type: text/html"])
  185. if message.kind_of?(Hash)
  186. title = message['title']
  187. header = message['header']
  188. message = message['body']
  189. end
  190. CGI::print(*header){
  191. CGI::tag("HTML"){
  192. CGI::tag("HEAD"){ CGI.tag("TITLE"){ title } } +
  193. CGI::tag("BODY"){ message }
  194. }
  195. }
  196. true
  197. end
  198. # print error message to $> and exit
  199. def CGI::error
  200. CGI::message({'title'=>'ERROR', 'body'=>
  201. CGI::tag("PRE"){
  202. "ERROR: " + CGI::tag("STRONG"){ escapeHTML($!.to_s) } + "\n" + escapeHTML($@.join("\n"))
  203. }
  204. })
  205. exit
  206. end
  207. end