diff options
author | Azul <azul@riseup.net> | 2016-08-29 09:41:12 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-08-29 09:41:12 +0200 |
commit | 58d687f927aabc8aa2fdfc46132cb71706bdde46 (patch) | |
tree | 5324fe4255437b1e1c050e1b2e087d3023ad22b5 /lib/nickserver | |
parent | 16d77cc03068b19791e704abfc2eb14f8cd141f0 (diff) |
refactor: split up ResponseHandler
Now we have a Dispatcher and two ResponseHandlers that have the same interface.
Moving towards a Chain of Responsibility pattern.
Diffstat (limited to 'lib/nickserver')
-rw-r--r-- | lib/nickserver/dispatcher.rb | 46 | ||||
-rw-r--r-- | lib/nickserver/error_response.rb | 11 | ||||
-rw-r--r-- | lib/nickserver/reel_server.rb | 4 | ||||
-rw-r--r-- | lib/nickserver/request_handler.rb | 106 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/email_handler.rb | 49 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/fingerprint_handler.rb | 20 |
6 files changed, 128 insertions, 108 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb new file mode 100644 index 0000000..b818b98 --- /dev/null +++ b/lib/nickserver/dispatcher.rb @@ -0,0 +1,46 @@ +require 'nickserver/request' +require 'nickserver/request_handlers/email_handler' +require 'nickserver/request_handlers/fingerprint_handler' + +module Nickserver + class Dispatcher + + def initialize(responder) + @responder = responder + end + + def respond_to(params, headers) + request = Nickserver::Request.new params, headers + response = handle request + send_response response.status, response.content + end + + protected + + def handle(request) + handler = handler_for_request request + handler.call request + 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 + Proc.new { Nickserver::Response.new(404, "Not Found\n") } + end + end + + def send_response(status = 200, content = '') + responder.respond status, content + end + + attr_reader :responder + + end +end diff --git a/lib/nickserver/error_response.rb b/lib/nickserver/error_response.rb new file mode 100644 index 0000000..1065e4e --- /dev/null +++ b/lib/nickserver/error_response.rb @@ -0,0 +1,11 @@ +require 'nickserver/response' + +module Nickserver + class ErrorResponse < Nickserver::Response + def initialize(message) + @status = 500 + @message = message + "\n" + end + + end +end diff --git a/lib/nickserver/reel_server.rb b/lib/nickserver/reel_server.rb index 549b738..b681577 100644 --- a/lib/nickserver/reel_server.rb +++ b/lib/nickserver/reel_server.rb @@ -2,7 +2,7 @@ silence_warnings do require 'reel' end require 'nickserver/adapters/celluloid_http' -require 'nickserver/request_handler' +require 'nickserver/dispatcher' module Nickserver class ReelServer < Reel::Server::HTTP @@ -32,7 +32,7 @@ module Nickserver protected def handler_for(request) - RequestHandler.new(request) + Dispatcher.new(request) end def params(request) diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb deleted file mode 100644 index 1239c44..0000000 --- a/lib/nickserver/request_handler.rb +++ /dev/null @@ -1,106 +0,0 @@ -require 'nickserver/hkp/source' -require 'nickserver/couch_db/source' -require 'nickserver/request' - -module Nickserver - class RequestHandler - - def initialize(responder) - @responder = responder - end - - def respond_to(params, headers) - request = Nickserver::Request.new params, headers - response = handle request - send_response response.status, response.content - end - - protected - - def handle(request) - handler = handler_for_request request - handler.call request - rescue RuntimeError => exc - puts "Error: #{exc}" - puts exc.backtrace - ErrorResponse.new(exc.to_s) - end - - def handler_for_request(request) - if request.email - EmailHandler.new - elsif request.fingerprint - FingerprintHandler.new - else - Proc.new { Nickserver::Response.new(404, "Not Found\n") } - end - end - - 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 FingerprintHandler - - def call(request) - fingerprint = request.fingerprint - if fingerprint.length == 40 && !fingerprint[/\H/] - source = Nickserver::Hkp::Source.new - source.get_key_by_fingerprint(fingerprint) - else - ErrorResponse.new('Fingerprint invalid: ' + fingerprint) - end - end - - end - - class ErrorResponse < Nickserver::Response - def initialize(message) - @status = 500 - @message = message + "\n" - end - end - - def send_response(status = 200, content = '') - responder.respond status, content - end - - attr_reader :responder - - class MissingHostHeader < StandardError - end - end -end 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 diff --git a/lib/nickserver/request_handlers/fingerprint_handler.rb b/lib/nickserver/request_handlers/fingerprint_handler.rb new file mode 100644 index 0000000..3202c41 --- /dev/null +++ b/lib/nickserver/request_handlers/fingerprint_handler.rb @@ -0,0 +1,20 @@ +require 'nickserver/hkp/source' +require 'nickserver/error_response' + +module Nickserver + module RequestHandlers + class FingerprintHandler + + def call(request) + fingerprint = request.fingerprint + if fingerprint.length == 40 && !fingerprint[/\H/] + source = Nickserver::Hkp::Source.new + source.get_key_by_fingerprint(fingerprint) + else + ErrorResponse.new('Fingerprint invalid: ' + fingerprint) + end + end + + end + end +end |