# ProgRub p733
# http://microjet.ath.cx/webrickguide/html/HTTPRequest.html

require 'webrick'

#webrick bug? elkerulesere
class WEBrick::HTTPServlet::FileHandler
  def get_instance(server, *options)
    self
  end
end

n = 0

my_proc = lambda do |req,resp|
  resp['Content-Type'] = "text/html"
  resp.body = %{
  <html><body>
       Call no. #{n += 1} <br> from #{req['User-Agent']} <br>
       ize: #{req.script_name}, #{req.class}
      <p>
       Query parameters: #{l = "" ; req.query.each {|k, v| l << "<br>#{k}: #{v}"} ; l}
      <hr>
      Request header:#{l = "" ; req.header.each {|k, v| l << "<br>#{k}: #{v}"} ; l}
      </body></html>
}
end

foo = WEBrick::HTTPServlet::ProcHandler.new(my_proc)

s = WEBrick::HTTPServer.new(:Port => 9090)

fh = WEBrick::HTTPServlet::FileHandler.new(s, __FILE__)


s.mount("/hello", foo)
s.mount("/ize",fh)
trap("INT"){ s.shutdown }
s.start


