1require "pp"
2
3module DemoApplication
4  def initialize(config, enctype)
5    super
6    @enctype = enctype
7  end
8
9  def do_GET(req, res)
10    if req.path_info != "/"
11      res.set_redirect(WEBrick::HTTPStatus::Found, req.script_name + "/")
12    end
13    res.body =<<-_end_of_html_
14      <HTML>
15       <FORM method="POST" enctype=#{@enctype}>
16        text: <INPUT type="text" name="text"><BR>
17        file: <INPUT type="file" name="file"><BR>
18        check:
19        <INPUT type="checkbox" name="check" value="a">a,
20        <INPUT type="checkbox" name="check" value="b">b,
21        <INPUT type="checkbox" name="check" value="c">c,
22        <BR>
23        <INPUT type="submit">
24       </FORM>
25      </HTML>
26    _end_of_html_
27    res['content-type'] = 'text/html; charset=iso-8859-1'
28  end
29
30  def do_POST(req, res)
31    if req["content-length"].to_i > 1024*10
32      raise WEBrick::HTTPStatus::Forbidden, "file size too large"
33    end
34    res.body =<<-_end_of_html_
35      <HTML>
36       <H2>Query Parameters</H2>
37       #{display_query(req.query)}
38       <A href="#{req.path}">return</A>
39       <H2>Request</H2>
40       <PRE>#{WEBrick::HTMLUtils::escape(PP::pp(req, "", 80))}</PRE>
41       <H2>Response</H2>
42       <PRE>#{WEBrick::HTMLUtils::escape(PP::pp(res, "", 80))}</PRE>
43      </HTML>
44    _end_of_html_
45    res['content-type'] = 'text/html; charset=iso-8859-1'
46  end
47
48  private
49
50  def display_query(q)
51    ret = ""
52    q.each{|key, val|
53      ret << "<H3>#{WEBrick::HTMLUtils::escape(key)}</H3>"
54      ret << "<TABLE border=1>"
55      ret << make_tr("val", val.inspect)
56      ret << make_tr("val.to_a", val.to_a.inspect)
57      ret << make_tr("val.to_ary", val.to_ary.inspect)
58      ret << "</TABLE>"
59    }
60    ret
61  end
62
63  def make_tr(arg0, arg1)
64    "<TR><TD>#{arg0}</TD><TD>#{WEBrick::HTMLUtils::escape(arg1)}</TD></TR>"
65  end
66end
67