summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-08-26 19:39:26 +0200
committerAzul <azul@riseup.net>2016-08-26 19:43:49 +0200
commit2b530aad5ce09e760d02ec3d16c5779375bebbbc (patch)
treec323390273eef5fdc75cba26262217713175d14c /lib
parentc134e0940a44ba3fb3f0f8ee86faa8053a9e0b44 (diff)
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/nickserver/request_handler.rb63
1 files 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")