summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorRick Bradley <rick@rickbradley.com>2007-10-16 13:57:56 -0500
committerRick Bradley <rick@rickbradley.com>2007-10-16 13:57:56 -0500
commit2a497fff66a7827059b712e84dcaff171ccab6be (patch)
tree1e66995191c44dd3e5951f13a1dd73263bf60395 /lib/puppet
parent6ab78f62ee589e542fd653a54109c0f5141ea026 (diff)
downloadpuppet-2a497fff66a7827059b712e84dcaff171ccab6be.tar.gz
puppet-2a497fff66a7827059b712e84dcaff171ccab6be.tar.xz
puppet-2a497fff66a7827059b712e84dcaff171ccab6be.zip
Refactored to use a Handler base class for server+protocol handlers. Finally eliminated dependency on Puppet.start, etc., from WEBrick HTTP server class. {webrick,mongrel}+REST now support request handling uniformly; need encode/decode next.
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/network/http/handler.rb57
-rw-r--r--lib/puppet/network/http/mongrel/rest.rb47
-rw-r--r--lib/puppet/network/http/webrick.rb9
-rw-r--r--lib/puppet/network/http/webrick/rest.rb21
4 files changed, 76 insertions, 58 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
diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb
index f9a6e3796..8f3de957e 100644
--- a/lib/puppet/network/http/mongrel/rest.rb
+++ b/lib/puppet/network/http/mongrel/rest.rb
@@ -1,47 +1,9 @@
-class Puppet::Network::HTTP::MongrelREST
- 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)}'"
- # TODO: here, raise an exception, or do some defaulting or something
- end
-
+require 'puppet/network/http/handler'
+
+class Puppet::Network::HTTP::MongrelREST < Puppet::Network::HTTP::Handler
+
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
-
def register_handler
@model = find_model_for_handler(@handler)
@server.register('/' + @handler.to_s, self)
@@ -55,5 +17,4 @@ class Puppet::Network::HTTP::MongrelREST
def path(request)
request.params[Mongrel::Const::REQUEST_PATH]
end
-
end
diff --git a/lib/puppet/network/http/webrick.rb b/lib/puppet/network/http/webrick.rb
index 53aa2e99b..c4b2ed3c6 100644
--- a/lib/puppet/network/http/webrick.rb
+++ b/lib/puppet/network/http/webrick.rb
@@ -18,19 +18,14 @@ class Puppet::Network::HTTP::WEBrick
@protocols = args[:protocols]
@handlers = args[:handlers]
@server = WEBrick::HTTPServer.new(:BindAddress => args[:address], :Port => args[:port])
-
setup_handlers
-
- # TODO / FIXME is this really necessary? -- or can we do it in both mongrel and webrick?
- Puppet.newservice(@server)
- Puppet.start
-
+ @server.start
@listening = true
end
def unlisten
raise "WEBrick server is not listening" unless listening?
- shutdown
+ @server.shutdown
@listening = false
end
diff --git a/lib/puppet/network/http/webrick/rest.rb b/lib/puppet/network/http/webrick/rest.rb
index 7b9a3912b..cefffd76d 100644
--- a/lib/puppet/network/http/webrick/rest.rb
+++ b/lib/puppet/network/http/webrick/rest.rb
@@ -1,8 +1,10 @@
-class Puppet::Network::HTTP::WEBrickREST
- def initialize(args = {})
- raise ArgumentError unless @server = args[:server]
- raise ArgumentError unless @handler = args[:handler]
- register_handler
+require 'puppet/network/http/handler'
+
+class Puppet::Network::HTTP::WEBrickREST < Puppet::Network::HTTP::Handler
+
+ # WEBrick uses a service() method to respond to requests. Simply delegate to the handler response() method.
+ def service(request, response)
+ process(request, response)
end
private
@@ -13,8 +15,11 @@ class Puppet::Network::HTTP::WEBrickREST
@server.mount('/' + @handler.to_s + 's', self)
end
- def find_model_for_handler(handler)
- Puppet::Indirector::Indirection.model(handler) ||
- raise(ArgumentError, "Cannot locate indirection [#{handler}].")
+ def http_method(request)
+ request.request_method
+ end
+
+ def path(request)
+ request.path
end
end \ No newline at end of file