diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/nickserver/hkp/fetch_key_info.rb | 28 | ||||
-rw-r--r-- | lib/nickserver/hkp/parse_key_info.rb | 22 | ||||
-rw-r--r-- | lib/nickserver/hkp/source.rb | 14 |
3 files changed, 35 insertions, 29 deletions
diff --git a/lib/nickserver/hkp/fetch_key_info.rb b/lib/nickserver/hkp/fetch_key_info.rb index b7481d0..b23af15 100644 --- a/lib/nickserver/hkp/fetch_key_info.rb +++ b/lib/nickserver/hkp/fetch_key_info.rb @@ -1,30 +1,26 @@ -require 'em-http' - # # used to fetch an array of KeyInfo objects that match the given uid. # module Nickserver; module Hkp class FetchKeyInfo - include EM::Deferrable - def search(uid) + def initialize(adapter) + @adapter = adapter + end + + def search(uid, &block) # in practice, exact=on seems to have no effect params = {op: 'vindex', search: uid, exact: 'on', options: 'mr', fingerprint: 'on'} - EventMachine::HttpRequest.new(Config.hkp_url).get(query: params).callback {|http| - parser = ParseKeyInfo.new http.response_header, http.response - keys = parser.keys(uid) - if keys.any? - self.succeed keys - else - self.fail parser.status(uid), parser.msg(uid) - end - }.errback {|http| - self.fail 500, http.error - } - self + adapter.get(Config.hkp_url, query: params) do |status, response| + parser = ParseKeyInfo.new status, response + yield parser.status_for(uid), parser.response_for(uid) + end end + protected + attr_reader :adapter + end end; end diff --git a/lib/nickserver/hkp/parse_key_info.rb b/lib/nickserver/hkp/parse_key_info.rb index d8abe4f..9d59d6b 100644 --- a/lib/nickserver/hkp/parse_key_info.rb +++ b/lib/nickserver/hkp/parse_key_info.rb @@ -12,18 +12,26 @@ module Nickserver; module Hkp # which the output of sks does. MATCH_PUB_KEY = /(^pub:.+?\n(^uid:.+?\n)+)/m - # header -- header of the hkp response + # status -- http status of the hkp response # vindex_result -- raw output from a vindex hkp query (machine readable) - def initialize(header, vindex_result) - @header = header + def initialize(status, vindex_result) + @status = status @vindex_result = vindex_result end - def status(uid) + def status_for(uid) if hkp_ok? && keys(uid).empty? error_status(uid) else - header.status + status + end + end + + def response_for(uid) + if keys(uid).any? + keys(uid) + else + msg(uid) end end @@ -41,7 +49,7 @@ module Nickserver; module Hkp protected - attr_reader :header + attr_reader :status attr_reader :vindex_result def error_status(uid) @@ -78,7 +86,7 @@ module Nickserver; module Hkp end def hkp_ok? - header.status == 200 + status == 200 end def error_message(uid, key, err) diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index 48a63d8..1af8ac9 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -14,12 +14,14 @@ module Nickserver; module Hkp end def query(nick, &block) - FetchKeyInfo.new.search(nick).callback {|key_info_list| - best = pick_best_key(key_info_list) - get_key_by_fingerprint(nick, best.keyid, &block) - }.errback {|status, msg| - yield Nickserver::Response.new(status, msg) - } + FetchKeyInfo.new(adapter).search(nick) do |status, response| + if status == 200 + best = pick_best_key(response) + get_key_by_fingerprint(nick, best.keyid, &block) + else + yield Nickserver::Response.new(status, response) + end + end end protected |