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