summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/http/handler.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/network/http/handler.rb')
-rw-r--r--lib/puppet/network/http/handler.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb
new file mode 100644
index 000000000..54cec417b
--- /dev/null
+++ b/lib/puppet/network/http/handler.rb
@@ -0,0 +1,57 @@
+class Puppet::Network::HTTP::Handler
+ def initialize(args = {})
+ raise ArgumentError unless @server = args[:server]
+ raise ArgumentError unless @handler = args[:handler]
+ register_handler
+ end
+
+ # handle an HTTP request coming from Mongrel
+ def process(request, response)
+ return @model.find if get?(request) and singular?(request)
+ return @model.search if get?(request) and plural?(request)
+ return @model.destroy if delete?(request) and singular?(request)
+ return @model.new.save if put?(request) and singular?(request)
+ raise ArgumentError, "Did not understand HTTP #{http_method(request)} request for '#{path(request)}'"
+ end
+
+ private
+
+ def find_model_for_handler(handler)
+ Puppet::Indirector::Indirection.model(handler) ||
+ raise(ArgumentError, "Cannot locate indirection [#{handler}].")
+ end
+
+ def get?(request)
+ http_method(request) == 'GET'
+ end
+
+ def put?(request)
+ http_method(request) == 'PUT'
+ end
+
+ def delete?(request)
+ http_method(request) == 'DELETE'
+ end
+
+ def singular?(request)
+ %r{/#{@handler.to_s}$}.match(path(request))
+ end
+
+ def plural?(request)
+ %r{/#{@handler.to_s}s$}.match(path(request))
+ end
+
+ # methods specific to a given web server
+
+ def register_handler
+ raise UnimplementedError
+ end
+
+ def http_method(request)
+ raise UnimplementedError
+ end
+
+ def path(request)
+ raise UnimplementedError
+ end
+end