summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-06-20 12:16:27 +0200
committerAzul <azul@riseup.net>2016-06-20 13:29:43 +0200
commitb185742d959a0a3854c5e628354a538a15bdea15 (patch)
tree331cfb3d07f49f1e12e5cbf50f63e80692e0868f
parent6925fc2fe17d7e22e22af664171cd98956c62cf1 (diff)
seperate generic RequestHandler from EmServer
The request handler takes a responder (the EmServer) and an adapter as initialization arguments. It will then respond to requests by querying the sources using the adapter and calling send_response on the responder.
-rw-r--r--lib/nickserver/em_server.rb80
-rw-r--r--lib/nickserver/request_handler.rb85
-rw-r--r--test/unit/request_handler_test.rb41
3 files changed, 138 insertions, 68 deletions
diff --git a/lib/nickserver/em_server.rb b/lib/nickserver/em_server.rb
index faace4b..bcec4cd 100644
--- a/lib/nickserver/em_server.rb
+++ b/lib/nickserver/em_server.rb
@@ -2,6 +2,7 @@ require 'eventmachine'
silence_warnings do
require 'evma_httpserver'
end
+require 'nickserver/request_handler'
module Nickserver
class EmServer < EM::Connection
@@ -9,7 +10,7 @@ module Nickserver
def self.start(options = {})
EventMachine.run do
- EM.start_server options[:host], options[:port], Nickserver::EmServer
+ EM.start_server options[:host], options[:port], self
end
end
@@ -19,32 +20,10 @@ module Nickserver
end
def process_http_request
- uid = get_uid_from_request
- if uid.nil?
- send_not_found
- elsif uid !~ EmailAddress
- send_error("Not a valid address")
- else
- send_key(uid)
- end
- rescue RuntimeError => exc
- puts "Error: #{exc}"
- puts exc.backtrace
- send_error(exc.to_s)
- end
-
- private
-
- def send_error(msg = "not supported")
- send_response(status: 500, content: "500 #{msg}\n")
+ handler.respond_to params, @http_headers
end
- def send_not_found(msg = "Not Found")
- send_response(status: 404, content: "404 #{msg}\n")
- end
-
- def send_response(opts = {})
- options = {status: 200, content_type: 'text/plain', content: ''}.merge(opts)
+ def send_response(options = {})
response = EM::DelegatedHttpResponse.new(self)
response.status = options[:status]
response.content_type options[:content_type]
@@ -54,54 +33,19 @@ module Nickserver
end
end
- def get_uid_from_request
- if @http_query_string
- params = CGI.parse(@http_query_string)
- elsif @http_post_content
- params = CGI.parse(@http_post_content)
- end
- if params && params["address"] && params["address"].any?
- return params["address"].first
- else
- return nil
- end
- end
+ private
- def send_key(uid)
- if local_address?(uid)
- 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
+ def handler
+ @handler ||= RequestHandler.new(self, Nickserver::Adapters::EmHttp.new)
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?(uid)
- uid_domain = uid.sub(/^.*@(.*)$/, "\\1")
- if Config.domain
- return uid_domain == Config.domain
- else
- # no domain configured, use Host header
- host_header = @http_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
+ def params
+ if @http_query_string
+ CGI.parse(@http_query_string)
+ elsif @http_post_content
+ CGI.parse(@http_post_content)
end
end
- def adapter
- @adapter ||= Nickserver::Adapters::EmHttp.new
- end
end
end
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
new file mode 100644
index 0000000..26b6ec1
--- /dev/null
+++ b/lib/nickserver/request_handler.rb
@@ -0,0 +1,85 @@
+module Nickserver
+ class RequestHandler
+
+ def initialize(responder, adapter)
+ @responder = responder
+ @adapter = adapter
+ end
+
+ def respond_to(params, headers)
+ uid = get_uid_from_params(params)
+ if uid.nil?
+ send_not_found
+ elsif uid !~ EmailAddress
+ send_error("Not a valid address")
+ else
+ send_key(uid, headers)
+ end
+ rescue RuntimeError => exc
+ puts "Error: #{exc}"
+ puts exc.backtrace
+ send_error(exc.to_s)
+ end
+
+ protected
+
+ def get_uid_from_params(params)
+ if params && params["address"] && params["address"].any?
+ return params["address"].first
+ else
+ return nil
+ end
+ end
+
+ def send_key(uid, headers)
+ if local_address?(uid, 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
+ 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?(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
+ end
+ def send_error(msg = "not supported")
+ send_response(status: 500, content: "500 #{msg}\n")
+ end
+
+ def send_not_found(msg = "Not Found")
+ send_response(status: 404, content: "404 #{msg}\n")
+ end
+
+ def send_response(opts = {})
+ responder.send_response default_response.merge(opts)
+ end
+
+ def default_response
+ {status: 200, content_type: 'text/plain', content: ''}
+ end
+
+ attr_reader :responder, :adapter
+
+ end
+end
diff --git a/test/unit/request_handler_test.rb b/test/unit/request_handler_test.rb
new file mode 100644
index 0000000..c9d316f
--- /dev/null
+++ b/test/unit/request_handler_test.rb
@@ -0,0 +1,41 @@
+require 'test_helper'
+require 'nickserver/request_handler'
+
+class Nickserver::RequestHandlerTest < Minitest::Test
+
+ def test_empty_query
+ handle
+ assert_response status: 404, content: "404 Not Found\n"
+ end
+
+ def test_invalid_query
+ handle address: ['asdf']
+ assert_response status: 500, content: "500 Not a valid address\n"
+ end
+
+ protected
+
+ def handle(params = {}, headers = {})
+ @params = Hash[ params.map{ |k,v| [k.to_s, v] } ]
+ @headers = headers
+ end
+
+ def assert_response(args)
+ args[:content_type] ||= 'text/plain'
+ responder.expect :send_response, nil, [args]
+ handler.respond_to @params, @headers
+ responder.verify
+ end
+
+ def handler
+ Nickserver::RequestHandler.new responder, adapter
+ end
+
+ def responder
+ @responder ||= Minitest::Mock.new
+ end
+
+ def adapter
+ @adapter ||= Minitest::Mock.new
+ end
+end