summaryrefslogtreecommitdiff
path: root/lib/cgi-lib.rb
blob: 83ea6118b979d39da151993a350835edcd91b4fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#
# Get CGI String
#
# EXAMPLE:
# require "cgi-lib.rb"
# foo = CGI.new
# foo['field']   <== value of 'field'
# foo.keys       <== array of fields
# and foo has Hash class methods
#
# foo.cookie['name']  <== cookie value of 'name'
# foo.cookie.keys     <== all cookie names
# and foo.cookie has Hash class methods
#
# make raw cookie string
# cookie1 = CGI.cookie({'name'    => 'name',
#                       'value'   => 'value',
#                       'path'    => 'path',   # optional
#                       'domain'  => 'domain', # optional
#                       'expires' => Time.now, # optional
#                       'secure'  => true      # optional
#                      })
#
# print CGI.header("Content-Type: text/html", cookie1, cookie2)
#
# print CGI.header("HTTP/1.0 200 OK", "Content-Type: text/html")
# print CGI.header # == print CGI.header("Content-Type: text/html")
#
# make HTML tag string
# CGI.tag("element", {"attribute_name"=>"attribute_value"}){"content"}
#
# print CGI.tag("HTML"){
#         CGI.tag("HEAD"){ CGI.tag("TITLE"){"TITLE"} } +
#         CGI.tag("BODY"){
#           CGI.tag("FORM", {"ACTION"=>"test.rb", "METHOD"=>"POST"}){
#             CGI.tag("INPUT", {"TYPE"=>"submit", "VALUE"=>"submit"})
#           } +
#           CGI.tag("HR")
#         }
#       }
#
# print HTTP header and strings to STDOUT
# CGI.print{ "string" } # add HTTP header "Content-Type: text/html"
# CGI.print("Content-Type: text/plain"){ "string" }
# CGI.print("HTTP/1.0 200 OK", "Content-Type: text/html"){ "string" }


# if running on Windows(IIS or PWS) then change cwd.
if ENV['SERVER_SOFTWARE'] =~ /^Microsoft-/ then
  Dir.chdir ENV['PATH_TRANSLATED'].sub(/[^\\]+$/, '')
end

require "delegate"

class CGI < SimpleDelegator

  CR  = "\015"
  LF  = "\012"
  EOL = CR + LF

  attr("inputs")
  attr("cookie")

  # original is CGI.pm
  def read_from_cmdline
    require "shellwords.rb"
    words = Shellwords.shellwords(if not ARGV.empty? then
                         ARGV.join(' ')
                       else
                         STDERR.print "(offline mode: enter name=value pairs on standard input)\n" if STDIN.tty?
                         readlines.join(' ').gsub(/\n/, '')
                       end.gsub(/\\=/, '%3D').gsub(/\\&/, '%26'))

    if words.find{|x| x =~ /=/} then words.join('&') else words.join('+') end
  end
  
  # escape url encode
  def escape(str)
    str.gsub!(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
    str
  end

  # unescape url encoded
  def unescape(str)
    str.gsub!(/\+/, ' ')
    str.gsub!(/%([0-9a-fA-F]{2})/){ [$1.hex].pack("c") }
    str
  end

  # escape HTML
  def escapeHTML(str)
    str.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
  end

  module_function :escape, :unescape, :escapeHTML

  def initialize(input = $stdin)

    @inputs = {}
    case ENV['REQUEST_METHOD']
    when "GET"
      # exception messages should be printed to stdout.
      STDERR.reopen(STDOUT)
      ENV['QUERY_STRING'] or ""
    when "POST"
      # exception messages should be printed to stdout.
      STDERR.reopen(STDOUT)
      input.read Integer(ENV['CONTENT_LENGTH'])
    else
      read_from_cmdline
    end.split(/&/).each do |x|
      key, val = x.split(/=/,2).collect{|x|unescape(x)}
      if @inputs.include?(key)
        @inputs[key] += "\0" + (val or "")
      else
        @inputs[key] = (val or "")
      end
    end

    super(@inputs)

    if ENV.has_key?('HTTP_COOKIE')
      @cookie = {}
      ENV['HTTP_COOKIE'].split("; ").each do |x|
        key, val = x.split(/=/,2).collect{|x|unescape(x)}
        if @cookie.include?(key)
          @cookie[key] += "\0" + (val or "")
        else
          @cookie[key] = (val or "")
        end
      end
    end
  end

  def CGI.header(*options)
    options.push("Content-Type: text/html") if options.empty?
    if options.find{|item| /^Expires: |^Set-Cookie: /i === item}
      options.push("Date: " + Time.now.gmtime.strftime("%a, %d %b %Y %X %Z"))
    end
    options.join(EOL) + EOL + EOL
  end

  def CGI.cookie(options)
    "Set-Cookie: " + options['name'] + '=' + escape(options['value']) +
    (options['domain']  ? '; domain='  + options['domain'] : '') +
    (options['path']    ? '; path='    + options['path']   : '') +
    (options['expires'] ? '; expires=' + options['expires'].strftime("%a, %d %b %Y %X %Z") : '') +
    (options['secure']  ? '; secure' : '')
  end

  def CGI.tag(element, attributes = {})
    "<" + escapeHTML(element) + attributes.collect{|name, value|
      " " + escapeHTML(name) + '="' + escapeHTML(value) + '"'
    }.to_s + ">" +
    (iterator? ? yield.to_s + "</" + escapeHTML(element) + ">" : "")
  end

  def CGI.print(*header)
    header.push("Content-Type: text/html") if header.empty?
    STDOUT.print CGI.header(*header) + yield.to_s
  end

  def CGI.message(message, title = "", header = ["Content-Type: text/html"])
    if message.kind_of?(Hash)
      title   = message['title']
      header  = message['header']
      message = message['body']
    end
    CGI.print(*header){
      CGI.tag("HTML"){
        CGI.tag("HEAD"){ CGI.tag("TITLE"){ title } } +
        CGI.tag("BODY"){ message }
      }
    }
    TRUE
  end

  def CGI.error
    CGI.message({'title'=>'ERROR', 'body'=>
      CGI.tag("PRE"){
        "ERROR: " + CGI.tag("STRONG"){ escapeHTML($!.to_s) } + "\n" +
        escapeHTML($@.join("\n"))
      }
    })
    exit
  end
end