From 58d687f927aabc8aa2fdfc46132cb71706bdde46 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 29 Aug 2016 09:41:12 +0200 Subject: refactor: split up ResponseHandler Now we have a Dispatcher and two ResponseHandlers that have the same interface. Moving towards a Chain of Responsibility pattern. --- lib/nickserver/request_handlers/email_handler.rb | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/nickserver/request_handlers/email_handler.rb (limited to 'lib/nickserver/request_handlers/email_handler.rb') diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb new file mode 100644 index 0000000..3f7515d --- /dev/null +++ b/lib/nickserver/request_handlers/email_handler.rb @@ -0,0 +1,49 @@ +require 'nickserver/email_address' +require 'nickserver/error_response' +require 'nickserver/hkp/source' +require 'nickserver/couch_db/source' + +module Nickserver + module RequestHandlers + class EmailHandler + + def call(request) + email = EmailAddress.new(request.email) + if email.invalid? + ErrorResponse.new("Not a valid address") + else + send_key(email, request) + end + end + + protected + + def send_key(email, request) + if local_address?(email, request) + source = Nickserver::CouchDB::Source.new + else + source = Nickserver::Hkp::Source.new + end + source.query(email) + rescue MissingHostHeader + ErrorResponse.new("HTTP request must include a Host header.") + end + + # + # Return true if the user address is for a user of this service provider. + # e.g. if the provider is example.org, then alice@example.org returns true. + # + # If 'domain' is not configured, we rely on the Host header of the HTTP request. + # + def local_address?(email, request) + domain = Config.domain || request.domain + raise MissingHostHeader if domain == '' + email.domain? domain + end + end + + + class MissingHostHeader < StandardError + end + end +end -- cgit v1.2.3 From becd26b0bdf44b3625caaa7643914d0379a4fea5 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 29 Aug 2016 10:26:54 +0200 Subject: refactor: let handlers check if they are applicable Instead of testing the preconditions for each handler in the dispatcher 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. --- lib/nickserver/request_handlers/email_handler.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/nickserver/request_handlers/email_handler.rb') 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 -- cgit v1.2.3 From 0784391a21b75ca52892e992a614b0f927ade00e Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 29 Aug 2016 11:59:54 +0200 Subject: refactor: split EmailHandler in 3 InvalidEmailHandler - handle emails with an invalid format LocalEmailHandler - handle emails on the local domain EmailHandler - handle all other emails by using hkp This is a preparation to add leap provider email lookup and remove hkp eventually. But for now we keep the behaviour the same and only refactor. --- lib/nickserver/request_handlers/email_handler.rb | 40 +++--------------------- 1 file changed, 4 insertions(+), 36 deletions(-) (limited to 'lib/nickserver/request_handlers/email_handler.rb') diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb index b163b27..96fcaf3 100644 --- a/lib/nickserver/request_handlers/email_handler.rb +++ b/lib/nickserver/request_handlers/email_handler.rb @@ -1,7 +1,5 @@ require 'nickserver/email_address' -require 'nickserver/error_response' require 'nickserver/hkp/source' -require 'nickserver/couch_db/source' module Nickserver module RequestHandlers @@ -9,46 +7,16 @@ module Nickserver 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") - else - send_key(email, request) - end - end - - def send_key(email, request) - if local_address?(email, request) - source = Nickserver::CouchDB::Source.new - else - source = Nickserver::Hkp::Source.new - end source.query(email) - rescue MissingHostHeader - ErrorResponse.new("HTTP request must include a Host header.") end - # - # Return true if the user address is for a user of this service provider. - # e.g. if the provider is example.org, then alice@example.org returns true. - # - # If 'domain' is not configured, we rely on the Host header of the HTTP request. - # - def local_address?(email, request) - domain = Config.domain || request.domain - raise MissingHostHeader if domain == '' - email.domain? domain - end - end + protected + def source + Nickserver::Hkp::Source.new + end - class MissingHostHeader < StandardError end end end -- cgit v1.2.3