From 2b530aad5ce09e760d02ec3d16c5779375bebbbc Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 26 Aug 2016 19:39:26 +0200 Subject: refactor: Request helper class in RequestHandler We have a specific way of encoding the thing we are looking for in the request. I added a small helper class to access the fingerprint and email. This also cleans up the RequestHandler code to some extend. --- lib/nickserver/request_handler.rb | 63 ++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb index 856ec5d..8841b43 100644 --- a/lib/nickserver/request_handler.rb +++ b/lib/nickserver/request_handler.rb @@ -4,16 +4,46 @@ require 'nickserver/couch_db/source' module Nickserver class RequestHandler + class Request + def initialize(params, headers) + @params = params || {} + @headers = headers + end + + def email + param("address") + end + + def fingerprint + param("fingerprint") + end + + def domain + host_header = headers['Host'] + raise MissingHostHeader if host_header.nil? + host_header.split(':')[0].strip.sub(/^nicknym\./, '') + end + + protected + + def param(key) + params[key] && params[key].first + end + + attr_reader :params, :headers + end + def initialize(responder, adapter) @responder = responder @adapter = adapter end def respond_to(params, headers) - if params && params["address"] && params["address"].any? - by_email(params, headers) - elsif params && params["fingerprint"] && params["fingerprint"].any? - by_fingerprint(params) + request = Request.new params, headers + if request.email + by_email(request) + elsif request.fingerprint + by_fingerprint(request) else send_not_found end @@ -26,17 +56,17 @@ module Nickserver protected - def by_email(params, headers) - email = EmailAddress.new(params["address"].first) + def by_email(request) + email = EmailAddress.new(request.email) if email.invalid? send_error("Not a valid address") else - send_key(email, headers) + send_key(email, request) end end - def by_fingerprint(params) - fingerprint = params["fingerprint"].first + def by_fingerprint(request) + fingerprint = request.fingerprint if fingerprint.length == 40 && !fingerprint[/\H/] source = Nickserver::Hkp::Source.new(adapter) key_response = source.get_key_by_fingerprint(fingerprint) @@ -46,8 +76,8 @@ module Nickserver end end - def send_key(email, headers) - if local_address?(email, headers) + def send_key(email, request) + if local_address?(email, request) source = Nickserver::CouchDB::Source.new(adapter) else source = Nickserver::Hkp::Source.new(adapter) @@ -64,15 +94,8 @@ module Nickserver # # If 'domain' is not configured, we rely on the Host header of the HTTP request. # - def local_address?(email, headers) - email.domain?(Config.domain || domain_from_headers(headers)) - end - - # no domain configured, use Host header - def domain_from_headers(headers) - host_header = headers['Host'] - raise MissingHostHeader if host_header.nil? - host_header.split(':')[0].strip.sub(/^nicknym\./, '') + def local_address?(email, request) + email.domain?(Config.domain || request.domain) end def send_error(msg = "not supported") -- cgit v1.2.3