diff options
Diffstat (limited to 'lib/nickserver')
-rw-r--r-- | lib/nickserver/dispatcher.rb | 33 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/email_handler.rb | 9 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/fingerprint_handler.rb | 7 |
3 files changed, 38 insertions, 11 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index b818b98..833a2ad 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -1,3 +1,18 @@ +# +# Dispatcher +# +# Dispatch a request so it get's handled by the correct handler. +# +# The dispatcher hands a request to one handler after the other until one of +# them responds. +# +# This is similar to the Chain of Responsibility patter but we iterate over the +# 'handler_chain' array instead of a linked list. +# +# To change the order of handlers or add other handlers change the array in the +# handler_chain function. +# + require 'nickserver/request' require 'nickserver/request_handlers/email_handler' require 'nickserver/request_handlers/fingerprint_handler' @@ -18,22 +33,22 @@ module Nickserver protected def handle(request) - handler = handler_for_request request - handler.call request + handler_chain.each do |handler| + response = handler.call request + return response if response + end rescue RuntimeError => exc puts "Error: #{exc}" puts exc.backtrace ErrorResponse.new(exc.to_s) end - def handler_for_request(request) - if request.email - RequestHandlers::EmailHandler.new - elsif request.fingerprint - RequestHandlers::FingerprintHandler.new - else + def handler_chain + [ + RequestHandlers::EmailHandler.new, + RequestHandlers::FingerprintHandler.new, Proc.new { Nickserver::Response.new(404, "Not Found\n") } - end + ] end def send_response(status = 200, content = '') diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb index 3f7515d..b163b27 100644 --- a/lib/nickserver/request_handlers/email_handler.rb +++ b/lib/nickserver/request_handlers/email_handler.rb @@ -8,6 +8,13 @@ module Nickserver class EmailHandler def call(request) + return unless request.email + handle_request(request) + end + + protected + + def handle_request(request) email = EmailAddress.new(request.email) if email.invalid? ErrorResponse.new("Not a valid address") @@ -16,8 +23,6 @@ module Nickserver end end - protected - def send_key(email, request) if local_address?(email, request) source = Nickserver::CouchDB::Source.new diff --git a/lib/nickserver/request_handlers/fingerprint_handler.rb b/lib/nickserver/request_handlers/fingerprint_handler.rb index 3202c41..3c04fcd 100644 --- a/lib/nickserver/request_handlers/fingerprint_handler.rb +++ b/lib/nickserver/request_handlers/fingerprint_handler.rb @@ -6,6 +6,13 @@ module Nickserver class FingerprintHandler def call(request) + return unless request.fingerprint + handle_request(request) + end + + protected + + def handle_request(request) fingerprint = request.fingerprint if fingerprint.length == 40 && !fingerprint[/\H/] source = Nickserver::Hkp::Source.new |