summaryrefslogtreecommitdiff
path: root/lib/nickserver
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-09-23 06:45:18 +0000
committerazul <azul@riseup.net>2016-09-23 06:45:18 +0000
commit6721f0732facd87404eecc288357fd1bd0de48cf (patch)
tree58b0f80b545987d5fc7f3dfdd4a3c1563cbc216e /lib/nickserver
parente2aedcaade71dfe9103fdc8e705f59ece5f3a4d0 (diff)
parent68ffe9928620d3e5e3b96152ed4d37da90f6a89b (diff)
Merge branch 'feature/deal-with-network-failures' into 'master'
Feature/deal with network failures Also activates the new nicknym lookup. See merge request !5
Diffstat (limited to 'lib/nickserver')
-rw-r--r--lib/nickserver/dispatcher.rb27
-rw-r--r--lib/nickserver/handler_chain.rb48
-rw-r--r--lib/nickserver/hkp/source.rb2
3 files changed, 69 insertions, 8 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb
index 7a584e5..869f721 100644
--- a/lib/nickserver/dispatcher.rb
+++ b/lib/nickserver/dispatcher.rb
@@ -14,14 +14,17 @@
#
require 'nickserver/request'
+require 'nickserver/handler_chain'
require 'nickserver/request_handlers/invalid_email_handler'
require 'nickserver/request_handlers/local_email_handler'
+require 'nickserver/request_handlers/leap_email_handler'
require 'nickserver/request_handlers/hkp_email_handler'
require 'nickserver/request_handlers/fingerprint_handler'
module Nickserver
class Dispatcher
+
def initialize(responder)
@responder = responder
end
@@ -35,10 +38,7 @@ module Nickserver
protected
def handle(request)
- handler_chain.each do |handler|
- response = handler.call request
- return response if response
- end
+ handler_chain.handle request
rescue RuntimeError => exc
puts "Error: #{exc}"
puts exc.backtrace
@@ -46,13 +46,26 @@ module Nickserver
end
def handler_chain
- [
- RequestHandlers::InvalidEmailHandler,
+ @handler_chain ||= init_handler_chain
+ end
+
+ def init_handler_chain
+ chain = HandlerChain.new RequestHandlers::InvalidEmailHandler,
RequestHandlers::LocalEmailHandler,
+ RequestHandlers::LeapEmailHandler,
RequestHandlers::HkpEmailHandler,
RequestHandlers::FingerprintHandler,
+ Proc.new {|_req| proxy_error_response },
Proc.new { Nickserver::Response.new(404, "404 Not Found\n") }
- ]
+ chain.continue_on HTTP::ConnectionError
+ return chain
+ end
+
+ def proxy_error_response
+ exception = handler_chain.rescued_exceptions.first
+ if exception
+ Nickserver::Response.new(502, exception.to_s)
+ end
end
def send_response(response)
diff --git a/lib/nickserver/handler_chain.rb b/lib/nickserver/handler_chain.rb
new file mode 100644
index 0000000..afc24a5
--- /dev/null
+++ b/lib/nickserver/handler_chain.rb
@@ -0,0 +1,48 @@
+#
+# Handler Chain
+#
+# A chain of handlers that respond to call. Invoking handle(*args) on the chain
+# will call the handlers with the given args until one of them returns a result
+# that is truethy (i.e. not false or nil).
+#
+# You can specify exception classes to rescue with
+# handler_chain.continue_on ErrorClass1, ErrorClass2
+# These exceptions will be rescued and tracked. The chain will proceed even if
+# one handler raised the given exception. Afterwards you can inspect them with
+# handler_chain.rescued_exceptions
+#
+
+module Nickserver
+ class HandlerChain
+
+ def initialize(*handlers)
+ @handlers = handlers
+ @exceptions_to_rescue = []
+ @rescued_exceptions = []
+ end
+
+ def continue_on(*exceptions)
+ self.exceptions_to_rescue += exceptions
+ end
+
+ def handle(*args)
+ result = nil
+ _handled_by = @handlers.find{|h| result = try_handler(h, *args)}
+ result
+ end
+
+ attr_reader :rescued_exceptions
+
+ protected
+
+ attr_writer :rescued_exceptions
+ attr_accessor :exceptions_to_rescue
+
+ def try_handler(handler, *args)
+ result = handler.call(*args)
+ rescue *exceptions_to_rescue
+ self.rescued_exceptions << $!
+ result = false
+ end
+ end
+end
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index e104aa8..82c94a0 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -19,7 +19,7 @@ module Nickserver; module Hkp
if status == 200
best = pick_best_key(response)
get_key_by_fingerprint(best.keyid, nick)
- else
+ elsif status != 404 # 404 means no key found and we proceed
Nickserver::Response.new(status, response)
end
end