1#
2# prochandler.rb -- ProcHandler Class
3#
4# Author: IPR -- Internet Programming with Ruby -- writers
5# Copyright (c) 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
6# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
7# reserved.
8#
9# $IPR: prochandler.rb,v 1.7 2002/09/21 12:23:42 gotoyuzo Exp $
10
11require 'webrick/httpservlet/abstract.rb'
12
13module WEBrick
14  module HTTPServlet
15
16    ##
17    # Mounts a proc at a path that accepts a request and response.
18    #
19    # Instead of mounting this servlet with WEBrick::HTTPServer#mount use
20    # WEBrick::HTTPServer#mount_proc:
21    #
22    #   server.mount_proc '/' do |req, res|
23    #     res.body = 'it worked!'
24    #     res.status = 200
25    #   end
26
27    class ProcHandler < AbstractServlet
28      # :stopdoc:
29      def get_instance(server, *options)
30        self
31      end
32
33      def initialize(proc)
34        @proc = proc
35      end
36
37      def do_GET(request, response)
38        @proc.call(request, response)
39      end
40
41      alias do_POST do_GET
42      # :startdoc:
43    end
44
45  end
46end
47