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.rb66
1 files changed, 31 insertions, 35 deletions
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
index 26b6ec1..608db83 100644
--- a/lib/nickserver/request_handler.rb
+++ b/lib/nickserver/request_handler.rb
@@ -1,3 +1,6 @@
+require 'nickserver/hkp/source'
+require 'nickserver/couch_db/source'
+
module Nickserver
class RequestHandler
@@ -7,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}"
@@ -23,23 +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
- source.query(uid) do |response|
- send_response(status: response.status, content: response.content)
- end
+ response = source.query(email)
+ send_response response.status, response.content
+ rescue MissingHostHeader
+ send_error("HTTP request must include a Host header.")
end
#
@@ -48,38 +50,32 @@ 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.split(/\0/).grep(/^Host: /).first
- if host_header.nil?
- send_error("HTTP request must include a Host header.")
- else
- host = host_header.split(':')[1].strip.sub(/^nicknym\./, '')
- return uid_domain == host
- end
- end
+ def local_address?(email, headers)
+ email.domain?(Config.domain || domain_from_headers(headers))
end
- def send_error(msg = "not supported")
- send_response(status: 500, content: "500 #{msg}\n")
+
+ # 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_not_found(msg = "Not Found")
- send_response(status: 404, content: "404 #{msg}\n")
+ def send_error(msg = "not supported")
+ send_response 500, "500 #{msg}\n"
end
- def send_response(opts = {})
- responder.send_response default_response.merge(opts)
+ def send_not_found(msg = "Not Found")
+ send_response 404, "404 #{msg}\n"
end
- def default_response
- {status: 200, content_type: 'text/plain', content: ''}
+ def send_response(status = 200, content = '')
+ responder.respond status, content
end
attr_reader :responder, :adapter
+ class MissingHostHeader < StandardError
+ end
end
end