summaryrefslogtreecommitdiff
path: root/lib/nickserver/request_handler.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/request_handler.rb')
-rw-r--r--lib/nickserver/request_handler.rb48
1 files changed, 23 insertions, 25 deletions
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
index 295d3c0..608db83 100644
--- a/lib/nickserver/request_handler.rb
+++ b/lib/nickserver/request_handler.rb
@@ -10,13 +10,13 @@ module Nickserver
end
def respond_to(params, headers)
- uid = get_uid_from_params(params)
- if uid.nil?
+ email = get_email_from_params(params)
+ if email.nil?
send_not_found
- elsif uid !~ EmailAddress
+ elsif email.invalid?
send_error("Not a valid address")
else
- send_key(uid, headers)
+ send_key(email, headers)
end
rescue RuntimeError => exc
puts "Error: #{exc}"
@@ -26,22 +26,22 @@ module Nickserver
protected
- def get_uid_from_params(params)
+ def get_email_from_params(params)
if params && params["address"] && params["address"].any?
- return params["address"].first
- else
- return nil
+ EmailAddress.new(params["address"].first)
end
end
- def send_key(uid, headers)
- if local_address?(uid, headers)
+ def send_key(email, headers)
+ if local_address?(email, headers)
source = Nickserver::CouchDB::Source.new(adapter)
else
source = Nickserver::Hkp::Source.new(adapter)
end
- response = source.query(uid)
+ response = source.query(email)
send_response response.status, response.content
+ rescue MissingHostHeader
+ send_error("HTTP request must include a Host header.")
end
#
@@ -50,21 +50,17 @@ module Nickserver
#
# If 'domain' is not configured, we rely on the Host header of the HTTP request.
#
- def local_address?(uid, headers)
- uid_domain = uid.sub(/^.*@(.*)$/, "\\1")
- if Config.domain
- return uid_domain == Config.domain
- else
- # no domain configured, use Host header
- host_header = headers['Host']
- if host_header.nil?
- send_error("HTTP request must include a Host header.")
- else
- host = host_header.split(':')[0].strip.sub(/^nicknym\./, '')
- return uid_domain == host
- end
- end
+ 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\./, '')
+ end
+
def send_error(msg = "not supported")
send_response 500, "500 #{msg}\n"
end
@@ -79,5 +75,7 @@ module Nickserver
attr_reader :responder, :adapter
+ class MissingHostHeader < StandardError
+ end
end
end